Browse Source

Refactored FirstRunDialog now StackedWidget, cleaned next/previous logic

George 4 years ago
parent
commit
785da1d6df

+ 0 - 1
main.cpp

@@ -50,7 +50,6 @@ int main(int argc, char *argv[])
     ASettings::setup();
 
     aDB()->connect();
-
     if (!ASettings::read("setup/setup_complete").toBool()) {
         FirstRunDialog dialog;
         dialog.exec();

+ 44 - 12
src/gui/dialogues/firstrundialog.cpp

@@ -8,13 +8,20 @@
 #include "src/classes/asettings.h"
 const auto TEMPLATE_URL = QLatin1String("https://raw.githubusercontent.com/fiffty-50/openpilotlog/develop/assets/database/templates/");
 
+static inline
+void prompt_error_box(QString title, QString text, QWidget* parent = nullptr)
+{
+    QMessageBox(QMessageBox::Warning, title, text, QMessageBox::Ok, parent).exec();
+}
+
 FirstRunDialog::FirstRunDialog(QWidget *parent) :
     QDialog(parent),
     ui(new Ui::FirstRunDialog)
 {
     ui->setupUi(this);
-    ui->tabWidget->setCurrentIndex(0);
+    ui->stackedWidget->setCurrentIndex(0);
     ui->lastnameLineEdit->setFocus();
+    ui->previousPushButton->setEnabled(false);
     ui->nightComboBox->setCurrentIndex(1);
 
     auto *themeGroup = new QButtonGroup;
@@ -33,18 +40,43 @@ FirstRunDialog::~FirstRunDialog()
 
 void FirstRunDialog::on_previousPushButton_clicked()
 {
-    if(ui->tabWidget->currentIndex()>0)
-        ui->tabWidget->setCurrentIndex(ui->tabWidget->currentIndex()-1);
+    auto current_idx = ui->stackedWidget->currentIndex();
+    switch (current_idx) {
+    case 0:
+        return;
+    case 1:
+        ui->previousPushButton->setEnabled(false);
+        break;
+    case 2:
+        ui->nextPushButton->setText("Next");
+        break;
+    }
+    ui->stackedWidget->setCurrentIndex(current_idx - 1);
+
 }
 
 void FirstRunDialog::on_nextPushButton_clicked()
 {
-    if(ui->tabWidget->currentIndex()<2){
-        ui->tabWidget->setCurrentIndex(ui->tabWidget->currentIndex()+1);
-    } else {
-        emit ui->finishButton->clicked();
+    auto current_idx = ui->stackedWidget->currentIndex();
+    // [G]: per index do appropriate error handling
+    switch (current_idx) {
+    case 0:
+        if(ui->firstnameLineEdit->text().isEmpty()
+                || ui->lastnameLineEdit->text().isEmpty())
+        {
+            prompt_error_box("Error", "Please enter first and last name");
+            return;
+        }
+        ui->previousPushButton->setEnabled(true);
+        break;
+    case 1:
+        ui->nextPushButton->setText("Done");
+        break;
+    case 2:
+        finish();
+        return;
     }
-
+    ui->stackedWidget->setCurrentIndex(current_idx + 1);
 }
 
 void FirstRunDialog::on_themeGroup_toggled(int id)
@@ -52,12 +84,12 @@ void FirstRunDialog::on_themeGroup_toggled(int id)
     ASettings::write("main/theme", id);
 }
 
-void FirstRunDialog::on_finishButton_clicked()
+void FirstRunDialog::finish()
 {
     if(ui->lastnameLineEdit->text().isEmpty() || ui->firstnameLineEdit->text().isEmpty()){
-        auto mb = new QMessageBox(this);
-        mb->setText("You have to enter a valid first and last name for the logbook.");
-        mb->show();
+        auto mb = QMessageBox(this);
+        mb.setText("You have to enter a valid first and last name for the logbook.");
+        mb.show();
     } else {
         ASettings::write("userdata/lastname", ui->lastnameLineEdit->text());
         ASettings::write("userdata/firstname", ui->firstnameLineEdit->text());

+ 5 - 2
src/gui/dialogues/firstrundialog.h

@@ -25,12 +25,15 @@ private slots:
 
     void on_themeGroup_toggled(int id);
 
-    void on_finishButton_clicked();
 
-    bool finishSetup();
 
 private:
     Ui::FirstRunDialog *ui;
+    // [G]: finish is the old signal.
+    // finishSetup does something with template of database which
+    // goes over my head but everything works for now. Better naming needed
+    bool finishSetup();
+    void finish();
 
 };
 

+ 33 - 62
src/gui/dialogues/firstrundialog.ui

@@ -14,15 +14,23 @@
    <string>Dialog</string>
   </property>
   <layout class="QGridLayout" name="gridLayout">
-   <item row="0" column="0" colspan="2">
-    <widget class="QTabWidget" name="tabWidget">
-     <property name="currentIndex">
-      <number>0</number>
+   <item row="1" column="0">
+    <widget class="QPushButton" name="previousPushButton">
+     <property name="text">
+      <string>Previous</string>
+     </property>
+    </widget>
+   </item>
+   <item row="1" column="1">
+    <widget class="QPushButton" name="nextPushButton">
+     <property name="text">
+      <string>Next</string>
      </property>
-     <widget class="QWidget" name="tab_1">
-      <attribute name="title">
-       <string>General</string>
-      </attribute>
+    </widget>
+   </item>
+   <item row="0" column="0" colspan="2">
+    <widget class="QStackedWidget" name="stackedWidget">
+     <widget class="QWidget" name="stackedWidgetPage1">
       <layout class="QGridLayout" name="gridLayout_2">
        <item row="0" column="1">
         <widget class="QLabel" name="label">
@@ -130,10 +138,7 @@
        </item>
       </layout>
      </widget>
-     <widget class="QWidget" name="tab_2">
-      <attribute name="title">
-       <string>Flight Logging</string>
-      </attribute>
+     <widget class="QWidget" name="stackedWidgetPage2">
       <layout class="QGridLayout" name="gridLayout_3">
        <item row="0" column="1">
         <widget class="QLabel" name="label_2">
@@ -387,11 +392,22 @@
        </item>
       </layout>
      </widget>
-     <widget class="QWidget" name="tab_3">
-      <attribute name="title">
-       <string>Finish</string>
-      </attribute>
+     <widget class="QWidget" name="stackedWidgetPage3">
       <layout class="QGridLayout" name="gridLayout_4">
+       <item row="2" column="1">
+        <widget class="QCheckBox" name="lightThemeCheckBox">
+         <property name="text">
+          <string>Light Theme</string>
+         </property>
+        </widget>
+       </item>
+       <item row="3" column="1">
+        <widget class="QCheckBox" name="darkThemeCheckBox">
+         <property name="text">
+          <string>Dark Theme</string>
+         </property>
+        </widget>
+       </item>
        <item row="0" column="0" colspan="2">
         <widget class="QLabel" name="label_4">
          <property name="text">
@@ -422,54 +438,10 @@
          </property>
         </widget>
        </item>
-       <item row="2" column="1">
-        <widget class="QCheckBox" name="lightThemeCheckBox">
-         <property name="text">
-          <string>Light Theme</string>
-         </property>
-        </widget>
-       </item>
-       <item row="3" column="1">
-        <widget class="QCheckBox" name="darkThemeCheckBox">
-         <property name="text">
-          <string>Dark Theme</string>
-         </property>
-        </widget>
-       </item>
-       <item row="4" column="0" colspan="2">
-        <widget class="QPushButton" name="finishButton">
-         <property name="minimumSize">
-          <size>
-           <width>0</width>
-           <height>60</height>
-          </size>
-         </property>
-         <property name="styleSheet">
-          <string notr="true">background-color:rgb(78, 154, 6)</string>
-         </property>
-         <property name="text">
-          <string>Finish Setup</string>
-         </property>
-        </widget>
-       </item>
       </layout>
      </widget>
     </widget>
    </item>
-   <item row="1" column="0">
-    <widget class="QPushButton" name="previousPushButton">
-     <property name="text">
-      <string>Previous</string>
-     </property>
-    </widget>
-   </item>
-   <item row="1" column="1">
-    <widget class="QPushButton" name="nextPushButton">
-     <property name="text">
-      <string>Next</string>
-     </property>
-    </widget>
-   </item>
   </layout>
  </widget>
  <tabstops>
@@ -489,8 +461,7 @@
   <tabstop>systemThemeCheckBox</tabstop>
   <tabstop>lightThemeCheckBox</tabstop>
   <tabstop>darkThemeCheckBox</tabstop>
-  <tabstop>finishButton</tabstop>
-  <tabstop>tabWidget</tabstop>
+  <tabstop>stackedWidget</tabstop>
  </tabstops>
  <resources/>
  <connections/>