GUI: Use theme in whole program

This commit is contained in:
Daniel Marjamäki 2019-07-22 15:55:36 +02:00
parent 4986b02566
commit 70e0c66c35
7 changed files with 92 additions and 24 deletions

View File

@ -29,6 +29,7 @@ CodeEditorStyle::CodeEditorStyle(
const QColor& CmtFGColor, const QFont::Weight& CmtWeight,
const QColor& SymbFGColor, const QColor& SymbBGColor,
const QFont::Weight& SymbWeight) :
mSystemTheme(false),
widgetFGColor(CtrlFGColor),
widgetBGColor(CtrlBGColor),
highlightBGColor(HiLiBGColor),
@ -49,6 +50,7 @@ CodeEditorStyle::CodeEditorStyle(
bool CodeEditorStyle::operator==(const CodeEditorStyle& rhs) const
{
if (mSystemTheme != rhs.mSystemTheme) return false;
if (widgetFGColor != rhs.widgetFGColor) return false;
if (widgetBGColor != rhs.widgetBGColor) return false;
if (highlightBGColor != rhs.highlightBGColor) return false;
@ -73,10 +75,18 @@ bool CodeEditorStyle::operator!=(const CodeEditorStyle& rhs) const
return !(*this == rhs);
}
CodeEditorStyle CodeEditorStyle::loadSettings(QSettings *settings)
CodeEditorStyle CodeEditorStyle::getSystemTheme()
{
CodeEditorStyle theStyle(defaultStyleLight);
if (!settings) return theStyle;
theStyle.mSystemTheme = true;
return theStyle;
}
CodeEditorStyle CodeEditorStyle::loadSettings(QSettings *settings)
{
CodeEditorStyle theStyle(CodeEditorStyle::getSystemTheme());
if (!settings)
return theStyle;
if (!settings->childGroups().contains(SETTINGS_STYLE_GROUP))
return theStyle;
@ -153,10 +163,13 @@ CodeEditorStyle CodeEditorStyle::loadSettings(QSettings *settings)
void CodeEditorStyle::saveSettings(QSettings *settings,
const CodeEditorStyle& theStyle)
{
if (!settings) return;
if (!settings)
return;
if (settings->childGroups().contains(SETTINGS_STYLE_GROUP)) {
settings->remove(SETTINGS_STYLE_GROUP);
if (theStyle.isSystemTheme())
return;
}
settings->beginGroup(SETTINGS_STYLE_GROUP);
@ -206,3 +219,18 @@ void CodeEditorStyle::saveSettings(QSettings *settings,
}
settings->endGroup();
}
static QString rgbStyleString(QColor c)
{
return QString("rgb(%1,%2,%3)").arg(c.red()).arg(c.green()).arg(c.blue());
}
QString CodeEditorStyle::generateStyleString() const
{
if (isSystemTheme())
return QString();
return QString("background:%1; color:%2; selection-background-color:%3;")
.arg(rgbStyleString(widgetBGColor))
.arg(rgbStyleString(widgetFGColor))
.arg(rgbStyleString(highlightBGColor));
}

View File

@ -64,10 +64,18 @@ public:
bool operator==(const CodeEditorStyle& rhs) const;
bool operator!=(const CodeEditorStyle& rhs) const;
bool isSystemTheme() const {
return mSystemTheme;
}
static CodeEditorStyle getSystemTheme();
static CodeEditorStyle loadSettings(QSettings *settings);
static void saveSettings(QSettings *settings, const CodeEditorStyle& theStyle);
QString generateStyleString() const;
public:
bool mSystemTheme;
QColor widgetFGColor;
QColor widgetBGColor;
QColor highlightBGColor;

View File

@ -33,6 +33,7 @@
#include "applicationlist.h"
#include "aboutdialog.h"
#include "codeeditorstyle.h"
#include "common.h"
#include "threadhandler.h"
#include "fileviewdialog.h"
@ -154,6 +155,8 @@ MainWindow::MainWindow(TranslationHandler* th, QSettings* settings) :
loadSettings();
updateStyleSetting();
mThread->initialize(mUI.mResults);
if (mProjectFile)
formatAndSetTitle(tr("Project:") + ' ' + mProjectFile->getFilename());
@ -398,6 +401,16 @@ void MainWindow::saveSettings() const
mUI.mResults->saveSettings(mSettings);
}
void MainWindow::updateStyleSetting()
{
mUI.mResults->updateStyleSetting(mSettings);
QString styleSheet = CodeEditorStyle::loadSettings(mSettings).generateStyleString();
mUI.mToolBarMain->setStyleSheet(styleSheet);
mUI.mToolBarView->setStyleSheet(styleSheet);
mUI.mToolBarFilter->setStyleSheet(styleSheet);
this->setStyleSheet(styleSheet);
}
void MainWindow::doAnalyzeProject(ImportProject p, const bool checkLibrary, const bool checkConfiguration)
{
clearResults();
@ -1010,7 +1023,7 @@ void MainWindow::programSettings()
dialog.showNoErrorsMessage(),
dialog.showErrorId(),
dialog.showInconclusive());
mUI.mResults->updateStyleSetting(mSettings);
this->updateStyleSetting();
const QString newLang = mSettings->value(SETTINGS_LANGUAGE, "en").toString();
setLanguage(newLang);
}

View File

@ -227,6 +227,9 @@ protected slots:
private:
/** Set widget themes */
void updateStyleSetting();
/** Get filename for last results */
QString getLastResults() const;

View File

@ -244,6 +244,9 @@ void ResultsView::updateStyleSetting(QSettings *settings)
{
CodeEditorStyle theStyle(CodeEditorStyle::loadSettings(settings));
mUI.mCode->setStyle(theStyle);
QString styleString(theStyle.generateStyleString());
mUI.mTree->setStyleSheet(styleString);
mUI.mDetails->setStyleSheet(styleString);
}
void ResultsView::setCheckDirectory(const QString &dir)

View File

@ -420,26 +420,33 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="mTabEditorStyle">
<widget class="QWidget" name="mTabTheme">
<attribute name="title">
<string>Code Editor</string>
<string>Theme</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_11">
<item>
<widget class="QGroupBox" name="gboxEditorStyle">
<property name="title">
<string>Code Editor Style</string>
<string>Style</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_12">
<item>
<widget class="QRadioButton" name="choiceLight">
<widget class="QRadioButton" name="mThemeSystem">
<property name="text">
<string>System Style</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="mThemeLight">
<property name="text">
<string>Default Light Style</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="choiceDark">
<widget class="QRadioButton" name="mThemeDark">
<property name="text">
<string>Default Dark Style</string>
</property>
@ -448,14 +455,14 @@
<item>
<layout class="QHBoxLayout" name="hlEditCustom">
<item>
<widget class="QRadioButton" name="choiceCustom">
<widget class="QRadioButton" name="mThemeCustom">
<property name="text">
<string>Custom</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnEditCustom">
<widget class="QPushButton" name="mBtnEditTheme">
<property name="text">
<string>Edit...</string>
</property>

View File

@ -84,10 +84,11 @@ SettingsDialog::SettingsDialog(ApplicationList *list,
connect(mUI.mBtnBrowsePythonPath, &QPushButton::clicked, this, &SettingsDialog::browsePythonPath);
connect(mUI.mBtnBrowseMisraFile, &QPushButton::clicked, this, &SettingsDialog::browseMisraFile);
connect(mUI.btnEditCustom, SIGNAL(clicked()), this, SLOT(editCodeEditorStyle()));
connect(mUI.choiceLight, SIGNAL(released()), this, SLOT(setCodeEditorStyleDefault()));
connect(mUI.choiceDark, SIGNAL(released()), this, SLOT(setCodeEditorStyleDefault()));
connect(mUI.choiceCustom, SIGNAL(toggled(bool)), mUI.btnEditCustom, SLOT(setEnabled(bool)));
connect(mUI.mBtnEditTheme, SIGNAL(clicked()), this, SLOT(editCodeEditorStyle()));
connect(mUI.mThemeSystem, SIGNAL(released()), this, SLOT(setCodeEditorStyleDefault()));
connect(mUI.mThemeDark, SIGNAL(released()), this, SLOT(setCodeEditorStyleDefault()));
connect(mUI.mThemeLight, SIGNAL(released()), this, SLOT(setCodeEditorStyleDefault()));
connect(mUI.mThemeCustom, SIGNAL(toggled(bool)), mUI.mBtnEditTheme, SLOT(setEnabled(bool)));
mUI.mListWidget->setSortingEnabled(false);
populateApplicationList();
@ -185,7 +186,6 @@ void SettingsDialog::saveSettingValues() const
settings.setValue(SETTINGS_LANGUAGE, langcode);
}
CodeEditorStyle::saveSettings(&settings, *mCurrentStyle);
}
void SettingsDialog::saveCheckboxValue(QSettings *settings, QCheckBox *box,
@ -330,8 +330,12 @@ void SettingsDialog::browseMisraFile()
// Slot to set default light style
void SettingsDialog::setCodeEditorStyleDefault()
{
if (mUI.choiceLight->isChecked()) *mCurrentStyle = defaultStyleLight;
if (mUI.choiceDark->isChecked()) *mCurrentStyle = defaultStyleDark;
if (mUI.mThemeSystem->isChecked())
*mCurrentStyle = CodeEditorStyle::getSystemTheme();
if (mUI.mThemeLight->isChecked())
*mCurrentStyle = defaultStyleLight;
if (mUI.mThemeDark->isChecked())
*mCurrentStyle = defaultStyleDark;
manageStyleControls();
}
@ -359,11 +363,13 @@ void SettingsDialog::browseClangPath()
void SettingsDialog::manageStyleControls()
{
bool isDefaultLight = *mCurrentStyle == defaultStyleLight;
bool isDefaultDark = *mCurrentStyle == defaultStyleDark;
mUI.choiceLight->setChecked(isDefaultLight && !isDefaultDark);
mUI.choiceDark->setChecked(!isDefaultLight && isDefaultDark);
mUI.choiceCustom->setChecked(!isDefaultLight && !isDefaultDark);
mUI.btnEditCustom->setEnabled(!isDefaultLight && !isDefaultDark);
bool isSystemTheme = mCurrentStyle->isSystemTheme();
bool isDefaultLight = !isSystemTheme && *mCurrentStyle == defaultStyleLight;
bool isDefaultDark = !isSystemTheme && *mCurrentStyle == defaultStyleDark;
mUI.mThemeSystem->setChecked(isSystemTheme);
mUI.mThemeLight->setChecked(isDefaultLight && !isDefaultDark);
mUI.mThemeDark->setChecked(!isDefaultLight && isDefaultDark);
mUI.mThemeCustom->setChecked(!isSystemTheme && !isDefaultLight && !isDefaultDark);
mUI.mBtnEditTheme->setEnabled(!isSystemTheme && !isDefaultLight && !isDefaultDark);
}