/*************************************************************************** * __________ __ ___. * Open \______ \ ____ ____ | | _\_ |__ _______ ___ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ * \/ \/ \/ \/ \/ * $Id$ * * Copyright (C) 2010 Robert Bieber * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * ****************************************************************************/ #include "projectmodel.h" #include "configdocument.h" #include "ui_configdocument.h" #include "preferencesdialog.h" #include #include #include #include #include ConfigDocument::ConfigDocument(QMap& settings, QString file, QWidget *parent) : TabContent(parent), ui(new Ui::ConfigDocument), filePath(file), block(false) { ui->setupUi(this); editor = new CodeEditor(this); editor->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); editor->setLineWrapMode(CodeEditor::NoWrap); ui->splitter->addWidget(editor); QObject::connect(editor, SIGNAL(textChanged()), this, SLOT(textChanged())); /* Loading the splitter status */ QSettings appSettings; appSettings.beginGroup("ConfigDocument"); if(!appSettings.value("textVisible", true).toBool()) { editor->setVisible(false); ui->textButton->setChecked(false); } else { ui->textButton->setChecked(true); } if(!appSettings.value("linesVisible", false).toBool()) { ui->scrollArea->setVisible(false); ui->linesButton->setChecked(false); } else { ui->linesButton->setChecked(true); } if(!appSettings.value("splitter", QByteArray()).toByteArray().isNull()) ui->splitter->restoreState(appSettings.value("splitter").toByteArray()); appSettings.endGroup(); /* Populating the known keys list */ QFile fin(":/resources/configkeys"); fin.open(QFile::ReadOnly); QStringList* container = &primaryKeys; while(!fin.atEnd()) { QString current = QString(fin.readLine()); if(current == "-\n") container = &secondaryKeys; else if(current != "\n") container->append(current.trimmed()); } /* Loading the text file */ QFile finT(settings.value("configfile", "")); if(finT.open(QFile::Text | QFile::ReadOnly)) { editor->setPlainText(QString(finT.readAll())); finT.close(); } saved = toPlainText(); QObject::connect(ui->addKeyButton, SIGNAL(pressed()), this, SLOT(addClicked())); QObject::connect(ui->linesButton, SIGNAL(toggled(bool)), this, SLOT(buttonChecked())); QObject::connect(ui->textButton, SIGNAL(toggled(bool)), this, SLOT(buttonChecked())); settingsChanged(); } ConfigDocument::~ConfigDocument() { delete ui; editor->deleteLater(); } void ConfigDocument::changeEvent(QEvent *e) { QWidget::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; } } QString ConfigDocument::title() const { QStringList decompose = filePath.split("/"); return decompose.last(); } void ConfigDocument::save() { QFile fout(filePath); if(!fout.exists()) { saveAs(); return; } fout.open(QFile::WriteOnly); fout.write(toPlainText().toAscii()); fout.close(); saved = toPlainText(); emit titleChanged(title()); emit configFileChanged(file()); } void ConfigDocument::saveAs() { /* Determining the directory to open */ QString directory = filePath; QSettings settings; settings.beginGroup("ProjectModel"); if(directory == "") directory = settings.value("defaultDirectory", "").toString(); filePath = QFileDialog::getSaveFileName(this, tr("Save Document"), directory, ProjectModel::fileFilter()); directory = filePath; if(filePath == "") return; directory.chop(filePath.length() - filePath.lastIndexOf('/') - 1); settings.setValue("defaultDirectory", directory); settings.endGroup(); QFile fout(filePath); fout.open(QFile::WriteOnly); fout.write(toPlainText().toAscii()); fout.close(); saved = toPlainText(); emit titleChanged(title()); emit configFileChanged(file()); } bool ConfigDocument::requestClose() { if(toPlainText() != saved) { /* Spawning the "Are you sure?" dialog */ QMessageBox confirm(this); confirm.setWindowTitle(tr("Confirm Close")); confirm.setText(title() + tr(" has been modified.")); confirm.setInformativeText(tr("Do you want to save your changes?")); confirm.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); confirm.setDefaultButton(QMessageBox::Save); int confirmation = confirm.exec(); switch(confirmation) { case QMessageBox::Save: save(); /* After calling save, make sure the user actually went through */ if(toPlainText() != saved) return false; else return true; case QMessageBox::Discard: return true; case QMessageBox::Cancel: return false; } } return true; } QString ConfigDocument::toPlainText() const { return editor->toPlainText(); } void ConfigDocument::syncFromBoxes() { if(block) return; blockUpdates(); QString buffer = ""; for(int i = 0; i < keys.count(); i++) { buffer += keys[i]->currentText(); buffer += ":"; buffer += values[i]->text(); buffer += "\n"; } editor->setPlainText(buffer); } void ConfigDocument::syncFromText() { if(block) return; blockUpdates(); QStringList lines = editor->toPlainText().split("\n"); QList > splits; for(int i = 0; i < lines.count(); i++) { QString line = lines[i]; QStringList split = line.split(":"); if(split.count() != 2) continue; splits.append(QPair(split[0].trimmed(), split[1].trimmed())); } while(deleteButtons.count() > splits.count()) { deleteButtons[0]->deleteLater(); keys[0]->deleteLater(); values[0]->deleteLater(); containers[0]->deleteLater(); labels[0]->deleteLater(); deleteButtons.removeAt(0); keys.removeAt(0); values.removeAt(0); containers.removeAt(0); labels.removeAt(0); } int initialCount = deleteButtons.count(); for(int i = 0; i < splits.count(); i++) { if(i >= initialCount) { addRow(splits[i].first, splits[i].second); } else { int index = keys[i]->findText(splits[i].first); if(index != -1) keys[i]->setCurrentIndex(index); else keys[i]->setEditText(splits[i].first); values[i]->setText(splits[i].second); } } } void ConfigDocument::addRow(QString key, QString value) { QHBoxLayout* layout = new QHBoxLayout(); NoScrollCombo* keyEdit = new NoScrollCombo(this); QLineEdit* valueEdit = new QLineEdit(value, this); QPushButton* delButton = new QPushButton(tr("-"), this); QLabel* label = new QLabel(":"); /* Loading the combo box options */ keyEdit->setInsertPolicy(QComboBox::InsertAlphabetically); keyEdit->setEditable(true); keyEdit->addItems(primaryKeys); keyEdit->insertSeparator(keyEdit->count()); keyEdit->addItems(secondaryKeys); if(keyEdit->findText(key) != -1) keyEdit->setCurrentIndex(keyEdit->findText(key)); else keyEdit->setEditText(key); layout->addWidget(keyEdit); layout->addWidget(label); layout->addWidget(valueEdit); layout->addWidget(delButton); delButton->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed); delButton->setMaximumWidth(35); QObject::connect(delButton, SIGNAL(clicked()), this, SLOT(deleteClicked())); QObject::connect(keyEdit, SIGNAL(currentIndexChanged(QString)), this, SLOT(boxesChanged())); QObject::connect(keyEdit, SIGNAL(textChanged(QString)), this, SLOT(boxesChanged())); QObject::connect(valueEdit, SIGNAL(textChanged(QString)), this, SLOT(boxesChanged())); ui->configBoxes->addLayout(layout); containers.append(layout); keys.append(keyEdit); values.append(valueEdit); deleteButtons.append(delButton); labels.append(label); } void ConfigDocument::deleteClicked() { QPushButton* button = dynamic_cast(sender()); int row = deleteButtons.indexOf(button); deleteButtons[row]->deleteLater(); keys[row]->deleteLater(); values[row]->deleteLater(); containers[row]->deleteLater(); labels[row]->deleteLater(); deleteButtons.removeAt(row); keys.removeAt(row); values.removeAt(row); containers.removeAt(row); labels.removeAt(row); if(saved != toPlainText()) emit titleChanged(title() + "*"); else emit titleChanged(title()); } void ConfigDocument::addClicked() { addRow(tr("Key"), tr("Value")); } void ConfigDocument::boxesChanged() { syncFromBoxes(); contentsChanged(); } void ConfigDocument::textChanged() { syncFromText(); contentsChanged(); } void ConfigDocument::contentsChanged() { if(toPlainText() != saved) emit titleChanged(title() + "*"); else emit titleChanged(title()); } void ConfigDocument::buttonChecked() { editor->setVisible(ui->textButton->isChecked()); ui->scrollArea->setVisible(ui->linesButton->isChecked()); QSettings settings; settings.beginGroup("ConfigDocument"); settings.setValue("textVisible", ui->textButton->isChecked()); settings.setValue("linesVisible", ui->linesButton->isChecked()); settings.endGroup(); } void ConfigDocument::connectPrefs(PreferencesDialog *prefs) { QObject::connect(prefs, SIGNAL(accepted()), this, SLOT(settingsChanged())); } void ConfigDocument::settingsChanged() { /* Setting the editor colors */ QSettings settings; settings.beginGroup("SkinDocument"); QColor fg = settings.value("fgColor", Qt::black).value(); QColor bg = settings.value("bgColor", Qt::white).value(); QPalette palette; palette.setColor(QPalette::All, QPalette::Base, bg); palette.setColor(QPalette::All, QPalette::Text, fg); editor->setPalette(palette); QColor highlight = settings.value("errorColor", Qt::red).value(); editor->setErrorColor(highlight); /* Setting the font */ QFont def("Monospace"); def.setStyleHint(QFont::TypeWriter); QFont family = settings.value("fontFamily", def).value(); family.setPointSize(settings.value("fontSize", 12).toInt()); editor->setFont(family); editor->repaint(); settings.endGroup(); } 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2009 by Jens Arnold
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "plugin.h"
#include "lib/grey.h"
#include "lib/helper.h"

//#define TEST_GREYLIB  /* Uncomment for testing greylib instead of core gfx */

#ifdef TEST_GREYLIB
#define MYLCD(fn) grey_ ## fn
GREY_INFO_STRUCT
static unsigned char *gbuf;
static size_t gbuf_size = 0;
#else
#define MYLCD(fn) rb->lcd_ ## fn
#endif

#define DURATION (HZ) /* longer duration gives more precise results */
#define RND_SEED 0x43A678C3     /* arbirary */

PLUGIN_HEADER

static uint16_t rand_table[0x400];
static int log_fd;


static int log_init(void)
{
    char logfilename[MAX_PATH];
    int fd;

    rb->create_numbered_filename(logfilename, "/", "test_gfx_log_", ".txt",
                                 2 IF_CNFN_NUM_(, NULL));
    fd = rb->open(logfilename, O_RDWR|O_CREAT|O_TRUNC);
    return fd;
}

static void init_rand_table(void)
{
    int i;
    
    rb->srand(RND_SEED); /* make it reproducable */
    for (i = 0; i < 0x400; i++)
        rand_table[i] = rb->rand();
}

static void time_drawpixel(void)
{
    long time_start;  /* start tickcount */
    long time_end;    /* end tickcount */
    int count1, count2, count3, count4;

    /* Test 1: DRMODE_SOLID */
    MYLCD(set_drawmode)(DRMODE_SOLID);
    count1 = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        unsigned rnd = rand_table[count1++ & 0x3ff];
        MYLCD(drawpixel)((rnd >> 8) & 0x3f, rnd & 0x3f);
    }

    /* Test 2: DRMODE_FG */
    MYLCD(set_drawmode)(DRMODE_FG);
    count2 = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        unsigned rnd = rand_table[count2++ & 0x3ff];
        MYLCD(drawpixel)((rnd >> 8) & 0x3f, rnd & 0x3f);
    }
    /* Test 3: DRMODE_BG */
    MYLCD(set_drawmode)(DRMODE_BG);
    count3 = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        unsigned rnd = rand_table[count3++ & 0x3ff];
        MYLCD(drawpixel)((rnd >> 8) & 0x3f, rnd & 0x3f);
    }
    /* Test 4: DRMODE_COMPLEMENT */
    MYLCD(set_drawmode)(DRMODE_COMPLEMENT);
    count4 = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        unsigned rnd = rand_table[count4++ & 0x3ff];
        MYLCD(drawpixel)((rnd >> 8) & 0x3f, rnd & 0x3f);
    }

    rb->fdprintf(log_fd, "lcd_drawpixel (pixels/s): %d/%d/%d/%d\n",
                 count1, count2, count3, count4);
}

static void time_drawline(void)
{
    long time_start;  /* start tickcount */
    long time_end;    /* end tickcount */
    int count1, count2, count3, count4;

    /* Test 1: DRMODE_SOLID */
    MYLCD(set_drawmode)(DRMODE_SOLID);
    count1 = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        unsigned rnd1 = rand_table[count1++ & 0x3ff];
        unsigned rnd2 = rand_table[count1++ & 0x3ff];
        MYLCD(drawline)((rnd1 >> 8) & 0x3f, rnd1 & 0x3f,
                        (rnd2 >> 8) & 0x3f, rnd2 & 0x3f);
    }

    /* Test 2: DRMODE_FG */
    MYLCD(set_drawmode)(DRMODE_FG);
    count2 = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        unsigned rnd1 = rand_table[count2++ & 0x3ff];
        unsigned rnd2 = rand_table[count2++ & 0x3ff];
        MYLCD(drawline)((rnd1 >> 8) & 0x3f, rnd1 & 0x3f,
                        (rnd2 >> 8) & 0x3f, rnd2 & 0x3f);
    }
    /* Test 3: DRMODE_BG */
    MYLCD(set_drawmode)(DRMODE_BG);
    count3 = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        unsigned rnd1 = rand_table[count3++ & 0x3ff];
        unsigned rnd2 = rand_table[count3++ & 0x3ff];
        MYLCD(drawline)((rnd1 >> 8) & 0x3f, rnd1 & 0x3f,
                        (rnd2 >> 8) & 0x3f, rnd2 & 0x3f);
    }
    /* Test 4: DRMODE_COMPLEMENT */
    MYLCD(set_drawmode)(DRMODE_COMPLEMENT);
    count4 = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        unsigned rnd1 = rand_table[count4++ & 0x3ff];
        unsigned rnd2 = rand_table[count4++ & 0x3ff];
        MYLCD(drawline)((rnd1 >> 8) & 0x3f, rnd1 & 0x3f,
                        (rnd2 >> 8) & 0x3f, rnd2 & 0x3f);
    }

    rb->fdprintf(log_fd, "lcd_drawline  (lines/s):  %d/%d/%d/%d\n",
                 count1, count2, count3, count4);
}

static void time_hline(void)
{
    long time_start;  /* start tickcount */
    long time_end;    /* end tickcount */
    int count1, count2, count3, count4;

    /* Test 1: DRMODE_SOLID */
    MYLCD(set_drawmode)(DRMODE_SOLID);
    count1 = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        unsigned rnd1 = rand_table[count1++ & 0x3ff];
        unsigned rnd2 = rand_table[count1++ & 0x3ff];
        MYLCD(hline)((rnd1 >> 8) & 0x3f, rnd1 & 0x3f, rnd2 & 0x3f);
    }

    /* Test 2: DRMODE_FG */
    MYLCD(set_drawmode)(DRMODE_FG);
    count2 = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        unsigned rnd1 = rand_table[count2++ & 0x3ff];
        unsigned rnd2 = rand_table[count2++ & 0x3ff];
        MYLCD(hline)((rnd1 >> 8) & 0x3f, rnd1 & 0x3f, rnd2 & 0x3f);
    }
    /* Test 3: DRMODE_BG */
    MYLCD(set_drawmode)(DRMODE_BG);
    count3 = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        unsigned rnd1 = rand_table[count3++ & 0x3ff];
        unsigned rnd2 = rand_table[count3++ & 0x3ff];
        MYLCD(hline)((rnd1 >> 8) & 0x3f, rnd1 & 0x3f, rnd2 & 0x3f);
    }
    /* Test 4: DRMODE_COMPLEMENT */
    MYLCD(set_drawmode)(DRMODE_COMPLEMENT);
    count4 = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        unsigned rnd1 = rand_table[count4++ & 0x3ff];
        unsigned rnd2 = rand_table[count4++ & 0x3ff];
        MYLCD(hline)((rnd1 >> 8) & 0x3f, rnd1 & 0x3f, rnd2 & 0x3f);
    }

    rb->fdprintf(log_fd, "lcd_hline     (lines/s):  %d/%d/%d/%d\n",
                 count1, count2, count3, count4);
}

static void time_vline(void)
{
    long time_start;  /* start tickcount */
    long time_end;    /* end tickcount */
    int count1, count2, count3, count4;

    /* Test 1: DRMODE_SOLID */
    MYLCD(set_drawmode)(DRMODE_SOLID);
    count1 = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        unsigned rnd1 = rand_table[count1++ & 0x3ff];
        unsigned rnd2 = rand_table[count1++ & 0x3ff];
        MYLCD(vline)((rnd1 >> 8) & 0x3f, rnd1 & 0x3f, rnd2 & 0x3f);
    }

    /* Test 2: DRMODE_FG */
    MYLCD(set_drawmode)(DRMODE_FG);
    count2 = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        unsigned rnd1 = rand_table[count2++ & 0x3ff];
        unsigned rnd2 = rand_table[count2++ & 0x3ff];
        MYLCD(vline)((rnd1 >> 8) & 0x3f, rnd1 & 0x3f, rnd2 & 0x3f);
    }
    /* Test 3: DRMODE_BG */
    MYLCD(set_drawmode)(DRMODE_BG);
    count3 = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        unsigned rnd1 = rand_table[count3++ & 0x3ff];
        unsigned rnd2 = rand_table[count3++ & 0x3ff];
        MYLCD(vline)((rnd1 >> 8) & 0x3f, rnd1 & 0x3f, rnd2 & 0x3f);
    }
    /* Test 4: DRMODE_COMPLEMENT */
    MYLCD(set_drawmode)(DRMODE_COMPLEMENT);
    count4 = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        unsigned rnd1 = rand_table[count4++ & 0x3ff];
        unsigned rnd2 = rand_table[count4++ & 0x3ff];
        MYLCD(vline)((rnd1 >> 8) & 0x3f, rnd1 & 0x3f, rnd2 & 0x3f);
    }

    rb->fdprintf(log_fd, "lcd_vline     (lines/s):  %d/%d/%d/%d\n",
                 count1, count2, count3, count4);
}

static void time_fillrect(void)
{
    long time_start;  /* start tickcount */
    long time_end;    /* end tickcount */
    int count1, count2, count3, count4;

    /* Test 1: DRMODE_SOLID */
    MYLCD(set_drawmode)(DRMODE_SOLID);
    count1 = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        unsigned rnd1 = rand_table[count1++ & 0x3ff];