Browse Source

Tweaks to style and font selections

- AStyle tweaked to allow for more descriptive names (Stylesheet struct instead of QPairs `.first` and `.second`)
- Added warning about mixing stylesheets and custom fonts
- Added font setting to firstrundialog and minor tweaks
Felix Turo 4 years ago
parent
commit
1d80976cdb

+ 13 - 25
src/classes/astyle.cpp

@@ -27,28 +27,16 @@
 #include "src/testing/adebug.h"
 #include "src/classes/asettings.h"
 
-const QString AStyle::defaultStyle = QStringLiteral("Fusion");
-
-const QString AStyle::defaultStyleSheet = QString();
+const QString AStyle::defaultStyle = QLatin1String("Fusion");
 
 const QStringList AStyle::styles = QStyleFactory::keys();
 
-const QList<QPair <QString, QString>> AStyle::styleSheets = {
-    {QStringLiteral("Breeze"), QStringLiteral(":light.qss")},
-    {QStringLiteral("Breeze-Dark"), QStringLiteral(":dark.qss")}
+const QList<StyleSheet> AStyle::styleSheets = {
+    {QLatin1String("Breeze"),      QLatin1String(":light.qss")},
+    {QLatin1String("Breeze-Dark"), QLatin1String(":dark.qss")}
 };
 
 QString AStyle::currentStyle = defaultStyle;
-QString AStyle::currentStyleSheet = defaultStyleSheet;
-
-
-static inline QString read_stylesheet(const QString &stylesheet)
-{
-    QFile file(stylesheet);
-    file.open(QFile::ReadOnly | QFile::Text);
-    QTextStream stream(&file);
-    return stream.readAll();
-}
 
 /*!
  * \brief Setup style and stylesheet by reading from openPilotLog.ini
@@ -68,16 +56,16 @@ void AStyle::setup()
             return;
         }
     }
-    for (const auto &style_sheet_name : styleSheets) {
-        if (style_setting == style_sheet_name.first) {
-            setStyle(style_sheet_name);
-            currentStyle = style_sheet_name.first;
+    for (const auto &style_sheet : styleSheets) {
+        if (style_setting == style_sheet.styleSheetName) {
+            setStyle(style_sheet.styleSheetName);
+            currentStyle = style_sheet.styleSheetName;
             return;
         }
     }
 }
 
-void AStyle::setStyle(const QString style)
+void AStyle::setStyle(const QString &style)
 {
     DEB << "Setting style: " << style;
     qApp->setStyleSheet(QString());
@@ -85,11 +73,11 @@ void AStyle::setStyle(const QString style)
     currentStyle = style;
 }
 
-void AStyle::setStyle(const QPair<QString, QString> stylesheet)
+void AStyle::setStyle(const StyleSheet &style_sheet)
 {
-    DEB << "Setting stylesheet: " << stylesheet.first;
-    qApp->setStyleSheet(read_stylesheet(stylesheet.second));
-    currentStyleSheet = stylesheet.first;
+    DEB << "Setting stylesheet: " << style_sheet.styleSheetName;
+    qApp->setStyleSheet(read_stylesheet(style_sheet.fileName));
+    currentStyle = style_sheet.styleSheetName;
 }
 
 const QString& AStyle::style()

+ 25 - 9
src/classes/astyle.h

@@ -20,6 +20,28 @@
 #include <QString>
 #include <QFileInfo>
 #include <QMap>
+#include <QTextStream>
+
+/*!
+ * \brief The StyleSheet struct holds the Display Name and File Name (in the
+ * resource system) for the available stylesheets.
+ */
+struct StyleSheet
+{
+    StyleSheet(QLatin1String style_sheet_name, QLatin1String file_name)
+        : styleSheetName(style_sheet_name), fileName(file_name)
+    {}
+    QLatin1String styleSheetName;
+    QLatin1String fileName;
+};
+
+static inline QString read_stylesheet(const QString &stylesheet)
+{
+    QFile file(stylesheet);
+    file.open(QFile::ReadOnly | QFile::Text);
+    QTextStream stream(&file);
+    return stream.readAll();
+}
 
 /*!
  * \brief The AStyle class encapsulates style and stylesheet logic.
@@ -30,20 +52,14 @@ class AStyle
 {
 private:
     static QString currentStyle;
-    static QString currentStyleSheet;
 public:
     static const QStringList styles;
     static const QString defaultStyle;
-    /*!
-     * \brief contains a List of the available style sheets and their
-     * file name in the resource system.
-     */
-    static const QList<QPair<QString, QString>> styleSheets;
-    static const QString defaultStyleSheet;
+    static const QList<StyleSheet> styleSheets;
 
     static void setup();
-    static void setStyle(const QString style);
-    static void setStyle(const QPair<QString, QString> stylesheet);
+    static void setStyle(const QString &style);
+    static void setStyle(const StyleSheet &style_sheet);
     static const QString& style();
 };
 

+ 47 - 30
src/gui/dialogues/firstrundialog.cpp

@@ -82,7 +82,7 @@ void FirstRunDialog::on_nextPushButton_clicked()
         ui->nextPushButton->setText(tr("Done"));
         break;
     case 2:
-        if(!finish())
+        if(!finishSetup())
             QDialog::reject();
         else
             QDialog::accept();
@@ -91,9 +91,33 @@ void FirstRunDialog::on_nextPushButton_clicked()
     ui->stackedWidget->setCurrentIndex(current_idx + 1);
 }
 
-bool FirstRunDialog::finish()
+bool FirstRunDialog::finishSetup()
 {
+    writeSettings();
 
+    if (!setupDatabase()) {
+        QMessageBox message_box(QMessageBox::Critical, tr("Database setup failed"),
+                                tr("Errors have ocurred creating the database."
+                                   "Without a working database The application will not be usable.<br>"
+                                   "The following error has ocurred:<br>%1"
+                                   ).arg(aDB->lastError.text()));
+        message_box.exec();
+        return false;
+    }
+
+    if (!createUserEntry()) {
+        QMessageBox message_box(QMessageBox::Critical, tr("Database setup failed"),
+                                tr("Unable to execute database query<br>"
+                                   "The following error has occured:<br>%1"
+                                   ).arg(aDB->lastError.text()));
+        message_box.exec();
+        return false;
+    }
+    return true;
+}
+
+void FirstRunDialog::writeSettings()
+{
     ASettings::write(ASettings::FlightLogging::Function, ui->functionComboBox->currentText());
     ASettings::write(ASettings::FlightLogging::Approach, ui->approachComboBox->currentIndex());
     ASettings::write(ASettings::FlightLogging::NightLogging, ui->nightComboBox->currentIndex());
@@ -104,33 +128,8 @@ bool FirstRunDialog::finish()
     ASettings::write(ASettings::FlightLogging::PopupCalendar, true);
     ASettings::write(ASettings::FlightLogging::PilotFlying, true);
     ASettings::write(ASettings::FlightLogging::FlightTimeFormat, Opl::Time::Default);
-
-    QMap<QString, QVariant> data;
+    ASettings::write(ASettings::Main::UseSystemFont, true);
     ASettings::write(ASettings::UserData::DisplaySelfAs, ui->aliasComboBox->currentIndex());
-    data.insert(Opl::Db::PILOTS_LASTNAME, ui->lastnameLineEdit->text());
-    data.insert(Opl::Db::PILOTS_FIRSTNAME, ui->firstnameLineEdit->text());
-    data.insert(Opl::Db::PILOTS_ALIAS, QStringLiteral("self"));
-    data.insert(Opl::Db::PILOTS_EMPLOYEEID, ui->employeeidLineEdit->text());
-    data.insert(Opl::Db::PILOTS_PHONE, ui->phoneLineEdit->text());
-    data.insert(Opl::Db::PILOTS_EMAIL, ui->emailLineEdit->text());
-
-    QMessageBox db_fail_msg_box(QMessageBox::Critical, tr("Database setup failed"),
-                                tr("Errors have ocurred creating the database."
-                                   "Without a working database The application will not be usable."));
-    if (!setupDatabase()) {
-        db_fail_msg_box.exec();
-        return false;
-    }
-
-    aDB->updateLayout();
-
-    auto pilot = APilotEntry(1);
-    pilot.setData(data);
-    if(!aDB->commit(pilot)){
-        db_fail_msg_box.exec();
-        return false;
-    }
-    return true;
 }
 
 bool FirstRunDialog::setupDatabase()
@@ -147,10 +146,11 @@ bool FirstRunDialog::setupDatabase()
         if (!ADataBaseSetup::downloadTemplates()) { // To do: return true only if size of dl != 0
             QMessageBox message_box(this);
             message_box.setText(tr("Downloading latest data has failed.<br><br>Using local data instead."));
+            message_box.exec();
             useLocalTemplates = true; // fall back
         } else {
-        useLocalTemplates = true;
-    }
+            useLocalTemplates = true;
+        }
     }
 
     aDB->disconnect();
@@ -165,9 +165,26 @@ bool FirstRunDialog::setupDatabase()
 
     if(!ADataBaseSetup::importDefaultData(useLocalTemplates))
         return false;
+    aDB->updateLayout();
     return true;
 }
 
+bool FirstRunDialog::createUserEntry()
+{
+    QMap<QString, QVariant> data;
+    data.insert(Opl::Db::PILOTS_LASTNAME, ui->lastnameLineEdit->text());
+    data.insert(Opl::Db::PILOTS_FIRSTNAME, ui->firstnameLineEdit->text());
+    data.insert(Opl::Db::PILOTS_ALIAS, QStringLiteral("self"));
+    data.insert(Opl::Db::PILOTS_EMPLOYEEID, ui->employeeidLineEdit->text());
+    data.insert(Opl::Db::PILOTS_PHONE, ui->phoneLineEdit->text());
+    data.insert(Opl::Db::PILOTS_EMAIL, ui->emailLineEdit->text());
+
+    auto pilot = APilotEntry(1);
+    pilot.setData(data);
+
+    return aDB->commit(pilot);
+}
+
 void FirstRunDialog::reject()
 {
     QMessageBox confirm(QMessageBox::Critical,

+ 6 - 4
src/gui/dialogues/firstrundialog.h

@@ -41,14 +41,16 @@ private slots:
 
     void on_nextPushButton_clicked();
 
-private:
+private:    
     Ui::FirstRunDialog *ui;
+    bool useLocalTemplates;
 
-    void reject() override;
+    void writeSettings();
     bool setupDatabase();
-    bool finish();
-    bool useLocalTemplates;
+    bool createUserEntry();
+    bool finishSetup();
 
+    void reject() override;
 };
 
 #endif // FIRSTRUNDIALOG_H

+ 29 - 6
src/gui/widgets/settingswidget.cpp

@@ -67,8 +67,8 @@ void SettingsWidget::setupComboBoxes(){
         // Style combo box
         const QSignalBlocker blocker_style(ui->styleComboBox);
         ui->styleComboBox->addItems(AStyle::styles);
-        for (const auto &style_sheet_name : AStyle::styleSheets) {
-            ui->styleComboBox->addItem(style_sheet_name.first);
+        for (const auto &style_sheet : AStyle::styleSheets) {
+            ui->styleComboBox->addItem(style_sheet.styleSheetName);
         }
         ui->styleComboBox->model()->sort(0);
 
@@ -117,10 +117,11 @@ void SettingsWidget::readSettings()
     {
         const QSignalBlocker font_blocker1(ui->fontSpinBox);
         const QSignalBlocker font_blocker2(ui->fontComboBox);
+        const QSignalBlocker font_blocker3(ui->fontCheckBox);
         ui->fontSpinBox->setValue(ASettings::read(ASettings::Main::FontSize).toUInt());
         ui->fontComboBox->setCurrentFont(QFont(ASettings::read(ASettings::Main::Font).toString()));
+        ui->fontCheckBox->setChecked(ASettings::read(ASettings::Main::UseSystemFont).toBool());
     }
-    ui->fontCheckBox->setChecked(ASettings::read(ASettings::Main::UseSystemFont).toBool());
 }
 
 void SettingsWidget::setupValidators()
@@ -353,9 +354,9 @@ void SettingsWidget::on_styleComboBox_currentTextChanged(const QString& new_styl
         }
     }
 
-    for (const auto &style_sheet_name : AStyle::styleSheets) {
-        if (new_style_setting == style_sheet_name.first) {
-            AStyle::setStyle(style_sheet_name);
+    for (const auto &style_sheet : AStyle::styleSheets) {
+        if (new_style_setting == style_sheet.styleSheetName) {
+            AStyle::setStyle(style_sheet);
             ASettings::write(ASettings::Main::Style, new_style_setting);
             return;
         }
@@ -366,6 +367,7 @@ void SettingsWidget::on_fontComboBox_currentFontChanged(const QFont &f)
 {
     qApp->setFont(f);
     ASettings::write(ASettings::Main::Font, f.toString());
+    DEB << "Setting Font:" << f.toString();
 }
 
 void SettingsWidget::on_fontSpinBox_valueChanged(int arg1)
@@ -374,10 +376,18 @@ void SettingsWidget::on_fontSpinBox_valueChanged(int arg1)
     f.setPointSize(arg1);
     qApp->setFont(f);
     ASettings::write(ASettings::Main::FontSize, arg1);
+    DEB << "Setting Font:" << f.toString();
 }
 
 void SettingsWidget::on_fontCheckBox_stateChanged(int arg1)
 {
+    if (usingStylesheet() && arg1 == Qt::Unchecked) {
+        QMessageBox message_box(this);
+        message_box.setText(tr("The style you have currently selected may not be fully compatible "
+                               "with changing to a custom font while the application is running.<br><br>"
+                               "Applying your changes may require restarting the application.<br>"));
+        message_box.exec();
+    }
     switch (arg1) {
     case Qt::Unchecked:
     {
@@ -405,3 +415,16 @@ void SettingsWidget::on_fontCheckBox_stateChanged(int arg1)
         break;
     }
 }
+
+/*!
+ * \brief Determines if the user has selected a stylesheet or is using a Qt Style Factory Style
+ * \return
+ */
+bool SettingsWidget::usingStylesheet()
+{
+    for (const auto &style_sheet : AStyle::styleSheets) {
+        if (style_sheet.styleSheetName == ui->styleComboBox->currentText())
+            return true;
+    }
+    return false;
+}

+ 1 - 0
src/gui/widgets/settingswidget.h

@@ -79,6 +79,7 @@ private:
 
     void updatePersonalDetails();
 
+    bool usingStylesheet();
 signals:
     void viewSelectionChanged(int view_id);
 };