From 9d969f1fddc77b3d7a3a63e790dc974de648b7a5 Mon Sep 17 00:00:00 2001 From: Bryant Date: Mon, 20 May 2013 20:31:38 -0700 Subject: [PATCH] The style changing UI now works somewhat. Doesn't interact with QSettings yet. --- src/ui/MainWindow.cc | 55 ++++++-------- src/ui/MainWindow.h | 16 ++-- src/ui/QGCSettingsWidget.cc | 146 +++++++++++++++++++++--------------- src/ui/QGCSettingsWidget.h | 11 ++- src/ui/QGCSettingsWidget.ui | 80 ++++---------------- 5 files changed, 137 insertions(+), 171 deletions(-) diff --git a/src/ui/MainWindow.cc b/src/ui/MainWindow.cc index 757c7c5ff..659449874 100644 --- a/src/ui/MainWindow.cc +++ b/src/ui/MainWindow.cc @@ -68,9 +68,12 @@ This file is part of the QGROUNDCONTROL project #include "PxQuadMAV.h" #include "SlugsMAV.h" - #include "LogCompressor.h" +// Set up some constants +const QString MainWindow::defaultDarkStyle = ":files/styles/style-dark.css"; +const QString MainWindow::defaultLightStyle = ":files/styles/style-light.css"; + MainWindow* MainWindow::instance(QSplashScreen* screen) { static MainWindow* _instance = 0; @@ -156,7 +159,7 @@ MainWindow::MainWindow(QWidget *parent): setCorner(Qt::BottomRightCorner, Qt::BottomDockWidgetArea); // Setup UI state machines - centerStackActionGroup->setExclusive(true); + centerStackActionGroup->setExclusive(true); centerStack = new QStackedWidget(this); setCentralWidget(centerStack); @@ -1220,45 +1223,31 @@ void MainWindow::enableAutoReconnect(bool enabled) bool MainWindow::loadStyle(QGC_MAINWINDOW_STYLE style, QString cssFile) { - qApp->setStyle("plastique"); - - // Set up the - switch (style) - { - default: - style = QGC_MAINWINDOW_STYLE_DARK; - case QGC_MAINWINDOW_STYLE_DARK: - darkStyleFileName = ":files/styles/style-dark.css"; - break; - case QGC_MAINWINDOW_STYLE_LIGHT: - styleFileName = ":files/styles/style-light.css"; - break; - case QGC_MAINWINDOW_STYLE_CUSTOM_DARK: - case QGC_MAINWINDOW_STYLE_CUSTOM_LIGHT: - styleFileName = cssFile; - break; - } - currentStyle = style; + qApp->setStyle("plastique"); - return loadStyleSheet(styleFileName); -} + // Store the new style classification. + currentStyle = style; -bool MainWindow::loadStyleSheet(QString cssFile) -{ // Load the new stylesheet. QFile styleSheet(cssFile); // Attempt to open the stylesheet, replacing the 'ICONDIR' token here with the proper application path. if (styleSheet.open(QIODevice::ReadOnly | QIODevice::Text)) { + // Signal to the user that the app will pause to apply a new stylesheet + qApp->setOverrideCursor(Qt::WaitCursor); + QString style = QString(styleSheet.readAll()); - style.replace("ICONDIR", QCoreApplication::applicationDirPath()+ "files/styles/"); + style.replace("ICONDIR", QCoreApplication::applicationDirPath() + "files/styles/"); qApp->setStyleSheet(style); - return true; + + // And restore the cursor before returning. + qApp->restoreOverrideCursor(); + return true; } // Otherwise alert return a failure code. - return false; + return false; } /** @@ -1499,8 +1488,8 @@ void MainWindow::addLink() // Go fishing for this link's configuration window QList actions = ui.menuNetwork->actions(); - const int32_t& linkIndex(LinkManager::instance()->getLinks().indexOf(link)); - const int32_t& linkID(LinkManager::instance()->getLinks()[linkIndex]->getId()); + const int32_t& linkIndex(LinkManager::instance()->getLinks().indexOf(link)); + const int32_t& linkID(LinkManager::instance()->getLinks()[linkIndex]->getId()); foreach (QAction* act, actions) { @@ -1526,8 +1515,8 @@ void MainWindow::addLink(LinkInterface *link) bool found(false); - const int32_t& linkIndex(LinkManager::instance()->getLinks().indexOf(link)); - const int32_t& linkID(LinkManager::instance()->getLinks()[linkIndex]->getId()); + const int32_t& linkIndex(LinkManager::instance()->getLinks().indexOf(link)); + const int32_t& linkID(LinkManager::instance()->getLinks()[linkIndex]->getId()); foreach (QAction* act, actions) { @@ -1614,7 +1603,7 @@ void MainWindow::UASCreated(UASInterface* uas) QIcon icon; // Set matching icon switch (uas->getSystemType()) - { + { case MAV_TYPE_GENERIC: icon = QIcon(":files/images/mavs/generic.svg"); break; diff --git a/src/ui/MainWindow.h b/src/ui/MainWindow.h index 58d9eee8e..c93d0cbd1 100644 --- a/src/ui/MainWindow.h +++ b/src/ui/MainWindow.h @@ -104,13 +104,14 @@ public: enum QGC_MAINWINDOW_STYLE { QGC_MAINWINDOW_STYLE_DARK, - QGC_MAINWINDOW_STYLE_LIGHT, - QGC_MAINWINDOW_STYLE_CUSTOM_DARK, - QGC_MAINWINDOW_STYLE_CUSTOM_LIGHT + QGC_MAINWINDOW_STYLE_LIGHT }; + static const QString defaultDarkStyle; + static const QString defaultLightStyle; + /** @brief Get current visual style */ - int getStyle() + QGC_MAINWINDOW_STYLE getStyle() { return currentStyle; } @@ -202,10 +203,9 @@ public slots: /** @brief Save power by reducing update rates */ void enableLowPowerMode(bool enabled) { lowPowerMode = enabled; } /** @brief Load a specific style. - * If it's a custom style, load the file indicated by the cssFile path. - */ + * If it's a custom style, load the file indicated by the cssFile path. + */ bool loadStyle(QGC_MAINWINDOW_STYLE style, QString cssFile); - bool loadStyleSheet(QString cssFile); /** @brief Add a custom tool widget */ void createCustomWidget(); @@ -328,7 +328,7 @@ protected: void buildCommonWidgets(); void connectCommonWidgets(); void connectCommonActions(); - void connectSenseSoarActions(); + void connectSenseSoarActions(); void loadSettings(); void storeSettings(); diff --git a/src/ui/QGCSettingsWidget.cc b/src/ui/QGCSettingsWidget.cc index 251b69c3f..bafb540c2 100644 --- a/src/ui/QGCSettingsWidget.cc +++ b/src/ui/QGCSettingsWidget.cc @@ -47,11 +47,11 @@ QGCSettingsWidget::QGCSettingsWidget(QWidget *parent, Qt::WindowFlags flags) : connect(ui->titleBarCheckBox,SIGNAL(clicked(bool)),MainWindow::instance(),SLOT(enableDockWidgetTitleBars(bool))); // Style - MainWindow::QGC_MAINWINDOW_STYLE style = (MainWindow::QGC_MAINWINDOW_STYLE)MainWindow::instance()->getStyle(); + MainWindow::QGC_MAINWINDOW_STYLE style = MainWindow::instance()->getStyle(); ui->styleChooser->setCurrentIndex(style); - connect(ui->styleChooser, SIGNAL(currentIndexChanged(int)), this, SLOT(styleChanged(int))); - connect(ui->darkStyleCustomButton, SIGNAL(clicked()), this, SLOT(selectStylesheet())); - connect(ui->lightStyleCustomButton, SIGNAL(clicked()), this, SLOT(selectStylesheet())); + connect(ui->styleChooser, SIGNAL(currentIndexChanged(int)), this, SLOT(styleChanged(int))); + connect(ui->styleCustomButton, SIGNAL(clicked()), this, SLOT(selectStylesheet())); + connect(ui->styleDefaultButton, SIGNAL(clicked()), this, SLOT(setDefaultStyle())); // Close / destroy connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(deleteLater())); @@ -65,68 +65,96 @@ QGCSettingsWidget::~QGCSettingsWidget() void QGCSettingsWidget::selectStylesheet() { // Let user select style sheet. The root directory for the file picker is the user's home directory if they haven't loaded a custom style. - // Otherwise it defaults to the directory of that custom file. - QString findDir; - if (MainWindow::instance()->getStyle() == MainWindow::QGC_MAINWINDOW_STYLE_CUSTOM_DARK || MainWindow::instance()->getStyle() == MainWindow::QGC_MAINWINDOW_STYLE_CUSTOM_LIGHT) - { - findDir = QDir::homePath(); - } - else - { - findDir = MainWindow::instance()->getStyleSheet(); - } - - QString newStyleFileName = QFileDialog::getOpenFileName(this, tr("Specify stylesheet"), findDir, tr("CSS Stylesheet (*.css);;")); - - // Load the new style sheet if a valid one was selected. - if (!newStyleFileName.isNull()) + // Otherwise it defaults to the directory of that custom file. + QString findDir; + QString oldStylesheet(ui->styleSheetFile->text()); + QFile styleSheet(oldStylesheet); + if (styleSheet.exists() && oldStylesheet[0] != ':') { - QFile styleSheet(newStyleFileName); - if (styleSheet.exists()) + findDir = styleSheet.fileName(); + } + else + { + findDir = QDir::homePath(); + } + + // Prompt the user to select a new style sheet. Do nothing if they cancel. + QString newStyleFileName = QFileDialog::getOpenFileName(this, tr("Specify stylesheet"), findDir, tr("CSS Stylesheet (*.css);;")); + if (newStyleFileName.isNull()) { + return; + } + + // Load the new style sheet if a valid one was selected, notifying the user + // of an error if necessary. + QFile newStyleFile(newStyleFileName); + if (!newStyleFile.exists() || !updateStyle(newStyleFileName)) + { + QMessageBox msgBox; + msgBox.setIcon(QMessageBox::Information); + msgBox.setText(tr("QGroundControl did not load a new style")); + msgBox.setInformativeText(tr("Stylesheet file %1 was not readable").arg(newStyleFileName)); + msgBox.setStandardButtons(QMessageBox::Ok); + msgBox.setDefaultButton(QMessageBox::Ok); + msgBox.exec(); + } + // And update the UI as needed. + else + { + switch (ui->styleChooser->currentIndex()) { - if (!updateStyle()) - { - QMessageBox msgBox; - msgBox.setIcon(QMessageBox::Information); - msgBox.setText(tr("QGroundControl did not load a new style")); - msgBox.setInformativeText(tr("Stylesheet file %1 was not readable").arg(newStyleFileName)); - msgBox.setStandardButtons(QMessageBox::Ok); - msgBox.setDefaultButton(QMessageBox::Ok); - msgBox.exec(); - } + case 0: + darkStyleSheet = newStyleFileName; + ui->styleSheetFile->setText(darkStyleSheet); + MainWindow::instance()->loadStyle(MainWindow::QGC_MAINWINDOW_STYLE_DARK, darkStyleSheet); + case 1: + lightStyleSheet = newStyleFileName; + ui->styleSheetFile->setText(lightStyleSheet); + MainWindow::instance()->loadStyle(MainWindow::QGC_MAINWINDOW_STYLE_LIGHT, lightStyleSheet); } - else - { - QMessageBox msgBox; - msgBox.setIcon(QMessageBox::Information); - msgBox.setText(tr("QGroundControl did not load a new style")); - msgBox.setInformativeText(tr("Stylesheet file %1 was not readable").arg(newStyleFileName)); - msgBox.setStandardButtons(QMessageBox::Ok); - msgBox.setDefaultButton(QMessageBox::Ok); - msgBox.exec(); - } } } -bool QGCSettingsWidget::updateStyle() +bool QGCSettingsWidget::updateStyle(QString style) { - switch (ui->styleChooser->currentIndex()) - { - case 0: - return MainWindow::instance()->loadStyle(MainWindow::QGC_MAINWINDOW_STYLE_DARK, QString()); - case 1: - return MainWindow::instance()->loadStyle(MainWindow::QGC_MAINWINDOW_STYLE_LIGHT, QString()); - case 2: - return MainWindow::instance()->loadStyle(MainWindow::QGC_MAINWINDOW_STYLE_CUSTOM_DARK, QString()); - case 3: - return MainWindow::instance()->loadStyle(MainWindow::QGC_MAINWINDOW_STYLE_CUSTOM_LIGHT, QString()); - default: - return false; - } + switch (ui->styleChooser->currentIndex()) + { + case 0: + darkStyleSheet = style; + return MainWindow::instance()->loadStyle(MainWindow::QGC_MAINWINDOW_STYLE_DARK, darkStyleSheet); + case 1: + lightStyleSheet = style; + return MainWindow::instance()->loadStyle(MainWindow::QGC_MAINWINDOW_STYLE_LIGHT, lightStyleSheet); + default: + return false; + } } void QGCSettingsWidget::styleChanged(int index) -{ - // And trigger a style update. - updateStyle(); -} \ No newline at end of file +{ + if (index == 1) + { + ui->styleSheetFile->setText(lightStyleSheet); + updateStyle(lightStyleSheet); + } + else + { + ui->styleSheetFile->setText(darkStyleSheet); + updateStyle(darkStyleSheet); + } +} + +void QGCSettingsWidget::setDefaultStyle() +{ + if (ui->styleChooser->currentIndex() == 1) + { + lightStyleSheet = MainWindow::defaultLightStyle; + ui->styleSheetFile->setText(lightStyleSheet); + updateStyle(lightStyleSheet); + } + else + { + darkStyleSheet = MainWindow::defaultDarkStyle; + ui->styleSheetFile->setText(darkStyleSheet); + updateStyle(darkStyleSheet); + } +} diff --git a/src/ui/QGCSettingsWidget.h b/src/ui/QGCSettingsWidget.h index 9ee73151a..9145299d5 100644 --- a/src/ui/QGCSettingsWidget.h +++ b/src/ui/QGCSettingsWidget.h @@ -17,12 +17,15 @@ public: ~QGCSettingsWidget(); public slots: - void styleChanged(int index); + void styleChanged(int index); + void setDefaultStyle(); + void selectStylesheet(); private: - Ui::QGCSettingsWidget *ui; - void selectStylesheet(); - bool updateStyle(); + Ui::QGCSettingsWidget* ui; + QString darkStyleSheet; + QString lightStyleSheet; + bool updateStyle(QString style); }; #endif // QGCSETTINGSWIDGET_H diff --git a/src/ui/QGCSettingsWidget.ui b/src/ui/QGCSettingsWidget.ui index 6c2e616b4..4cd210a47 100644 --- a/src/ui/QGCSettingsWidget.ui +++ b/src/ui/QGCSettingsWidget.ui @@ -6,8 +6,8 @@ 0 0 - 392 - 313 + 528 + 321 @@ -77,38 +77,18 @@ Style + + false + + + false + QLayout::SetMinimumSize - - - - - 0 - 0 - - - - Use: - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - @@ -128,41 +108,7 @@ - - - - 0 - 0 - - - - Dark: - - - - - - - - - - Custom - - - - - - - Default - - - - - - - - - + 0 @@ -170,22 +116,22 @@ - Light: + Stylesheet: - + - + Custom - + Default -- 2.22.0