Browse Source

Refactored Theme key, added StyleSheet key, extended AStyle with stylesheet

George 4 years ago
parent
commit
536837fb13
5 changed files with 51 additions and 11 deletions
  1. 5 4
      main.cpp
  2. 2 1
      src/classes/asettings.cpp
  3. 2 1
      src/classes/asettings.h
  4. 37 4
      src/classes/astyle.cpp
  5. 5 1
      src/classes/astyle.h

+ 5 - 4
main.cpp

@@ -64,10 +64,11 @@ int main(int argc, char *argv[])
 
     //sqlite does not deal well with multiple connections, ensure only one instance is running
     ARunGuard guard(QStringLiteral("opl_single_key"));
-        if ( !guard.tryToRun() ){
-            DEB << "Another Instance is already running. Exiting.";
-            return 2;
-        }
+    if ( !guard.tryToRun() ){
+        DEB << "Another Instance is already running. Exiting.";
+        return 2;
+    }
+
 
     MainWindow w;
     //w.showMaximized();

+ 2 - 1
src/classes/asettings.cpp

@@ -21,7 +21,8 @@
 
 
 QMap<ASettings::Main, QString> ASettings::mainMap = {
-    {Main::Theme,   QStringLiteral("theme")},
+    {Main::Style,      QStringLiteral("style")},
+    {Main::StyleSheet, QStringLiteral("stylesheet")},
 };
 
 QMap<ASettings::LogBook, QString> ASettings::logBookMap = {

+ 2 - 1
src/classes/asettings.h

@@ -31,7 +31,8 @@ public:
     };
 
     enum class Main {
-        Theme,
+        Style,
+        StyleSheet,
     };
 
     enum class LogBook {

+ 37 - 4
src/classes/astyle.cpp

@@ -11,31 +11,64 @@ const QString AStyle::defaultStyle = QStringLiteral("fusion");
 const QString AStyle::defaultStyle = QStringLiteral("Windows");
 #endif
 
+const QString AStyle::defaultQStyleSheet = QStringLiteral("dark");
 const QStringList AStyle::styles = QStyleFactory::keys();
 QString AStyle::currentStyle;
+QString AStyle::currentStyleSheet;
+
+static inline
+QString read_stylesheet(const QString stylesheet)
+{
+    DEB << "reading:" << ":" + stylesheet + ".qss";
+    QFile file(":" + stylesheet + ".qss");
+    file.open(QFile::ReadOnly | QFile::Text);
+    QTextStream stream(&file);
+    return stream.readAll();
+}
 
 // [G]: Are there leaks when style changes?
 void AStyle::setup()
 {
-    QVariant app_style = ASettings::read(ASettings::Main::Theme);
+    QVariant app_style = ASettings::read(ASettings::Main::Style);
+    QVariant app_stylesheet = ASettings::read(ASettings::Main::StyleSheet);
+
     if(!app_style.toBool()){
         DEB << "Setting style to default:" << defaultStyle;
         app_style = defaultStyle;
-        ASettings::write(ASettings::Main::Theme, app_style);
+        ASettings::write(ASettings::Main::Style, app_style);
     }
     DEB << "Style set to:" << app_style;
     QApplication::setStyle(QStyleFactory::create(app_style.toString()));
     currentStyle = app_style.toString();
+
+    if(!app_stylesheet.toBool()){
+        DEB << "Setting stylesheet to default:" << defaultQStyleSheet;
+        app_stylesheet = defaultQStyleSheet;
+        ASettings::write(ASettings::Main::StyleSheet, app_stylesheet);
+    }
+    DEB << "Stylesheet set to:" << app_stylesheet;
+    qApp->setStyleSheet(read_stylesheet(app_stylesheet.toString()));
+    currentStyleSheet = app_stylesheet.toString();
 }
 
-void AStyle::setStyle(const QString& style)
+void AStyle::setStyle(const QString style)
 {
     DEB << "Setting style to:" << style;
     QApplication::setStyle(QStyleFactory::create(style));
-    ASettings::write(ASettings::Main::Theme, style);
+    ASettings::write(ASettings::Main::Style, style);
+}
+
+void AStyle::setStyleSheet(const QString stylesheet)
+{
+    qApp->setStyleSheet(read_stylesheet(stylesheet));
 }
 
 const QString& AStyle::style()
 {
     return currentStyle;
 }
+
+const QString& AStyle::styleSheet()
+{
+    return currentStyleSheet;
+}

+ 5 - 1
src/classes/astyle.h

@@ -7,13 +7,17 @@ class AStyle
 {
 private:
     static QString currentStyle;
+    static QString currentStyleSheet;
 public:
     static const QStringList styles;
     static const QString defaultStyle;
+    static const QString defaultQStyleSheet;
 
     static void setup();
-    static void setStyle(const QString& style);
+    static void setStyle(const QString style);
+    static void setStyleSheet(const QString style_sheet);
     static const QString& style();
+    static const QString& styleSheet();
 };
 
 #endif // ASTYLE_H