Falkon Develop
Cross-platform Qt-based web browser
qmlsidebar.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 "qmlsidebar.h"
19#include "mainapplication.h"
20#include "qztools.h"
21#include "sidebar.h"
23#include "qml/qmlengine.h"
24#include "qml/qmlstaticdata.h"
25#include <QAction>
26#include <QQuickWidget>
27#include <QQmlContext>
28
29QmlSideBar::QmlSideBar(QObject *parent)
30 : QObject(parent)
31{
32 m_sideBarHelper = new QmlSideBarHelper(this);
33
34 connect(this, &QmlSideBar::titleChanged, m_sideBarHelper, &QmlSideBarHelper::setTitle);
35 connect(this, &QmlSideBar::iconChanged, m_sideBarHelper, &QmlSideBarHelper::setIcon);
36 connect(this, &QmlSideBar::shortcutChanged, m_sideBarHelper, &QmlSideBarHelper::setShortcut);
37 connect(this, &QmlSideBar::checkableChanged, m_sideBarHelper, &QmlSideBarHelper::setCheckable);
38 connect(this, &QmlSideBar::itemChanged, m_sideBarHelper, &QmlSideBarHelper::setItem);
39}
40
42{
43 SideBarManager::removeSidebar(m_sideBarHelper);
44}
45
47{
49}
50
51QString QmlSideBar::name() const
52{
53 return m_name;
54}
55
57{
58 return m_sideBarHelper;
59}
60
61void QmlSideBar::setName(const QString &name)
62{
63 m_name = name;
64 Q_EMIT nameChanged(m_name);
65}
66
67QString QmlSideBar::title() const
68{
69 return m_title;
70}
71
72void QmlSideBar::setTitle(const QString &title)
73{
74 m_title = title;
75 Q_EMIT titleChanged(title);
76}
77
78QString QmlSideBar::icon() const
79{
80 return m_iconUrl;
81}
82
83void QmlSideBar::setIcon(const QString &icon)
84{
85 m_iconUrl = icon;
86 Q_EMIT iconChanged(m_iconUrl);
87}
88
89QString QmlSideBar::shortcut() const
90{
91 return m_shortcut;
92}
93
94void QmlSideBar::setShortcut(const QString &shortcut)
95{
96 m_shortcut = shortcut;
97 Q_EMIT shortcutChanged(m_shortcut);
98}
99
101{
102 return m_checkable;
103}
104
105void QmlSideBar::setCheckable(bool checkable)
106{
107 m_checkable = checkable;
108 Q_EMIT checkableChanged(m_checkable);
109}
110
111QQmlComponent *QmlSideBar::item() const
112{
113 return m_item;
114}
115
116void QmlSideBar::setItem(QQmlComponent *item)
117{
118 m_item = item;
119 Q_EMIT itemChanged(m_item);
120}
121
123 : SideBarInterface(parent)
124{
125}
126
128{
129 return m_title;
130}
131
133{
134 auto *action = new QAction(m_title);
135 action->setShortcut(QKeySequence(m_shortcut));
136 action->setCheckable(m_checkable);
137 if (!m_item) {
138 return action;
139 }
140 auto qmlEngine = qobject_cast<QmlEngine*>(m_item->creationContext()->engine());
141 if (qmlEngine) {
142 return action;
143 }
144 const QString pluginPath = qmlEngine->extensionPath();
145 const QIcon icon = QmlStaticData::instance().getIcon(m_iconUrl, pluginPath);
146 action->setIcon(icon);
147 return action;
148}
149
151{
152 Q_UNUSED(mainWindow)
153
154 auto *widget = new QQuickWidget();
155 widget->setContent(m_item->url(), m_item, m_item->create(m_item->creationContext()));
156 widget->setResizeMode(QQuickWidget::SizeRootObjectToView);
157
158 return widget;
159}
160
161void QmlSideBarHelper::setTitle(const QString &title)
162{
163 m_title = title;
164}
165
166void QmlSideBarHelper::setIcon(const QString &icon)
167{
168 m_iconUrl = icon;
169}
170
171void QmlSideBarHelper::setShortcut(const QString &shortcut)
172{
173 m_shortcut = shortcut;
174}
175
177{
178 m_checkable = checkable;
179}
180
181void QmlSideBarHelper::setItem(QQmlComponent *item)
182{
183 m_item = item;
184}
void setItem(QQmlComponent *item)
Definition: qmlsidebar.cpp:181
QAction * createMenuAction() override
Definition: qmlsidebar.cpp:132
void setIcon(const QString &icon)
Definition: qmlsidebar.cpp:166
void setTitle(const QString &title)
Definition: qmlsidebar.cpp:161
void setCheckable(bool checkable)
Definition: qmlsidebar.cpp:176
QmlSideBarHelper(QObject *parent=nullptr)
Definition: qmlsidebar.cpp:122
void setShortcut(const QString &shortcut)
Definition: qmlsidebar.cpp:171
QString title() const override
Definition: qmlsidebar.cpp:127
QWidget * createSideBarWidget(BrowserWindow *mainWindow) override
Definition: qmlsidebar.cpp:150
QQmlComponent * item
the GUI of the sidebar. This must be provided as QML Window. This is a default property.
Definition: qmlsidebar.h:72
void iconChanged(const QString &icon)
This signal is emitted when icon property is changed.
QString shortcut
shortcut for the sidebar action.
Definition: qmlsidebar.h:61
SideBarInterface * sideBar() const
Definition: qmlsidebar.cpp:56
~QmlSideBar() override
Definition: qmlsidebar.cpp:41
QmlSideBar(QObject *parent=nullptr)
Definition: qmlsidebar.cpp:29
QString name
name of the sidebar. This is required property.
Definition: qmlsidebar.h:36
bool checkable
represents whether the sidebar action is checkable
Definition: qmlsidebar.h:66
void checkableChanged(bool checkable)
This signal is emitted when checkable property is changed.
void nameChanged(const QString &name)
This signal is emitted when name property is changed.
void itemChanged(QQmlComponent *item)
void componentComplete() override
Definition: qmlsidebar.cpp:46
void titleChanged(const QString &title)
This signal is emitted when title property is changed.
QString title
title of the sidebar action.
Definition: qmlsidebar.h:41
QString icon
icon path of the sidebar action.
Definition: qmlsidebar.h:56
void shortcutChanged(const QString &shortcut)
This signal is emitted when shortcut property is changed.
static QmlStaticData & instance()
QIcon getIcon(const QString &iconPath, const QString &pluginPath)
static void removeSidebar(SideBarInterface *interface)
Definition: sidebar.cpp:141
static void addSidebar(const QString &id, SideBarInterface *interface)
Definition: sidebar.cpp:136