Falkon Develop
Cross-platform Qt-based web browser
tabmanagerplugin.cpp
Go to the documentation of this file.
1/* ============================================================
2* TabManager plugin for Falkon
3* Copyright (C) 2013-2017 S. Razi Alavizadeh <s.r.alavizadeh@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 "tabmanagerplugin.h"
19#include "tabmanagerwidget.h"
20#include "browserwindow.h"
21#include "pluginproxy.h"
22#include "mainapplication.h"
23#include "sidebar.h"
24#include "tabwidget.h"
25#include "tabbar.h"
26#include "tabmanagersettings.h"
27#include "../config.h"
28
29#include <QInputDialog>
30#include <QSettings>
31#include <QAction>
32#include <QTimer>
33#include <QMenu>
34
35QString TabManagerPlugin::s_settingsPath;
36
38 : QObject()
39 , m_controller(nullptr)
40 , m_tabManagerWidget(nullptr)
41 , m_viewType(Undefined)
42 , m_initState(false)
43 , m_asTabBarReplacement(false)
44{
45}
46
47void TabManagerPlugin::init(InitState state, const QString &settingsPath)
48{
49 Q_UNUSED(state)
50
51 m_controller = new TabManagerWidgetController(this);
52 connect(mApp->plugins(), SIGNAL(mainWindowCreated(BrowserWindow*)), this, SLOT(mainWindowCreated(BrowserWindow*)));
53 connect(mApp->plugins(), SIGNAL(mainWindowDeleted(BrowserWindow*)), m_controller, SLOT(mainWindowDeleted(BrowserWindow*)));
54 connect(mApp->plugins(), SIGNAL(webPageCreated(WebPage*)), m_controller, SIGNAL(requestRefreshTree()));
55 connect(mApp->plugins(), SIGNAL(webPageDeleted(WebPage*)), m_controller, SIGNAL(requestRefreshTree(WebPage*)));
56
57 s_settingsPath = settingsPath + QL1S("/TabManager");
58 m_initState = true;
59
60 // load settings
61 QSettings settings(s_settingsPath + QL1S("/tabmanager.ini"), QSettings::IniFormat);
62 settings.beginGroup("View");
63 m_controller->setGroupType(TabManagerWidget::GroupType(settings.value("GroupType", TabManagerWidget::GroupByWindow).toInt()));
64 m_viewType = ViewType(settings.value("ViewType", ShowAsWindow).toInt());
65 m_asTabBarReplacement = settings.value("AsTabBarReplacement", false).toBool();
66 settings.endGroup();
67
68 setAsTabBarReplacement(m_asTabBarReplacement);
70}
71
73{
75
76 setTabBarVisible(true);
78
79 delete m_controller;
80}
81
83{
84 return (QString::fromLatin1(Qz::VERSION) == QLatin1String(FALKON_VERSION));
85}
86
87void TabManagerPlugin::showSettings(QWidget* parent)
88{
89 auto* settings = new TabManagerSettings(this, parent);
90 settings->exec();
91}
92
94{
95 if (viewType() == ShowAsWindow) {
96 QAction* showAction = m_controller->createMenuAction();
97 showAction->setCheckable(false);
98 connect(showAction, SIGNAL(triggered()), m_controller, SLOT(raiseTabManager()));
99 menu->addAction(showAction);
100 }
101}
102
104{
105 if (viewType() == ShowAsSideBar) {
106 SideBarManager::addSidebar(QStringLiteral("TabManager"), m_controller);
107 }
108 else if (viewType() == ShowAsWindow) {
109 if (!m_tabManagerWidget) {
110 m_tabManagerWidget = m_controller->createTabManagerWidget(mApp->getWindow(), nullptr, true);
111 m_tabManagerWidget->setWindowFlags(Qt::Window);
112 }
113 }
114
115 if (m_initState) {
116 const auto windows = mApp->windows();
117 for (BrowserWindow* window : windows) {
118 mainWindowCreated(window, false);
119 }
120 m_initState = false;
121 }
122}
123
124void TabManagerPlugin::mainWindowCreated(BrowserWindow* window, bool refresh)
125{
126 if (window) {
127 window->tabWidget()->tabBar()->setForceHidden(m_asTabBarReplacement);
128
129 if (m_viewType == ShowAsWindow) {
130 m_controller->addStatusBarIcon(window);
131 }
132
133 connect(window->tabWidget(), SIGNAL(currentChanged(int)), m_controller, SIGNAL(requestRefreshTree()));
134 connect(window->tabWidget(), SIGNAL(pinStateChanged(int,bool)), m_controller, SIGNAL(requestRefreshTree()));
135 }
136
137 if (refresh) {
138 m_controller->emitRefreshTree();
139 }
140}
141
142void TabManagerPlugin::setTabBarVisible(bool visible)
143{
144 const auto windows = mApp->windows();
145 for (BrowserWindow* window : windows) {
146 window->tabWidget()->tabBar()->setForceHidden(!visible);
147 }
148}
149
151{
152 if (viewType() == ShowAsSideBar) {
153 SideBarManager::removeSidebar(m_controller);
154 }
155 else if (viewType() == ShowAsWindow) {
156 // remove statusbar icon
157 const auto windows = mApp->windows();
158 for (BrowserWindow* window : windows) {
159 m_controller->removeStatusBarIcon(window);
160 }
161
162 m_tabManagerWidget->close();
163 delete m_tabManagerWidget;
164 m_tabManagerWidget = nullptr;
165 }
166}
167
168
170{
171 return m_viewType;
172}
173
175{
176 if (m_viewType != type) {
178 m_viewType = type;
180
181 if (!m_initState) {
182 if (m_viewType == ShowAsSideBar) {
183 mApp->getWindow()->sideBarManager()->showSideBar(QStringLiteral("TabManager"));
184 }
185 else if (m_viewType == ShowAsWindow) {
186 // add statusbar icon
187 const auto windows = mApp->windows();
188 for (BrowserWindow* window : windows) {
189 m_controller->addStatusBarIcon(window);
190 }
191 }
192 }
193 }
194}
195
197{
198 return s_settingsPath;
199}
200
202{
203 QSettings settings(s_settingsPath + QL1S("/tabmanager.ini"), QSettings::IniFormat);
204 settings.beginGroup("View");
205 settings.setValue("GroupType", m_controller->groupType());
206 settings.setValue("ViewType", viewType());
207 settings.setValue("AsTabBarReplacement", asTabBarReplacement());
208 settings.endGroup();
209}
210
212{
213 return m_asTabBarReplacement;
214}
215
217{
218 m_asTabBarReplacement = yes;
219 setTabBarVisible(!m_asTabBarReplacement);
220}
221
TabWidget * tabWidget() const
static void removeSidebar(SideBarInterface *interface)
Definition: sidebar.cpp:141
static void addSidebar(const QString &id, SideBarInterface *interface)
Definition: sidebar.cpp:136
void setForceHidden(bool hidden)
Definition: tabbar.cpp:161
void init(InitState state, const QString &settingsPath) override
void setViewType(ViewType type)
static QString settingsPath()
void unload() override
bool asTabBarReplacement() const
bool testPlugin() override
void showSettings(QWidget *parent) override
void populateExtensionsMenu(QMenu *menu) override
void setAsTabBarReplacement(bool yes)
TabManagerWidget * createTabManagerWidget(BrowserWindow *mainClass, QWidget *parent=nullptr, bool defaultWidget=false)
void setGroupType(TabManagerWidget::GroupType type)
void removeStatusBarIcon(BrowserWindow *window)
TabManagerWidget::GroupType groupType()
void addStatusBarIcon(BrowserWindow *window)
TabBar * tabBar() const
Definition: tabwidget.cpp:580
#define mApp
FALKON_EXPORT const char * VERSION
Definition: qzcommon.cpp:26
State state
#define QL1S(x)
Definition: qzcommon.h:44