tools/qml/qmlruntime.cpp
changeset 37 758a864f9613
parent 33 3e2da88830cd
equal deleted inserted replaced
36:ef0373b55136 37:758a864f9613
    83 #include <QProcess>
    83 #include <QProcess>
    84 #include <QMenuBar>
    84 #include <QMenuBar>
    85 #include <QMenu>
    85 #include <QMenu>
    86 #include <QAction>
    86 #include <QAction>
    87 #include <QFileDialog>
    87 #include <QFileDialog>
       
    88 #include <QInputDialog>
    88 #include <QTimer>
    89 #include <QTimer>
    89 #include <QGraphicsObject>
    90 #include <QGraphicsObject>
    90 #include <QNetworkProxyFactory>
    91 #include <QNetworkProxyFactory>
    91 #include <QKeyEvent>
    92 #include <QKeyEvent>
    92 #include <QMutex>
    93 #include <QMutex>
    96 
    97 
    97 #ifdef GL_SUPPORTED
    98 #ifdef GL_SUPPORTED
    98 #include <QGLWidget>
    99 #include <QGLWidget>
    99 #endif
   100 #endif
   100 
   101 
       
   102 #if defined(Q_WS_S60)
       
   103 #include <aknappui.h> // For locking app orientation
       
   104 #endif
       
   105 
   101 #include <qdeclarativetester.h>
   106 #include <qdeclarativetester.h>
   102 
   107 
   103 QT_BEGIN_NAMESPACE
   108 QT_BEGIN_NAMESPACE
       
   109 
       
   110 class DragAndDropView : public QDeclarativeView
       
   111 {
       
   112     Q_OBJECT
       
   113 public:
       
   114     DragAndDropView(QDeclarativeViewer *parent = 0)
       
   115     : QDeclarativeView(parent)
       
   116     {
       
   117         setAcceptDrops(true);
       
   118     }
       
   119 
       
   120     void dragEnterEvent(QDragEnterEvent *event)
       
   121     {
       
   122         const QMimeData *mimeData = event->mimeData();
       
   123         if (mimeData->hasUrls())
       
   124             event->acceptProposedAction();
       
   125     }
       
   126 
       
   127     void dragMoveEvent(QDragMoveEvent *event)
       
   128     {
       
   129         event->acceptProposedAction();
       
   130     }
       
   131 
       
   132     void dragLeaveEvent(QDragLeaveEvent *event)
       
   133     {
       
   134         event->accept();
       
   135     }
       
   136 
       
   137     void dropEvent(QDropEvent *event)
       
   138     {
       
   139         const QMimeData *mimeData = event->mimeData();
       
   140         if (!mimeData->hasUrls())
       
   141             return;
       
   142         const QList<QUrl> urlList = mimeData->urls();
       
   143         foreach (const QUrl &url, urlList) {
       
   144             if (url.scheme() == QLatin1String("file")) {
       
   145                 static_cast<QDeclarativeViewer *>(parent())->open(url.toLocalFile());
       
   146                 event->accept();
       
   147                 return;
       
   148             }
       
   149         }
       
   150     }
       
   151 };
   104 
   152 
   105 class Runtime : public QObject
   153 class Runtime : public QObject
   106 {
   154 {
   107     Q_OBJECT
   155     Q_OBJECT
   108 
   156 
   555 
   603 
   556 QDeclarativeViewer::QDeclarativeViewer(QWidget *parent, Qt::WindowFlags flags)
   604 QDeclarativeViewer::QDeclarativeViewer(QWidget *parent, Qt::WindowFlags flags)
   557     : QMainWindow(parent, flags)
   605     : QMainWindow(parent, flags)
   558       , loggerWindow(new LoggerWidget(this))
   606       , loggerWindow(new LoggerWidget(this))
   559       , frame_stream(0)
   607       , frame_stream(0)
       
   608       , rotateAction(0)
   560       , orientation(0)
   609       , orientation(0)
   561       , showWarningsWindow(0)
   610       , showWarningsWindow(0)
   562       , m_scriptOptions(0)
   611       , m_scriptOptions(0)
   563       , tester(0)
   612       , tester(0)
   564       , useQmlFileBrowser(true)
   613       , useQmlFileBrowser(true)
   594         recdlg->warning->setText(warn);
   643         recdlg->warning->setText(warn);
   595     } else {
   644     } else {
   596         recdlg->warning->hide();
   645         recdlg->warning->hide();
   597     }
   646     }
   598 
   647 
   599     canvas = new QDeclarativeView(this);
   648     canvas = new DragAndDropView(this);
   600 
   649 
   601     canvas->setAttribute(Qt::WA_OpaquePaintEvent);
   650     canvas->setAttribute(Qt::WA_OpaquePaintEvent);
   602     canvas->setAttribute(Qt::WA_NoSystemBackground);
   651     canvas->setAttribute(Qt::WA_NoSystemBackground);
   603 
   652 
   604     canvas->setFocus();
   653     canvas->setFocus();
   605 
   654 
   606     QObject::connect(canvas, SIGNAL(sceneResized(QSize)), this, SLOT(sceneResized(QSize)));
   655     QObject::connect(canvas, SIGNAL(sceneResized(QSize)), this, SLOT(sceneResized(QSize)));
   607     QObject::connect(canvas, SIGNAL(statusChanged(QDeclarativeView::Status)), this, SLOT(statusChanged()));
   656     QObject::connect(canvas, SIGNAL(statusChanged(QDeclarativeView::Status)), this, SLOT(statusChanged()));
   608     QObject::connect(canvas->engine(), SIGNAL(quit()), QCoreApplication::instance (), SLOT(quit()));
   657     QObject::connect(canvas->engine(), SIGNAL(quit()), this, SLOT(close()));
   609 
   658 
   610     QObject::connect(warningsWidget(), SIGNAL(opened()), this, SLOT(warningsWidgetOpened()));
   659     QObject::connect(warningsWidget(), SIGNAL(opened()), this, SLOT(warningsWidgetOpened()));
   611     QObject::connect(warningsWidget(), SIGNAL(closed()), this, SLOT(warningsWidgetClosed()));
   660     QObject::connect(warningsWidget(), SIGNAL(closed()), this, SLOT(warningsWidgetClosed()));
   612 
   661 
   613     if (!(flags & Qt::FramelessWindowHint)) {
   662     if (!(flags & Qt::FramelessWindowHint)) {
   662 }
   711 }
   663 
   712 
   664 void QDeclarativeViewer::createMenu()
   713 void QDeclarativeViewer::createMenu()
   665 {
   714 {
   666     QAction *openAction = new QAction(tr("&Open..."), this);
   715     QAction *openAction = new QAction(tr("&Open..."), this);
   667     openAction->setShortcut(QKeySequence("Ctrl+O"));
   716     openAction->setShortcuts(QKeySequence::Open);
   668     connect(openAction, SIGNAL(triggered()), this, SLOT(openFile()));
   717     connect(openAction, SIGNAL(triggered()), this, SLOT(openFile()));
   669 
   718 
       
   719     QAction *openUrlAction = new QAction(tr("Open &URL..."), this);
       
   720     connect(openUrlAction, SIGNAL(triggered()), this, SLOT(openUrl()));
       
   721 
   670     QAction *reloadAction = new QAction(tr("&Reload"), this);
   722     QAction *reloadAction = new QAction(tr("&Reload"), this);
   671     reloadAction->setShortcut(QKeySequence("Ctrl+R"));
   723     reloadAction->setShortcuts(QKeySequence::Refresh);
   672     connect(reloadAction, SIGNAL(triggered()), this, SLOT(reload()));
   724     connect(reloadAction, SIGNAL(triggered()), this, SLOT(reload()));
   673 
   725 
   674     QAction *snapshotAction = new QAction(tr("&Take Snapshot"), this);
   726     QAction *snapshotAction = new QAction(tr("&Take Snapshot"), this);
   675     snapshotAction->setShortcut(QKeySequence("F3"));
   727     snapshotAction->setShortcut(QKeySequence("F3"));
   676     connect(snapshotAction, SIGNAL(triggered()), this, SLOT(takeSnapShot()));
   728     connect(snapshotAction, SIGNAL(triggered()), this, SLOT(takeSnapShot()));
   697 
   749 
   698     QAction *fullscreenAction = new QAction(tr("Full Screen"), this);
   750     QAction *fullscreenAction = new QAction(tr("Full Screen"), this);
   699     fullscreenAction->setCheckable(true);
   751     fullscreenAction->setCheckable(true);
   700     connect(fullscreenAction, SIGNAL(triggered()), this, SLOT(toggleFullScreen()));
   752     connect(fullscreenAction, SIGNAL(triggered()), this, SLOT(toggleFullScreen()));
   701 
   753 
   702     QAction *rotateOrientation = new QAction(tr("Rotate orientation"), this);
   754     rotateAction = new QAction(tr("Rotate orientation"), this);
   703     rotateOrientation->setShortcut(QKeySequence("Ctrl+T"));
   755     rotateAction->setShortcut(QKeySequence("Ctrl+T"));
   704     connect(rotateOrientation, SIGNAL(triggered()), this, SLOT(rotateOrientation()));
   756     connect(rotateAction, SIGNAL(triggered()), this, SLOT(rotateOrientation()));
   705 
   757 
   706     orientation = new QActionGroup(this);
   758     orientation = new QActionGroup(this);
   707     orientation->setExclusive(true);
   759     orientation->setExclusive(true);
   708     connect(orientation, SIGNAL(triggered(QAction*)), this, SLOT(changeOrientation(QAction*)));
   760     connect(orientation, SIGNAL(triggered(QAction*)), this, SLOT(changeOrientation(QAction*)));
   709 
   761 
       
   762 #if defined(Q_OS_SYMBIAN)
       
   763     QAction *autoOrientationAction = new QAction(tr("Auto-orientation"), this);
       
   764     autoOrientationAction->setCheckable(true);
       
   765 #endif
   710     QAction *portraitAction = new QAction(tr("Portrait"), this);
   766     QAction *portraitAction = new QAction(tr("Portrait"), this);
   711     portraitAction->setCheckable(true);
   767     portraitAction->setCheckable(true);
   712     QAction *landscapeAction = new QAction(tr("Landscape"), this);
   768     QAction *landscapeAction = new QAction(tr("Landscape"), this);
   713     landscapeAction->setCheckable(true);
   769     landscapeAction->setCheckable(true);
       
   770 #if !defined(Q_OS_SYMBIAN)
   714     QAction *portraitInvAction = new QAction(tr("Portrait (inverted)"), this);
   771     QAction *portraitInvAction = new QAction(tr("Portrait (inverted)"), this);
   715     portraitInvAction->setCheckable(true);
   772     portraitInvAction->setCheckable(true);
   716     QAction *landscapeInvAction = new QAction(tr("Landscape (inverted)"), this);
   773     QAction *landscapeInvAction = new QAction(tr("Landscape (inverted)"), this);
   717     landscapeInvAction->setCheckable(true);
   774     landscapeInvAction->setCheckable(true);
       
   775 #endif
   718 
   776 
   719     QAction *aboutAction = new QAction(tr("&About Qt..."), this);
   777     QAction *aboutAction = new QAction(tr("&About Qt..."), this);
       
   778     aboutAction->setMenuRole(QAction::AboutQtRole);
   720     connect(aboutAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
   779     connect(aboutAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
   721 
   780 
       
   781     QAction *closeAction = new QAction(tr("&Close"), this);
       
   782     closeAction->setShortcuts(QKeySequence::Close);
       
   783     connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
       
   784 
   722     QAction *quitAction = new QAction(tr("&Quit"), this);
   785     QAction *quitAction = new QAction(tr("&Quit"), this);
   723     quitAction->setShortcut(QKeySequence("Ctrl+Q"));
   786     quitAction->setMenuRole(QAction::QuitRole);
       
   787     quitAction->setShortcuts(QKeySequence::Quit);
   724     connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
   788     connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
   725 
   789 
   726     QMenuBar *menu = menuBar();
   790     QMenuBar *menu = menuBar();
   727     if (!menu)
   791     if (!menu)
   728 	return;
   792 	return;
   729 
   793 
   730 #if defined(Q_WS_MAEMO_5)
   794 #if defined(Q_WS_MAEMO_5)
   731     menu->addAction(openAction);
   795     menu->addAction(openAction);
       
   796     menu->addAction(openUrlAction);
   732     menu->addAction(reloadAction);
   797     menu->addAction(reloadAction);
   733 
   798 
   734     menu->addAction(snapshotAction);
   799     menu->addAction(snapshotAction);
   735     menu->addAction(recordAction);
   800     menu->addAction(recordAction);
   736 
   801 
   747     return;
   812     return;
   748 #endif // Q_WS_MAEMO_5
   813 #endif // Q_WS_MAEMO_5
   749 
   814 
   750     QMenu *fileMenu = menu->addMenu(tr("&File"));
   815     QMenu *fileMenu = menu->addMenu(tr("&File"));
   751     fileMenu->addAction(openAction);
   816     fileMenu->addAction(openAction);
       
   817     fileMenu->addAction(openUrlAction);
   752     fileMenu->addAction(reloadAction);
   818     fileMenu->addAction(reloadAction);
   753     fileMenu->addSeparator();
   819     fileMenu->addSeparator();
       
   820     fileMenu->addAction(closeAction);
       
   821 #if !defined(Q_OS_SYMBIAN)    
   754     fileMenu->addAction(quitAction);
   822     fileMenu->addAction(quitAction);
   755 
   823 
   756 #if !defined(Q_OS_SYMBIAN)
       
   757     QMenu *recordMenu = menu->addMenu(tr("&Recording"));
   824     QMenu *recordMenu = menu->addMenu(tr("&Recording"));
   758     recordMenu->addAction(snapshotAction);
   825     recordMenu->addAction(snapshotAction);
   759     recordMenu->addAction(recordAction);
   826     recordMenu->addAction(recordAction);
   760 
   827 
   761     QMenu *debugMenu = menu->addMenu(tr("&Debugging"));
   828     QMenu *debugMenu = menu->addMenu(tr("&Debugging"));
   762     debugMenu->addAction(slowAction);
   829     debugMenu->addAction(slowAction);
   763     debugMenu->addAction(showWarningsWindow);
   830     debugMenu->addAction(showWarningsWindow);
   764 #endif // ! Q_OS_SYMBIAN
   831 #endif // ! Q_OS_SYMBIAN
   765 
   832 
   766     QMenu *settingsMenu = menu->addMenu(tr("S&ettings"));
   833     QMenu *settingsMenu = menu->addMenu(tr("&Settings"));
   767     settingsMenu->addAction(proxyAction);
   834     settingsMenu->addAction(proxyAction);
   768 #if !defined(Q_OS_SYMBIAN)
   835 #if defined(Q_OS_SYMBIAN)
       
   836     settingsMenu->addAction(fullscreenAction);
       
   837 #else 
   769     settingsMenu->addAction(recordOptions);
   838     settingsMenu->addAction(recordOptions);
   770     settingsMenu->addMenu(loggerWindow->preferencesMenu());
   839     settingsMenu->addMenu(loggerWindow->preferencesMenu());
   771 #else // ! Q_OS_SYMBIAN
   840 #endif // !Q_OS_SYMBIAN
   772     settingsMenu->addAction(fullscreenAction);
   841     settingsMenu->addAction(rotateAction);
   773 #endif // Q_OS_SYMBIAN
       
   774     settingsMenu->addAction(rotateOrientation);
       
   775 
   842 
   776     QMenu *propertiesMenu = settingsMenu->addMenu(tr("Properties"));
   843     QMenu *propertiesMenu = settingsMenu->addMenu(tr("Properties"));
   777 
   844 
       
   845 #if defined(Q_OS_SYMBIAN)
       
   846     orientation->addAction(autoOrientationAction);
       
   847 #endif
   778     orientation->addAction(portraitAction);
   848     orientation->addAction(portraitAction);
   779     orientation->addAction(landscapeAction);
   849     orientation->addAction(landscapeAction);
       
   850 #if !defined(Q_OS_SYMBIAN)
   780     orientation->addAction(portraitInvAction);
   851     orientation->addAction(portraitInvAction);
   781     orientation->addAction(landscapeInvAction);
   852     orientation->addAction(landscapeInvAction);
       
   853 #endif
   782     propertiesMenu->addActions(orientation->actions());
   854     propertiesMenu->addActions(orientation->actions());
   783 
   855 
   784     QMenu *helpMenu = menu->addMenu(tr("&Help"));
   856     QMenu *helpMenu = menu->addMenu(tr("&Help"));
   785     helpMenu->addAction(aboutAction);
   857     helpMenu->addAction(aboutAction);
   786 }
   858 }
   800     reload ();
   872     reload ();
   801 }
   873 }
   802 
   874 
   803 void QDeclarativeViewer::rotateOrientation()
   875 void QDeclarativeViewer::rotateOrientation()
   804 {
   876 {
       
   877 #if defined(Q_WS_S60)
       
   878     CAknAppUi *appUi = static_cast<CAknAppUi *>(CEikonEnv::Static()->AppUi());
       
   879     if (appUi) {
       
   880         CAknAppUi::TAppUiOrientation oldOrientation = appUi->Orientation();
       
   881         QString newOrientation;
       
   882         if (oldOrientation == CAknAppUi::EAppUiOrientationPortrait) {
       
   883             newOrientation = QLatin1String("Landscape");
       
   884         } else {
       
   885             newOrientation = QLatin1String("Portrait");
       
   886         }
       
   887         foreach (QAction *action, orientation->actions()) {
       
   888             if (action->text() == newOrientation) {
       
   889                 changeOrientation(action);
       
   890             }
       
   891         }
       
   892     }
       
   893 #else
   805     QAction *current = orientation->checkedAction();
   894     QAction *current = orientation->checkedAction();
   806     QList<QAction *> actions = orientation->actions();
   895     QList<QAction *> actions = orientation->actions();
   807     int index = actions.indexOf(current);
   896     int index = actions.indexOf(current);
   808     if (index < 0)
   897     if (index < 0)
   809         return;
   898         return;
   810 
   899 
   811     QAction *newOrientation = actions[(index + 1) % actions.count()];
   900     QAction *newOrientation = actions[(index + 1) % actions.count()];
   812     changeOrientation(newOrientation);
   901     changeOrientation(newOrientation);
       
   902 #endif
   813 }
   903 }
   814 
   904 
   815 void QDeclarativeViewer::toggleFullScreen()
   905 void QDeclarativeViewer::toggleFullScreen()
   816 {
   906 {
   817     if (isFullScreen())
   907     if (isFullScreen())
   925 
  1015 
   926 void QDeclarativeViewer::openFile()
  1016 void QDeclarativeViewer::openFile()
   927 {
  1017 {
   928     QString cur = canvas->source().toLocalFile();
  1018     QString cur = canvas->source().toLocalFile();
   929     if (useQmlFileBrowser) {
  1019     if (useQmlFileBrowser) {
   930         open("qrc:/content/Browser.qml");
  1020         open("qrc:/browser/Browser.qml");
   931     } else {
  1021     } else {
   932         QString fileName = QFileDialog::getOpenFileName(this, tr("Open QML file"), cur, tr("QML Files (*.qml)"));
  1022         QString fileName = QFileDialog::getOpenFileName(this, tr("Open QML file"), cur, tr("QML Files (*.qml)"));
   933         if (!fileName.isEmpty()) {
  1023         if (!fileName.isEmpty()) {
   934             QFileInfo fi(fileName);
  1024             QFileInfo fi(fileName);
   935             open(fi.absoluteFilePath());
  1025             open(fi.absoluteFilePath());
   936         }
  1026         }
   937     }
  1027     }
       
  1028 }
       
  1029 
       
  1030 void QDeclarativeViewer::openUrl()
       
  1031 {
       
  1032     QString cur = canvas->source().toLocalFile();
       
  1033     QString url= QInputDialog::getText(this, tr("Open QML file"), tr("URL of main QML file:"), QLineEdit::Normal, cur);
       
  1034     if (!url.isEmpty())
       
  1035         open(url);
   938 }
  1036 }
   939 
  1037 
   940 void QDeclarativeViewer::statusChanged()
  1038 void QDeclarativeViewer::statusChanged()
   941 {
  1039 {
   942     if (canvas->status() == QDeclarativeView::Error && tester)
  1040     if (canvas->status() == QDeclarativeView::Error && tester)
  1108 
  1206 
  1109 bool QDeclarativeViewer::event(QEvent *event)
  1207 bool QDeclarativeViewer::event(QEvent *event)
  1110 {
  1208 {
  1111     if (event->type() == QEvent::WindowActivate) {
  1209     if (event->type() == QEvent::WindowActivate) {
  1112         Runtime::instance()->setActiveWindow(true);
  1210         Runtime::instance()->setActiveWindow(true);
       
  1211         DeviceOrientation::instance()->resumeListening();
  1113     } else if (event->type() == QEvent::WindowDeactivate) {
  1212     } else if (event->type() == QEvent::WindowDeactivate) {
  1114         Runtime::instance()->setActiveWindow(false);
  1213         Runtime::instance()->setActiveWindow(false);
       
  1214         DeviceOrientation::instance()->pauseListening();
  1115     }
  1215     }
  1116     return QWidget::event(event);
  1216     return QWidget::event(event);
  1117 }
  1217 }
  1118 
  1218 
  1119 void QDeclarativeViewer::senseImageMagick()
  1219 void QDeclarativeViewer::senseImageMagick()
  1317 
  1417 
  1318 void QDeclarativeViewer::changeOrientation(QAction *action)
  1418 void QDeclarativeViewer::changeOrientation(QAction *action)
  1319 {
  1419 {
  1320     if (!action)
  1420     if (!action)
  1321         return;
  1421         return;
       
  1422     QString o = action->text();
  1322     action->setChecked(true);
  1423     action->setChecked(true);
  1323 
  1424 #if defined(Q_WS_S60)
  1324     QString o = action->text();
  1425     CAknAppUi *appUi = static_cast<CAknAppUi *>(CEikonEnv::Static()->AppUi());
       
  1426     if (appUi) {
       
  1427         CAknAppUi::TAppUiOrientation orientation = appUi->Orientation();
       
  1428         if (o == QLatin1String("Auto-orientation")) {
       
  1429             appUi->SetOrientationL(CAknAppUi::EAppUiOrientationAutomatic);
       
  1430             rotateAction->setVisible(false);
       
  1431         } else if (o == QLatin1String("Portrait")) {
       
  1432             appUi->SetOrientationL(CAknAppUi::EAppUiOrientationPortrait);
       
  1433             rotateAction->setVisible(true);
       
  1434         } else if (o == QLatin1String("Landscape")) {
       
  1435             appUi->SetOrientationL(CAknAppUi::EAppUiOrientationLandscape);
       
  1436             rotateAction->setVisible(true);
       
  1437         }
       
  1438     }
       
  1439 #else
  1325     if (o == QLatin1String("Portrait"))
  1440     if (o == QLatin1String("Portrait"))
  1326         DeviceOrientation::instance()->setOrientation(DeviceOrientation::Portrait);
  1441         DeviceOrientation::instance()->setOrientation(DeviceOrientation::Portrait);
  1327     else if (o == QLatin1String("Landscape"))
  1442     else if (o == QLatin1String("Landscape"))
  1328         DeviceOrientation::instance()->setOrientation(DeviceOrientation::Landscape);
  1443         DeviceOrientation::instance()->setOrientation(DeviceOrientation::Landscape);
  1329     else if (o == QLatin1String("Portrait (inverted)"))
  1444     else if (o == QLatin1String("Portrait (inverted)"))
  1330         DeviceOrientation::instance()->setOrientation(DeviceOrientation::PortraitInverted);
  1445         DeviceOrientation::instance()->setOrientation(DeviceOrientation::PortraitInverted);
  1331     else if (o == QLatin1String("Landscape (inverted)"))
  1446     else if (o == QLatin1String("Landscape (inverted)"))
  1332         DeviceOrientation::instance()->setOrientation(DeviceOrientation::LandscapeInverted);
  1447         DeviceOrientation::instance()->setOrientation(DeviceOrientation::LandscapeInverted);
       
  1448 #endif
  1333 }
  1449 }
  1334 
  1450 
  1335 void QDeclarativeViewer::orientationChanged()
  1451 void QDeclarativeViewer::orientationChanged()
  1336 {
  1452 {
  1337     updateSizeHints();
  1453     updateSizeHints();