From b0ee3f98f36ce232ef7a9c3f8cc8641a7b98e604 Mon Sep 17 00:00:00 2001
From: James Turner <zakalawe@mac.com>
Date: Sun, 10 Jan 2016 12:56:46 -0600
Subject: [PATCH] =?utf8?q?=E2=80=98only=20show=20installed=20checkbox?=
 =?utf8?q?=E2=80=99=20for=20Qt=20launcher?=
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

- will be replaced when I think of a prettier UI, but this
  feature is important to have for now.
---
 src/GUI/Launcher.ui            | 20 ++++++++++++++++
 src/GUI/QtLauncher.cxx         | 44 +++++++++++++++++++++++++++++++++-
 src/GUI/QtLauncher_private.hxx |  2 ++
 3 files changed, 65 insertions(+), 1 deletion(-)

diff --git a/src/GUI/Launcher.ui b/src/GUI/Launcher.ui
index 9e696ca28..b10749fd2 100644
--- a/src/GUI/Launcher.ui
+++ b/src/GUI/Launcher.ui
@@ -186,6 +186,26 @@
        </item>
        <item>
         <layout class="QHBoxLayout" name="horizontalLayout_5">
+         <item>
+          <widget class="QCheckBox" name="onlyShowInstalledCheck">
+           <property name="text">
+            <string>Only show installed aircraft</string>
+           </property>
+          </widget>
+         </item>
+         <item>
+          <spacer name="horizontalSpacer_3">
+           <property name="orientation">
+            <enum>Qt::Horizontal</enum>
+           </property>
+           <property name="sizeHint" stdset="0">
+            <size>
+             <width>40</width>
+             <height>20</height>
+            </size>
+           </property>
+          </spacer>
+         </item>
          <item>
           <widget class="QCheckBox" name="ratingsFilterCheck">
            <property name="sizePolicy">
diff --git a/src/GUI/QtLauncher.cxx b/src/GUI/QtLauncher.cxx
index 53bae459e..d99d25023 100644
--- a/src/GUI/QtLauncher.cxx
+++ b/src/GUI/QtLauncher.cxx
@@ -252,7 +252,8 @@ class AircraftProxyModel : public QSortFilterProxyModel
 public:
     AircraftProxyModel(QObject* pr) :
         QSortFilterProxyModel(pr),
-        m_ratingsFilter(true)
+        m_ratingsFilter(true),
+        m_onlyShowInstalled(false)
     {
         for (int i=0; i<4; ++i) {
             m_ratings[i] = 3;
@@ -276,6 +277,16 @@ public slots:
         invalidate();
     }
 
+    void setInstalledFilterEnabled(bool e)
+    {
+        if (e == m_onlyShowInstalled) {
+            return;
+        }
+
+        m_onlyShowInstalled = e;
+        invalidate();
+    }
+
 protected:
     bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
     {
@@ -283,6 +294,15 @@ protected:
             return false;
         }
 
+        if (m_onlyShowInstalled) {
+            QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
+            QVariant v = index.data(AircraftPackageStatusRole);
+            AircraftItemStatus status = static_cast<AircraftItemStatus>(v.toInt());
+            if (status == PackageNotInstalled) {
+                return false;
+            }
+        }
+
         if (m_ratingsFilter) {
             QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
             for (int i=0; i<4; ++i) {
@@ -297,6 +317,7 @@ protected:
 
 private:
     bool m_ratingsFilter;
+    bool m_onlyShowInstalled;
     int m_ratings[4];
 };
 
@@ -454,6 +475,8 @@ QtLauncher::QtLauncher() :
     m_aircraftProxy = new AircraftProxyModel(this);
     connect(m_ui->ratingsFilterCheck, &QAbstractButton::toggled,
             m_aircraftProxy, &AircraftProxyModel::setRatingFilterEnabled);
+    connect(m_ui->onlyShowInstalledCheck, &QAbstractButton::toggled,
+            m_aircraftProxy, &AircraftProxyModel::setInstalledFilterEnabled);
     connect(m_ui->aircraftFilter, &QLineEdit::textChanged,
             m_aircraftProxy, &QSortFilterProxyModel::setFilterFixedString);
 
@@ -473,6 +496,8 @@ QtLauncher::QtLauncher() :
 
     connect(m_ui->editRatingFilter, &QPushButton::clicked,
             this, &QtLauncher::onEditRatingsFilter);
+    connect(m_ui->onlyShowInstalledCheck, &QCheckBox::toggled,
+            this, &QtLauncher::onShowInstalledAircraftToggled);
 
     QIcon historyIcon(":/history-icon");
     m_ui->aircraftHistory->setIcon(historyIcon);
@@ -577,6 +602,11 @@ void QtLauncher::restoreSettings()
     m_ui->location->restoreSettings();
 
     // rating filters
+    m_ui->onlyShowInstalledCheck->setChecked(settings.value("only-show-installed", false).toBool());
+    if (m_ui->onlyShowInstalledCheck->isChecked()) {
+        m_ui->ratingsFilterCheck->setEnabled(false);
+    }
+
     m_ui->ratingsFilterCheck->setChecked(settings.value("ratings-filter", true).toBool());
     int index = 0;
     Q_FOREACH(QVariant v, settings.value("min-ratings").toList()) {
@@ -599,6 +629,7 @@ void QtLauncher::saveSettings()
     settings.setValue("enable-realwx", m_ui->fetchRealWxrCheckbox->isChecked());
     settings.setValue("start-paused", m_ui->startPausedCheck->isChecked());
     settings.setValue("ratings-filter", m_ui->ratingsFilterCheck->isChecked());
+    settings.setValue("only-show-installed", m_ui->onlyShowInstalledCheck->isChecked());
     settings.setValue("recent-aircraft", QUrl::toStringList(m_recentAircraft));
 
     settings.setValue("timeofday", m_ui->timeOfDayCombo->currentIndex());
@@ -995,6 +1026,17 @@ void QtLauncher::onRembrandtToggled(bool b)
     m_ui->msaaCheckbox->setEnabled(!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());
+    }
+}
+
 void QtLauncher::onSubsytemIdleTimeout()
 {
     globals->get_subsystem_mgr()->update(0.0);
diff --git a/src/GUI/QtLauncher_private.hxx b/src/GUI/QtLauncher_private.hxx
index 64b38014a..feeb9d47b 100644
--- a/src/GUI/QtLauncher_private.hxx
+++ b/src/GUI/QtLauncher_private.hxx
@@ -83,6 +83,8 @@ private slots:
 
     void onAircraftInstalledCompleted(QModelIndex index);
     void onAircraftInstallFailed(QModelIndex index, QString errorMessage);
+
+    void onShowInstalledAircraftToggled(bool b);
 private:
 
     /**
-- 
2.39.5