Falkon Develop
Cross-platform Qt-based web browser
qmlbrowseraction.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 "qmlbrowseraction.h"
19#include "qztools.h"
20#include "navigationbar.h"
21#include "statusbar.h"
22#include "pluginproxy.h"
24#include "qml/qmlengine.h"
25#include "qml/qmlstaticdata.h"
26#include <QQuickWidget>
27#include <QQmlContext>
28
30 : QObject(parent)
31{
32 m_button = new QmlBrowserActionButton();
33
41
43
44 connect(mApp->plugins(), &PluginProxy::mainWindowCreated, this, &QmlBrowserAction::addButton);
45}
46
48{
49 const QList<BrowserWindow*> windows = mApp->windows();
50 for (BrowserWindow *window : windows) {
51 addButton(window);
52 }
53}
54
56{
57 const QList<BrowserWindow*> windows = mApp->windows();
58 for (BrowserWindow *window : windows) {
59 removeButton(window);
60 }
61}
62
64{
65 return m_button;
66}
67
68QmlBrowserAction::Locations QmlBrowserAction::location() const
69{
70 return m_locations;
71}
72
73QString QmlBrowserAction::identity() const
74{
75 return m_identity;
76}
77
78void QmlBrowserAction::setIdentity(const QString &identity)
79{
80 m_identity = identity;
81 Q_EMIT identityChanged(m_identity);
82}
83
84QString QmlBrowserAction::name() const
85{
86 return m_name;
87}
88
89void QmlBrowserAction::setName(const QString &name)
90{
91 m_name = name;
92 Q_EMIT nameChanged(m_name);
93}
94
95QString QmlBrowserAction::title() const
96{
97 return m_title;
98}
99
100void QmlBrowserAction::setTitle(const QString &title)
101{
102 m_title = title;
103 Q_EMIT titleChanged(m_title);
104}
105
106QString QmlBrowserAction::toolTip() const
107{
108 return m_toolTip;
109}
110
111void QmlBrowserAction::setToolTip(const QString &toolTip)
112{
113 m_toolTip = toolTip;
114 Q_EMIT toolTipChanged(m_toolTip);
115}
116
117QString QmlBrowserAction::icon() const
118{
119 return m_icon;
120}
121
122void QmlBrowserAction::setIcon(const QString &icon)
123{
124 m_icon = icon;
125 Q_EMIT iconChanged(m_icon);
126}
127
128QString QmlBrowserAction::badgeText() const
129{
130 return m_badgeText;
131}
132
133void QmlBrowserAction::setBadgeText(const QString &badgeText)
134{
135 m_badgeText = badgeText;
136 Q_EMIT badgeTextChanged(m_badgeText);
137}
138
139QQmlComponent *QmlBrowserAction::popup() const
140{
141 return m_popup;
142}
143
144void QmlBrowserAction::setPopup(QQmlComponent *popup)
145{
146 m_popup = popup;
147 Q_EMIT popupChanged(m_popup);
148}
149
150void QmlBrowserAction::setLocation(const Locations &locations)
151{
152 m_locations = locations;
153 Q_EMIT locationChanged(m_locations);
154}
155
156void QmlBrowserAction::addButton(BrowserWindow *window)
157{
158 if (location().testFlag(NavigationToolBar)) {
159 window->navigationBar()->addToolButton(button());
160 }
161
162 if (location().testFlag(StatusBar)) {
163 window->statusBar()->addButton(button());
164 }
165}
166
167void QmlBrowserAction::removeButton(BrowserWindow *window)
168{
169 if (location().testFlag(NavigationToolBar)) {
171 }
172
173 if (location().testFlag(StatusBar)) {
174 window->statusBar()->removeButton(button());
175 }
176}
177
180{
182}
183
185{
186 return m_id;
187}
188
189void QmlBrowserActionButton::setId(const QString &id)
190{
191 m_id = id;
192}
193
195{
196 return m_name;
197}
198
199void QmlBrowserActionButton::setName(const QString &name)
200{
201 m_name = name;
202}
203
204void QmlBrowserActionButton::setTitle(const QString &title)
205{
207}
208
209void QmlBrowserActionButton::setToolTip(const QString &toolTip)
210{
212}
213
214void QmlBrowserActionButton::setIcon(const QString &icon)
215{
216 m_iconUrl = icon;
217 if (!m_popup) {
218 return;
219 }
220 auto qmlEngine = qobject_cast<QmlEngine*>(m_popup->creationContext()->engine());
221 if (!qmlEngine) {
222 return;
223 }
224 const QString pluginPath = qmlEngine->extensionPath();
225 QIcon qicon = QmlStaticData::instance().getIcon(m_iconUrl, pluginPath);
227}
228
229void QmlBrowserActionButton::setBadgeText(const QString &badgeText)
230{
232}
233
234void QmlBrowserActionButton::setPopup(QQmlComponent *popup)
235{
236 m_popup = popup;
237}
238
240{
241 if (!m_popup) {
242 qWarning() << "No popup to show";
243 return;
244 }
245
246 auto *quickWidget = new QQuickWidget();
247 quickWidget->setContent(m_popup->url(), m_popup, m_popup->create(m_popup->creationContext()));
248
249 auto *widget = new QWidget();
250 quickWidget->setParent(widget);
251
252 widget->setWindowFlag(Qt::Popup);
253 widget->setAttribute(Qt::WA_DeleteOnClose);
254 widget->move(clickController->callPopupPosition(quickWidget->size()));
255
256 connect(quickWidget, &QQuickWidget::destroyed, this, [clickController]{
257 clickController->callPopupClosed();
258 });
259
260 widget->show();
261}
void clicked(AbstractButtonInterface::ClickController *controller)
void setTitle(const QString &text)
void setBadgeText(const QString &badgeText)
void setToolTip(const QString &toolTip)
void setIcon(const QIcon &icon)
NavigationBar * navigationBar() const
StatusBar * statusBar() const
void removeToolButton(AbstractButtonInterface *button)
void addToolButton(AbstractButtonInterface *button)
void mainWindowCreated(BrowserWindow *window)
QString id() const override
QString name() const override
void setName(const QString &name)
void setToolTip(const QString &toolTip)
void setTitle(const QString &title)
void setIcon(const QString &icon)
QmlBrowserActionButton(QObject *parent=nullptr)
void setPopup(QQmlComponent *popup)
void positionPopup(ClickController *clickController)
void setBadgeText(const QString &badgeText)
void setId(const QString &id)
QString identity
identity for the button. This is a required property.
@ NavigationToolBar
to add the button in navigation tool bar
QQmlComponent * popup
the popup shown when the button is clicked. This must be a QML Window.
void badgeTextChanged(const QString &badgeText)
This signal is emitted when the badgeText property is changed.
QString title
title of the button.
QString icon
icon path of button
QString name
name of the button. This is a required property.
void identityChanged(const QString &identity)
This signal is emitted when identity property is changed.
void titleChanged(const QString &title)
This signal is emitted when title property is changed.
void toolTipChanged(const QString &toolTip)
This signal is emitted when the toolTip property is changed.
QmlBrowserActionButton * button() const
~QmlBrowserAction() override
QString toolTip
tool tip of the button.
void nameChanged(const QString &name)
This signal is emitted when name property is changed.
void iconChanged(const QString &icon)
This signal is emitted when the icon property is changed.
void locationChanged(const Locations &locations)
This signal is emitted when the locations property is changed.
QString badgeText
badge text of the button
void clicked()
This signal is emitted when the button is clicked.
void componentComplete() override
Locations location
represents locations where the button is to be added.
QmlBrowserAction(QObject *parent=nullptr)
void popupChanged(QQmlComponent *popup)
This signal is emitted when the popup property is changed.
static QmlStaticData & instance()
QIcon getIcon(const QString &iconPath, const QString &pluginPath)
void addButton(AbstractButtonInterface *button)
Definition: statusbar.cpp:217
void removeButton(AbstractButtonInterface *button)
Definition: statusbar.cpp:235
#define mApp
QPoint callPopupPosition(const QSize &size) const