1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +01:00

VerticalTabs: Add support for changing themes

This commit is contained in:
David Rosca 2018-02-03 19:24:09 +01:00
parent 2c60d8bcfb
commit 2c731111d7
No known key found for this signature in database
GPG Key ID: EBC3FC294452C6D8
8 changed files with 128 additions and 3 deletions

View File

@ -0,0 +1,16 @@
/* Vertical Tabs Default theme */
TabTreeCloseButton
{
qproperty-showOnNormal: 0;
qproperty-showOnHovered: 1;
qproperty-showOnSelected: 1;
}
TabTreeView
{
color: 0;
qproperty-hoverColor: 0;
qproperty-selectedColor: 0;
qproperty-backgroundIndentation: 0;
}

View File

@ -1,5 +1,6 @@
<RCC>
<qresource prefix="/verticaltabs">
<file>data/icon.svg</file>
<file>data/themes/default.css</file>
</qresource>
</RCC>

View File

@ -42,8 +42,10 @@ QAction *VerticalTabsController::createMenuAction()
QWidget *VerticalTabsController::createSideBarWidget(BrowserWindow *window)
{
VerticalTabsWidget *widget = new VerticalTabsWidget(window);
connect(m_plugin, &VerticalTabsPlugin::viewTypeChanged, widget, &VerticalTabsWidget::setViewType);
widget->setViewType(m_plugin->viewType());
widget->setStyleSheet(m_plugin->styleSheet());
connect(m_plugin, &VerticalTabsPlugin::viewTypeChanged, widget, &VerticalTabsWidget::setViewType);
connect(m_plugin, &VerticalTabsPlugin::styleSheetChanged, widget, &VerticalTabsWidget::setStyleSheet);
return widget;
}

View File

@ -57,12 +57,14 @@ void VerticalTabsPlugin::init(InitState state, const QString &settingsPath)
m_viewType = static_cast<ViewType>(settings.value(QSL("ViewType"), TabListView).toInt());
m_replaceTabBar = settings.value(QSL("ReplaceTabBar"), false).toBool();
m_addChildBehavior = static_cast<AddChildBehavior>(settings.value(QSL("AddChildBehavior"), AppendChild).toInt());
m_theme = settings.value(QSL("Theme"), QSL(":verticaltabs/data/themes/default.css")).toString();
settings.endGroup();
m_controller = new VerticalTabsController(this);
SideBarManager::addSidebar(QSL("VerticalTabs"), m_controller);
setWebTabBehavior(m_addChildBehavior);
loadStyleSheet(m_theme);
connect(mApp->plugins(), &PluginProxy::mainWindowCreated, this, &VerticalTabsPlugin::mainWindowCreated);
@ -156,6 +158,31 @@ void VerticalTabsPlugin::setAddChildBehavior(AddChildBehavior behavior)
settings.setValue(QSL("VerticalTabs/AddChildBehavior"), m_addChildBehavior);
}
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;
}
void VerticalTabsPlugin::mainWindowCreated(BrowserWindow *window)
{
if (window->sideBarManager()->activeSideBar().isEmpty()) {
@ -176,3 +203,16 @@ void VerticalTabsPlugin::setWebTabBehavior(AddChildBehavior behavior)
{
WebTab::setAddChildBehavior(behavior == AppendChild ? WebTab::AppendChild : WebTab::PrependChild);
}
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);
}

View File

@ -58,17 +58,26 @@ public:
AddChildBehavior addChildBehavior() const;
void setAddChildBehavior(AddChildBehavior behavior);
QString theme() const;
void setTheme(const QString &theme);
QString styleSheet() const;
signals:
void viewTypeChanged(ViewType type);
void styleSheetChanged(const QString &styleSheet);
private:
void mainWindowCreated(BrowserWindow *window);
void setTabBarVisible(bool visible);
void setWebTabBehavior(AddChildBehavior behavior);
void loadStyleSheet(const QString &theme);
QString m_settingsPath;
VerticalTabsController *m_controller = nullptr;
ViewType m_viewType = TabListView;
bool m_replaceTabBar = false;
AddChildBehavior m_addChildBehavior = AppendChild;
QString m_theme;
QString m_styleSheet;
};

View File

@ -19,6 +19,9 @@
#include "ui_verticaltabssettings.h"
#include "verticaltabsplugin.h"
#include <QDir>
#include <QFileDialog>
VerticalTabsSettings::VerticalTabsSettings(VerticalTabsPlugin *plugin, QWidget *parent)
: QDialog(parent)
, ui(new Ui::VerticalTabsSettings)
@ -33,11 +36,15 @@ VerticalTabsSettings::VerticalTabsSettings(VerticalTabsPlugin *plugin, QWidget *
ui->prependChild->setChecked(m_plugin->addChildBehavior() == VerticalTabsPlugin::PrependChild);
ui->replaceTabBar->setChecked(m_plugin->replaceTabBar());
loadThemes();
connect(ui->theme, SIGNAL(activated(int)), this, SLOT(themeValueChanged(int)));
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, [this]() {
m_plugin->setViewType(ui->tabListView->isChecked() ? VerticalTabsPlugin::TabListView : VerticalTabsPlugin::TabTreeView);
m_plugin->setAddChildBehavior(ui->appendChild->isChecked() ? VerticalTabsPlugin::AppendChild : VerticalTabsPlugin::PrependChild);
m_plugin->setReplaceTabBar(ui->replaceTabBar->isChecked());
m_plugin->setTheme(ui->theme->currentData().toString());
accept();
});
}
@ -46,3 +53,38 @@ VerticalTabsSettings::~VerticalTabsSettings()
{
delete ui;
}
void VerticalTabsSettings::themeValueChanged(int index)
{
const int customIndex = ui->theme->count() - 1;
if (index == customIndex) {
const QString path = QFileDialog::getOpenFileName(this, tr("Theme file"), QDir::homePath(), {QSL("*.css")});
if (path.isEmpty()) {
loadThemes();
} else {
ui->theme->setToolTip(path);
ui->theme->setItemData(customIndex, path);
}
} else {
ui->theme->setToolTip(QString());
}
}
void VerticalTabsSettings::loadThemes()
{
ui->theme->clear();
bool found = false;
const auto files = QDir(QSL(":verticaltabs/data/themes")).entryInfoList({QSL("*.css")});
for (const QFileInfo &file : files) {
ui->theme->addItem(file.baseName(), file.absoluteFilePath());
if (file.absoluteFilePath() == m_plugin->theme()) {
ui->theme->setCurrentIndex(ui->theme->count() - 1);
found = true;
}
}
ui->theme->setToolTip(m_plugin->theme());
ui->theme->addItem(tr("Custom..."), found ? QString() : m_plugin->theme());
if (!found) {
ui->theme->setCurrentIndex(ui->theme->count() - 1);
}
}

View File

@ -32,7 +32,12 @@ public:
explicit VerticalTabsSettings(VerticalTabsPlugin *plugin, QWidget *parent = nullptr);
~VerticalTabsSettings();
private slots:
void themeValueChanged(int index);
private:
void loadThemes();
Ui::VerticalTabsSettings *ui;
VerticalTabsPlugin *m_plugin;
};

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>460</width>
<height>317</height>
<height>344</height>
</rect>
</property>
<property name="windowTitle">
@ -57,13 +57,23 @@
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Theme:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="theme"/>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QCheckBox" name="replaceTabBar">
<property name="text">
<string>Use as replacement for main TabBar.</string>
<string>Use as replacement for main tab bar</string>
</property>
</widget>
</item>