From 7f5f3a8bfc6eabb1df3d5de49b49fe1fb5931683 Mon Sep 17 00:00:00 2001 From: Anmol Gautam Date: Sun, 3 Jun 2018 18:24:03 +0530 Subject: [PATCH] corrected windowId(s) for qml plugins --- src/lib/app/mainapplication.cpp | 8 ++++ src/lib/app/mainapplication.h | 3 ++ src/lib/plugins/qml/api/tabs/qmltabs.cpp | 53 +++++++----------------- src/lib/plugins/qml/api/tabs/qmltabs.h | 1 - 4 files changed, 25 insertions(+), 40 deletions(-) diff --git a/src/lib/app/mainapplication.cpp b/src/lib/app/mainapplication.cpp index 4f8232dff..3e5079c11 100644 --- a/src/lib/app/mainapplication.cpp +++ b/src/lib/app/mainapplication.cpp @@ -103,6 +103,7 @@ MainApplication::MainApplication(int &argc, char** argv) , m_desktopNotifications(0) , m_webProfile(0) , m_autoSaver(0) + , m_newWindowId(0) #if defined(Q_OS_WIN) && !defined(Q_OS_OS2) , m_registerQAppAssociation(0) #endif @@ -406,6 +407,11 @@ QList MainApplication::windows() const return m_windows; } +QHash MainApplication::windowIdHash() const +{ + return m_windowIdHash; +} + BrowserWindow* MainApplication::getWindow() const { if (m_lastActiveWindow) { @@ -425,6 +431,7 @@ BrowserWindow* MainApplication::createWindow(Qz::BrowserWindowType type, const Q connect(window, SIGNAL(destroyed(QObject*)), this, SLOT(windowDestroyed(QObject*))); m_windows.prepend(window); + m_windowIdHash.insert(window, m_newWindowId++); return window; } @@ -868,6 +875,7 @@ void MainApplication::windowDestroyed(QObject* window) Q_ASSERT(m_windows.contains(static_cast(window))); m_windows.removeOne(static_cast(window)); + m_windowIdHash.remove(static_cast(window)); } void MainApplication::onFocusChanged() diff --git a/src/lib/app/mainapplication.h b/src/lib/app/mainapplication.h index b6dca475b..82d4ce434 100644 --- a/src/lib/app/mainapplication.h +++ b/src/lib/app/mainapplication.h @@ -76,6 +76,7 @@ public: int windowCount() const; QList windows() const; + QHash windowIdHash() const; BrowserWindow* getWindow() const; BrowserWindow* createWindow(Qz::BrowserWindowType type, const QUrl &startUrl = QUrl()); @@ -188,6 +189,8 @@ private: ProxyStyle *m_proxyStyle = nullptr; QList m_windows; + int m_newWindowId; + QHash m_windowIdHash; QPointer m_lastActiveWindow; QList m_postLaunchActions; diff --git a/src/lib/plugins/qml/api/tabs/qmltabs.cpp b/src/lib/plugins/qml/api/tabs/qmltabs.cpp index b869d7b23..4db0b8b96 100644 --- a/src/lib/plugins/qml/api/tabs/qmltabs.cpp +++ b/src/lib/plugins/qml/api/tabs/qmltabs.cpp @@ -27,23 +27,6 @@ QmlTabs::QmlTabs(QObject *parent) } connect(mApp->plugins(), &PluginProxy::mainWindowCreated, this, &QmlTabs::windowCreated); - - connect(mApp->plugins(), &PluginProxy::mainWindowDeleted, this, [this](BrowserWindow *window){ - // FIXME: make it more efficient - windowIdHash.remove(window); - // If the window is not destroyed (which is almost always), - // then remove bias(= 1) from the index of window in the list - // for the correct index - int bias = 0; - for (int i = 0; i < mApp->windowCount(); i++) { - BrowserWindow *windowAtI = mApp->windows().at(i); - if (windowAtI == window) { - bias = 1; - continue; - } - windowIdHash[windowAtI] = i - bias; - } - }); } bool QmlTabs::setCurrentIndex(const QVariantMap &map) @@ -326,54 +309,46 @@ BrowserWindow *QmlTabs::getWindow(const QVariantMap &map) const { int windowId = map.value(QSL("windowId"), -1).toInt(); - if (windowId < -1 || windowId >= mApp->windowCount()) { - qWarning() << "Unable to get window" << "windowId is out of bounds"; - return nullptr; - } - - BrowserWindow *window; if (windowId == -1) { - window = mApp->getWindow(); - } else { - window = mApp->windows().at(windowId); + return mApp->getWindow(); } - return window; + for(BrowserWindow *window : mApp->windowIdHash().keys()) { + if (mApp->windowIdHash().value(window) == windowId) { + return window; + } + } + + qWarning() << "Unable to get window with given windowId"; + return nullptr; } void QmlTabs::windowCreated(BrowserWindow *window) { - // FIXME: make it more efficient - for (int i = 0; i < mApp->windowCount(); i++) { - windowIdHash[mApp->windows().at(i)] = i; - } + int windowId = mApp->windowIdHash().value(window); - connect(window->tabWidget(), &TabWidget::changed, this, [this, window]{ + connect(window->tabWidget(), &TabWidget::changed, this, [this, windowId]{ QVariantMap map; - int windowId = windowIdHash.value(window); map.insert(QSL("windowId"), windowId); emit changed(map); }); - connect(window->tabWidget(), &TabWidget::tabInserted, this, [this, window](int index){ + connect(window->tabWidget(), &TabWidget::tabInserted, this, [this, windowId](int index){ QVariantMap map; - int windowId = windowIdHash.value(window); map.insert(QSL("windowId"), windowId); map.insert(QSL("index"), index); emit tabInserted(map); }); - connect(window->tabWidget(), &TabWidget::tabRemoved, this, [this, window](int index){ + connect(window->tabWidget(), &TabWidget::tabRemoved, this, [this, windowId](int index){ QVariantMap map; - int windowId = windowIdHash.value(window); map.insert(QSL("windowId"), windowId); map.insert(QSL("index"), index); emit tabRemoved(map); }); - connect(window->tabWidget(), &TabWidget::tabMoved, this, [this, window](int from, int to){ + connect(window->tabWidget(), &TabWidget::tabMoved, this, [this, windowId](int from, int to){ QVariantMap map; - int windowId = windowIdHash.value(window); map.insert(QSL("windowId"), windowId); map.insert(QSL("from"), from); map.insert(QSL("to"), to); diff --git a/src/lib/plugins/qml/api/tabs/qmltabs.h b/src/lib/plugins/qml/api/tabs/qmltabs.h index f189c2dc1..33ae13de7 100644 --- a/src/lib/plugins/qml/api/tabs/qmltabs.h +++ b/src/lib/plugins/qml/api/tabs/qmltabs.h @@ -51,5 +51,4 @@ Q_SIGNALS: private: BrowserWindow *getWindow(const QVariantMap &map) const; void windowCreated(BrowserWindow *window); - QHash windowIdHash; };