1 // QtLauncher.cxx - GUI launcher dialog using Qt5
3 // Written by James Turner, started December 2014.
5 // Copyright (C) 2014 James Turner <zakalawe@mac.com>
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "QtLauncher.hxx"
24 #include <QProgressDialog>
25 #include <QCoreApplication>
26 #include <QAbstractListModel>
35 #include <QSortFilterProxyModel>
37 #include <QDesktopServices>
40 #include <QFileDialog>
41 #include <QMessageBox>
43 #include <QApplication>
46 #include <simgear/timing/timestamp.hxx>
47 #include <simgear/props/props_io.hxx>
48 #include <simgear/structure/exception.hxx>
49 #include <simgear/structure/subsystem_mgr.hxx>
50 #include <simgear/misc/sg_path.hxx>
52 #include "ui_Launcher.h"
53 #include "EditRatingsFilterDialog.hxx"
54 #include "AircraftItemDelegate.hxx"
55 #include "AircraftModel.hxx"
56 #include "PathsDialog.hxx"
58 #include <Main/globals.hxx>
59 #include <Navaids/NavDataCache.hxx>
60 #include <Airports/airport.hxx>
61 #include <Airports/dynamics.hxx> // for parking
62 #include <Main/options.hxx>
63 #include <Main/fg_init.hxx>
64 #include <Viewer/WindowBuilder.hxx>
65 #include <Network/HTTPClient.hxx>
67 using namespace flightgear;
69 const int MAX_RECENT_AIRPORTS = 32;
70 const int MAX_RECENT_AIRCRAFT = 20;
72 namespace { // anonymous namespace
76 QString baseLabel = QT_TR_NOOP("Initialising navigation data, this may take several minutes");
77 NavDataCache* cache = NavDataCache::createInstance();
78 if (cache->isRebuildRequired()) {
79 QProgressDialog rebuildProgress(baseLabel,
80 QString() /* cancel text */,
82 rebuildProgress.setWindowModality(Qt::WindowModal);
83 rebuildProgress.show();
85 NavDataCache::RebuildPhase phase = cache->rebuild();
87 while (phase != NavDataCache::REBUILD_DONE) {
88 // sleep to give the rebuild thread more time
89 SGTimeStamp::sleepForMSec(50);
90 phase = cache->rebuild();
93 case NavDataCache::REBUILD_AIRPORTS:
94 rebuildProgress.setLabelText(QT_TR_NOOP("Loading airport data"));
97 case NavDataCache::REBUILD_FIXES:
98 rebuildProgress.setLabelText(QT_TR_NOOP("Loading waypoint data"));
101 case NavDataCache::REBUILD_NAVAIDS:
102 rebuildProgress.setLabelText(QT_TR_NOOP("Loading navigation data"));
106 case NavDataCache::REBUILD_POIS:
107 rebuildProgress.setLabelText(QT_TR_NOOP("Loading point-of-interest data"));
111 rebuildProgress.setLabelText(baseLabel);
114 if (phase == NavDataCache::REBUILD_UNKNOWN) {
115 rebuildProgress.setValue(0);
116 rebuildProgress.setMaximum(0);
118 rebuildProgress.setValue(cache->rebuildPhaseCompletionPercentage());
119 rebuildProgress.setMaximum(100);
122 QCoreApplication::processEvents();
127 class ArgumentsTokenizer
133 explicit Arg(QString k, QString v = QString()) : arg(k), value(v) {}
139 QList<Arg> tokenize(QString in) const
142 const int len = in.count();
148 for (; index < len; ++index) {
150 nc = index < (len - 1) ? in.at(index + 1) : QChar();
154 if (c == QChar('-')) {
155 if (nc == QChar('-')) {
160 // should we pemit single hyphen arguments?
161 // choosing to fail for now
164 } else if (c.isSpace()) {
170 if (c == QChar('=')) {
173 } else if (c.isSpace()) {
175 result.append(Arg(key));
177 // could check for illegal charatcers here
183 if (c == QChar('"')) {
185 } else if (c.isSpace()) {
187 result.append(Arg(key, value));
194 if (c == QChar('\\')) {
195 // check for escaped double-quote inside quoted value
196 if (nc == QChar('"')) {
199 } else if (c == QChar('"')) {
206 } // of character loop
208 // ensure last argument isn't lost
210 result.append(Arg(key));
211 } else if (state == Value) {
212 result.append(Arg(key, value));
227 } // of anonymous namespace
229 class AirportSearchModel : public QAbstractListModel
233 AirportSearchModel() :
234 m_searchActive(false)
238 void setSearch(QString t)
245 std::string term(t.toUpper().toStdString());
246 // try ICAO lookup first
247 FGAirportRef ref = FGAirport::findByIdent(term);
249 m_ids.push_back(ref->guid());
250 m_airports.push_back(ref);
252 m_search.reset(new NavDataCache::ThreadedAirportSearch(term));
253 QTimer::singleShot(100, this, SLOT(onSearchResultsPoll()));
254 m_searchActive = true;
260 bool isSearchActive() const
262 return m_searchActive;
265 virtual int rowCount(const QModelIndex&) const
267 // if empty, return 1 for special 'no matches'?
271 virtual QVariant data(const QModelIndex& index, int role) const
273 if (!index.isValid())
276 FGAirportRef apt = m_airports[index.row()];
278 apt = FGPositioned::loadById<FGAirport>(m_ids[index.row()]);
279 m_airports[index.row()] = apt;
282 if (role == Qt::DisplayRole) {
283 QString name = QString::fromStdString(apt->name());
284 return QString("%1: %2").arg(QString::fromStdString(apt->ident())).arg(name);
287 if (role == Qt::EditRole) {
288 return QString::fromStdString(apt->ident());
291 if (role == Qt::UserRole) {
292 return static_cast<qlonglong>(m_ids[index.row()]);
298 QString firstIdent() const
303 if (!m_airports.front().valid()) {
304 m_airports[0] = FGPositioned::loadById<FGAirport>(m_ids.front());
307 return QString::fromStdString(m_airports.front()->ident());
311 void searchComplete();
314 void onSearchResultsPoll()
316 PositionedIDVec newIds = m_search->results();
318 beginInsertRows(QModelIndex(), m_ids.size(), newIds.size() - 1);
319 for (unsigned int i=m_ids.size(); i < newIds.size(); ++i) {
320 m_ids.push_back(newIds[i]);
321 m_airports.push_back(FGAirportRef()); // null ref
325 if (m_search->isComplete()) {
326 m_searchActive = false;
328 emit searchComplete();
330 QTimer::singleShot(100, this, SLOT(onSearchResultsPoll()));
335 PositionedIDVec m_ids;
336 mutable std::vector<FGAirportRef> m_airports;
338 QScopedPointer<NavDataCache::ThreadedAirportSearch> m_search;
341 class AircraftProxyModel : public QSortFilterProxyModel
345 AircraftProxyModel(QObject* pr) :
346 QSortFilterProxyModel(pr),
347 m_ratingsFilter(true)
349 for (int i=0; i<4; ++i) {
354 void setRatings(int* ratings)
356 ::memcpy(m_ratings, ratings, sizeof(int) * 4);
361 void setRatingFilterEnabled(bool e)
363 if (e == m_ratingsFilter) {
372 bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
374 if (!QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent)) {
378 if (m_ratingsFilter) {
379 QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
380 for (int i=0; i<4; ++i) {
381 if (m_ratings[i] > index.data(AircraftRatingRole + i).toInt()) {
391 bool m_ratingsFilter;
395 QtLauncher::QtLauncher() :
399 m_ui.reset(new Ui::Launcher);
402 #if QT_VERSION >= 0x050300
403 // don't require Qt 5.3
404 m_ui->commandLineArgs->setPlaceholderText("--option=value --prop:/sim/name=value");
407 #if QT_VERSION >= 0x050200
408 m_ui->aircraftFilter->setClearButtonEnabled(true);
411 for (int i=0; i<4; ++i) {
412 m_ratingFilters[i] = 3;
415 m_subsystemIdleTimer = new QTimer(this);
416 m_subsystemIdleTimer->setInterval(0);
417 connect(m_subsystemIdleTimer, &QTimer::timeout,
418 this, &QtLauncher::onSubsytemIdleTimeout);
419 m_subsystemIdleTimer->start();
421 m_airportsModel = new AirportSearchModel;
422 m_ui->searchList->setModel(m_airportsModel);
423 connect(m_ui->searchList, &QListView::clicked,
424 this, &QtLauncher::onAirportChoiceSelected);
425 connect(m_airportsModel, &AirportSearchModel::searchComplete,
426 this, &QtLauncher::onAirportSearchComplete);
428 // create and configure the proxy model
429 m_aircraftProxy = new AircraftProxyModel(this);
430 connect(m_ui->ratingsFilterCheck, &QAbstractButton::toggled,
431 m_aircraftProxy, &AircraftProxyModel::setRatingFilterEnabled);
432 connect(m_ui->aircraftFilter, &QLineEdit::textChanged,
433 m_aircraftProxy, &QSortFilterProxyModel::setFilterFixedString);
435 connect(m_ui->runwayCombo, SIGNAL(currentIndexChanged(int)),
436 this, SLOT(updateAirportDescription()));
437 connect(m_ui->parkingCombo, SIGNAL(currentIndexChanged(int)),
438 this, SLOT(updateAirportDescription()));
439 connect(m_ui->runwayRadio, SIGNAL(toggled(bool)),
440 this, SLOT(updateAirportDescription()));
441 connect(m_ui->parkingRadio, SIGNAL(toggled(bool)),
442 this, SLOT(updateAirportDescription()));
443 connect(m_ui->onFinalCheckbox, SIGNAL(toggled(bool)),
444 this, SLOT(updateAirportDescription()));
447 connect(m_ui->airportDiagram, &AirportDiagram::clickedRunway,
448 this, &QtLauncher::onAirportDiagramClicked);
450 connect(m_ui->runButton, SIGNAL(clicked()), this, SLOT(onRun()));
451 connect(m_ui->quitButton, SIGNAL(clicked()), this, SLOT(onQuit()));
452 connect(m_ui->airportEdit, SIGNAL(returnPressed()),
453 this, SLOT(onSearchAirports()));
455 connect(m_ui->airportHistory, &QPushButton::clicked,
456 this, &QtLauncher::onPopupAirportHistory);
457 connect(m_ui->aircraftHistory, &QPushButton::clicked,
458 this, &QtLauncher::onPopupAircraftHistory);
462 QAction* qa = new QAction(this);
463 qa->setShortcut(QKeySequence("Ctrl+Q"));
464 connect(qa, &QAction::triggered, this, &QtLauncher::onQuit);
467 connect(m_ui->editRatingFilter, &QPushButton::clicked,
468 this, &QtLauncher::onEditRatingsFilter);
470 QIcon historyIcon(":/history-icon");
471 m_ui->aircraftHistory->setIcon(historyIcon);
472 m_ui->airportHistory->setIcon(historyIcon);
474 m_ui->searchIcon->setPixmap(QPixmap(":/search-icon"));
476 connect(m_ui->timeOfDayCombo, SIGNAL(currentIndexChanged(int)),
477 this, SLOT(updateSettingsSummary()));
478 connect(m_ui->seasonCombo, SIGNAL(currentIndexChanged(int)),
479 this, SLOT(updateSettingsSummary()));
480 connect(m_ui->fetchRealWxrCheckbox, SIGNAL(toggled(bool)),
481 this, SLOT(updateSettingsSummary()));
482 connect(m_ui->rembrandtCheckbox, SIGNAL(toggled(bool)),
483 this, SLOT(updateSettingsSummary()));
484 connect(m_ui->terrasyncCheck, SIGNAL(toggled(bool)),
485 this, SLOT(updateSettingsSummary()));
486 connect(m_ui->startPausedCheck, SIGNAL(toggled(bool)),
487 this, SLOT(updateSettingsSummary()));
488 connect(m_ui->msaaCheckbox, SIGNAL(toggled(bool)),
489 this, SLOT(updateSettingsSummary()));
491 connect(m_ui->rembrandtCheckbox, SIGNAL(toggled(bool)),
492 this, SLOT(onRembrandtToggled(bool)));
493 connect(m_ui->terrasyncCheck, &QCheckBox::toggled,
494 this, &QtLauncher::onToggleTerrasync);
495 updateSettingsSummary();
498 simgear::pkg::RootRef r(globals->packageRoot());
500 FGHTTPClient* http = new FGHTTPClient;
501 globals->add_subsystem("http", http);
503 // we guard against re-init in the global phase; bind and postinit
504 // will happen as normal
507 m_aircraftModel = new AircraftItemModel(this, r);
508 m_aircraftProxy->setSourceModel(m_aircraftModel);
510 m_aircraftProxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
511 m_aircraftProxy->setSortCaseSensitivity(Qt::CaseInsensitive);
512 m_aircraftProxy->setSortRole(Qt::DisplayRole);
513 m_aircraftProxy->setDynamicSortFilter(true);
515 m_ui->aircraftList->setModel(m_aircraftProxy);
516 m_ui->aircraftList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
517 AircraftItemDelegate* delegate = new AircraftItemDelegate(m_ui->aircraftList);
518 m_ui->aircraftList->setItemDelegate(delegate);
519 m_ui->aircraftList->setSelectionMode(QAbstractItemView::SingleSelection);
520 connect(m_ui->aircraftList, &QListView::clicked,
521 this, &QtLauncher::onAircraftSelected);
522 connect(delegate, &AircraftItemDelegate::variantChanged,
523 this, &QtLauncher::onAircraftSelected);
525 connect(m_ui->pathsButton, &QPushButton::clicked,
526 this, &QtLauncher::onEditPaths);
529 m_aircraftModel->setPaths(settings.value("aircraft-paths").toStringList());
530 m_aircraftModel->scanDirs();
533 QtLauncher::~QtLauncher()
538 void QtLauncher::initApp(int& argc, char** argv)
540 static bool qtInitDone = false;
544 QApplication* app = new QApplication(argc, argv);
545 app->setOrganizationName("FlightGear");
546 app->setApplicationName("FlightGear");
547 app->setOrganizationDomain("flightgear.org");
549 // avoid double Apple menu and other weirdness if both Qt and OSG
550 // try to initialise various Cocoa structures.
551 flightgear::WindowBuilder::setPoseAsStandaloneApp(false);
553 Qt::KeyboardModifiers mods = app->queryKeyboardModifiers();
554 if (mods & Qt::AltModifier) {
555 qWarning() << "Alt pressed during launch";
557 // wipe out our settings
562 Options::sharedInstance()->addOption("restore-defaults", "");
567 bool QtLauncher::runLauncherDialog()
569 Q_INIT_RESOURCE(resources);
571 // startup the nav-cache now. This pre-empts normal startup of
572 // the cache, but no harm done. (Providing scenery paths are consistent)
576 // setup scenery paths now, especially TerraSync path for airport
577 // parking locations (after they're downloaded)
581 if (dlg.result() != QDialog::Accepted) {
588 void QtLauncher::restoreSettings()
591 m_ui->rembrandtCheckbox->setChecked(settings.value("enable-rembrandt", false).toBool());
592 m_ui->terrasyncCheck->setChecked(settings.value("enable-terrasync", true).toBool());
593 m_ui->fullScreenCheckbox->setChecked(settings.value("start-fullscreen", false).toBool());
594 m_ui->msaaCheckbox->setChecked(settings.value("enable-msaa", false).toBool());
595 m_ui->fetchRealWxrCheckbox->setChecked(settings.value("enable-realwx", true).toBool());
596 m_ui->startPausedCheck->setChecked(settings.value("start-paused", false).toBool());
597 m_ui->timeOfDayCombo->setCurrentIndex(settings.value("timeofday", 0).toInt());
598 m_ui->seasonCombo->setCurrentIndex(settings.value("season", 0).toInt());
600 // full paths to -set.xml files
601 m_recentAircraft = settings.value("recent-aircraft").toStringList();
603 if (!m_recentAircraft.empty()) {
604 m_selectedAircraft = m_recentAircraft.front();
606 // select the default C172p
609 updateSelectedAircraft();
612 m_recentAirports = settings.value("recent-airports").toStringList();
613 if (!m_recentAirports.empty()) {
614 setAirport(FGAirport::findByIdent(m_recentAirports.front().toStdString()));
616 updateAirportDescription();
619 m_ui->ratingsFilterCheck->setChecked(settings.value("ratings-filter", true).toBool());
621 Q_FOREACH(QVariant v, settings.value("min-ratings").toList()) {
622 m_ratingFilters[index++] = v.toInt();
625 m_aircraftProxy->setRatingFilterEnabled(m_ui->ratingsFilterCheck->isChecked());
626 m_aircraftProxy->setRatings(m_ratingFilters);
628 m_ui->commandLineArgs->setPlainText(settings.value("additional-args").toString());
631 void QtLauncher::saveSettings()
634 settings.setValue("enable-rembrandt", m_ui->rembrandtCheckbox->isChecked());
635 settings.setValue("enable-terrasync", m_ui->terrasyncCheck->isChecked());
636 settings.setValue("enable-msaa", m_ui->msaaCheckbox->isChecked());
637 settings.setValue("start-fullscreen", m_ui->fullScreenCheckbox->isChecked());
638 settings.setValue("enable-realwx", m_ui->fetchRealWxrCheckbox->isChecked());
639 settings.setValue("start-paused", m_ui->startPausedCheck->isChecked());
640 settings.setValue("ratings-filter", m_ui->ratingsFilterCheck->isChecked());
641 settings.setValue("recent-aircraft", m_recentAircraft);
642 settings.setValue("recent-airports", m_recentAirports);
643 settings.setValue("timeofday", m_ui->timeOfDayCombo->currentIndex());
644 settings.setValue("season", m_ui->seasonCombo->currentIndex());
645 settings.setValue("additional-args", m_ui->commandLineArgs->toPlainText());
648 void QtLauncher::setEnableDisableOptionFromCheckbox(QCheckBox* cbox, QString name) const
650 flightgear::Options* opt = flightgear::Options::sharedInstance();
651 std::string stdName(name.toStdString());
652 if (cbox->isChecked()) {
653 opt->addOption("enable-" + stdName, "");
655 opt->addOption("disable-" + stdName, "");
659 void QtLauncher::onRun()
663 flightgear::Options* opt = flightgear::Options::sharedInstance();
664 setEnableDisableOptionFromCheckbox(m_ui->terrasyncCheck, "terrasync");
665 setEnableDisableOptionFromCheckbox(m_ui->fetchRealWxrCheckbox, "real-weather-fetch");
666 setEnableDisableOptionFromCheckbox(m_ui->rembrandtCheckbox, "rembrandt");
667 setEnableDisableOptionFromCheckbox(m_ui->fullScreenCheckbox, "fullscreen");
668 setEnableDisableOptionFromCheckbox(m_ui->startPausedCheck, "freeze");
670 // MSAA is more complex
671 if (!m_ui->rembrandtCheckbox->isChecked()) {
672 if (m_ui->msaaCheckbox->isChecked()) {
673 globals->get_props()->setIntValue("/sim/rendering/multi-sample-buffers", 1);
674 globals->get_props()->setIntValue("/sim/rendering/multi-samples", 4);
676 globals->get_props()->setIntValue("/sim/rendering/multi-sample-buffers", 0);
681 if (!m_selectedAircraft.isEmpty()) {
682 QFileInfo setFileInfo(m_selectedAircraft);
683 opt->addOption("aircraft-dir", setFileInfo.dir().absolutePath().toStdString());
684 QString setFile = setFileInfo.fileName();
685 Q_ASSERT(setFile.endsWith("-set.xml"));
686 setFile.truncate(setFile.count() - 8); // drop the '-set.xml' portion
687 opt->addOption("aircraft", setFile.toStdString());
689 // manage aircraft history
690 if (m_recentAircraft.contains(m_selectedAircraft))
691 m_recentAircraft.removeOne(m_selectedAircraft);
692 m_recentAircraft.prepend(m_selectedAircraft);
693 if (m_recentAircraft.size() > MAX_RECENT_AIRCRAFT)
694 m_recentAircraft.pop_back();
697 // airport / location
698 if (m_selectedAirport) {
699 opt->addOption("airport", m_selectedAirport->ident());
702 if (m_ui->runwayRadio->isChecked()) {
703 int index = m_ui->runwayCombo->itemData(m_ui->runwayCombo->currentIndex()).toInt();
704 if ((index >= 0) && m_selectedAirport) {
705 // explicit runway choice
706 opt->addOption("runway", m_selectedAirport->getRunwayByIndex(index)->ident());
709 if (m_ui->onFinalCheckbox->isChecked()) {
710 opt->addOption("glideslope", "3.0");
711 opt->addOption("offset-distance", "10.0"); // in nautical miles
713 } else if (m_ui->parkingRadio->isChecked()) {
715 opt->addOption("parkpos", m_ui->parkingCombo->currentText().toStdString());
719 if (m_ui->timeOfDayCombo->currentIndex() != 0) {
720 QString dayval = m_ui->timeOfDayCombo->currentText().toLower();
721 opt->addOption("timeofday", dayval.toStdString());
724 if (m_ui->seasonCombo->currentIndex() != 0) {
725 QString dayval = m_ui->timeOfDayCombo->currentText().toLower();
726 opt->addOption("season", dayval.toStdString());
730 QString downloadDir = settings.value("download-dir").toString();
731 if (!downloadDir.isEmpty()) {
734 int result = QMessageBox::question(this, tr("Create download folder?"),
735 tr("The selected location for downloads does not exist. Create it?"),
736 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
737 if (result == QMessageBox::Cancel) {
741 if (result == QMessageBox::Yes) {
742 d.mkpath(downloadDir);
746 qDebug() << "Download dir is:" << downloadDir;
747 opt->addOption("download-dir", downloadDir.toStdString());
751 Q_FOREACH(QString path, settings.value("scenery-paths").toStringList()) {
752 opt->addOption("fg-scenery", path.toStdString());
756 Q_FOREACH(QString path, settings.value("aircraft-paths").toStringList()) {
757 // can't use fg-aircraft for this, as it is processed before the launcher is run
758 globals->append_aircraft_path(path.toStdString());
761 // additional arguments
762 ArgumentsTokenizer tk;
763 Q_FOREACH(ArgumentsTokenizer::Arg a, tk.tokenize(m_ui->commandLineArgs->toPlainText())) {
764 if (a.arg.startsWith("prop:")) {
765 QString v = a.arg.mid(5) + "=" + a.value;
766 opt->addOption("prop", v.toStdString());
768 opt->addOption(a.arg.toStdString(), a.value.toStdString());
775 void QtLauncher::onQuit()
780 void QtLauncher::onSearchAirports()
782 QString search = m_ui->airportEdit->text();
783 m_airportsModel->setSearch(search);
785 if (m_airportsModel->isSearchActive()) {
786 m_ui->searchStatusText->setText(QString("Searching for '%1'").arg(search));
787 m_ui->locationStack->setCurrentIndex(2);
788 } else if (m_airportsModel->rowCount(QModelIndex()) == 1) {
789 QString ident = m_airportsModel->firstIdent();
790 setAirport(FGAirport::findByIdent(ident.toStdString()));
791 m_ui->locationStack->setCurrentIndex(0);
795 void QtLauncher::onAirportSearchComplete()
797 int numResults = m_airportsModel->rowCount(QModelIndex());
798 if (numResults == 0) {
799 m_ui->searchStatusText->setText(QString("No matching airports for '%1'").arg(m_ui->airportEdit->text()));
800 } else if (numResults == 1) {
801 QString ident = m_airportsModel->firstIdent();
802 setAirport(FGAirport::findByIdent(ident.toStdString()));
803 m_ui->locationStack->setCurrentIndex(0);
805 m_ui->locationStack->setCurrentIndex(1);
809 void QtLauncher::onAirportChanged()
811 m_ui->runwayCombo->setEnabled(m_selectedAirport);
812 m_ui->parkingCombo->setEnabled(m_selectedAirport);
813 m_ui->airportDiagram->setAirport(m_selectedAirport);
815 m_ui->runwayRadio->setChecked(true); // default back to runway mode
816 // unelss multiplayer is enabled ?
818 if (!m_selectedAirport) {
819 m_ui->airportDescription->setText(QString());
820 m_ui->airportDiagram->setEnabled(false);
824 m_ui->airportDiagram->setEnabled(true);
826 m_ui->runwayCombo->clear();
827 m_ui->runwayCombo->addItem("Automatic", -1);
828 for (unsigned int r=0; r<m_selectedAirport->numRunways(); ++r) {
829 FGRunwayRef rwy = m_selectedAirport->getRunwayByIndex(r);
830 // add runway with index as data role
831 m_ui->runwayCombo->addItem(QString::fromStdString(rwy->ident()), r);
833 m_ui->airportDiagram->addRunway(rwy);
836 m_ui->parkingCombo->clear();
837 FGAirportDynamics* dynamics = m_selectedAirport->getDynamics();
838 PositionedIDVec parkings = NavDataCache::instance()->airportItemsOfType(
839 m_selectedAirport->guid(),
840 FGPositioned::PARKING);
841 if (parkings.empty()) {
842 m_ui->parkingCombo->setEnabled(false);
843 m_ui->parkingRadio->setEnabled(false);
845 m_ui->parkingCombo->setEnabled(true);
846 m_ui->parkingRadio->setEnabled(true);
847 Q_FOREACH(PositionedID parking, parkings) {
848 FGParking* park = dynamics->getParking(parking);
849 m_ui->parkingCombo->addItem(QString::fromStdString(park->getName()),
850 static_cast<qlonglong>(parking));
852 m_ui->airportDiagram->addParking(park);
857 void QtLauncher::onAirportDiagramClicked(FGRunwayRef rwy)
860 m_ui->runwayRadio->setChecked(true);
861 int rwyIndex = m_ui->runwayCombo->findText(QString::fromStdString(rwy->ident()));
862 m_ui->runwayCombo->setCurrentIndex(rwyIndex);
865 updateAirportDescription();
868 void QtLauncher::onToggleTerrasync(bool enabled)
872 QString downloadDir = settings.value("download-dir").toString();
873 if (downloadDir.isEmpty()) {
874 downloadDir = QString::fromStdString(flightgear::defaultDownloadDir());
877 QFileInfo info(downloadDir);
878 if (!info.exists()) {
880 msg.setWindowTitle(tr("Create download folder?"));
881 msg.setText(tr("The download folder '%1' does not exist, create it now? "
882 "Click 'change location' to choose another folder "
883 "to store downloaded files").arg(downloadDir));
884 msg.addButton(QMessageBox::Yes);
885 msg.addButton(QMessageBox::Cancel);
886 msg.addButton(tr("Change location"), QMessageBox::ActionRole);
887 int result = msg.exec();
889 if (result == QMessageBox::Cancel) {
890 m_ui->terrasyncCheck->setChecked(false);
894 if (result == QMessageBox::ActionRole) {
900 d.mkpath(downloadDir);
905 void QtLauncher::updateAirportDescription()
907 if (!m_selectedAirport) {
908 m_ui->airportDescription->setText(QString("No airport selected"));
912 QString ident = QString::fromStdString(m_selectedAirport->ident()),
913 name = QString::fromStdString(m_selectedAirport->name());
914 QString locationOnAirport;
915 if (m_ui->runwayRadio->isChecked()) {
916 bool onFinal = m_ui->onFinalCheckbox->isChecked();
917 int comboIndex = m_ui->runwayCombo->currentIndex();
918 QString runwayName = (comboIndex == 0) ?
920 QString("runway %1").arg(m_ui->runwayCombo->currentText());
923 locationOnAirport = QString("on 10-mile final to %1").arg(runwayName);
925 locationOnAirport = QString("on %1").arg(runwayName);
928 int runwayIndex = m_ui->runwayCombo->itemData(comboIndex).toInt();
929 FGRunwayRef rwy = (runwayIndex >= 0) ?
930 m_selectedAirport->getRunwayByIndex(runwayIndex) : FGRunwayRef();
931 m_ui->airportDiagram->setSelectedRunway(rwy);
932 } else if (m_ui->parkingRadio->isChecked()) {
933 locationOnAirport = QString("at parking position %1").arg(m_ui->parkingCombo->currentText());
936 m_ui->airportDescription->setText(QString("%2 (%1): %3").arg(ident).arg(name).arg(locationOnAirport));
939 void QtLauncher::onAirportChoiceSelected(const QModelIndex& index)
941 m_ui->locationStack->setCurrentIndex(0);
942 setAirport(FGPositioned::loadById<FGAirport>(index.data(Qt::UserRole).toULongLong()));
945 void QtLauncher::onAircraftSelected(const QModelIndex& index)
947 m_selectedAircraft = index.data(AircraftPathRole).toString();
948 updateSelectedAircraft();
951 void QtLauncher::updateSelectedAircraft()
954 QFileInfo info(m_selectedAircraft);
955 AircraftItem item(info.dir(), m_selectedAircraft);
956 m_ui->thumbnail->setPixmap(item.thumbnail());
957 m_ui->aircraftDescription->setText(item.description);
958 } catch (sg_exception& e) {
959 m_ui->thumbnail->setPixmap(QPixmap());
960 m_ui->aircraftDescription->setText("");
964 void QtLauncher::onPopupAirportHistory()
966 if (m_recentAirports.isEmpty()) {
971 Q_FOREACH(QString aptCode, m_recentAirports) {
972 FGAirportRef apt = FGAirport::findByIdent(aptCode.toStdString());
973 QString name = QString::fromStdString(apt->name());
974 QAction* act = m.addAction(QString("%1 - %2").arg(aptCode).arg(name));
975 act->setData(aptCode);
978 QPoint popupPos = m_ui->airportHistory->mapToGlobal(m_ui->airportHistory->rect().bottomLeft());
979 QAction* triggered = m.exec(popupPos);
981 FGAirportRef apt = FGAirport::findByIdent(triggered->data().toString().toStdString());
983 m_ui->airportEdit->clear();
984 m_ui->locationStack->setCurrentIndex(0);
988 QModelIndex QtLauncher::proxyIndexForAircraftPath(QString path) const
990 return m_aircraftProxy->mapFromSource(sourceIndexForAircraftPath(path));
993 QModelIndex QtLauncher::sourceIndexForAircraftPath(QString path) const
995 AircraftItemModel* sourceModel = qobject_cast<AircraftItemModel*>(m_aircraftProxy->sourceModel());
996 Q_ASSERT(sourceModel);
997 return sourceModel->indexOfAircraftPath(path);
1000 void QtLauncher::onPopupAircraftHistory()
1002 if (m_recentAircraft.isEmpty()) {
1007 Q_FOREACH(QString path, m_recentAircraft) {
1008 QModelIndex index = sourceIndexForAircraftPath(path);
1009 if (!index.isValid()) {
1013 QAction* act = m.addAction(index.data(Qt::DisplayRole).toString());
1017 QPoint popupPos = m_ui->aircraftHistory->mapToGlobal(m_ui->aircraftHistory->rect().bottomLeft());
1018 QAction* triggered = m.exec(popupPos);
1020 m_selectedAircraft = triggered->data().toString();
1021 QModelIndex index = proxyIndexForAircraftPath(m_selectedAircraft);
1022 m_ui->aircraftList->selectionModel()->setCurrentIndex(index,
1023 QItemSelectionModel::ClearAndSelect);
1024 m_ui->aircraftFilter->clear();
1025 updateSelectedAircraft();
1029 void QtLauncher::setAirport(FGAirportRef ref)
1031 if (m_selectedAirport == ref)
1034 m_selectedAirport = ref;
1038 // maintain the recent airport list
1039 QString icao = QString::fromStdString(ref->ident());
1040 if (m_recentAirports.contains(icao)) {
1042 m_recentAirports.removeOne(icao);
1043 m_recentAirports.push_front(icao);
1045 // insert and trim list if necessary
1046 m_recentAirports.push_front(icao);
1047 if (m_recentAirports.size() > MAX_RECENT_AIRPORTS) {
1048 m_recentAirports.pop_back();
1053 updateAirportDescription();
1056 void QtLauncher::onEditRatingsFilter()
1058 EditRatingsFilterDialog dialog(this);
1059 dialog.setRatings(m_ratingFilters);
1062 if (dialog.result() == QDialog::Accepted) {
1064 for (int i=0; i<4; ++i) {
1065 m_ratingFilters[i] = dialog.getRating(i);
1066 vl.append(m_ratingFilters[i]);
1068 m_aircraftProxy->setRatings(m_ratingFilters);
1071 settings.setValue("min-ratings", vl);
1075 void QtLauncher::updateSettingsSummary()
1077 QStringList summary;
1078 if (m_ui->timeOfDayCombo->currentIndex() > 0) {
1079 summary.append(QString(m_ui->timeOfDayCombo->currentText().toLower()));
1082 if (m_ui->seasonCombo->currentIndex() > 0) {
1083 summary.append(QString(m_ui->seasonCombo->currentText().toLower()));
1086 if (m_ui->rembrandtCheckbox->isChecked()) {
1087 summary.append("Rembrandt enabled");
1088 } else if (m_ui->msaaCheckbox->isChecked()) {
1089 summary.append("anti-aliasing");
1092 if (m_ui->fetchRealWxrCheckbox->isChecked()) {
1093 summary.append("live weather");
1096 if (m_ui->terrasyncCheck->isChecked()) {
1097 summary.append("automatic scenery downloads");
1100 if (m_ui->startPausedCheck->isChecked()) {
1101 summary.append("paused");
1104 QString s = summary.join(", ");
1105 s[0] = s[0].toUpper();
1106 m_ui->settingsDescription->setText(s);
1109 void QtLauncher::onRembrandtToggled(bool b)
1111 // Rembrandt and multi-sample are exclusive
1112 m_ui->msaaCheckbox->setEnabled(!b);
1115 void QtLauncher::onSubsytemIdleTimeout()
1117 globals->get_subsystem_mgr()->update(0.0);
1120 void QtLauncher::onEditPaths()
1122 PathsDialog dlg(this, globals->packageRoot());
1124 if (dlg.result() == QDialog::Accepted) {
1125 // re-scan the aircraft list
1127 m_aircraftModel->setPaths(settings.value("aircraft-paths").toStringList());
1128 m_aircraftModel->scanDirs();
1132 #include "QtLauncher.moc"