tools/activeqt/testcon/mainwindow.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
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 tools applications 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 
       
    42 #include "mainwindow.h"
       
    43 #include "changeproperties.h"
       
    44 #include "invokemethod.h"
       
    45 #include "ambientproperties.h"
       
    46 #include "controlinfo.h"
       
    47 #include "docuwindow.h"
       
    48 
       
    49 #include <QtGui>
       
    50 #include <qt_windows.h>
       
    51 #include <ActiveQt/ActiveQt>
       
    52 
       
    53 QT_BEGIN_NAMESPACE
       
    54 
       
    55 QAxObject *ax_mainWindow = 0;
       
    56 
       
    57 static QTextEdit *debuglog = 0;
       
    58 
       
    59 static void redirectDebugOutput(QtMsgType type, const char*msg)
       
    60 {
       
    61     Q_UNUSED(type);
       
    62     debuglog->append(QLatin1String(msg));
       
    63 }
       
    64 
       
    65 QT_END_NAMESPACE
       
    66 
       
    67 QT_USE_NAMESPACE
       
    68 
       
    69 MainWindow::MainWindow(QWidget *parent)
       
    70     : QMainWindow(parent)
       
    71 {
       
    72     setupUi(this);
       
    73     setObjectName(QLatin1String("MainWindow"));
       
    74 
       
    75     QAxScriptManager::registerEngine(QLatin1String("PerlScript"), QLatin1String(".pl"));
       
    76     QAxScriptManager::registerEngine(QLatin1String("Python"), QLatin1String(".py"));
       
    77 
       
    78     dlgInvoke = 0;
       
    79     dlgProperties = 0;
       
    80     dlgAmbient = 0;
       
    81     scripts = 0;
       
    82     debuglog = logDebug;
       
    83     oldDebugHandler = qInstallMsgHandler(redirectDebugOutput);
       
    84     QHBoxLayout *layout = new QHBoxLayout(Workbase);
       
    85     workspace = new QWorkspace(Workbase);
       
    86     layout->addWidget(workspace);
       
    87     layout->setMargin(0);
       
    88 
       
    89     connect(workspace, SIGNAL(windowActivated(QWidget*)), this, SLOT(updateGUI()));
       
    90     connect(actionFileExit, SIGNAL(triggered()), qApp, SLOT(quit()));
       
    91 }
       
    92 
       
    93 MainWindow::~MainWindow()
       
    94 {
       
    95     qInstallMsgHandler(oldDebugHandler);
       
    96     debuglog = 0;
       
    97 }
       
    98 
       
    99 
       
   100 void MainWindow::on_actionFileNew_triggered()
       
   101 {
       
   102     QAxSelect select(this);
       
   103     if (select.exec()) {
       
   104         QAxWidget *container = new QAxWidget(workspace);
       
   105         container->setAttribute(Qt::WA_DeleteOnClose);
       
   106         container->setControl(select.clsid());
       
   107 	container->setObjectName(container->windowTitle());
       
   108         workspace->addWindow(container);
       
   109         container->show();
       
   110     }
       
   111     updateGUI();
       
   112 }
       
   113 
       
   114 void MainWindow::on_actionFileLoad_triggered()
       
   115 {
       
   116     QString fname = QFileDialog::getOpenFileName(this, tr("Load"), QString(), QLatin1String("*.qax"));
       
   117     if (fname.isEmpty())
       
   118 	return;
       
   119 
       
   120     QFile file(fname);
       
   121     if (!file.open(QIODevice::ReadOnly)) {
       
   122 	QMessageBox::information(this, tr("Error Loading File"), tr("The file could not be opened for reading.\n%1").arg(fname));
       
   123 	return;
       
   124     }
       
   125 
       
   126     QAxWidget *container = new QAxWidget(workspace);
       
   127     workspace->addWindow(container);
       
   128     
       
   129     QDataStream d(&file);
       
   130     d >> *container;
       
   131 
       
   132     container->setObjectName(container->windowTitle());
       
   133     container->show();
       
   134 
       
   135     updateGUI();
       
   136 }
       
   137 
       
   138 void MainWindow::on_actionFileSave_triggered()
       
   139 {
       
   140     QAxWidget *container = qobject_cast<QAxWidget*>(workspace->activeWindow());
       
   141     if (!container)
       
   142         return;
       
   143 
       
   144     QString fname = QFileDialog::getSaveFileName(this, tr("Save"), QString(), QLatin1String("*.qax"));
       
   145     if (fname.isEmpty())
       
   146 	return;
       
   147 
       
   148     QFile file(fname);
       
   149     if (!file.open(QIODevice::WriteOnly)) {
       
   150 	QMessageBox::information(this, tr("Error Saving File"), tr("The file could not be opened for writing.\n%1").arg(fname));
       
   151 	return;
       
   152     }
       
   153     QDataStream d(&file);
       
   154     d << *container;
       
   155 }
       
   156 
       
   157 
       
   158 void MainWindow::on_actionContainerSet_triggered()
       
   159 {
       
   160     QAxWidget *container = qobject_cast<QAxWidget*>(workspace->activeWindow());
       
   161     if (!container)
       
   162         return;
       
   163 
       
   164     QAxSelect select(this);
       
   165     if (select.exec())
       
   166 	container->setControl(select.clsid());
       
   167     updateGUI();
       
   168 }
       
   169 
       
   170 void MainWindow::on_actionContainerClear_triggered()
       
   171 {
       
   172     QAxWidget *container = qobject_cast<QAxWidget*>(workspace->activeWindow());
       
   173     if (container)
       
   174 	container->clear();
       
   175     updateGUI();
       
   176 }
       
   177 
       
   178 void MainWindow::on_actionContainerProperties_triggered()
       
   179 {
       
   180     if (!dlgAmbient) {
       
   181 	dlgAmbient = new AmbientProperties(this);
       
   182 	dlgAmbient->setControl(workspace);
       
   183     }
       
   184     dlgAmbient->show();
       
   185 }
       
   186 
       
   187 
       
   188 void MainWindow::on_actionControlInfo_triggered()
       
   189 {
       
   190     QAxWidget *container = qobject_cast<QAxWidget*>(workspace->activeWindow());
       
   191     if (!container)
       
   192         return;
       
   193 
       
   194     ControlInfo info(this);
       
   195     info.setControl(container);
       
   196     info.exec();
       
   197 }
       
   198 
       
   199 void MainWindow::on_actionControlProperties_triggered()
       
   200 {
       
   201     QAxWidget *container = qobject_cast<QAxWidget*>(workspace->activeWindow());
       
   202     if (!container)
       
   203         return;
       
   204 
       
   205     if (!dlgProperties) {
       
   206 	dlgProperties = new ChangeProperties(this);
       
   207 	connect(container, SIGNAL(propertyChanged(const QString&)), dlgProperties, SLOT(updateProperties()));
       
   208     }
       
   209     dlgProperties->setControl(container);
       
   210     dlgProperties->show();
       
   211 }
       
   212 
       
   213 void MainWindow::on_actionControlMethods_triggered()
       
   214 {
       
   215     QAxWidget *container = qobject_cast<QAxWidget*>(workspace->activeWindow());
       
   216     if (!container)
       
   217         return;
       
   218 
       
   219     if (!dlgInvoke)
       
   220 	dlgInvoke = new InvokeMethod(this);
       
   221     dlgInvoke->setControl(container);
       
   222     dlgInvoke->show();
       
   223 }
       
   224 
       
   225 void MainWindow::on_VerbMenu_aboutToShow()
       
   226 {
       
   227     VerbMenu->clear();
       
   228 
       
   229     QAxWidget *container = qobject_cast<QAxWidget*>(workspace->activeWindow());
       
   230     if (!container)
       
   231         return;
       
   232 
       
   233     QStringList verbs = container->verbs();
       
   234     for (int i = 0; i < verbs.count(); ++i) {
       
   235         VerbMenu->addAction(verbs.at(i));
       
   236     }
       
   237 
       
   238     if (!verbs.count()) { // no verbs?
       
   239         VerbMenu->addAction(tr("-- Object does not support any verbs --"))->setEnabled(false);
       
   240     }
       
   241 }
       
   242 
       
   243 void MainWindow::on_VerbMenu_triggered(QAction *action)
       
   244 {
       
   245     QAxWidget *container = qobject_cast<QAxWidget*>(workspace->activeWindow());
       
   246     if (!container)
       
   247         return;
       
   248 
       
   249     container->doVerb(action->text());
       
   250 }
       
   251 
       
   252 void MainWindow::on_actionControlDocumentation_triggered()
       
   253 {
       
   254     QAxWidget *container = qobject_cast<QAxWidget*>(workspace->activeWindow());
       
   255     if (!container)
       
   256         return;
       
   257     
       
   258     QString docu = container->generateDocumentation();
       
   259     if (docu.isEmpty())
       
   260 	return;
       
   261 
       
   262     DocuWindow *docwindow = new DocuWindow(docu, workspace, container);
       
   263     workspace->addWindow(docwindow);
       
   264     docwindow->show();
       
   265 }
       
   266 
       
   267 
       
   268 void MainWindow::on_actionControlPixmap_triggered()
       
   269 {
       
   270     QAxWidget *container = qobject_cast<QAxWidget*>(workspace->activeWindow());
       
   271     if (!container)
       
   272 	return;
       
   273 
       
   274     QPixmap pm = QPixmap::grabWidget(container);
       
   275 
       
   276     QLabel *label = new QLabel(workspace);
       
   277     label->setAttribute(Qt::WA_DeleteOnClose);
       
   278     label->setPixmap(pm);
       
   279     label->setWindowTitle(tr("%1 - Pixmap").arg(container->windowTitle()));
       
   280 
       
   281     workspace->addWindow(label);
       
   282     label->show();
       
   283 }
       
   284 
       
   285 void MainWindow::on_actionScriptingRun_triggered()
       
   286 {
       
   287 #ifndef QT_NO_QAXSCRIPT
       
   288     if (!scripts)
       
   289 	return;
       
   290 
       
   291     // If we have only one script loaded we can use the cool dialog
       
   292     QStringList scriptList = scripts->scriptNames();
       
   293     if (scriptList.count() == 1) {
       
   294 	InvokeMethod scriptInvoke(this);
       
   295 	scriptInvoke.setWindowTitle(tr("Execute Script Function"));
       
   296 	scriptInvoke.setControl(scripts->script(scriptList[0])->scriptEngine());
       
   297 	scriptInvoke.exec();
       
   298 	return;
       
   299     }
       
   300 
       
   301     bool ok = false;
       
   302     QStringList macroList = scripts->functions(QAxScript::FunctionNames);
       
   303     QString macro = QInputDialog::getItem(this, tr("Select Macro"), tr("Macro:"), macroList, 0, true, &ok);
       
   304 
       
   305     if (!ok)
       
   306 	return;
       
   307 
       
   308     QVariant result = scripts->call(macro);
       
   309     if (result.isValid())
       
   310 	logMacros->append(tr("Return value of %1: %2").arg(macro).arg(result.toString()));
       
   311 #endif
       
   312 }
       
   313 
       
   314 void MainWindow::on_actionScriptingLoad_triggered()
       
   315 {
       
   316 #ifndef QT_NO_QAXSCRIPT
       
   317     QString file = QFileDialog::getOpenFileName(this, tr("Open Script"), QString(), QAxScriptManager::scriptFileFilter());
       
   318 
       
   319     if (file.isEmpty())
       
   320 	return;
       
   321 
       
   322     if (!scripts) {
       
   323 	scripts = new QAxScriptManager(this);
       
   324 	scripts->addObject(this);
       
   325     }
       
   326 
       
   327     QWidgetList widgets = workspace->windowList();
       
   328     QWidgetList::Iterator it(widgets.begin());
       
   329     while (it != widgets.end()) {
       
   330 	QAxBase *ax = (QAxBase*)(*it)->qt_metacast("QAxBase");
       
   331 	++it;
       
   332 	if (!ax)
       
   333 	    continue;
       
   334 	scripts->addObject(ax);
       
   335     }
       
   336 
       
   337     QAxScript *script = scripts->load(file, file);
       
   338     if (script) {
       
   339 	connect(script, SIGNAL(error(int, const QString&, int, const QString&)),
       
   340 		this,   SLOT(logMacro(int,  const QString&, int, const QString&)));
       
   341 	actionScriptingRun->setEnabled(true);
       
   342     }
       
   343 #else
       
   344     QMessageBox::information(this, tr("Function not available"),
       
   345 	tr("QAxScript functionality is not available with this compiler."));
       
   346 #endif
       
   347 }
       
   348 
       
   349 void MainWindow::updateGUI()
       
   350 {
       
   351     QAxWidget *container = qobject_cast<QAxWidget*>(workspace->activeWindow());
       
   352 
       
   353     bool hasControl = container && !container->isNull();
       
   354     actionFileNew->setEnabled(true);
       
   355     actionFileLoad->setEnabled(true);
       
   356     actionFileSave->setEnabled(hasControl);
       
   357     actionContainerSet->setEnabled(container != 0);
       
   358     actionContainerClear->setEnabled(hasControl);
       
   359     actionControlProperties->setEnabled(hasControl);
       
   360     actionControlMethods->setEnabled(hasControl);
       
   361     actionControlInfo->setEnabled(hasControl);
       
   362     actionControlDocumentation->setEnabled(hasControl);
       
   363     actionControlPixmap->setEnabled(hasControl);
       
   364     VerbMenu->setEnabled(hasControl);
       
   365     if (dlgInvoke)
       
   366 	dlgInvoke->setControl(hasControl ? container : 0);
       
   367     if (dlgProperties)
       
   368 	dlgProperties->setControl(hasControl ? container : 0);
       
   369 
       
   370     QWidgetList list = workspace->windowList();
       
   371     QWidgetList::Iterator it = list.begin();
       
   372     while (it != list.end()) {
       
   373 	QWidget *container = *it;
       
   374 
       
   375 	QAxWidget *ax = qobject_cast<QAxWidget*>(container);
       
   376 	if (ax) {
       
   377 	    container->disconnect(SIGNAL(signal(const QString&, int, void*)));
       
   378 	    if (actionLogSignals->isChecked())
       
   379 		connect(container, SIGNAL(signal(const QString&, int, void*)), this, SLOT(logSignal(const QString&, int, void*)));
       
   380 
       
   381 	    container->disconnect(SIGNAL(exception(int,const QString&,const QString&,const QString&)));
       
   382 	    connect(container, SIGNAL(exception(int,const QString&,const QString&,const QString&)),
       
   383 		this, SLOT(logException(int,const QString&,const QString&,const QString&)));
       
   384 
       
   385 	    container->disconnect(SIGNAL(propertyChanged(const QString&)));
       
   386 	    if (actionLogProperties->isChecked()) 
       
   387 		connect(container, SIGNAL(propertyChanged(const QString&)), this, SLOT(logPropertyChanged(const QString&)));
       
   388 	    container->blockSignals(actionFreezeEvents->isChecked());
       
   389 	}
       
   390 
       
   391 	++it;
       
   392     }
       
   393 }
       
   394 
       
   395 
       
   396 void MainWindow::logPropertyChanged(const QString &prop)
       
   397 {
       
   398     QAxWidget *container = qobject_cast<QAxWidget*>(workspace->activeWindow());
       
   399     if (!container)
       
   400         return;
       
   401 
       
   402     QVariant var = container->property(prop.toLatin1());
       
   403     logProperties->append(tr("%1: Property Change: %2 - { %3 }").arg(container->windowTitle(), prop, var.toString()));
       
   404 }
       
   405 
       
   406 void MainWindow::logSignal(const QString &signal, int argc, void *argv)
       
   407 {
       
   408     QAxWidget *container = qobject_cast<QAxWidget*>(workspace->activeWindow());
       
   409     if (!container)
       
   410         return;
       
   411 
       
   412     QString paramlist;
       
   413     VARIANT *params = (VARIANT*)argv;
       
   414     for (int a = argc-1; a >= 0; --a) {
       
   415 	if (a == argc-1)
       
   416 	    paramlist = QLatin1String(" - {");
       
   417 	QVariant qvar = VARIANTToQVariant(params[a], 0);
       
   418 	paramlist += QLatin1String(" ") + qvar.toString();
       
   419 	if (a > 0)
       
   420 	    paramlist += QLatin1String(",");
       
   421 	else
       
   422 	    paramlist += QLatin1String(" ");
       
   423     }
       
   424     if (argc)
       
   425 	paramlist += QLatin1String("}");
       
   426     logSignals->append(container->windowTitle() + QLatin1String(": ") + signal + paramlist);
       
   427 }
       
   428 
       
   429 void MainWindow::logException(int code, const QString&source, const QString&desc, const QString&help)
       
   430 {
       
   431     Q_UNUSED(desc);
       
   432     QAxWidget *container = qobject_cast<QAxWidget*>(sender());
       
   433     if (!container)
       
   434         return;
       
   435 
       
   436     QString str = tr("%1: Exception code %2 thrown by %3").
       
   437 	arg(container->windowTitle()).arg(code).arg(source);
       
   438     logDebug->append(str);
       
   439     logDebug->append(tr("\tDescription: %1").arg(desc));
       
   440 
       
   441     if (!help.isEmpty())
       
   442 	logDebug->append(tr("\tHelp available at %1").arg(help));
       
   443     else
       
   444 	logDebug->append(tr("\tNo help available."));
       
   445 }
       
   446 
       
   447 void MainWindow::logMacro(int code, const QString &description, int sourcePosition, const QString &sourceText)
       
   448 {
       
   449     /* FIXME This needs to be rewritten to not use string concatentation, such
       
   450      * that it can be translated in a sane way. */
       
   451     QString message = tr("Script: ");
       
   452     if (code)
       
   453 	message += QString::number(code) + QLatin1String(" ");
       
   454     message += QLatin1String("'") + description + QLatin1String("'");
       
   455     if (sourcePosition)
       
   456 	message += tr(" at position ") + QString::number(sourcePosition);
       
   457     if (!sourceText.isEmpty())
       
   458 	message += QLatin1String(" '") + sourceText + QLatin1String("'");
       
   459 
       
   460     logMacros->append(message);
       
   461 }