1 #include "PathsDialog.hxx"
2 #include "ui_PathsDialog.h"
8 #include "CatalogListModel.hxx"
9 #include "AddCatalogDialog.hxx"
11 #include <Main/options.hxx>
12 #include <Main/globals.hxx>
13 #include <Network/HTTPClient.hxx>
15 PathsDialog::PathsDialog(QWidget *parent, simgear::pkg::RootRef root) :
17 m_ui(new Ui::PathsDialog),
22 m_catalogsModel = new CatalogListModel(this, m_packageRoot);
23 m_ui->catalogsList->setModel(m_catalogsModel);
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);
30 m_ui->aircraftPathsList->setDragEnabled(true);
31 m_ui->aircraftPathsList->setDragDropMode(QAbstractItemView::InternalMove);
32 m_ui->aircraftPathsList->setDropIndicatorShown(true);
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);
41 connect(m_ui->addSceneryPath, &QToolButton::clicked,
42 this, &PathsDialog::onAddSceneryPath);
43 connect(m_ui->removeSceneryPath, &QToolButton::clicked,
44 this, &PathsDialog::onRemoveSceneryPath);
46 connect(m_ui->addAircraftPath, &QToolButton::clicked,
47 this, &PathsDialog::onAddAircraftPath);
48 connect(m_ui->removeAircraftPath, &QToolButton::clicked,
49 this, &PathsDialog::onRemoveAircraftPath);
51 connect(m_ui->changeDownloadDir, &QPushButton::clicked,
52 this, &PathsDialog::onChangeDownloadDir);
54 connect(m_ui->clearDownloadDir, &QPushButton::clicked,
55 this, &PathsDialog::onClearDownloadDir);
59 QStringList sceneryPaths = settings.value("scenery-paths").toStringList();
60 m_ui->sceneryPathsList->addItems(sceneryPaths);
62 QStringList aircraftPaths = settings.value("aircraft-paths").toStringList();
63 m_ui->aircraftPathsList->addItems(aircraftPaths);
65 QVariant downloadDir = settings.value("download-dir");
66 if (downloadDir.isValid()) {
67 m_downloadDir = downloadDir.toString();
73 PathsDialog::~PathsDialog()
78 void PathsDialog::accept()
82 for (int i=0; i<m_ui->sceneryPathsList->count(); ++i) {
83 paths.append(m_ui->sceneryPathsList->item(i)->text());
86 settings.setValue("scenery-paths", paths);
89 for (int i=0; i<m_ui->aircraftPathsList->count(); ++i) {
90 paths.append(m_ui->aircraftPathsList->item(i)->text());
93 settings.setValue("aircraft-paths", paths);
95 if (m_downloadDir.isEmpty()) {
96 settings.remove("download-dir");
98 settings.setValue("download-dir", m_downloadDir);
104 void PathsDialog::onAddSceneryPath()
106 QString path = QFileDialog::getExistingDirectory(this, tr("Choose scenery folder"));
107 if (!path.isEmpty()) {
108 m_ui->sceneryPathsList->addItem(path);
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)
117 void PathsDialog::onRemoveSceneryPath()
119 if (m_ui->sceneryPathsList->currentItem()) {
120 delete m_ui->sceneryPathsList->currentItem();
124 void PathsDialog::onAddAircraftPath()
126 QString path = QFileDialog::getExistingDirectory(this, tr("Choose aircraft folder"));
127 if (!path.isEmpty()) {
128 m_ui->aircraftPathsList->addItem(path);
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)
136 void PathsDialog::onRemoveAircraftPath()
138 if (m_ui->aircraftPathsList->currentItem()) {
139 delete m_ui->aircraftPathsList->currentItem();
143 void PathsDialog::onAddCatalog()
145 QScopedPointer<AddCatalogDialog> dlg(new AddCatalogDialog(this, m_packageRoot));
147 if (dlg->result() == QDialog::Accepted) {
148 m_catalogsModel->refresh();
152 void PathsDialog::onAddDefaultCatalog()
154 // check it's not a duplicate somehow
155 FGHTTPClient* http = static_cast<FGHTTPClient*>(globals->get_subsystem("http"));
156 if (http->isDefaultCatalogInstalled())
159 QScopedPointer<AddCatalogDialog> dlg(new AddCatalogDialog(this, m_packageRoot));
160 QUrl url(QString::fromStdString(http->getDefaultCatalogUrl()));
161 dlg->setUrlAndDownload(url);
163 if (dlg->result() == QDialog::Accepted) {
164 m_catalogsModel->refresh();
169 void PathsDialog::onRemoveCatalog()
171 QModelIndex mi = m_ui->catalogsList->currentIndex();
172 FGHTTPClient* http = static_cast<FGHTTPClient*>(globals->get_subsystem("http"));
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();
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.");
184 s = s.arg(mi.data(Qt::DisplayRole).toString());
189 mb.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
190 mb.setDefaultButton(QMessageBox::No);
193 if (mb.result() == QMessageBox::Yes) {
194 m_packageRoot->removeCatalogById(pkgId.toStdString());
201 void PathsDialog::onChangeDownloadDir()
203 QString path = QFileDialog::getExistingDirectory(this,
204 tr("Choose downloads folder"),
206 if (!path.isEmpty()) {
207 m_downloadDir = path;
212 void PathsDialog::onClearDownloadDir()
214 // does this need an 'are you sure'?
215 m_downloadDir.clear();
219 void PathsDialog::updateUi()
221 QString s = m_downloadDir;
223 s = QString::fromStdString(flightgear::defaultDownloadDir());
224 s.append(tr(" (default)"));
225 m_ui->clearDownloadDir->setEnabled(false);
227 m_ui->clearDownloadDir->setEnabled(true);
230 QString m = tr("Download location: %1").arg(s);
231 m_ui->downloadLocation->setText(m);
233 FGHTTPClient* http = static_cast<FGHTTPClient*>(globals->get_subsystem("http"));
234 m_ui->addDefaultCatalogButton->setEnabled(!http->isDefaultCatalogInstalled());