Skip to content

Commit

Permalink
Change theme without restarting app
Browse files Browse the repository at this point in the history
For issue #29
  • Loading branch information
wh201906 committed May 21, 2023
1 parent c73e12c commit 4d15a2f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 27 deletions.
25 changes: 0 additions & 25 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,31 +97,6 @@ int main(int argc, char *argv[])
QString theme = m_settings->value("Theme_Name").toString();
m_settings->endGroup();

QFile* themeFile = new QFile();
QTextStream* themeStream = new QTextStream();
QString qssString = a.styleSheet(); // default behavior
if(theme == "(none)")
;
else if(theme == "qdss_dark")
{
themeFile->setFileName(":/qdarkstyle/dark/darkstyle.qss");
themeFile->open(QFile::ReadOnly | QFile::Text);
themeStream->setDevice(themeFile);
qssString = themeStream->readAll();
}
else if(theme == "qdss_light")
{
themeFile->setFileName(":/qdarkstyle/light/lightstyle.qss");
themeFile->open(QFile::ReadOnly | QFile::Text);
themeStream->setDevice(themeFile);
qssString = themeStream->readAll();
}
a.setStyleSheet(qssString);
delete themeFile;
delete themeStream;
themeFile = nullptr;
themeStream = nullptr;

m_settings = nullptr;

MainWindow w;
Expand Down
27 changes: 27 additions & 0 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ MainWindow::MainWindow(QWidget *parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// might not be empty(specified by -stylesheet option)
m_appDefaultQss = qApp->styleSheet();
contextMenu = new QMenu();

IOConnection = new Connection();
Expand Down Expand Up @@ -77,6 +79,7 @@ MainWindow::MainWindow(QWidget *parent)
ui->funcTab->insertTab(4, fileTab, tr("File"));

settingsTab = new SettingsTab();
connect(settingsTab, &SettingsTab::themeChanged, this, &MainWindow::onThemeChanged);
connect(settingsTab, &SettingsTab::opacityChanged, this, &MainWindow::onOpacityChanged); // not a slot function, but works fine.
connect(settingsTab, &SettingsTab::fullScreenStateChanged, this, &MainWindow::setFullScreen);
connect(settingsTab, &SettingsTab::updateAvailableDeviceTypes, deviceTab, &DeviceTab::getAvailableTypes);
Expand Down Expand Up @@ -532,6 +535,30 @@ void MainWindow::onOpacityChanged(qreal value)
}
}

void MainWindow::onThemeChanged(const QString& themeName)
{
QFile themeFile;
QTextStream themeStream;
QString qssString = qApp->styleSheet(); // default behavior
if(themeName == "(none)")
qssString = m_appDefaultQss;
else if(themeName == "qdss_dark")
{
themeFile.setFileName(":/qdarkstyle/dark/darkstyle.qss");
themeFile.open(QFile::ReadOnly | QFile::Text);
themeStream.setDevice(&themeFile);
qssString = themeStream.readAll();
}
else if(themeName == "qdss_light")
{
themeFile.setFileName(":/qdarkstyle/light/lightstyle.qss");
themeFile.open(QFile::ReadOnly | QFile::Text);
themeStream.setDevice(&themeFile);
qssString = themeStream.readAll();
}
qApp->setStyleSheet(qssString);
}

void MainWindow::dockInit()
{
setDockNestingEnabled(true);
Expand Down
3 changes: 3 additions & 0 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public slots:
void showUpTab(int tabID);
void setFullScreen(bool isFullScreen);
void onOpacityChanged(qreal value);
void onThemeChanged(const QString& themeName);
void onDockTopLevelChanged(bool topLevel); // for opacity

protected:
Expand Down Expand Up @@ -113,6 +114,8 @@ private slots:
SettingsTab* settingsTab;
QList<QDockWidget*> dockList;

QString m_appDefaultQss;

#ifndef Q_OS_ANDROID
QCheckBox* onTopBox;

Expand Down
5 changes: 4 additions & 1 deletion src/settingstab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ void SettingsTab::loadPreference()
on_Android_forceLandscapeBox_clicked();
// Android_dockBox only affect the config file
#else
on_Theme_setButton_clicked();
on_Opacity_Box_valueChanged(ui->Opacity_Box->value());
#endif
if(fontValid)
Expand Down Expand Up @@ -338,8 +339,10 @@ void SettingsTab::on_Conf_exportButton_clicked()

void SettingsTab::on_Theme_setButton_clicked()
{
QString themeName = ui->Theme_nameBox->currentData().toString();
m_settings->beginGroup("SerialTest");
m_settings->setValue("Theme_Name", ui->Theme_nameBox->currentData().toString());
m_settings->setValue("Theme_Name", themeName);
m_settings->endGroup();
emit themeChanged(themeName);
}

1 change: 1 addition & 0 deletions src/settingstab.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ private slots:
MySettings* m_settings;
void createConfFile(const QString &path, bool overwrite = false);
signals:
void themeChanged(const QString& themeName);
void opacityChanged(qreal value);
void fontChanged(QFont font);
void fullScreenStateChanged(bool isFullScreen);
Expand Down
2 changes: 1 addition & 1 deletion src/ui/settingstab.ui
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ If no config file is detected, This app will create one in the current working d
<item>
<widget class="QLabel" name="label_9">
<property name="text">
<string>Theme *:</string>
<string>Theme:</string>
</property>
</widget>
</item>
Expand Down

0 comments on commit 4d15a2f

Please sign in to comment.