Falkon Develop
Cross-platform Qt-based web browser
qmlwindows.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "qmlwindows.h"
19#include "mainapplication.h"
20#include "pluginproxy.h"
21#include "qml/qmlstaticdata.h"
22#include <QQmlEngine>
23
24QmlWindows::QmlWindows(QObject *parent)
25 : QObject(parent)
26{
27 connect(mApp->plugins(), &PluginProxy::mainWindowCreated, this, [this](BrowserWindow *window){
28 QmlWindow *qmlWindow = QmlStaticData::instance().getWindow(window);
29 Q_EMIT created(qmlWindow);
30 });
31
32 connect(mApp->plugins(), &PluginProxy::mainWindowDeleted, this, [this](BrowserWindow *window){
33 QmlWindow *qmlWindow = QmlStaticData::instance().getWindow(window);
34 Q_EMIT removed(qmlWindow);
35 });
36}
37
39{
40 return QmlStaticData::instance().getWindow(getBrowserWindow(id));
41}
42
44{
45 return QmlStaticData::instance().getWindow(mApp->getWindow());
46}
47
48QList<QObject*> QmlWindows::getAll() const
49{
50 QList<QObject*> list;
51 const QList<BrowserWindow*> windows = mApp->windows();
52 list.reserve(windows.size());
53 for (BrowserWindow *window : windows) {
54 list.append(QmlStaticData::instance().getWindow(window));
55 }
56 return list;
57}
58
59QmlWindow *QmlWindows::create(const QVariantMap &map) const
60{
61 const QUrl url = QUrl::fromEncoded(map.value(QSL("url")).toString().toUtf8());
62 const Qz::BrowserWindowType type = Qz::BrowserWindowType(map.value(QSL("type"), QmlEnums::NewWindow).toInt());
63 BrowserWindow *window = mApp->createWindow(type, url);
64 return QmlStaticData::instance().getWindow(window);
65}
66
67void QmlWindows::remove(int windowId) const
68{
69 BrowserWindow *window = getBrowserWindow(windowId);
70 window->close();
71}
72
73BrowserWindow *QmlWindows::getBrowserWindow(int windowId) const
74{
75 const QList<BrowserWindow*> windows = mApp->windows();
76 for (BrowserWindow *window : windows) {
77 if (QmlStaticData::instance().windowIdHash().value(window, -1) == windowId) {
78 return window;
79 }
80 }
81
82 qWarning() << "Unable to get window with id:" << windowId;
83 return nullptr;
84}
void mainWindowDeleted(BrowserWindow *window)
void mainWindowCreated(BrowserWindow *window)
@ NewWindow
Represents new window.
Definition: qmlenums.h:48
static QmlStaticData & instance()
QmlWindow * getWindow(BrowserWindow *window)
The class exposing Browser window to QML.
Definition: qmlwindow.h:27
Q_INVOKABLE void remove(int windowId) const
Removes a browser window.
Definition: qmlwindows.cpp:67
Q_INVOKABLE QList< QObject * > getAll() const
Get all the browser window.
Definition: qmlwindows.cpp:48
Q_INVOKABLE QmlWindow * getCurrent() const
Gets the current browser window.
Definition: qmlwindows.cpp:43
Q_INVOKABLE QmlWindow * create(const QVariantMap &map) const
Creates a browser window.
Definition: qmlwindows.cpp:59
Q_INVOKABLE QmlWindow * get(int id) const
Gets a browser window.
Definition: qmlwindows.cpp:38
QmlWindows(QObject *parent=nullptr)
Definition: qmlwindows.cpp:24
#define mApp
int value(const QColor &c)
Definition: colors.cpp:238
BrowserWindowType
Definition: qzcommon.h:64
#define QSL(x)
Definition: qzcommon.h:40