core,ui: Provide ability to set a custom style sheet file

Fixes #268.
This commit is contained in:
Oleg Shparber 2017-03-05 21:36:49 -05:00
parent cdba884398
commit 40e54b1b53
6 changed files with 70 additions and 1 deletions

View File

@ -82,6 +82,7 @@ void Settings::load()
darkModeEnabled = settings->value(QStringLiteral("dark_mode"), false).toBool();
highlightOnNavigateEnabled = settings->value(QStringLiteral("highlight_on_navigate"), true).toBool();
customCssFile = settings->value(QStringLiteral("custom_css_file")).toString();
settings->endGroup();
settings->beginGroup(GroupProxy);
@ -145,6 +146,7 @@ void Settings::save()
settings->setValue(QStringLiteral("minimum_font_size"), minimumFontSize);
settings->setValue(QStringLiteral("dark_mode"), darkModeEnabled);
settings->setValue(QStringLiteral("highlight_on_navigate"), highlightOnNavigateEnabled);
settings->setValue(QStringLiteral("custom_css_file"), customCssFile);
settings->endGroup();
settings->beginGroup(GroupProxy);

View File

@ -60,6 +60,7 @@ public:
int minimumFontSize;
bool darkModeEnabled;
bool highlightOnNavigateEnabled;
QString customCssFile;
// TODO: bool askOnExternalLink;
// TODO: QString customCss;

View File

@ -39,6 +39,7 @@
#include <QCloseEvent>
#include <QDesktopServices>
#include <QFileInfo>
#include <QKeyEvent>
#include <QMenu>
#include <QMessageBox>
@ -801,6 +802,13 @@ void MainWindow::applySettings()
}
}
if (QFileInfo(m_settings->customCssFile).exists()) {
QScopedPointer<QFile> file(new QFile(m_settings->customCssFile));
if (file->open(QIODevice::ReadOnly)) {
ba += file->readAll();
}
}
const QString cssUrl = QLatin1String("data:text/css;charset=utf-8;base64,") + ba.toBase64();
QWebSettings::globalSettings()->setUserStyleSheetUrl(QUrl(cssUrl));
}

View File

@ -63,6 +63,17 @@ SettingsDialog::~SettingsDialog()
delete ui;
}
void SettingsDialog::chooseCustomCssFile()
{
const QString file = QFileDialog::getOpenFileName(this, tr("Choose CSS File"),
ui->customCssFileEdit->text(),
tr("CSS Files (*.css);;All Files (*.*)"));
if (file.isEmpty())
return;
ui->customCssFileEdit->setText(QDir::toNativeSeparators(file));
}
void SettingsDialog::on_storageButton_clicked()
{
const QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
@ -94,6 +105,7 @@ void SettingsDialog::loadSettings()
ui->minimumFontSizeSpinBox->setValue(settings->minimumFontSize);
ui->darkModeCheckBox->setChecked(settings->darkModeEnabled);
ui->highlightOnNavigateCheckBox->setChecked(settings->highlightOnNavigateEnabled);
ui->customCssFileEdit->setText(QDir::toNativeSeparators(settings->customCssFile));
// Network Tab
switch (settings->proxyType) {
@ -134,6 +146,7 @@ void SettingsDialog::saveSettings()
settings->minimumFontSize = ui->minimumFontSizeSpinBox->text().toInt();
settings->darkModeEnabled = ui->darkModeCheckBox->isChecked();
settings->highlightOnNavigateEnabled = ui->highlightOnNavigateCheckBox->isChecked();
settings->customCssFile = QDir::fromNativeSeparators(ui->customCssFileEdit->text());
if (QDir::fromNativeSeparators(ui->storageEdit->text()) != settings->docsetPath) {
settings->docsetPath = QDir::fromNativeSeparators(ui->storageEdit->text());

View File

@ -44,6 +44,7 @@ public:
~SettingsDialog() override;
private slots:
void chooseCustomCssFile();
void on_storageButton_clicked();
private:

View File

@ -219,13 +219,41 @@
</property>
</widget>
</item>
<item row="2" column="0">
<item row="2" column="0" colspan="2">
<widget class="QCheckBox" name="highlightOnNavigateCheckBox">
<property name="text">
<string>&amp;Highlight on Navigate</string>
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="customCssFileLabel">
<property name="text">
<string>&amp;Custom CSS File:</string>
</property>
<property name="buddy">
<cstring>customCssFileEdit</cstring>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="customCssFileEdit">
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="customCssFileBrowseButton">
<property name="text">
<string>&amp;Browse...</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
@ -625,5 +653,21 @@
</hint>
</hints>
</connection>
<connection>
<sender>customCssFileBrowseButton</sender>
<signal>clicked()</signal>
<receiver>SettingsDialog</receiver>
<slot>chooseCustomCssFile()</slot>
<hints>
<hint type="sourcelabel">
<x>526</x>
<y>232</y>
</hint>
<hint type="destinationlabel">
<x>299</x>
<y>249</y>
</hint>
</hints>
</connection>
</connections>
</ui>