Falkon Develop
Cross-platform Qt-based web browser
verticaltabsplugin.cpp
Go to the documentation of this file.
1/* ============================================================
2* VerticalTabs plugin for Falkon
3* Copyright (C) 2018 David Rosca <nowrep@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 "verticaltabsplugin.h"
22
23#include "browserwindow.h"
24#include "pluginproxy.h"
25#include "mainapplication.h"
26#include "tabwidget.h"
27#include "tabbar.h"
28#include "sidebar.h"
29#include "networkmanager.h"
30#include "../config.h"
31
32#include <QSettings>
33#include <QFile>
34
36 : QObject()
37{
38}
39
40void VerticalTabsPlugin::init(InitState state, const QString &settingsPath)
41{
42 m_settingsPath = settingsPath + QL1S("/extensions.ini");
43
44 QSettings settings(m_settingsPath, QSettings::IniFormat);
45 settings.beginGroup(QSL("VerticalTabs"));
46 m_viewType = static_cast<ViewType>(settings.value(QSL("ViewType"), TabListView).toInt());
47 m_replaceTabBar = settings.value(QSL("ReplaceTabBar"), false).toBool();
48 m_addChildBehavior = static_cast<AddChildBehavior>(settings.value(QSL("AddChildBehavior"), AppendChild).toInt());
49 m_theme = settings.value(QSL("Theme"), QSL(":verticaltabs/data/themes/default.css")).toString();
50 settings.endGroup();
51
52 m_controller = new VerticalTabsController(this);
53 SideBarManager::addSidebar(QSL("VerticalTabs"), m_controller);
54
55 m_schemeHandler = new VerticalTabsSchemeHandler(this);
56 mApp->networkManager()->registerExtensionSchemeHandler(QSL("verticaltabs"), m_schemeHandler);
57
58 mApp->plugins()->registerAppEventHandler(PluginProxy::KeyPressHandler, this);
59
60 setWebTabBehavior(m_addChildBehavior);
61 loadStyleSheet(m_theme);
62
63 connect(mApp->plugins(), &PluginProxy::mainWindowCreated, this, &VerticalTabsPlugin::mainWindowCreated);
64
65 if (state == LateInitState) {
66 const auto windows = mApp->windows();
67 for (BrowserWindow *window : windows) {
68 mainWindowCreated(window);
69 if (window->sideBarManager()->activeSideBar().isEmpty()) {
70 window->sideBarManager()->showSideBar(QSL("VerticalTabs"));
71 }
72 }
73 }
74}
75
77{
78 setTabBarVisible(true);
79
81 delete m_controller;
82 m_controller = nullptr;
83
84 mApp->networkManager()->unregisterExtensionSchemeHandler(m_schemeHandler);
85}
86
88{
89 return (QString::fromLatin1(Qz::VERSION) == QSL(FALKON_VERSION));
90}
91
93{
94 auto *settings = new VerticalTabsSettings(this, parent);
95 settings->exec();
96}
97
98bool VerticalTabsPlugin::keyPress(Qz::ObjectName type, QObject *obj, QKeyEvent *event)
99{
100 if (type == Qz::ON_TabWidget) {
101 return m_controller->handleKeyPress(event, static_cast<TabWidget*>(obj));
102 }
103 return false;
104}
105
107{
108 return m_viewType;
109}
110
112{
113 if (m_viewType == type) {
114 return;
115 }
116
117 m_viewType = type;
118
119 QSettings settings(m_settingsPath, QSettings::IniFormat);
120 settings.setValue(QSL("VerticalTabs/ViewType"), m_viewType);
121
122 Q_EMIT viewTypeChanged(m_viewType);
123}
124
126{
127 return m_replaceTabBar;
128}
129
131{
132 if (m_replaceTabBar == replace) {
133 return;
134 }
135
136 m_replaceTabBar = replace;
137 setTabBarVisible(!m_replaceTabBar);
138
139 QSettings settings(m_settingsPath, QSettings::IniFormat);
140 settings.setValue(QSL("VerticalTabs/ReplaceTabBar"), m_replaceTabBar);
141}
142
144{
145 return m_addChildBehavior;
146}
147
149{
150 if (m_addChildBehavior == behavior) {
151 return;
152 }
153
154 m_addChildBehavior = behavior;
155 setWebTabBehavior(m_addChildBehavior);
156
157 QSettings settings(m_settingsPath, QSettings::IniFormat);
158 settings.setValue(QSL("VerticalTabs/AddChildBehavior"), m_addChildBehavior);
159}
160
162{
163 return m_theme;
164}
165
166void VerticalTabsPlugin::setTheme(const QString &theme)
167{
168 if (theme.isEmpty()) {
169 return;
170 }
171
172 // Don't check if same to allow live reloading stylesheet
173
174 m_theme = theme;
175 loadStyleSheet(m_theme);
176
177 QSettings settings(m_settingsPath, QSettings::IniFormat);
178 settings.setValue(QSL("VerticalTabs/Theme"), m_theme);
179}
180
182{
183 return m_styleSheet;
184}
185
186void VerticalTabsPlugin::mainWindowCreated(BrowserWindow *window)
187{
188 Q_UNUSED(window)
189 setTabBarVisible(!m_replaceTabBar);
190}
191
192void VerticalTabsPlugin::setTabBarVisible(bool visible)
193{
194 const auto windows = mApp->windows();
195 for (BrowserWindow *window : windows) {
196 window->tabWidget()->tabBar()->setForceHidden(!visible);
197 }
198}
199
200void VerticalTabsPlugin::setWebTabBehavior(AddChildBehavior behavior)
201{
203}
204
205void VerticalTabsPlugin::loadStyleSheet(const QString &theme)
206{
207 QFile file(theme);
208 if (!file.open(QFile::ReadOnly)) {
209 qWarning() << "Failed to open stylesheet file" << theme;
210 file.setFileName(QSL(":verticaltabs/data/themes/default.css"));
211 file.open(QFile::ReadOnly);
212 }
213
214 m_styleSheet = QString::fromUtf8(file.readAll());
215 Q_EMIT styleSheetChanged(m_styleSheet);
216}
void mainWindowCreated(BrowserWindow *window)
static void removeSidebar(SideBarInterface *interface)
Definition: sidebar.cpp:141
static void addSidebar(const QString &id, SideBarInterface *interface)
Definition: sidebar.cpp:136
bool handleKeyPress(QKeyEvent *event, TabWidget *tabWidget)
void init(InitState state, const QString &settingsPath) override
void setReplaceTabBar(bool replace)
void setAddChildBehavior(AddChildBehavior behavior)
bool testPlugin() override
AddChildBehavior addChildBehavior() const
void viewTypeChanged(VerticalTabsPlugin::ViewType type)
void unload() override
void styleSheetChanged(const QString &styleSheet)
ViewType viewType() const
void setViewType(ViewType type)
bool keyPress(Qz::ObjectName type, QObject *obj, QKeyEvent *event) override
void showSettings(QWidget *parent=nullptr) override
QString styleSheet() const
void setTheme(const QString &theme)
static void setAddChildBehavior(AddChildBehavior behavior)
Definition: webtab.cpp:655
@ PrependChild
Definition: webtab.h:66
@ AppendChild
Definition: webtab.h:65
#define mApp
ObjectName
Definition: qzcommon.h:89
@ ON_TabWidget
Definition: qzcommon.h:92
FALKON_EXPORT const char * VERSION
Definition: qzcommon.cpp:26
State state
#define QL1S(x)
Definition: qzcommon.h:44
#define QSL(x)
Definition: qzcommon.h:40