tests/arthur/lance/interactivewidget.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 #include "interactivewidget.h"
       
    42 #include <QtGui/QToolBox>
       
    43 
       
    44 InteractiveWidget::InteractiveWidget()
       
    45 {
       
    46     m_onScreenWidget = new OnScreenWidget<QWidget>("");
       
    47     m_onScreenWidget->setMinimumSize(320, 240);
       
    48 
       
    49     setCentralWidget(m_onScreenWidget);
       
    50 
       
    51     ui_textEdit = new QTextEdit();
       
    52     ui_textEdit->installEventFilter(this);
       
    53 
       
    54     QWidget *panelContent = new QWidget();
       
    55     QVBoxLayout *vlayout = new QVBoxLayout(panelContent);
       
    56     vlayout->setMargin(0);
       
    57     vlayout->setSpacing(0);
       
    58 
       
    59     // create and populate the command toolbox
       
    60     m_commandsToolBox = new QToolBox();
       
    61     QListWidget *currentListWidget = 0;
       
    62     foreach (PaintCommands::PaintCommandInfos paintCommandInfo, PaintCommands::s_commandInfoTable) {
       
    63         if (paintCommandInfo.isSectionHeader()) {
       
    64             currentListWidget = new QListWidget();
       
    65             m_commandsToolBox->addItem(currentListWidget, QIcon(":/icons/tools.png"), "commands - "+paintCommandInfo.identifier);
       
    66             connect(currentListWidget, SIGNAL(itemActivated(QListWidgetItem*)), SLOT(cmdSelected(QListWidgetItem*)));
       
    67         } else {
       
    68             (new QListWidgetItem(paintCommandInfo.identifier, currentListWidget))->setToolTip(paintCommandInfo.syntax);
       
    69         }
       
    70     }
       
    71 
       
    72     // create and populate the enumerations toolbox
       
    73     m_enumsToolBox = new QToolBox();
       
    74     typedef QPair<QString,QStringList> EnumListType;
       
    75     foreach (EnumListType enumInfos, PaintCommands::s_enumsTable) {
       
    76         currentListWidget = new QListWidget();
       
    77         m_commandsToolBox->addItem(currentListWidget, QIcon(":/icons/enum.png"), "enums - "+enumInfos.first);
       
    78         connect(currentListWidget, SIGNAL(itemActivated(QListWidgetItem*)), SLOT(enumSelected(QListWidgetItem*)));
       
    79         foreach (QString enumItem, enumInfos.second)
       
    80             new QListWidgetItem(enumItem, currentListWidget);
       
    81     }
       
    82 
       
    83     // add other widgets and layout
       
    84     vlayout->addWidget(m_commandsToolBox);
       
    85     vlayout->addWidget(m_enumsToolBox);
       
    86 
       
    87     QPushButton *run = new QPushButton("&Run");
       
    88     QPushButton *load = new QPushButton("&Load");
       
    89     QPushButton *save = new QPushButton("&Save");
       
    90     run->setFocusPolicy(Qt::NoFocus);
       
    91 
       
    92     vlayout->addSpacing(20);
       
    93     vlayout->addWidget(run);
       
    94     vlayout->addWidget(load);
       
    95     vlayout->addWidget(save);
       
    96 
       
    97     QDockWidget *panel = new QDockWidget("Commands");
       
    98     panel->setWidget(panelContent);
       
    99     addDockWidget(Qt::LeftDockWidgetArea, panel);
       
   100 
       
   101     QDockWidget *editor = new QDockWidget("Editor");
       
   102     editor->setWidget(ui_textEdit);
       
   103     addDockWidget(Qt::RightDockWidgetArea, editor);
       
   104 
       
   105     // connect gui signals
       
   106     connect(run, SIGNAL(clicked()), SLOT(run()));
       
   107     connect(load, SIGNAL(clicked()), SLOT(load()));
       
   108     connect(save, SIGNAL(clicked()), SLOT(save()));
       
   109 }
       
   110 
       
   111 /***************************************************************************************************/
       
   112 void InteractiveWidget::run()
       
   113 {
       
   114     m_onScreenWidget->m_commands.clear();
       
   115     QString script = ui_textEdit->toPlainText();
       
   116     QStringList lines = script.split("\n");
       
   117     for (int i = 0; i < lines.size(); ++i)
       
   118         m_onScreenWidget->m_commands.append(lines.at(i));
       
   119     m_onScreenWidget->repaint();
       
   120 }
       
   121 
       
   122 /***************************************************************************************************/
       
   123 void InteractiveWidget::cmdSelected(QListWidgetItem *item)
       
   124 {
       
   125     if (ui_textEdit->textCursor().atBlockStart()) {
       
   126         ui_textEdit->insertPlainText(PaintCommands::findCommandById(item->text())->sample + "\n");
       
   127     } else {
       
   128         ui_textEdit->moveCursor(QTextCursor::EndOfLine);
       
   129         ui_textEdit->insertPlainText("\n" + PaintCommands::findCommandById(item->text())->sample);
       
   130     }
       
   131     ui_textEdit->setFocus();
       
   132 }
       
   133 
       
   134 /***************************************************************************************************/
       
   135 void InteractiveWidget::enumSelected(QListWidgetItem *item)
       
   136 {
       
   137     ui_textEdit->insertPlainText(item->text());
       
   138     ui_textEdit->setFocus();
       
   139 }
       
   140 
       
   141 /***************************************************************************************************/
       
   142 void InteractiveWidget::load()
       
   143 {
       
   144     QString fname = QFileDialog::getOpenFileName(
       
   145         this,
       
   146         QString("Load QPaintEngine Script"),
       
   147         QFileInfo(m_filename).absoluteFilePath(),
       
   148         QString("QPaintEngine Script (*.qps);;All files (*.*)"));
       
   149 
       
   150     load(fname);
       
   151 }
       
   152 
       
   153 /***************************************************************************************************/
       
   154 void InteractiveWidget::load(const QString &fname)
       
   155 {
       
   156     if (!fname.isEmpty()) {
       
   157         m_filename = fname;
       
   158         ui_textEdit->clear();
       
   159         QFile file(fname);
       
   160         file.open(QIODevice::ReadOnly | QIODevice::Text);
       
   161         QTextStream textFile(&file);
       
   162         QString script = textFile.readAll();
       
   163         ui_textEdit->setPlainText(script);
       
   164         m_onScreenWidget->m_filename = fname;
       
   165     }
       
   166 }
       
   167 
       
   168 /***************************************************************************************************/
       
   169 void InteractiveWidget::save()
       
   170 {
       
   171     QString script = ui_textEdit->toPlainText();
       
   172     if (!script.endsWith("\n"))
       
   173         script += QString("\n");
       
   174     QString fname = QFileDialog::getSaveFileName(this,
       
   175                             QString("Save QPaintEngine Script"),
       
   176                             QFileInfo(m_filename).absoluteFilePath(),
       
   177                             QString("QPaintEngine Script (*.qps);;All files (*.*)"));
       
   178     if (!fname.isEmpty()) {
       
   179         m_filename = fname;
       
   180         QFile file(fname);
       
   181         file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text);
       
   182         QTextStream textFile(&file);
       
   183         textFile << script;
       
   184         m_onScreenWidget->m_filename = fname;
       
   185     }
       
   186 }
       
   187 
       
   188 /***************************************************************************************************/
       
   189 bool InteractiveWidget::eventFilter(QObject *o, QEvent *e)
       
   190 {
       
   191     if (qobject_cast<QTextEdit *>(o) && e->type() == QEvent::KeyPress) {
       
   192         QKeyEvent *ke = static_cast<QKeyEvent *>(e);
       
   193         if (ke->key() == Qt::Key_Tab) {
       
   194             m_commandsToolBox->currentWidget()->setFocus();
       
   195             return true;
       
   196         } else if (ke->key() == Qt::Key_Return && ke->modifiers() == Qt::ControlModifier) {
       
   197             run();
       
   198             return true;
       
   199         }
       
   200     }
       
   201     return false;
       
   202 }