Reload theme without restart (#137)

This commit is contained in:
Konstantinos Sideris 2017-11-25 22:47:06 +02:00
parent 54cf3418bc
commit 0dad256743
3 changed files with 44 additions and 28 deletions

View file

@ -36,8 +36,9 @@ public:
void save(); void save();
void load(); void load();
void setTheme(QString theme) { theme_ = theme; } void applyTheme();
void setTray(bool state) { isTrayEnabled_ = state; } void setTheme(QString theme);
void setTray(bool state);
QString theme() const { return !theme_.isEmpty() ? theme_ : "light"; } QString theme() const { return !theme_.isEmpty() ? theme_ : "light"; }
bool isTrayEnabled() const { return isTrayEnabled_; } bool isTrayEnabled() const { return isTrayEnabled_; }

View file

@ -15,6 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <QApplication>
#include <QComboBox> #include <QComboBox>
#include <QDebug> #include <QDebug>
#include <QLabel> #include <QLabel>
@ -33,7 +34,46 @@ UserSettings::load()
{ {
QSettings settings; QSettings settings;
isTrayEnabled_ = settings.value("user/window/tray", true).toBool(); isTrayEnabled_ = settings.value("user/window/tray", true).toBool();
theme_ = settings.value("user/theme", "default").toString(); theme_ = settings.value("user/theme", "light").toString();
applyTheme();
}
void
UserSettings::setTheme(QString theme)
{
theme_ = theme;
save();
applyTheme();
}
void
UserSettings::setTray(bool state)
{
isTrayEnabled_ = state;
save();
}
void
UserSettings::applyTheme()
{
QFile stylefile;
QPalette pal;
if (theme() == "light") {
stylefile.setFileName(":/styles/styles/nheko.qss");
pal.setColor(QPalette::Link, QColor("#333"));
} else if (theme() == "dark") {
stylefile.setFileName(":/styles/styles/nheko-dark.qss");
pal.setColor(QPalette::Link, QColor("#d7d9dc"));
} else {
stylefile.setFileName(":/styles/styles/system.qss");
}
stylefile.open(QFile::ReadOnly);
QString stylesheet = QString(stylefile.readAll());
QApplication::setPalette(pal);
qobject_cast<QApplication *>(QApplication::instance())->setStyleSheet(stylesheet);
} }
void void

View file

@ -73,31 +73,6 @@ main(int argc, char *argv[])
QSettings settings; QSettings settings;
QFile stylefile;
if (!settings.contains("user/theme"))
settings.setValue("user/theme", "light");
const auto theme = settings.value("user/theme", "light").toString();
QPalette pal;
if (theme == "light") {
stylefile.setFileName(":/styles/styles/nheko.qss");
pal.setColor(QPalette::Link, QColor("#333"));
} else if (theme == "dark") {
stylefile.setFileName(":/styles/styles/nheko-dark.qss");
pal.setColor(QPalette::Link, QColor("#d7d9dc"));
} else {
stylefile.setFileName(":/styles/styles/system.qss");
}
app.setPalette(pal);
stylefile.open(QFile::ReadOnly);
QString stylesheet = QString(stylefile.readAll());
app.setStyleSheet(stylesheet);
// Set the default if a value has not been set. // Set the default if a value has not been set.
if (settings.value("font/size").toInt() == 0) if (settings.value("font/size").toInt() == 0)
settings.setValue("font/size", 12); settings.setValue("font/size", 12);