diff options
author | Svetlana Abramenkova <sabramenkova@luxoft.com> | 2019-12-20 15:52:20 +0300 |
---|---|---|
committer | Svetlana Abramenkova <sabramenkova@luxoft.com> | 2020-01-17 19:34:09 +0300 |
commit | 6b71d03501bd933eebc4ea4390711893ef82364c (patch) | |
tree | 8c3a989c28eec233c4c133bfed47aac8d71f2f57 | |
parent | dced530910a158d72b5168a29f65c418fc705151 (diff) |
MainWindow refactoring
Removed WindowView from MainWindow which doesn't use now, moved preview generator into a separate window,
because of it - removed MasterRuntime
Change-Id: I815e23a554e95622fab4153a2f39b968cbe64b0c
Reviewed-by: Nikolay Zamotaev <nzamotaev@luxoft.com>
-rw-r--r-- | src/bench/benchlivenodeengine.cpp | 24 | ||||
-rw-r--r-- | src/bench/benchlivenodeengine.h | 3 | ||||
-rw-r--r-- | src/bench/main.cpp | 1 | ||||
-rw-r--r-- | src/bench/mainwindow.cpp | 19 | ||||
-rw-r--r-- | src/bench/mainwindow.h | 6 | ||||
-rw-r--r-- | src/bench/runtimemanager.cpp | 135 | ||||
-rw-r--r-- | src/bench/runtimemanager.h | 10 | ||||
-rw-r--r-- | src/bench/runtimeprocess.cpp | 13 | ||||
-rw-r--r-- | src/bench/runtimeprocess.h | 3 | ||||
-rw-r--r-- | src/widgets/widgets.pri | 6 | ||||
-rw-r--r-- | src/widgets/windowwidget.cpp | 303 | ||||
-rw-r--r-- | src/widgets/windowwidget.h | 71 |
12 files changed, 11 insertions, 583 deletions
diff --git a/src/bench/benchlivenodeengine.cpp b/src/bench/benchlivenodeengine.cpp index 8c481a7..b260ab8 100644 --- a/src/bench/benchlivenodeengine.cpp +++ b/src/bench/benchlivenodeengine.cpp @@ -34,7 +34,6 @@ #include "directorypreviewadapter.h" #include "previewimageprovider.h" #include "qmlpreviewadapter.h" -#include "widgets/windowwidget.h" #include "liveruntime.h" #include "widgets/workspaceview.h" @@ -43,7 +42,6 @@ BenchLiveNodeEngine::BenchLiveNodeEngine(QObject *parent) : LiveNodeEngine(parent), - m_ww(0), m_imageProvider(new PreviewImageProvider(this)), m_workspaceView(0), m_clipToRootObject(false) @@ -59,11 +57,6 @@ BenchLiveNodeEngine::~BenchLiveNodeEngine() delete fallbackView(); } -void BenchLiveNodeEngine::setWindowWidget(WindowWidget *widget) -{ - m_ww = widget; -} - void BenchLiveNodeEngine::setWorkspaceView(WorkspaceView *view) { m_workspaceView = view; @@ -129,22 +122,5 @@ void BenchLiveNodeEngine::initPlugins() void BenchLiveNodeEngine::reloadHelper() { - // avoid flickering - fallbackView()->resize(m_ww->size()); - LiveNodeEngine::reloadDocument(); - - QAbstractScrollArea *scroller = m_ww; - - ContentAdapterInterface *adapter = activePlugin(); - if (adapter && adapter->isFullScreen()) { - m_ww->setCenteringEnabled(false); - scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - scroller->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - } else { - m_ww->setCenteringEnabled(true); - scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); - scroller->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); - } - m_ww->forceInitialResize(); } diff --git a/src/bench/benchlivenodeengine.h b/src/bench/benchlivenodeengine.h index b11d641..b55380f 100644 --- a/src/bench/benchlivenodeengine.h +++ b/src/bench/benchlivenodeengine.h @@ -34,7 +34,6 @@ #include "livenodeengine.h" -class WindowWidget; class PreviewImageProvider; class WorkspaceView; class BenchLiveNodeEngine : public LiveNodeEngine @@ -46,7 +45,6 @@ public: ~BenchLiveNodeEngine(); void setWorkspaceView(WorkspaceView* view); - void setWindowWidget(WindowWidget *widget); public slots: void refresh(); @@ -60,7 +58,6 @@ private Q_SLOTS: QImage convertIconToImage(const QFileInfo& info, const QSize& requestedSize); private: - WindowWidget* m_ww; QPointer<PreviewImageProvider> m_imageProvider; WorkspaceView* m_workspaceView; bool m_clipToRootObject; diff --git a/src/bench/main.cpp b/src/bench/main.cpp index 1687803..4485cdf 100644 --- a/src/bench/main.cpp +++ b/src/bench/main.cpp @@ -413,6 +413,7 @@ MasterApplication::MasterApplication(int &argc, char **argv) if (!options()->noRemote()) listenForArguments(); } + connect(m_window, &MainWindow::quitEvent, this, &MasterApplication::quit); } MasterApplication::~MasterApplication() diff --git a/src/bench/mainwindow.cpp b/src/bench/mainwindow.cpp index 98db53d..a7cc0b3 100644 --- a/src/bench/mainwindow.cpp +++ b/src/bench/mainwindow.cpp @@ -31,7 +31,6 @@ ****************************************************************************/ #include "mainwindow.h" -#include "widgets/windowwidget.h" #include <QToolBar> #include <QtNetwork> @@ -132,7 +131,6 @@ MainWindow::MainWindow(QWidget *parent) connect(m_workspaceView, &WorkspaceView::pathActivated, m_hostManager, &HostManager::followTreeSelection); connect(m_hub, &LiveHubEngine::activateDocument, this, &MainWindow::updateWindowTitle); connect(m_hub, &LiveHubEngine::activateDocument, m_node, &LiveNodeEngine::loadDocument); - connect(m_node, &LiveNodeEngine::activeWindowChanged, this, &MainWindow::onActiveWindowChanged); connect(m_node->qmlEngine(), &QQmlEngine::quit, this, &MainWindow::logQuitEvent); connect(m_allHosts, &AllHostsWidget::publishAll, m_hostManager, &HostManager::publishAll); connect(m_allHosts, &AllHostsWidget::currentFileChanged, m_hostManager, &HostManager::setCurrentFile); @@ -140,7 +138,6 @@ MainWindow::MainWindow(QWidget *parent) connect(m_hostManager, &HostManager::logWidgetAdded, this, &MainWindow::onLogWidgetAdded); connect(m_hostManager, &HostManager::openHostConfig, this, &MainWindow::openPreferences); connect(m_newWorkspaceConfigWizard, &NewProjectWizard::accepted, this, &MainWindow::newWorkspaceConfig); - connect(m_workspaceView, &WorkspaceView::pathActivated, m_runtimeManager, &RuntimeManager::setPrimeCurrentFile); connect(m_workspaceView, &WorkspaceView::newRuntimeWindow, m_runtimeManager, &RuntimeManager::newRuntimeWindow); connect(m_workspaceView, &WorkspaceView::initConnectToServer, m_runtimeManager, &RuntimeManager::initConnectToServer); connect(m_runtimeManager, &RuntimeManager::logWidgetAdded, this, &MainWindow::onLogWidgetAdded); @@ -164,23 +161,10 @@ void MainWindow::setupContent() setupWorkspaceView(); setupHostView(); - m_ww = new WindowWidget(this); - m_ww->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); - m_ww->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); - m_ww->setBackgroundRole(QPalette::Dark); - m_node->setWindowWidget(m_ww); - m_ww->setHidden(true); - m_workspaceView->setWindowTitle("Workspace"); setCentralWidget(m_workspaceView); } -void MainWindow::onActiveWindowChanged(QQuickWindow *activeWindow) -{ - m_ww->setCenteringEnabled(true); - m_ww->setHostedWindow(activeWindow); -} - void MainWindow::onLogWidgetAdded(QDockWidget *logDock) { m_logDockMenu->addAction(logDock->toggleViewAction()); @@ -396,7 +380,6 @@ void MainWindow::init() m_workspaceView->restoreFromSettings(&s); m_runtimeManager->setWorkspace(m_workspacePath); - m_runtimeManager->startPrimeRuntime(); m_initialized = true; } @@ -462,7 +445,6 @@ void MainWindow::setupToolBar() void MainWindow::activateDocument(const LiveDocument &path) { m_workspaceView->activateDocument(path); - m_runtimeManager->setPrimeCurrentFile(path); } void MainWindow::takeSnapshot() @@ -536,6 +518,7 @@ void MainWindow::closeEvent(QCloseEvent *event) m_closeEvent = true; writeSettings(); m_runtimeManager->finishProcesses(); + emit quitEvent(); QMainWindow::closeEvent(event); } diff --git a/src/bench/mainwindow.h b/src/bench/mainwindow.h index 7ed74eb..bd6ba79 100644 --- a/src/bench/mainwindow.h +++ b/src/bench/mainwindow.h @@ -44,7 +44,6 @@ class LiveDocument; class LiveRuntime; class LiveHubEngine; class BenchLiveNodeEngine; -class WindowWidget; class HostModel; class HostManager; class AllHostsWidget; @@ -77,6 +76,9 @@ public: public slots: void setImportPaths(const QStringList& pathList); +signals: + void quitEvent(); + protected: void closeEvent(QCloseEvent *event); void showEvent(QShowEvent *event); @@ -106,7 +108,6 @@ private slots: void openWorkspaceConfig(); void newWorkspaceConfigWizard(); void newWorkspaceConfig(); - void onActiveWindowChanged(QQuickWindow *activeWindow); void onLogWidgetAdded(QDockWidget* logDock); void onLogWidgetRemoved(QDockWidget* logDock); @@ -115,7 +116,6 @@ private: private: bool m_initialized; - WindowWidget *m_ww; WorkspaceView *m_workspaceView; QString m_workspacePath; LogView *m_log; diff --git a/src/bench/runtimemanager.cpp b/src/bench/runtimemanager.cpp index e45f5c5..2bf7d5d 100644 --- a/src/bench/runtimemanager.cpp +++ b/src/bench/runtimemanager.cpp @@ -41,10 +41,7 @@ RuntimeManager::RuntimeManager(QObject *parent) : QObject(parent) ,m_currentFile(nullptr) ,m_imports(nullptr) ,m_pluginPath(nullptr) - ,m_primeRuntimeConnected(false) - ,m_primeCurrentFile(nullptr) { - m_primeRuntime = new RuntimeProcess(this, Constants::PRIMERUNTIME_PORT(), true); QSettings s; QString path = s.value(Constants::RUNTIME_SETTINGS_KEY()).toString(); if (!path.isEmpty()) { @@ -53,36 +50,12 @@ RuntimeManager::RuntimeManager(QObject *parent) : QObject(parent) // Assuming the qmlliveruntime binary located at the same folder with qmllivebench m_runtimeBinaryPath = Constants::DEFAULT_RUNTIME_LOCATION(); } - connect(m_primeRuntime, &RuntimeProcess::errorOccurred, this, &RuntimeManager::onPrimeRuntimeError); - connect(m_primeRuntime, &RuntimeProcess::stateChanged, this, &RuntimeManager::onPrimeRuntimeChanged); } RuntimeManager::~RuntimeManager() { finishProcesses(); } -void RuntimeManager::startPrimeRuntime() -{ - QDockWidget *dock =new QDockWidget("PrimeRuntime"); - dock->setObjectName("PrimeRuntimeLogDock"); - LogView *view = new LogView(false, dock); - connect(m_primeRuntime, &RuntimeProcess::remoteLog, view, &LogView::appendToLog); - connect(m_primeRuntime, &RuntimeProcess::clearLog, view, &LogView::clear); - connect(m_primeRuntime, &RuntimeProcess::connected, view, &LogView::clear); - connect(m_primeRuntime, &RuntimeProcess::connected, this, &RuntimeManager::onPrimeRuntimeConnected); - dock->setWidget(view); - emit logWidgetAdded(dock); - - QStringList arguments = argumentsList(Constants::PRIMERUNTIME_PORT(), "Prime QML Live Runtime", true); - - if (m_engine == nullptr) { - qWarning() << "Failed to start Prime QML Live Runtime: nullptr QML engine object"; - return; - } - m_primeRuntime->setLiveHubEngine(m_engine); - m_primeRuntime->start(m_runtimeBinaryPath, arguments); -} - void RuntimeManager::setRuntimeBinaryPath(const QString &path) { m_runtimeBinaryPath = path; @@ -93,64 +66,9 @@ void RuntimeManager::setLiveHubEngine(LiveHubEngine *engine) m_engine = engine; } -void RuntimeManager::onPrimeRuntimeError(QProcess::ProcessError error) -{ - if (error == QProcess::ProcessError::Crashed) { - qWarning() << "QML Live Prime Runtime crashed with QProcess::ExitStatus = " << m_primeRuntime->exitStatus(); - } else { - qWarning() << "RuntimeManager::onPrimeRuntimeError: " << error; - } -} - void RuntimeManager::setWorkspace(const QString &workspace) { m_workspace = workspace; - m_primeRuntime->setWorkspace(workspace); -} - -void RuntimeManager::onPrimeRuntimeChanged() -{ - switch (m_primeRuntime->state()) { - case QProcess::Running: - qInfo() << "Prime QML Live Runtime RUNNING"; - m_primeRuntime->connectToServer(); - break; - case QProcess::Starting: - qInfo() << "Prime QML Live Runtime STARTING"; - break; - case QProcess::NotRunning: - if (QFileInfo(m_runtimeBinaryPath).exists()) { - qWarning() << "Prime QML Live Runtime NOT RUNNING: failed to start qmlliveruntime at: "<<m_runtimeBinaryPath<<" - you may have insufficient permissions to invoke it."; - } - else { - qWarning() << "Prime QML Live Runtime NOT RUNNING: qmlliveruntime is missing at: "<<m_runtimeBinaryPath; - qWarning() << "Please specify qmlliveruntime binary location at 'Preferences->QML Live Runtime'"; - } - break; - default: - break; - } -} - -void RuntimeManager::setPrimeCurrentFile(const LiveDocument ¤tFile) -{ - if (!currentFile.isFileIn(m_engine->workspace())) { - qCritical() << "The selected file for preview is not located in the workspace. File: " << currentFile.relativeFilePath(); - return; - } - - if (m_primeRuntime->state() != QProcess::Running) { - QStringList arguments = argumentsList(Constants::PRIMERUNTIME_PORT(), "Prime QML Live Runtime", true); - - if (m_engine == nullptr) { - qCritical() << "Failed to start Prime QML Live Runtime: nullptr QML engine object"; - return; - } - - m_primeRuntime->setLiveHubEngine(m_engine); - m_primeRuntime->start(m_runtimeBinaryPath, arguments); - } - m_primeRuntime->setCurrentFile(currentFile); } void RuntimeManager::initConnectToServer() @@ -225,15 +143,6 @@ void RuntimeManager::finishProcesses() } } } - - if (m_primeRuntime->state() == QProcess::Running || m_primeRuntime->state() == QProcess::Starting){ - m_primeRuntime->terminate(); - if (!m_primeRuntime->waitForFinished(1000)) { - qInfo() << "QML Live Prime Runtime with port " + QString::number(m_primeRuntime->port()) + " was not finished within 500 msec, KILLING..."; - m_primeRuntime->kill(); - } - } - delete m_primeRuntime; } QStringList RuntimeManager::argumentsList(const int& port, const QString& title, const bool hideButtons) @@ -263,28 +172,6 @@ QStringList RuntimeManager::argumentsList(const int& port, const QString& title, void RuntimeManager::updateRuntimePath(const QString& path) { m_runtimeBinaryPath = path; - if (m_primeRuntime->state() == QProcess::NotRunning){ - restartPrimeRuntime(); - } -} - -void RuntimeManager::restartPrimeRuntime() -{ - QStringList arguments = argumentsList(Constants::PRIMERUNTIME_PORT(), "Prime QML Live Runtime", true); - - if (m_engine == nullptr) { - qWarning()<<"Failed to start Prime QML Live Runtime: nullptr QML engine object"; - return; - } - if (m_primeRuntime->state() == QProcess::Running) { - m_primeRuntime->terminate(); - } - if (!m_primeRuntime->waitForFinished(1000)) { - m_primeRuntime->kill(); - } - - m_primeRuntime->setLiveHubEngine(m_engine); - m_primeRuntime->start(m_runtimeBinaryPath, arguments); } void RuntimeManager::onError(QProcess::ProcessError error) @@ -321,26 +208,4 @@ void RuntimeManager::restartAll() for (int i = 0; i < m_logDocks.count(); i++) { emit logWidgetRemoved(m_logDocks.at(i)); } - - stopPrimeRuntime(); -} - -void RuntimeManager::stopPrimeRuntime() -{ - if (m_primeRuntime->state() == QProcess::Running) { - m_primeRuntime->terminate(); - } - if (!m_primeRuntime->waitForFinished(1000)) { - m_primeRuntime->kill(); - } - m_primeRuntime->clearLog(); - m_primeRuntime->remoteLog(LogView::InternalMsgType::InternalInfo, "Please select file for preview."); -} - -void RuntimeManager::onPrimeRuntimeConnected() -{ - qInfo() << "PrimeRuntime connected "; - m_primeRuntimeConnected = true; - if (m_primeCurrentFile != nullptr) - m_primeRuntime->setCurrentFile(*m_primeCurrentFile); } diff --git a/src/bench/runtimemanager.h b/src/bench/runtimemanager.h index fe04f30..df7eadb 100644 --- a/src/bench/runtimemanager.h +++ b/src/bench/runtimemanager.h @@ -46,7 +46,6 @@ public: explicit RuntimeManager(QObject *parent = nullptr); ~RuntimeManager(); - void startPrimeRuntime(); void setRuntimeBinaryPath(const QString &path); void setLiveHubEngine(LiveHubEngine *engine); void setWorkspace(const QString &workspace); @@ -57,18 +56,12 @@ public: private: QStringList argumentsList(const int& port, const QString& titles, const bool hideButtons = false); - void restartPrimeRuntime(); - void stopPrimeRuntime(); signals: void logWidgetAdded(QDockWidget* log); void logWidgetRemoved(QDockWidget* log); public slots: - void onPrimeRuntimeError(QProcess::ProcessError error); - void onPrimeRuntimeChanged(); - void onPrimeRuntimeConnected(); - void setPrimeCurrentFile(const LiveDocument ¤tFile); void newRuntimeWindow(const QString &document); void initConnectToServer(); void onConnected(); @@ -80,7 +73,6 @@ private: int m_port; QString m_runtimeBinaryPath; QString m_workspace; - RuntimeProcess *m_primeRuntime; //this prime runtime supposed to be QML Live runtime which follows the activeDocumetChanged Slot LiveHubEngine *m_engine; LiveDocument* m_currentFile; RuntimeProcess* m_curproc; @@ -88,8 +80,6 @@ private: QString* m_pluginPath; QList<QDockWidget*> m_logDocks; QList<RuntimeProcess*> m_runtimes; - bool m_primeRuntimeConnected; - LiveDocument* m_primeCurrentFile; }; #endif // RUNTIMEMANAGER_H diff --git a/src/bench/runtimeprocess.cpp b/src/bench/runtimeprocess.cpp index 2f61d99..87efb18 100644 --- a/src/bench/runtimeprocess.cpp +++ b/src/bench/runtimeprocess.cpp @@ -32,9 +32,8 @@ #include "runtimeprocess.h" #include "livehubengine.h" -RuntimeProcess::RuntimeProcess(QObject *parent, int port, bool ismaster) : +RuntimeProcess::RuntimeProcess(QObject *parent, int port) : QProcess(parent) - ,m_ismaster(ismaster) ,m_doNotConnect(false) ,m_port(port) ,m_engine(nullptr) @@ -85,10 +84,6 @@ void RuntimeProcess::setLiveHubEngine(LiveHubEngine *engine) m_publisher.setWorkspace(m_engine->workspace()); connect(m_engine.data(), &LiveHubEngine::workspaceChanged, &m_publisher, &RemotePublisher::setWorkspace); - if (m_ismaster) { - connect(m_engine.data(), &LiveHubEngine::fileChanged, &m_publisher, &RemotePublisher::activateDocument); - } - connect(m_engine.data(), &LiveHubEngine::activateDocument, &m_publisher, &RemotePublisher::activateDocument); connect(m_engine.data(), &LiveHubEngine::beginPublishWorkspace, &m_publisher, &RemotePublisher::beginBulkSend); connect(m_engine.data(), &LiveHubEngine::endPublishWorkspace, &m_publisher, &RemotePublisher::endBulkSend); @@ -130,11 +125,9 @@ void RuntimeProcess::onConnected() void RuntimeProcess::onDisconnected() { - qInfo()<<"RuntimeProcess::onDisconnected ismaster="<<m_ismaster; - if (m_ismaster && !m_doNotConnect){ - connectToServer(); - } + qInfo()<<"RuntimeProcess::onDisconnected "; } + void RuntimeProcess::onConnectionError(QAbstractSocket::SocketError error) { qWarning()<<"RuntimeProcess::onConnectionError: Host connection error: "<<error; diff --git a/src/bench/runtimeprocess.h b/src/bench/runtimeprocess.h index f5522ce..6d66408 100644 --- a/src/bench/runtimeprocess.h +++ b/src/bench/runtimeprocess.h @@ -40,7 +40,7 @@ class RuntimeProcess : public QProcess { Q_OBJECT public: - explicit RuntimeProcess(QObject *parent = nullptr, int port = Constants::DEFAULT_BENCH_PORT(), bool ismaster = false); + explicit RuntimeProcess(QObject *parent = nullptr, int port = Constants::DEFAULT_BENCH_PORT()); ~RuntimeProcess(); void setCurrentFile(const LiveDocument ¤tFile); @@ -64,7 +64,6 @@ private Q_SLOTS: void updateErrors(); void onStateChanged(); private: - bool m_ismaster; bool m_doNotConnect; int m_port; RemotePublisher m_publisher; diff --git a/src/widgets/widgets.pri b/src/widgets/widgets.pri index 2089397..0089f79 100644 --- a/src/widgets/widgets.pri +++ b/src/widgets/widgets.pri @@ -4,12 +4,10 @@ SOURCES += \ $$PWD/logview.cpp \ $$PWD/workspaceview.cpp \ $$PWD/filesystemmodel.cpp \ - $$PWD/workspacedelegate.cpp \ - $$PWD/windowwidget.cpp + $$PWD/workspacedelegate.cpp HEADERS += \ $$PWD/logview.h \ $$PWD/workspaceview.h \ $$PWD/filesystemmodel.h \ - $$PWD/workspacedelegate.h \ - $$PWD/windowwidget.h + $$PWD/workspacedelegate.h diff --git a/src/widgets/windowwidget.cpp b/src/widgets/windowwidget.cpp deleted file mode 100644 index d01c752..0000000 --- a/src/widgets/windowwidget.cpp +++ /dev/null @@ -1,303 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2019 Luxoft Sweden AB -** Copyright (C) 2018 Pelagicore AG -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QML Live tool. -** -** $QT_BEGIN_LICENSE:GPL-QTAS$ -** Commercial License Usage -** Licensees holding valid commercial Qt Automotive Suite licenses may use -** this file in accordance with the commercial license agreement provided -** with the Software or, alternatively, in accordance with the terms -** contained in a written agreement between you and The Qt Company. For -** licensing terms and conditions see https://www.qt.io/terms-conditions. -** For further information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 or (at your option) any later version -** approved by the KDE Free Qt Foundation. The licenses are as published by -** the Free Software Foundation and appearing in the file LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -** SPDX-License-Identifier: GPL-3.0 -** -****************************************************************************/ - -#include "windowwidget.h" -#include <QQuickView> -#include <QQuickItem> -#include <QGuiApplication> -#include <QDebug> -#include <QScrollBar> -#include <QResizeEvent> - -WindowWidget::WindowWidget(QWidget *parent) : - QAbstractScrollArea(parent), m_hostedWindow(0), m_resizing(false), m_centering(false) -{ - setFrameShape(QFrame::NoFrame); -} - -void WindowWidget::setHostedWindow(QQuickWindow *hostedWindow) -{ - if (m_hostedWindow == hostedWindow) - return; - - if (m_hostedWindow) { - m_hostedWindow->removeEventFilter(this); - m_hostedWindow->hide(); - m_hostedWindow->setParent(0); - } - - m_hostedWindow = hostedWindow; - - if (m_hostedWindow) { - m_hostedWindow->installEventFilter(this); - - // force creation of the TL window in order to get its handle - if (!testAttribute(Qt::WA_WState_Created)) - create(0, true, true); - - if (!windowHandle()) - qWarning("Could not get a valid windowhandle for our widget based window"); - - // Ensure the native platform resources are not allocated before - // embedding the window. Otherwise it cannot be embedded reliably. - // - // For some reason this only happens with QQuickWindow (or the subclass - // actually instantiated by QML Window element) and not with QQuickView. - // - // At least one unfortunate side effect exists: geometry is not - // preserved due to the way it is stored (see QWindow::resize(QSize)) - // while this is needed for the QQuickView's auto resizing to work - // correctly. - // - // This is the rationale why it is done and why it is done conditionally. - // - // Another options would be: - // - // a) Remember and reset the geometry here - // b) Place the QWindow::destroy() call directly after loading a - // QQuickWindow from QML in LiveNodeEngine::reloadDocument() - // c) Eliminate what makes this only happen with QQuickWindow loaded - // from QML - if (!qobject_cast<QQuickView *>(m_hostedWindow)) { - m_hostedWindow->destroy(); - } - - m_hostedWindow->setFlags(Qt::Tool | Qt::FramelessWindowHint); - m_hostedWindow->setParent(windowHandle()); - m_hostedWindow->setVisible(isVisible()); - - viewport()->setBackgroundRole(backgroundRole()); - viewport()->setPalette(palette()); - viewport()->setBackgroundRole(QPalette::Background); - setFocusPolicy(Qt::StrongFocus); - m_hostedWindow->show(); - } -} - -QQuickWindow *WindowWidget::hostedWindow() const -{ - return m_hostedWindow; -} - -void WindowWidget::setVisible(bool visible) -{ - QAbstractScrollArea::setVisible(visible); - if (m_hostedWindow) - m_hostedWindow->setVisible(visible); -} - -void WindowWidget::setCenteringEnabled(bool enabled) -{ - m_centering = enabled; - updateScrollBars(); -} - -QSize WindowWidget::sizeHint() const -{ - QSize s = qmlSize(); - - return s + QSize(frameWidth() * 2, frameWidth() * 2); -} - -void WindowWidget::forceInitialResize() -{ - if (QQuickView *view = qobject_cast<QQuickView *>(m_hostedWindow)) { - if (view->resizeMode() == QQuickView::SizeRootObjectToView) - view->resize(size()); - } - updateGeometry(); - updateScrollBars(); - emit widthChanged(width()); - emit heightChanged(height()); - -} - -bool WindowWidget::event(QEvent *e) -{ - bool handled = false; - - if (m_hostedWindow) { - switch (e->type()) { - case QEvent::Wheel: { - QWheelEvent *oe = static_cast<QWheelEvent *>(e); - if (!viewport()->geometry().contains(oe->pos())) - break; - - QWheelEvent ne(m_hostedWindow->mapFromGlobal(oe->globalPos()), - oe->globalPos(), - oe->pixelDelta(), - oe->angleDelta(), - oe->delta(), - oe->orientation(), - oe->buttons(), - oe->modifiers()); - - qGuiApp->sendEvent(m_hostedWindow, &ne); - handled = true; - break; - } - case QEvent::KeyPress: - case QEvent::KeyRelease: { - qGuiApp->sendEvent(m_hostedWindow, e); - handled = true; - break; - } - case QEvent::MouseButtonPress: - case QEvent::MouseButtonRelease: - case QEvent::MouseButtonDblClick: - case QEvent::MouseMove: { - QMouseEvent *oe = static_cast<QMouseEvent *>(e); - - if (e->type() == QEvent::MouseButtonPress || e->type() == QEvent::MouseButtonDblClick) { - if (!viewport()->geometry().contains(oe->pos())) - break; - } - - QMouseEvent ne(oe->type(), - m_hostedWindow->mapFromGlobal(oe->globalPos()), - m_hostedWindow->mapFromGlobal(oe->globalPos()), - oe->globalPos(), - oe->button(), - oe->buttons(), - oe->modifiers()); - - qGuiApp->sendEvent(m_hostedWindow, &ne); - handled = true; - break; - } - case QEvent::Resize: { - QResizeEvent *re = static_cast<QResizeEvent*>(e); - if (QQuickView *view = qobject_cast<QQuickView *>(m_hostedWindow)) { - if (view->resizeMode() == QQuickView::SizeRootObjectToView) - view->resize(re->size()); - } - updateGeometry(); - updateScrollBars(); - if (re->oldSize().width() != re->size().width()) - emit widthChanged(re->size().width()); - if (re->oldSize().height() != re->size().height()) - emit heightChanged(re->size().height()); - break; - } - case QEvent::Hide: - m_hostedWindow->close(); - break; - default: - break; - } - } - return handled ? true : QAbstractScrollArea::event(e); -} - -bool WindowWidget::eventFilter(QObject *o, QEvent *e) -{ - if (o && o == m_hostedWindow) { - switch (e->type()) { - case QEvent::Resize: - updateScrollBars(); - break; - default: - break; - } - } - return QAbstractScrollArea::eventFilter(o, e); -} - -void WindowWidget::scrollContentsBy(int dx, int dy) -{ - Q_UNUSED(dx) - Q_UNUSED(dy) - - updateWindowPosition(); -} - -#include <QStyle> - -void WindowWidget::updateScrollBars() -{ - if (!m_hostedWindow) - return; - - if (window() && !m_resizing) { - m_resizing = true; - - QSize vpSize = viewport()->size(); - QSize vpMaxSize = maximumViewportSize(); - QSize wSize = qmlSize(); - - // does the window fit without scrollbars? - if (vpMaxSize.expandedTo(wSize) == vpMaxSize) - vpSize = vpMaxSize; - - // fix scrollbars - horizontalScrollBar()->setRange(0, (wSize - vpSize).width()); - horizontalScrollBar()->setPageStep(vpSize.width()); - verticalScrollBar()->setRange(0, (wSize - vpSize).height()); - verticalScrollBar()->setPageStep(vpSize.height()); - - //qWarning() << "wSize:" << wSize << "vpSize:" << vpSize << "vpRect" << viewport()->rect(); - //qWarning() << "Hrange:" << (wSize - vpSize).width() << "Hstep:" << vpSize.width(); - - updateWindowPosition(); - m_resizing = false; - } -} -void WindowWidget::updateWindowPosition() -{ - if (m_hostedWindow && !m_hostedWindow->contentItem()->childItems().isEmpty()) { - QSize wSize = qmlSize(); - Qt::LayoutDirection dir = layoutDirection(); - QRect scrolled = QStyle::visualRect(dir, viewport()->rect(), QRect(QPoint(-horizontalScrollBar()->value(), -verticalScrollBar()->value()), wSize)); - QRect aligned = QStyle::alignedRect(dir, m_centering ? Qt::AlignCenter : Qt::AlignTop | Qt::AlignLeft, wSize, viewport()->rect()); - - //qWarning() << "xAlign:" << aligned.x() << "xScrolled:" << scrolled.x(); - m_hostedWindow->setPosition(wSize.width() < viewport()->width() ? aligned.x() : scrolled.x(), - wSize.height() < viewport()->height() ? aligned.y() : scrolled.y()); - } -} - -QSize WindowWidget::qmlSize() const -{ - QSize s; - if (m_hostedWindow && !m_hostedWindow->contentItem()->childItems().isEmpty()) { - QQuickItem *const rootItem = m_hostedWindow->contentItem()->childItems().first(); - s = QSize(rootItem->width(), rootItem->height()); - } - - if (s.width() <= 20) - s.setWidth(800); - if (s.height() <= 20) - s.setHeight(600); - - return s; -} - diff --git a/src/widgets/windowwidget.h b/src/widgets/windowwidget.h deleted file mode 100644 index d3a5674..0000000 --- a/src/widgets/windowwidget.h +++ /dev/null @@ -1,71 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2019 Luxoft Sweden AB -** Copyright (C) 2018 Pelagicore AG -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QML Live tool. -** -** $QT_BEGIN_LICENSE:GPL-QTAS$ -** Commercial License Usage -** Licensees holding valid commercial Qt Automotive Suite licenses may use -** this file in accordance with the commercial license agreement provided -** with the Software or, alternatively, in accordance with the terms -** contained in a written agreement between you and The Qt Company. For -** licensing terms and conditions see https://www.qt.io/terms-conditions. -** For further information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 or (at your option) any later version -** approved by the KDE Free Qt Foundation. The licenses are as published by -** the Free Software Foundation and appearing in the file LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -** SPDX-License-Identifier: GPL-3.0 -** -****************************************************************************/ - -#pragma once - -#include <QAbstractScrollArea> -#include <QPointer> - -QT_FORWARD_DECLARE_CLASS(QQuickWindow); - -class WindowWidget : public QAbstractScrollArea -{ - Q_OBJECT -public: - explicit WindowWidget(QWidget *parent = 0); - - QQuickWindow *hostedWindow() const; - void setHostedWindow(QQuickWindow *hostedWindow); - void setVisible(bool visible); - void setCenteringEnabled(bool enabled); - QSize sizeHint() const; - void forceInitialResize(); - -Q_SIGNALS: - void widthChanged(int w); - void heightChanged(int w); - -protected: - bool event(QEvent *e); - bool eventFilter(QObject *o, QEvent *e); - void scrollContentsBy(int dx, int dy); - -private: - void updateScrollBars(); - void updateWindowPosition(); - QSize qmlSize() const; - - QPointer<QQuickWindow> m_hostedWindow; - bool m_resizing; - bool m_centering; -}; - |