]> git.mxchange.org Git - flightgear.git/blob - src/GUI/PathsDialog.cxx
Launcher: Maintain aircraft selection better
[flightgear.git] / src / GUI / PathsDialog.cxx
1 #include "PathsDialog.hxx"
2 #include "ui_PathsDialog.h"
3
4 #include <QSettings>
5 #include <QFileDialog>
6 #include <QMessageBox>
7
8 #include "CatalogListModel.hxx"
9 #include "AddCatalogDialog.hxx"
10
11 #include <Main/options.hxx>
12 #include <Main/globals.hxx>
13 #include <Network/HTTPClient.hxx>
14
15 PathsDialog::PathsDialog(QWidget *parent, simgear::pkg::RootRef root) :
16     QDialog(parent),
17     m_ui(new Ui::PathsDialog),
18     m_packageRoot(root)
19 {
20     m_ui->setupUi(this);
21     
22     m_catalogsModel = new CatalogListModel(this, m_packageRoot);
23     m_ui->catalogsList->setModel(m_catalogsModel);
24
25  // enable drag-drop to re-order the paths
26     m_ui->sceneryPathsList->setDragEnabled(true);
27     m_ui->sceneryPathsList->setDragDropMode(QAbstractItemView::InternalMove);
28     m_ui->sceneryPathsList->setDropIndicatorShown(true);
29
30     m_ui->aircraftPathsList->setDragEnabled(true);
31     m_ui->aircraftPathsList->setDragDropMode(QAbstractItemView::InternalMove);
32     m_ui->aircraftPathsList->setDropIndicatorShown(true);
33
34     connect(m_ui->addCatalog, &QToolButton::clicked,
35             this, &PathsDialog::onAddCatalog);
36     connect(m_ui->addDefaultCatalogButton, &QPushButton::clicked,
37             this, &PathsDialog::onAddDefaultCatalog);
38     connect(m_ui->removeCatalog, &QToolButton::clicked,
39             this, &PathsDialog::onRemoveCatalog);
40             
41     connect(m_ui->addSceneryPath, &QToolButton::clicked,
42             this, &PathsDialog::onAddSceneryPath);
43     connect(m_ui->removeSceneryPath, &QToolButton::clicked,
44             this, &PathsDialog::onRemoveSceneryPath);
45
46     connect(m_ui->addAircraftPath, &QToolButton::clicked,
47             this, &PathsDialog::onAddAircraftPath);
48     connect(m_ui->removeAircraftPath, &QToolButton::clicked,
49             this, &PathsDialog::onRemoveAircraftPath);
50
51     connect(m_ui->changeDownloadDir, &QPushButton::clicked,
52             this, &PathsDialog::onChangeDownloadDir);
53
54     connect(m_ui->clearDownloadDir, &QPushButton::clicked,
55             this, &PathsDialog::onClearDownloadDir);
56
57     QSettings settings;
58             
59     QStringList sceneryPaths = settings.value("scenery-paths").toStringList();
60     m_ui->sceneryPathsList->addItems(sceneryPaths);
61
62     QStringList aircraftPaths = settings.value("aircraft-paths").toStringList();
63     m_ui->aircraftPathsList->addItems(aircraftPaths);
64
65     QVariant downloadDir = settings.value("download-dir");
66     if (downloadDir.isValid()) {
67         m_downloadDir = downloadDir.toString();
68     }
69
70     updateUi();
71 }
72
73 PathsDialog::~PathsDialog()
74 {
75     delete m_ui;
76 }
77
78 void PathsDialog::accept()
79 {
80     QSettings settings;
81     QStringList paths;
82     for (int i=0; i<m_ui->sceneryPathsList->count(); ++i) {
83         paths.append(m_ui->sceneryPathsList->item(i)->text());
84     }
85
86     settings.setValue("scenery-paths", paths);
87     paths.clear();
88
89     for (int i=0; i<m_ui->aircraftPathsList->count(); ++i) {
90         paths.append(m_ui->aircraftPathsList->item(i)->text());
91     }
92
93     settings.setValue("aircraft-paths", paths);
94
95     if (m_downloadDir.isEmpty()) {
96         settings.remove("download-dir");
97     } else {
98         settings.setValue("download-dir", m_downloadDir);
99     }
100     
101     QDialog::accept();
102 }
103
104 void PathsDialog::onAddSceneryPath()
105 {
106     QString path = QFileDialog::getExistingDirectory(this, tr("Choose scenery folder"));
107     if (!path.isEmpty()) {
108         m_ui->sceneryPathsList->addItem(path);
109     }
110
111     // work around a Qt OS-X bug - this dialog is ending ordered
112     // behind the main settings dialog (consequence of modal-dialog
113     // showing a modla dialog showing a modial dialog)
114     window()->raise();
115 }
116
117 void PathsDialog::onRemoveSceneryPath()
118 {
119     if (m_ui->sceneryPathsList->currentItem()) {
120         delete m_ui->sceneryPathsList->currentItem();
121     }
122 }
123
124 void PathsDialog::onAddAircraftPath()
125 {
126     QString path = QFileDialog::getExistingDirectory(this, tr("Choose aircraft folder"));
127     if (!path.isEmpty()) {
128         m_ui->aircraftPathsList->addItem(path);
129     }
130     // work around a Qt OS-X bug - this dialog is ending ordered
131     // behind the main settings dialog (consequence of modal-dialog
132     // showing a modla dialog showing a modial dialog)
133     window()->raise();
134 }
135
136 void PathsDialog::onRemoveAircraftPath()
137 {
138     if (m_ui->aircraftPathsList->currentItem()) {
139         delete m_ui->aircraftPathsList->currentItem();
140     }
141 }
142
143 void PathsDialog::onAddCatalog()
144 {
145     QScopedPointer<AddCatalogDialog> dlg(new AddCatalogDialog(this, m_packageRoot));
146     dlg->exec();
147     if (dlg->result() == QDialog::Accepted) {
148         m_catalogsModel->refresh();
149     }
150 }
151
152 void PathsDialog::onAddDefaultCatalog()
153 {
154     // check it's not a duplicate somehow
155     FGHTTPClient* http = static_cast<FGHTTPClient*>(globals->get_subsystem("http"));
156     if (http->isDefaultCatalogInstalled())
157         return;
158
159      QScopedPointer<AddCatalogDialog> dlg(new AddCatalogDialog(this, m_packageRoot));
160      QUrl url(QString::fromStdString(http->getDefaultCatalogUrl()));
161      dlg->setUrlAndDownload(url);
162      dlg->exec();
163      if (dlg->result() == QDialog::Accepted) {
164          m_catalogsModel->refresh();
165          updateUi();
166      }
167 }
168
169 void PathsDialog::onRemoveCatalog()
170 {
171     QModelIndex mi = m_ui->catalogsList->currentIndex();
172     FGHTTPClient* http = static_cast<FGHTTPClient*>(globals->get_subsystem("http"));
173
174     if (mi.isValid()) {
175         QString s = QString("Remove aircraft hangar '%1'? All installed aircraft from this "
176                             "hangar will be removed.");
177         QString pkgId = mi.data(CatalogIdRole).toString();
178
179         if (pkgId.toStdString() == http->getDefaultCatalogId()) {
180             s = QString("Remove default aircraft hangar? "
181                         "This hangar contains all the default aircraft included with FlightGear. "
182                         "If you change your mind in the future, click the 'restore' button.");
183         } else {
184             s = s.arg(mi.data(Qt::DisplayRole).toString());
185         }
186
187         QMessageBox mb;
188         mb.setText(s);
189         mb.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
190         mb.setDefaultButton(QMessageBox::No);
191         mb.exec();
192         
193         if (mb.result() == QMessageBox::Yes) {
194             m_packageRoot->removeCatalogById(pkgId.toStdString());
195         }
196     }
197
198     updateUi();
199 }
200
201 void PathsDialog::onChangeDownloadDir()
202 {
203     QString path = QFileDialog::getExistingDirectory(this,
204                                                      tr("Choose downloads folder"),
205                                                      m_downloadDir);
206     if (!path.isEmpty()) {
207         m_downloadDir = path;
208         updateUi();
209     }
210 }
211
212 void PathsDialog::onClearDownloadDir()
213 {
214     // does this need an 'are you sure'?
215     m_downloadDir.clear();
216     updateUi();
217 }
218
219 void PathsDialog::updateUi()
220 {
221     QString s = m_downloadDir;
222     if (s.isEmpty()) {
223         s = QString::fromStdString(flightgear::defaultDownloadDir());
224         s.append(tr(" (default)"));
225         m_ui->clearDownloadDir->setEnabled(false);
226     } else {
227         m_ui->clearDownloadDir->setEnabled(true);
228     }
229
230     QString m = tr("Download location: %1").arg(s);
231     m_ui->downloadLocation->setText(m);
232
233     FGHTTPClient* http = static_cast<FGHTTPClient*>(globals->get_subsystem("http"));
234     m_ui->addDefaultCatalogButton->setEnabled(!http->isDefaultCatalogInstalled());
235 }