From 5341d327fd62b92ad4a5887ce8cf24e984059488 Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 8 Apr 2016 10:26:19 +0100 Subject: [PATCH] Restructure paths handling in the launcher MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit - move the dialog into a new ‘add-ons’ tab - separate out ‘restore settings’ from selecting a new fg-data - actually relaunch the app --- src/GUI/Launcher.ui | 34 +++---- src/GUI/PathsDialog.cxx | 175 +++++++++++++++++++++++---------- src/GUI/PathsDialog.hxx | 21 ++-- src/GUI/PathsDialog.ui | 121 ++++++++++++----------- src/GUI/QtLauncher.cxx | 164 +++++++++++++----------------- src/GUI/QtLauncher_private.hxx | 7 +- src/GUI/SetupRootDialog.ui | 2 +- 7 files changed, 288 insertions(+), 236 deletions(-) diff --git a/src/GUI/Launcher.ui b/src/GUI/Launcher.ui index 3ba4e55a5..a49f3cb8c 100644 --- a/src/GUI/Launcher.ui +++ b/src/GUI/Launcher.ui @@ -269,6 +269,16 @@ 8 + + + + Restore Defaults... + + + false + + + @@ -442,7 +452,7 @@ - + Additional options @@ -470,7 +480,7 @@ - + Qt::Vertical @@ -483,23 +493,6 @@ - - - - Configure add-on aircraft and scenery… - - - true - - - - - - - Change data files location… - - - @@ -537,6 +530,9 @@ false + + false + false diff --git a/src/GUI/PathsDialog.cxx b/src/GUI/PathsDialog.cxx index 1809c7114..9ded69d40 100644 --- a/src/GUI/PathsDialog.cxx +++ b/src/GUI/PathsDialog.cxx @@ -4,18 +4,21 @@ #include #include #include +#include +#include #include "CatalogListModel.hxx" #include "AddCatalogDialog.hxx" #include "AircraftModel.hxx" +#include "QtLauncher_private.hxx" #include
#include
#include -PathsDialog::PathsDialog(QWidget *parent, simgear::pkg::RootRef root) : - QDialog(parent), - m_ui(new Ui::PathsDialog), +AddOnsPage::AddOnsPage(QWidget *parent, simgear::pkg::RootRef root) : + QWidget(parent), + m_ui(new Ui::AddOnsPage), m_packageRoot(root) { m_ui->setupUi(this); @@ -33,27 +36,30 @@ PathsDialog::PathsDialog(QWidget *parent, simgear::pkg::RootRef root) : m_ui->aircraftPathsList->setDropIndicatorShown(true); connect(m_ui->addCatalog, &QToolButton::clicked, - this, &PathsDialog::onAddCatalog); + this, &AddOnsPage::onAddCatalog); connect(m_ui->addDefaultCatalogButton, &QPushButton::clicked, - this, &PathsDialog::onAddDefaultCatalog); + this, &AddOnsPage::onAddDefaultCatalog); connect(m_ui->removeCatalog, &QToolButton::clicked, - this, &PathsDialog::onRemoveCatalog); + this, &AddOnsPage::onRemoveCatalog); connect(m_ui->addSceneryPath, &QToolButton::clicked, - this, &PathsDialog::onAddSceneryPath); + this, &AddOnsPage::onAddSceneryPath); connect(m_ui->removeSceneryPath, &QToolButton::clicked, - this, &PathsDialog::onRemoveSceneryPath); + this, &AddOnsPage::onRemoveSceneryPath); connect(m_ui->addAircraftPath, &QToolButton::clicked, - this, &PathsDialog::onAddAircraftPath); + this, &AddOnsPage::onAddAircraftPath); connect(m_ui->removeAircraftPath, &QToolButton::clicked, - this, &PathsDialog::onRemoveAircraftPath); + this, &AddOnsPage::onRemoveAircraftPath); connect(m_ui->changeDownloadDir, &QPushButton::clicked, - this, &PathsDialog::onChangeDownloadDir); + this, &AddOnsPage::onChangeDownloadDir); connect(m_ui->clearDownloadDir, &QPushButton::clicked, - this, &PathsDialog::onClearDownloadDir); + this, &AddOnsPage::onClearDownloadDir); + + connect(m_ui->changeDataDir, &QPushButton::clicked, + this, &AddOnsPage::onChangeDataDir); QSettings settings; @@ -71,42 +77,17 @@ PathsDialog::PathsDialog(QWidget *parent, simgear::pkg::RootRef root) : updateUi(); } -PathsDialog::~PathsDialog() +AddOnsPage::~AddOnsPage() { delete m_ui; } -void PathsDialog::accept() -{ - QSettings settings; - QStringList paths; - for (int i=0; isceneryPathsList->count(); ++i) { - paths.append(m_ui->sceneryPathsList->item(i)->text()); - } - - settings.setValue("scenery-paths", paths); - paths.clear(); - - for (int i=0; iaircraftPathsList->count(); ++i) { - paths.append(m_ui->aircraftPathsList->item(i)->text()); - } - - settings.setValue("aircraft-paths", paths); - - if (m_downloadDir.isEmpty()) { - settings.remove("download-dir"); - } else { - settings.setValue("download-dir", m_downloadDir); - } - - QDialog::accept(); -} - -void PathsDialog::onAddSceneryPath() +void AddOnsPage::onAddSceneryPath() { QString path = QFileDialog::getExistingDirectory(this, tr("Choose scenery folder")); if (!path.isEmpty()) { m_ui->sceneryPathsList->addItem(path); + saveSceneryPaths(); } // work around a Qt OS-X bug - this dialog is ending ordered @@ -115,14 +96,15 @@ void PathsDialog::onAddSceneryPath() window()->raise(); } -void PathsDialog::onRemoveSceneryPath() +void AddOnsPage::onRemoveSceneryPath() { if (m_ui->sceneryPathsList->currentItem()) { delete m_ui->sceneryPathsList->currentItem(); + saveSceneryPaths(); } } -void PathsDialog::onAddAircraftPath() +void AddOnsPage::onAddAircraftPath() { QString path = QFileDialog::getExistingDirectory(this, tr("Choose aircraft folder")); if (!path.isEmpty()) { @@ -156,6 +138,8 @@ void PathsDialog::onAddAircraftPath() m_ui->aircraftPathsList->addItem(path); } } + + saveAircraftPaths(); } // work around a Qt OS-X bug - this dialog is ending ordered // behind the main settings dialog (consequence of modal-dialog @@ -163,14 +147,40 @@ void PathsDialog::onAddAircraftPath() window()->raise(); } -void PathsDialog::onRemoveAircraftPath() +void AddOnsPage::onRemoveAircraftPath() { if (m_ui->aircraftPathsList->currentItem()) { delete m_ui->aircraftPathsList->currentItem(); + saveAircraftPaths(); } } -void PathsDialog::onAddCatalog() +void AddOnsPage::saveAircraftPaths() +{ + QSettings settings; + QStringList paths; + + for (int i=0; iaircraftPathsList->count(); ++i) { + paths.append(m_ui->aircraftPathsList->item(i)->text()); + } + + settings.setValue("aircraft-paths", paths); +} + +void AddOnsPage::saveSceneryPaths() +{ + QSettings settings; + QStringList paths; + for (int i=0; isceneryPathsList->count(); ++i) { + paths.append(m_ui->sceneryPathsList->item(i)->text()); + } + + settings.setValue("scenery-paths", paths); + + emit sceneryPathsChanged(); +} + +void AddOnsPage::onAddCatalog() { QScopedPointer dlg(new AddCatalogDialog(this, m_packageRoot)); dlg->exec(); @@ -179,7 +189,7 @@ void PathsDialog::onAddCatalog() } } -void PathsDialog::onAddDefaultCatalog() +void AddOnsPage::onAddDefaultCatalog() { // check it's not a duplicate somehow FGHTTPClient* http = globals->get_subsystem(); @@ -196,7 +206,7 @@ void PathsDialog::onAddDefaultCatalog() } } -void PathsDialog::onRemoveCatalog() +void AddOnsPage::onRemoveCatalog() { QModelIndex mi = m_ui->catalogsList->currentIndex(); FGHTTPClient* http = globals->get_subsystem(); @@ -228,25 +238,74 @@ void PathsDialog::onRemoveCatalog() updateUi(); } -void PathsDialog::onChangeDownloadDir() +void AddOnsPage::onChangeDownloadDir() { QString path = QFileDialog::getExistingDirectory(this, tr("Choose downloads folder"), m_downloadDir); - if (!path.isEmpty()) { - m_downloadDir = path; - updateUi(); + if (path.isEmpty()) { + return; // user cancelled } + + m_downloadDir = path; + setDownloadDir(); } -void PathsDialog::onClearDownloadDir() +void AddOnsPage::onClearDownloadDir() { // does this need an 'are you sure'? m_downloadDir.clear(); + + setDownloadDir(); +} + +void AddOnsPage::setDownloadDir() +{ + QSettings settings; + if (m_downloadDir.isEmpty()) { + settings.remove("download-dir"); + } else { + settings.setValue("download-dir", m_downloadDir); + } + + if (m_downloadDir.isEmpty()) { + flightgear::Options::sharedInstance()->clearOption("download-dir"); + } else { + flightgear::Options::sharedInstance()->setOption("download-dir", m_downloadDir.toStdString()); + } + + emit downloadDirChanged(); updateUi(); } -void PathsDialog::updateUi() +void AddOnsPage::onChangeDataDir() +{ + QMessageBox mbox(this); + mbox.setText(tr("Change the data files used by FlightGear?")); + mbox.setInformativeText(tr("FlightGear requires additional files to operate. " + "(Also called the base package, or fg-data) " + "You can restart FlightGear and choose a " + "different data files location, or restore the default setting.")); + QPushButton* quitButton = mbox.addButton(tr("Restart FlightGear now"), QMessageBox::YesRole); + mbox.addButton(QMessageBox::Cancel); + mbox.setDefaultButton(QMessageBox::Cancel); + mbox.setIconPixmap(QPixmap(":/app-icon-large")); + + mbox.exec(); + if (mbox.clickedButton() != quitButton) { + return; + } + + { + QSettings settings; + // set the option to the magic marker value + settings.setValue("fg-root", "!ask"); + } // scope the ensure settings are written nicely + + QtLauncher::restartTheApp(QStringList()); +} + +void AddOnsPage::updateUi() { QString s = m_downloadDir; if (s.isEmpty()) { @@ -260,6 +319,18 @@ void PathsDialog::updateUi() QString m = tr("Download location: %1").arg(s); m_ui->downloadLocation->setText(m); + QString dataLoc; + QSettings settings; + QString root = settings.value("fg-root").toString(); + if (root.isNull()) { + dataLoc = tr("built-in"); + } else { + dataLoc = root; + } + + m_ui->dataLocation->setText(tr("Data location: %1").arg(dataLoc)); + + FGHTTPClient* http = globals->get_subsystem(); m_ui->addDefaultCatalogButton->setEnabled(!http->isDefaultCatalogInstalled()); } diff --git a/src/GUI/PathsDialog.hxx b/src/GUI/PathsDialog.hxx index acafe3d06..b6303421e 100644 --- a/src/GUI/PathsDialog.hxx +++ b/src/GUI/PathsDialog.hxx @@ -7,21 +7,22 @@ namespace Ui { -class PathsDialog; +class AddOnsPage; } class CatalogListModel; -class PathsDialog : public QDialog +class AddOnsPage : public QWidget { Q_OBJECT public: - explicit PathsDialog(QWidget *parent, simgear::pkg::RootRef root); - ~PathsDialog(); + explicit AddOnsPage(QWidget *parent, simgear::pkg::RootRef root); + ~AddOnsPage(); -protected: - virtual void accept(); +signals: + void downloadDirChanged(); + void sceneryPathsChanged(); private slots: void onAddSceneryPath(); @@ -36,10 +37,16 @@ private slots: void onChangeDownloadDir(); void onClearDownloadDir(); + + void onChangeDataDir(); private: void updateUi(); + void setDownloadDir(); + + void saveAircraftPaths(); + void saveSceneryPaths(); - Ui::PathsDialog* m_ui; + Ui::AddOnsPage* m_ui; CatalogListModel* m_catalogsModel; simgear::pkg::RootRef m_packageRoot; QString m_downloadDir; diff --git a/src/GUI/PathsDialog.ui b/src/GUI/PathsDialog.ui index 6b02983d6..723025976 100644 --- a/src/GUI/PathsDialog.ui +++ b/src/GUI/PathsDialog.ui @@ -1,13 +1,13 @@ - PathsDialog - + AddOnsPage + 0 0 - 600 - 600 + 608 + 617 @@ -15,53 +15,97 @@ - + TextLabel - + Qt::Horizontal + + QSizePolicy::Fixed + - 40 + 16 20 - + - Change... + FlightGear needs certain files (sometimes called 'fg-data') to function - these are included as part of stable releases. + + + true - + - Use default location + Change... - + - Aircraft hangars and automatic scenery downloads may cause this location to contain large numbers of files. Changing this location will cause files to be downloaded again. - - - true + TextLabel + + + + + + Qt::Horizontal + + + + 16 + 20 + + + + + + + + Aircraft hangars and automatic scenery downloads may cause this location to contain large numbers of files. Changing this location will cause files to be downloaded again. + + + true + + + + + + + Change... + + + + + + + Use default + + + + + @@ -317,51 +361,8 @@ - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - - buttonBox - accepted() - PathsDialog - accept() - - - 248 - 254 - - - 157 - 274 - - - - - buttonBox - rejected() - PathsDialog - reject() - - - 316 - 260 - - - 286 - 274 - - - - + diff --git a/src/GUI/QtLauncher.cxx b/src/GUI/QtLauncher.cxx index 0d32aa9f0..ae2a95c0a 100644 --- a/src/GUI/QtLauncher.cxx +++ b/src/GUI/QtLauncher.cxx @@ -360,14 +360,8 @@ void initApp(int& argc, char** argv) Qt::KeyboardModifiers mods = app->queryKeyboardModifiers(); if (mods & (Qt::AltModifier | Qt::ShiftModifier)) { qWarning() << "Alt/shift pressed during launch"; - - // wipe out our settings QSettings settings; - settings.clear(); - settings.setValue("fg-root", "!ask"); - - Options::sharedInstance()->addOption("restore-defaults", ""); } } } @@ -504,8 +498,6 @@ QtLauncher::QtLauncher() : connect(m_ui->runButton, SIGNAL(clicked()), this, SLOT(onRun())); connect(m_ui->quitButton, SIGNAL(clicked()), this, SLOT(onQuit())); - connect(m_ui->changeRootButton, SIGNAL(clicked()), this, SLOT(onChangeRoot())); - connect(m_ui->aircraftHistory, &QPushButton::clicked, this, &QtLauncher::onPopupAircraftHistory); @@ -574,9 +566,17 @@ QtLauncher::QtLauncher() : this, &QtLauncher::onAircraftInstallFailed); connect(m_aircraftModel, &AircraftItemModel::scanCompleted, this, &QtLauncher::updateSelectedAircraft); - connect(m_ui->pathsButton, &QPushButton::clicked, - this, &QtLauncher::onEditPaths); + connect(m_ui->restoreDefaultsButton, &QPushButton::clicked, + this, &QtLauncher::onRestoreDefaults); + + AddOnsPage* addOnsPage = new AddOnsPage(NULL, globals->packageRoot()); + connect(addOnsPage, &AddOnsPage::downloadDirChanged, + this, &QtLauncher::onDownloadDirChanged); + connect(addOnsPage, &AddOnsPage::sceneryPathsChanged, + this, &QtLauncher::setSceneryPaths); + + m_ui->tabWidget->addTab(addOnsPage, tr("Add-ons")); // after any kind of reset, try to restore selection and scroll // to match the m_selectedAircraft. This needs to be delayed // fractionally otherwise the scrollTo seems to be ignored, @@ -842,6 +842,11 @@ void QtLauncher::onRun() } } + if (settings.contains("restore-defaults-on-run")) { + settings.remove("restore-defaults-on-run"); + opt->addOption("restore-defaults", ""); + } + saveSettings(); qApp->exit(0); @@ -905,12 +910,9 @@ void QtLauncher::onToggleTerrasync(bool enabled) if (!info.exists()) { QMessageBox msg; msg.setWindowTitle(tr("Create download folder?")); - msg.setText(tr("The download folder '%1' does not exist, create it now? " - "Click 'change location' to choose another folder " - "to store downloaded files").arg(downloadDir)); + msg.setText(tr("The download folder '%1' does not exist, create it now?").arg(downloadDir)); msg.addButton(QMessageBox::Yes); msg.addButton(QMessageBox::Cancel); - msg.addButton(tr("Change location"), QMessageBox::ActionRole); int result = msg.exec(); if (result == QMessageBox::Cancel) { @@ -918,11 +920,6 @@ void QtLauncher::onToggleTerrasync(bool enabled) return; } - if (result == QMessageBox::ActionRole) { - onEditPaths(); - return; - } - QDir d(downloadDir); d.mkpath(downloadDir); } @@ -989,6 +986,30 @@ void QtLauncher::onCancelDownload(const QModelIndex& index) i->cancelDownload(); } +void QtLauncher::onRestoreDefaults() +{ + QMessageBox mbox(this); + mbox.setText(tr("Restore all settings to defaults?")); + mbox.setInformativeText(tr("Restoring settings to their defaults may affect available add-ons such as scenery or aircraft.")); + QPushButton* quitButton = mbox.addButton(tr("Restore and restart now"), QMessageBox::YesRole); + mbox.addButton(QMessageBox::Cancel); + mbox.setDefaultButton(QMessageBox::Cancel); + mbox.setIconPixmap(QPixmap(":/app-icon-large")); + + mbox.exec(); + if (mbox.clickedButton() != quitButton) { + return; + } + + { + QSettings settings; + settings.clear(); + settings.setValue("restore-defaults-on-run", true); + } + + restartTheApp(QStringList()); +} + void QtLauncher::maybeUpdateSelectedAircraft(QModelIndex index) { QUrl u = index.data(AircraftURIRole).toUrl(); @@ -1136,103 +1157,58 @@ void QtLauncher::onSubsytemIdleTimeout() globals->get_subsystem_mgr()->update(0.0); } -void QtLauncher::onEditPaths() +void QtLauncher::onDownloadDirChanged() { - QSettings settings; - QString previousDownloadDir = settings.value("download-dir").toString(); + // replace existing package root + globals->get_subsystem()->shutdown(); + globals->setPackageRoot(simgear::pkg::RootRef()); - PathsDialog dlg(this, globals->packageRoot()); - dlg.exec(); - if (dlg.result() == QDialog::Accepted) { - QString dd = settings.value("download-dir").toString(); - bool downloadDirChanged = (previousDownloadDir != dd); - if (downloadDirChanged) { - qDebug() << "download dir changed, resetting package root"; - - if (dd.isEmpty()) { - flightgear::Options::sharedInstance()->clearOption("download-dir"); - } else { - flightgear::Options::sharedInstance()->setOption("download-dir", dd.toStdString()); - } - - // replace existing package root - globals->get_subsystem()->shutdown(); - globals->setPackageRoot(simgear::pkg::RootRef()); - - // create new root with updated download-dir value - fgInitPackageRoot(); + // create new root with updated download-dir value + fgInitPackageRoot(); - globals->get_subsystem()->init(); - } + globals->get_subsystem()->init(); - // re-scan the aircraft list - m_aircraftModel->setPackageRoot(globals->packageRoot()); - m_aircraftModel->setPaths(settings.value("aircraft-paths").toStringList()); - m_aircraftModel->scanDirs(); + // re-scan the aircraft list + QSettings settings; + m_aircraftModel->setPackageRoot(globals->packageRoot()); + m_aircraftModel->setPaths(settings.value("aircraft-paths").toStringList()); + m_aircraftModel->scanDirs(); - // re-set scenery dirs - setSceneryPaths(); - } + // re-set scenery dirs + setSceneryPaths(); } -void QtLauncher::onChangeRoot() +simgear::pkg::PackageRef QtLauncher::packageForAircraftURI(QUrl uri) const { - QMessageBox mbox(this); - mbox.setText(tr("Change the data files location used by FlightGear?")); - mbox.setInformativeText(tr("FlightGear cannot work without its data files. " - "(Also called the base package) " - "To change which files are used, quit FlightGear and open it again, " - "while holding down the 'shift' key, and you will be able to choose a " - "different data files location, or restore the default setting.")); - QPushButton* quitButton = mbox.addButton(tr("Quit FlightGear now"), QMessageBox::YesRole); - mbox.addButton(QMessageBox::Cancel); - mbox.setDefaultButton(QMessageBox::Cancel); - mbox.setIconPixmap(QPixmap(":/app-icon-large")); - - mbox.exec(); - if (mbox.clickedButton() != quitButton) { - return; + if (uri.scheme() != "package") { + qWarning() << "invalid URL scheme:" << uri; + return simgear::pkg::PackageRef(); } - // following code doesn't work reliably, so we take the simpler - // option of asking the user to re-launch us while holding down - // the hot-key (shift) -#if 0 - { - QSettings settings; - // set the option to the magic marker value - settings.setValue("fg-root", "!ask"); - } // scope the ensure settings are written nicel + QString ident = uri.path(); + return globals->packageRoot()->getPackageById(ident.toStdString()); +} +void QtLauncher::restartTheApp(QStringList fgArgs) +{ // Spawn a new instance of myApplication: QProcess proc; -#if defined(Q_OS_MAC) QStringList args; +#if defined(Q_OS_MAC) QDir dir(qApp->applicationDirPath()); // returns the 'MacOS' dir dir.cdUp(); // up to 'contents' dir dir.cdUp(); // up to .app dir - args << dir.absolutePath(); + // see 'man open' for details, but '-n' ensures we launch a new instance, + // and we want to pass remaining arguments to us, not open. + args << "-n" << dir.absolutePath() << "--args" << "--launcher" << fgArgs; + qDebug() << "args" << args; proc.startDetached("open", args); #else - proc.startDetached(qApp->applicationFilePath()); + args << "--launcher" << fgArgs; + proc.startDetached(qApp->applicationFilePath(), args); #endif -#endif - - qDebug() << "doing app exit"; qApp->exit(-1); } -simgear::pkg::PackageRef QtLauncher::packageForAircraftURI(QUrl uri) const -{ - if (uri.scheme() != "package") { - qWarning() << "invalid URL scheme:" << uri; - return simgear::pkg::PackageRef(); - } - - QString ident = uri.path(); - return globals->packageRoot()->getPackageById(ident.toStdString()); -} - - #include "QtLauncher.moc" diff --git a/src/GUI/QtLauncher_private.hxx b/src/GUI/QtLauncher_private.hxx index c2a06b639..58dc053fe 100644 --- a/src/GUI/QtLauncher_private.hxx +++ b/src/GUI/QtLauncher_private.hxx @@ -53,6 +53,7 @@ public: void setSceneryPaths(); + static void restartTheApp(QStringList fgArgs); protected: virtual void closeEvent(QCloseEvent *event); @@ -85,9 +86,6 @@ private slots: void onSubsytemIdleTimeout(); - void onEditPaths(); - void onChangeRoot(); - void onAircraftInstalledCompleted(QModelIndex index); void onAircraftInstallFailed(QModelIndex index, QString errorMessage); @@ -95,6 +93,9 @@ private slots: void maybeRestoreAircraftSelection(); + void onRestoreDefaults(); + + void onDownloadDirChanged(); private: /** diff --git a/src/GUI/SetupRootDialog.ui b/src/GUI/SetupRootDialog.ui index 9e8465a56..71a31a916 100644 --- a/src/GUI/SetupRootDialog.ui +++ b/src/GUI/SetupRootDialog.ui @@ -116,7 +116,7 @@ - To download a compressed archive of the files, click the 'Download' button. Once the download is complete, extract the files to a suitabe location and choose the folder using the button above. + To download a compressed archive of the files, click the 'Download' button. Once the download is complete, extract the files to a suitable location and choose the folder using the button above. true -- 2.39.5