2018-02-02 11:28:29 +01:00
|
|
|
/* ============================================================
|
|
|
|
* VerticalTabs plugin for Falkon
|
|
|
|
* Copyright (C) 2018 David Rosca <nowrep@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
|
|
|
#include "verticaltabsplugin.h"
|
|
|
|
#include "verticaltabssettings.h"
|
|
|
|
#include "verticaltabscontroller.h"
|
2018-02-05 15:29:57 +01:00
|
|
|
#include "verticaltabsschemehandler.h"
|
2018-02-02 11:28:29 +01:00
|
|
|
|
|
|
|
#include "browserwindow.h"
|
|
|
|
#include "pluginproxy.h"
|
|
|
|
#include "mainapplication.h"
|
|
|
|
#include "tabwidget.h"
|
|
|
|
#include "tabbar.h"
|
|
|
|
#include "sidebar.h"
|
2018-02-05 15:29:57 +01:00
|
|
|
#include "networkmanager.h"
|
2018-02-02 11:28:29 +01:00
|
|
|
#include "../config.h"
|
2018-02-11 14:16:31 +01:00
|
|
|
#include "desktopfile.h"
|
2018-02-02 11:28:29 +01:00
|
|
|
|
|
|
|
#include <QSettings>
|
|
|
|
|
|
|
|
VerticalTabsPlugin::VerticalTabsPlugin()
|
|
|
|
: QObject()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-11 14:16:31 +01:00
|
|
|
DesktopFile VerticalTabsPlugin::metaData() const
|
2018-02-02 11:28:29 +01:00
|
|
|
{
|
2018-02-11 14:16:31 +01:00
|
|
|
return DesktopFile(QSL(":verticaltabs/metadata.desktop"));
|
2018-02-02 11:28:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void VerticalTabsPlugin::init(InitState state, const QString &settingsPath)
|
|
|
|
{
|
|
|
|
m_settingsPath = settingsPath + QL1S("/extensions.ini");
|
|
|
|
|
|
|
|
QSettings settings(m_settingsPath, QSettings::IniFormat);
|
|
|
|
settings.beginGroup(QSL("VerticalTabs"));
|
|
|
|
m_viewType = static_cast<ViewType>(settings.value(QSL("ViewType"), TabListView).toInt());
|
|
|
|
m_replaceTabBar = settings.value(QSL("ReplaceTabBar"), false).toBool();
|
2018-02-03 18:38:53 +01:00
|
|
|
m_addChildBehavior = static_cast<AddChildBehavior>(settings.value(QSL("AddChildBehavior"), AppendChild).toInt());
|
2018-02-03 19:24:09 +01:00
|
|
|
m_theme = settings.value(QSL("Theme"), QSL(":verticaltabs/data/themes/default.css")).toString();
|
2018-02-02 11:28:29 +01:00
|
|
|
settings.endGroup();
|
|
|
|
|
|
|
|
m_controller = new VerticalTabsController(this);
|
|
|
|
SideBarManager::addSidebar(QSL("VerticalTabs"), m_controller);
|
|
|
|
|
2018-02-11 11:50:05 +01:00
|
|
|
m_schemeHandler = new VerticalTabsSchemeHandler(this);
|
|
|
|
mApp->networkManager()->registerExtensionSchemeHandler(QSL("verticaltabs"), m_schemeHandler);
|
|
|
|
|
2018-02-06 15:48:30 +01:00
|
|
|
mApp->plugins()->registerAppEventHandler(PluginProxy::KeyPressHandler, this);
|
2018-02-04 11:13:18 +01:00
|
|
|
|
2018-02-03 18:38:53 +01:00
|
|
|
setWebTabBehavior(m_addChildBehavior);
|
2018-02-03 19:24:09 +01:00
|
|
|
loadStyleSheet(m_theme);
|
2018-02-03 18:38:53 +01:00
|
|
|
|
2018-02-02 11:28:29 +01:00
|
|
|
connect(mApp->plugins(), &PluginProxy::mainWindowCreated, this, &VerticalTabsPlugin::mainWindowCreated);
|
|
|
|
|
|
|
|
if (state == LateInitState) {
|
|
|
|
const auto windows = mApp->windows();
|
|
|
|
for (BrowserWindow *window : windows) {
|
|
|
|
mainWindowCreated(window);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VerticalTabsPlugin::unload()
|
|
|
|
{
|
|
|
|
setTabBarVisible(true);
|
|
|
|
|
2018-02-26 20:28:34 +01:00
|
|
|
SideBarManager::removeSidebar(m_controller);
|
2018-02-02 11:28:29 +01:00
|
|
|
delete m_controller;
|
|
|
|
m_controller = nullptr;
|
2018-02-05 15:29:57 +01:00
|
|
|
|
2018-02-11 11:50:05 +01:00
|
|
|
mApp->networkManager()->unregisterExtensionSchemeHandler(m_schemeHandler);
|
2018-02-02 11:28:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool VerticalTabsPlugin::testPlugin()
|
|
|
|
{
|
|
|
|
return (Qz::VERSION == QSL(FALKON_VERSION));
|
|
|
|
}
|
|
|
|
|
|
|
|
void VerticalTabsPlugin::showSettings(QWidget *parent)
|
|
|
|
{
|
|
|
|
VerticalTabsSettings *settings = new VerticalTabsSettings(this, parent);
|
|
|
|
settings->exec();
|
|
|
|
}
|
|
|
|
|
2018-02-25 20:39:40 +01:00
|
|
|
bool VerticalTabsPlugin::keyPress(Qz::ObjectName type, QObject *obj, QKeyEvent *event)
|
2018-02-04 11:13:18 +01:00
|
|
|
{
|
|
|
|
if (type == Qz::ON_TabWidget) {
|
|
|
|
return m_controller->handleKeyPress(event, static_cast<TabWidget*>(obj));
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-02 11:28:29 +01:00
|
|
|
VerticalTabsPlugin::ViewType VerticalTabsPlugin::viewType() const
|
|
|
|
{
|
|
|
|
return m_viewType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VerticalTabsPlugin::setViewType(ViewType type)
|
|
|
|
{
|
|
|
|
if (m_viewType == type) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_viewType = type;
|
|
|
|
|
|
|
|
QSettings settings(m_settingsPath, QSettings::IniFormat);
|
|
|
|
settings.setValue(QSL("VerticalTabs/ViewType"), m_viewType);
|
|
|
|
|
|
|
|
emit viewTypeChanged(m_viewType);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VerticalTabsPlugin::replaceTabBar() const
|
|
|
|
{
|
|
|
|
return m_replaceTabBar;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VerticalTabsPlugin::setReplaceTabBar(bool replace)
|
|
|
|
{
|
|
|
|
if (m_replaceTabBar == replace) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_replaceTabBar = replace;
|
|
|
|
setTabBarVisible(!m_replaceTabBar);
|
|
|
|
|
|
|
|
QSettings settings(m_settingsPath, QSettings::IniFormat);
|
|
|
|
settings.setValue(QSL("VerticalTabs/ReplaceTabBar"), m_replaceTabBar);
|
|
|
|
}
|
|
|
|
|
2018-02-03 18:38:53 +01:00
|
|
|
VerticalTabsPlugin::AddChildBehavior VerticalTabsPlugin::addChildBehavior() const
|
|
|
|
{
|
|
|
|
return m_addChildBehavior;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VerticalTabsPlugin::setAddChildBehavior(AddChildBehavior behavior)
|
|
|
|
{
|
|
|
|
if (m_addChildBehavior == behavior) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_addChildBehavior = behavior;
|
|
|
|
setWebTabBehavior(m_addChildBehavior);
|
|
|
|
|
|
|
|
QSettings settings(m_settingsPath, QSettings::IniFormat);
|
|
|
|
settings.setValue(QSL("VerticalTabs/AddChildBehavior"), m_addChildBehavior);
|
|
|
|
}
|
|
|
|
|
2018-02-03 19:24:09 +01:00
|
|
|
QString VerticalTabsPlugin::theme() const
|
|
|
|
{
|
|
|
|
return m_theme;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VerticalTabsPlugin::setTheme(const QString &theme)
|
|
|
|
{
|
|
|
|
if (theme.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't check if same to allow live reloading stylesheet
|
|
|
|
|
|
|
|
m_theme = theme;
|
|
|
|
loadStyleSheet(m_theme);
|
|
|
|
|
|
|
|
QSettings settings(m_settingsPath, QSettings::IniFormat);
|
|
|
|
settings.setValue(QSL("VerticalTabs/Theme"), m_theme);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString VerticalTabsPlugin::styleSheet() const
|
|
|
|
{
|
|
|
|
return m_styleSheet;
|
|
|
|
}
|
|
|
|
|
2018-02-02 11:28:29 +01:00
|
|
|
void VerticalTabsPlugin::mainWindowCreated(BrowserWindow *window)
|
|
|
|
{
|
|
|
|
if (window->sideBarManager()->activeSideBar().isEmpty()) {
|
|
|
|
window->sideBarManager()->showSideBar(QSL("VerticalTabs"));
|
|
|
|
}
|
|
|
|
setTabBarVisible(!m_replaceTabBar);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VerticalTabsPlugin::setTabBarVisible(bool visible)
|
|
|
|
{
|
|
|
|
const auto windows = mApp->windows();
|
|
|
|
for (BrowserWindow *window : windows) {
|
|
|
|
window->tabWidget()->tabBar()->setForceHidden(!visible);
|
|
|
|
}
|
|
|
|
}
|
2018-02-03 18:38:53 +01:00
|
|
|
|
|
|
|
void VerticalTabsPlugin::setWebTabBehavior(AddChildBehavior behavior)
|
|
|
|
{
|
|
|
|
WebTab::setAddChildBehavior(behavior == AppendChild ? WebTab::AppendChild : WebTab::PrependChild);
|
|
|
|
}
|
2018-02-03 19:24:09 +01:00
|
|
|
|
|
|
|
void VerticalTabsPlugin::loadStyleSheet(const QString &theme)
|
|
|
|
{
|
|
|
|
QFile file(theme);
|
|
|
|
if (!file.open(QFile::ReadOnly)) {
|
|
|
|
qWarning() << "Failed to open stylesheet file" << theme;
|
|
|
|
file.setFileName(QSL(":verticaltabs/data/themes/default.css"));
|
|
|
|
file.open(QFile::ReadOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_styleSheet = QString::fromUtf8(file.readAll());
|
|
|
|
emit styleSheetChanged(m_styleSheet);
|
|
|
|
}
|