From 4befe0e6ea6b5f21119364f1175a0b6c8e97395c Mon Sep 17 00:00:00 2001 From: James Turner Date: Sun, 10 Jan 2016 13:47:57 -0600 Subject: [PATCH] Launcher: Maintain aircraft selection better - when launching or toggling settings, try to keep the current aircraft selected and in-view. --- src/GUI/QtLauncher.cxx | 53 +++++++++++++++++++++++++++++----- src/GUI/QtLauncher_private.hxx | 6 ++++ 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/GUI/QtLauncher.cxx b/src/GUI/QtLauncher.cxx index d99d25023..c66ef3c3e 100644 --- a/src/GUI/QtLauncher.cxx +++ b/src/GUI/QtLauncher.cxx @@ -303,7 +303,7 @@ protected: } } - if (m_ratingsFilter) { + if (!m_onlyShowInstalled && m_ratingsFilter) { QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent); for (int i=0; i<4; ++i) { if (m_ratings[i] > index.data(AircraftRatingRole + i).toInt()) { @@ -475,6 +475,9 @@ QtLauncher::QtLauncher() : m_aircraftProxy = new AircraftProxyModel(this); connect(m_ui->ratingsFilterCheck, &QAbstractButton::toggled, m_aircraftProxy, &AircraftProxyModel::setRatingFilterEnabled); + connect(m_ui->ratingsFilterCheck, &QAbstractButton::toggled, + this, &QtLauncher::maybeRestoreAircraftSelection); + connect(m_ui->onlyShowInstalledCheck, &QAbstractButton::toggled, m_aircraftProxy, &AircraftProxyModel::setInstalledFilterEnabled); connect(m_ui->aircraftFilter, &QLineEdit::textChanged, @@ -554,6 +557,13 @@ QtLauncher::QtLauncher() : connect(m_ui->pathsButton, &QPushButton::clicked, this, &QtLauncher::onEditPaths); + // 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, + // unfortunately. + connect(m_aircraftProxy, &AircraftProxyModel::modelReset, + this, &QtLauncher::delayedAircraftModelReset); + restoreSettings(); QSettings settings; @@ -616,9 +626,30 @@ void QtLauncher::restoreSettings() m_aircraftProxy->setRatingFilterEnabled(m_ui->ratingsFilterCheck->isChecked()); m_aircraftProxy->setRatings(m_ratingFilters); + updateSelectedAircraft(); + maybeRestoreAircraftSelection(); + m_ui->commandLineArgs->setPlainText(settings.value("additional-args").toString()); } +void QtLauncher::delayedAircraftModelReset() +{ + QTimer::singleShot(1, this, &QtLauncher::maybeRestoreAircraftSelection); +} + +void QtLauncher::maybeRestoreAircraftSelection() +{ + QModelIndex aircraftIndex = m_aircraftModel->indexOfAircraftURI(m_selectedAircraft); + QModelIndex proxyIndex = m_aircraftProxy->mapFromSource(aircraftIndex); + if (proxyIndex.isValid()) { + m_ui->aircraftList->selectionModel()->setCurrentIndex(proxyIndex, + QItemSelectionModel::ClearAndSelect); + m_ui->aircraftList->selectionModel()->select(proxyIndex, + QItemSelectionModel::ClearAndSelect); + m_ui->aircraftList->scrollTo(proxyIndex); + } +} + void QtLauncher::saveSettings() { QSettings settings; @@ -848,6 +879,15 @@ void QtLauncher::onAircraftInstalledCompleted(QModelIndex index) maybeUpdateSelectedAircraft(index); } +void QtLauncher::onRatingsFilterToggled() +{ + QModelIndex aircraftIndex = m_aircraftModel->indexOfAircraftURI(m_selectedAircraft); + QModelIndex proxyIndex = m_aircraftProxy->mapFromSource(aircraftIndex); + if (proxyIndex.isValid()) { + m_ui->aircraftList->scrollTo(proxyIndex); + } +} + void QtLauncher::onAircraftInstallFailed(QModelIndex index, QString errorMessage) { qWarning() << Q_FUNC_INFO << index.data(AircraftURIRole) << errorMessage; @@ -870,6 +910,10 @@ void QtLauncher::onAircraftSelected(const QModelIndex& index) void QtLauncher::onRequestPackageInstall(const QModelIndex& index) { + // also select, otherwise UI is confusing + m_selectedAircraft = index.data(AircraftURIRole).toUrl(); + updateSelectedAircraft(); + QString pkg = index.data(AircraftPackageIdRole).toString(); simgear::pkg::PackageRef pref = globals->packageRoot()->getPackageById(pkg.toStdString()); if (pref->isInstalled()) { @@ -1029,12 +1073,7 @@ void QtLauncher::onRembrandtToggled(bool b) void QtLauncher::onShowInstalledAircraftToggled(bool b) { m_ui->ratingsFilterCheck->setEnabled(!b); - if (b) { - // don't filter installed aircraft by rating - m_aircraftProxy->setRatingFilterEnabled(false); - } else { - m_aircraftProxy->setRatingFilterEnabled(m_ui->ratingsFilterCheck->isChecked()); - } + maybeRestoreAircraftSelection(); } void QtLauncher::onSubsytemIdleTimeout() diff --git a/src/GUI/QtLauncher_private.hxx b/src/GUI/QtLauncher_private.hxx index feeb9d47b..c0a6d5c09 100644 --- a/src/GUI/QtLauncher_private.hxx +++ b/src/GUI/QtLauncher_private.hxx @@ -104,6 +104,12 @@ private: simgear::pkg::PackageRef packageForAircraftURI(QUrl uri) const; + void maybeRestoreAircraftSelection(); + // need to wait after a model reset before restoring selection and + // scrolling, to give the view time it seems. + void delayedAircraftModelReset(); + void onRatingsFilterToggled(); + QScopedPointer m_ui; AircraftProxyModel* m_aircraftProxy; AircraftItemModel* m_aircraftModel; -- 2.39.5