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 "CatalogListModel.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 SGPath p = SGPath::documents();
429 p.append("FlightGear");
430 p.append("Aircraft");
431 m_customAircraftDir = QString::fromStdString(p.str());
432 m_ui->customAircraftDirLabel->setText(QString("Custom aircraft folder: %1").arg(m_customAircraftDir));
434 globals->append_aircraft_path(m_customAircraftDir.toStdString());
436 // create and configure the proxy model
437 m_aircraftProxy = new AircraftProxyModel(this);
440 simgear::pkg::RootRef r(globals->packageRoot());
442 FGHTTPClient* http = new FGHTTPClient;
443 globals->add_subsystem("http", http);
445 // we guard against re-init in the global phase; bind and postinit
446 // will happen as normal
449 m_aircraftModel = new AircraftItemModel(this, r);
450 m_aircraftProxy->setSourceModel(m_aircraftModel);
452 m_aircraftProxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
453 m_aircraftProxy->setSortCaseSensitivity(Qt::CaseInsensitive);
454 m_aircraftProxy->setSortRole(Qt::DisplayRole);
455 m_aircraftProxy->setDynamicSortFilter(true);
457 m_ui->aircraftList->setModel(m_aircraftProxy);
458 m_ui->aircraftList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
459 AircraftItemDelegate* delegate = new AircraftItemDelegate(m_ui->aircraftList);
460 m_ui->aircraftList->setItemDelegate(delegate);
461 m_ui->aircraftList->setSelectionMode(QAbstractItemView::SingleSelection);
462 connect(m_ui->aircraftList, &QListView::clicked,
463 this, &QtLauncher::onAircraftSelected);
464 connect(delegate, &AircraftItemDelegate::variantChanged,
465 this, &QtLauncher::onAircraftSelected);
467 connect(m_ui->runwayCombo, SIGNAL(currentIndexChanged(int)),
468 this, SLOT(updateAirportDescription()));
469 connect(m_ui->parkingCombo, SIGNAL(currentIndexChanged(int)),
470 this, SLOT(updateAirportDescription()));
471 connect(m_ui->runwayRadio, SIGNAL(toggled(bool)),
472 this, SLOT(updateAirportDescription()));
473 connect(m_ui->parkingRadio, SIGNAL(toggled(bool)),
474 this, SLOT(updateAirportDescription()));
475 connect(m_ui->onFinalCheckbox, SIGNAL(toggled(bool)),
476 this, SLOT(updateAirportDescription()));
479 connect(m_ui->runButton, SIGNAL(clicked()), this, SLOT(onRun()));
480 connect(m_ui->quitButton, SIGNAL(clicked()), this, SLOT(onQuit()));
481 connect(m_ui->airportEdit, SIGNAL(returnPressed()),
482 this, SLOT(onSearchAirports()));
484 connect(m_ui->aircraftFilter, &QLineEdit::textChanged,
485 m_aircraftProxy, &QSortFilterProxyModel::setFilterFixedString);
487 connect(m_ui->airportHistory, &QPushButton::clicked,
488 this, &QtLauncher::onPopupAirportHistory);
489 connect(m_ui->aircraftHistory, &QPushButton::clicked,
490 this, &QtLauncher::onPopupAircraftHistory);
494 connect(m_ui->openAircraftDirButton, &QPushButton::clicked,
495 this, &QtLauncher::onOpenCustomAircraftDir);
497 QAction* qa = new QAction(this);
498 qa->setShortcut(QKeySequence("Ctrl+Q"));
499 connect(qa, &QAction::triggered, this, &QtLauncher::onQuit);
502 connect(m_ui->editRatingFilter, &QPushButton::clicked,
503 this, &QtLauncher::onEditRatingsFilter);
504 connect(m_ui->ratingsFilterCheck, &QAbstractButton::toggled,
505 m_aircraftProxy, &AircraftProxyModel::setRatingFilterEnabled);
507 QIcon historyIcon(":/history-icon");
508 m_ui->aircraftHistory->setIcon(historyIcon);
509 m_ui->airportHistory->setIcon(historyIcon);
511 m_ui->searchIcon->setPixmap(QPixmap(":/search-icon"));
513 connect(m_ui->timeOfDayCombo, SIGNAL(currentIndexChanged(int)),
514 this, SLOT(updateSettingsSummary()));
515 connect(m_ui->seasonCombo, SIGNAL(currentIndexChanged(int)),
516 this, SLOT(updateSettingsSummary()));
517 connect(m_ui->fetchRealWxrCheckbox, SIGNAL(toggled(bool)),
518 this, SLOT(updateSettingsSummary()));
519 connect(m_ui->rembrandtCheckbox, SIGNAL(toggled(bool)),
520 this, SLOT(updateSettingsSummary()));
521 connect(m_ui->terrasyncCheck, SIGNAL(toggled(bool)),
522 this, SLOT(updateSettingsSummary()));
523 connect(m_ui->startPausedCheck, SIGNAL(toggled(bool)),
524 this, SLOT(updateSettingsSummary()));
525 connect(m_ui->msaaCheckbox, SIGNAL(toggled(bool)),
526 this, SLOT(updateSettingsSummary()));
528 connect(m_ui->rembrandtCheckbox, SIGNAL(toggled(bool)),
529 this, SLOT(onRembrandtToggled(bool)));
531 updateSettingsSummary();
533 connect(m_ui->addSceneryPath, &QToolButton::clicked,
534 this, &QtLauncher::onAddSceneryPath);
535 connect(m_ui->removeSceneryPath, &QToolButton::clicked,
536 this, &QtLauncher::onRemoveSceneryPath);
538 connect(m_ui->addAircraftPath, &QToolButton::clicked,
539 this, &QtLauncher::onAddAircraftPath);
540 connect(m_ui->removeAircraftPath, &QToolButton::clicked,
541 this, &QtLauncher::onRemoveAircraftPath);
543 m_catalogsModel = new CatalogListModel(this, r);
544 m_ui->catalogsList->setModel(m_catalogsModel);
547 m_aircraftModel->setPaths(settings.value("aircraft-paths").toStringList());
548 m_aircraftModel->scanDirs();
551 QtLauncher::~QtLauncher()
556 void QtLauncher::initApp(int argc, char** argv)
558 static bool qtInitDone = false;
562 QApplication* app = new QApplication(argc, argv);
563 app->setOrganizationName("FlightGear");
564 app->setApplicationName("FlightGear");
565 app->setOrganizationDomain("flightgear.org");
567 // avoid double Apple menu and other weirdness if both Qt and OSG
568 // try to initialise various Cocoa structures.
569 flightgear::WindowBuilder::setPoseAsStandaloneApp(false);
571 Qt::KeyboardModifiers mods = app->queryKeyboardModifiers();
572 if (mods & Qt::AltModifier) {
573 qWarning() << "Alt pressed during launch";
575 // wipe out our settings
580 Options::sharedInstance()->addOption("restore-defaults", "");
585 bool QtLauncher::runLauncherDialog()
587 Q_INIT_RESOURCE(resources);
589 // startup the nav-cache now. This pre-empts normal startup of
590 // the cache, but no harm done. (Providing scenery paths are consistent)
594 // setup scenery paths now, especially TerraSync path for airport
595 // parking locations (after they're downloaded)
599 if (dlg.result() != QDialog::Accepted) {
606 void QtLauncher::restoreSettings()
609 m_ui->rembrandtCheckbox->setChecked(settings.value("enable-rembrandt", false).toBool());
610 m_ui->terrasyncCheck->setChecked(settings.value("enable-terrasync", true).toBool());
611 m_ui->fullScreenCheckbox->setChecked(settings.value("start-fullscreen", false).toBool());
612 m_ui->msaaCheckbox->setChecked(settings.value("enable-msaa", false).toBool());
613 m_ui->fetchRealWxrCheckbox->setChecked(settings.value("enable-realwx", true).toBool());
614 m_ui->startPausedCheck->setChecked(settings.value("start-paused", false).toBool());
615 m_ui->timeOfDayCombo->setCurrentIndex(settings.value("timeofday", 0).toInt());
616 m_ui->seasonCombo->setCurrentIndex(settings.value("season", 0).toInt());
618 // full paths to -set.xml files
619 m_recentAircraft = settings.value("recent-aircraft").toStringList();
621 if (!m_recentAircraft.empty()) {
622 m_selectedAircraft = m_recentAircraft.front();
624 // select the default C172p
627 updateSelectedAircraft();
630 m_recentAirports = settings.value("recent-airports").toStringList();
631 if (!m_recentAirports.empty()) {
632 setAirport(FGAirport::findByIdent(m_recentAirports.front().toStdString()));
634 updateAirportDescription();
637 m_ui->ratingsFilterCheck->setChecked(settings.value("ratings-filter", true).toBool());
639 Q_FOREACH(QVariant v, settings.value("min-ratings").toList()) {
640 m_ratingFilters[index++] = v.toInt();
643 m_aircraftProxy->setRatingFilterEnabled(m_ui->ratingsFilterCheck->isChecked());
644 m_aircraftProxy->setRatings(m_ratingFilters);
646 QStringList sceneryPaths = settings.value("scenery-paths").toStringList();
647 m_ui->sceneryPathsList->addItems(sceneryPaths);
649 QStringList aircraftPaths = settings.value("aircraft-paths").toStringList();
650 m_ui->aircraftPathsList->addItems(aircraftPaths);
652 m_ui->commandLineArgs->setPlainText(settings.value("additional-args").toString());
655 void QtLauncher::saveSettings()
658 settings.setValue("enable-rembrandt", m_ui->rembrandtCheckbox->isChecked());
659 settings.setValue("enable-terrasync", m_ui->terrasyncCheck->isChecked());
660 settings.setValue("enable-msaa", m_ui->msaaCheckbox->isChecked());
661 settings.setValue("start-fullscreen", m_ui->fullScreenCheckbox->isChecked());
662 settings.setValue("enable-realwx", m_ui->fetchRealWxrCheckbox->isChecked());
663 settings.setValue("start-paused", m_ui->startPausedCheck->isChecked());
664 settings.setValue("ratings-filter", m_ui->ratingsFilterCheck->isChecked());
665 settings.setValue("recent-aircraft", m_recentAircraft);
666 settings.setValue("recent-airports", m_recentAirports);
667 settings.setValue("timeofday", m_ui->timeOfDayCombo->currentIndex());
668 settings.setValue("season", m_ui->seasonCombo->currentIndex());
671 for (int i=0; i<m_ui->sceneryPathsList->count(); ++i) {
672 paths.append(m_ui->sceneryPathsList->item(i)->text());
675 settings.setValue("scenery-paths", paths);
678 for (int i=0; i<m_ui->aircraftPathsList->count(); ++i) {
679 paths.append(m_ui->aircraftPathsList->item(i)->text());
682 settings.setValue("aircraft-paths", paths);
683 settings.setValue("additional-args", m_ui->commandLineArgs->toPlainText());
686 void QtLauncher::setEnableDisableOptionFromCheckbox(QCheckBox* cbox, QString name) const
688 flightgear::Options* opt = flightgear::Options::sharedInstance();
689 std::string stdName(name.toStdString());
690 if (cbox->isChecked()) {
691 opt->addOption("enable-" + stdName, "");
693 opt->addOption("disable-" + stdName, "");
697 void QtLauncher::onRun()
701 flightgear::Options* opt = flightgear::Options::sharedInstance();
702 setEnableDisableOptionFromCheckbox(m_ui->terrasyncCheck, "terrasync");
703 setEnableDisableOptionFromCheckbox(m_ui->fetchRealWxrCheckbox, "real-weather-fetch");
704 setEnableDisableOptionFromCheckbox(m_ui->rembrandtCheckbox, "rembrandt");
705 setEnableDisableOptionFromCheckbox(m_ui->fullScreenCheckbox, "fullscreen");
706 setEnableDisableOptionFromCheckbox(m_ui->startPausedCheck, "freeze");
708 // MSAA is more complex
709 if (!m_ui->rembrandtCheckbox->isChecked()) {
710 if (m_ui->msaaCheckbox->isChecked()) {
711 globals->get_props()->setIntValue("/sim/rendering/multi-sample-buffers", 1);
712 globals->get_props()->setIntValue("/sim/rendering/multi-samples", 4);
714 globals->get_props()->setIntValue("/sim/rendering/multi-sample-buffers", 0);
719 if (!m_selectedAircraft.isEmpty()) {
720 QFileInfo setFileInfo(m_selectedAircraft);
721 opt->addOption("aircraft-dir", setFileInfo.dir().absolutePath().toStdString());
722 QString setFile = setFileInfo.fileName();
723 Q_ASSERT(setFile.endsWith("-set.xml"));
724 setFile.truncate(setFile.count() - 8); // drop the '-set.xml' portion
725 opt->addOption("aircraft", setFile.toStdString());
727 // manage aircraft history
728 if (m_recentAircraft.contains(m_selectedAircraft))
729 m_recentAircraft.removeOne(m_selectedAircraft);
730 m_recentAircraft.prepend(m_selectedAircraft);
731 if (m_recentAircraft.size() > MAX_RECENT_AIRCRAFT)
732 m_recentAircraft.pop_back();
735 // airport / location
736 if (m_selectedAirport) {
737 opt->addOption("airport", m_selectedAirport->ident());
740 if (m_ui->runwayRadio->isChecked()) {
741 int index = m_ui->runwayCombo->itemData(m_ui->runwayCombo->currentIndex()).toInt();
742 if ((index >= 0) && m_selectedAirport) {
743 // explicit runway choice
744 opt->addOption("runway", m_selectedAirport->getRunwayByIndex(index)->ident());
747 if (m_ui->onFinalCheckbox->isChecked()) {
748 opt->addOption("glideslope", "3.0");
749 opt->addOption("offset-distance", "10.0"); // in nautical miles
751 } else if (m_ui->parkingRadio->isChecked()) {
753 opt->addOption("parkpos", m_ui->parkingCombo->currentText().toStdString());
757 if (m_ui->timeOfDayCombo->currentIndex() != 0) {
758 QString dayval = m_ui->timeOfDayCombo->currentText().toLower();
759 opt->addOption("timeofday", dayval.toStdString());
762 if (m_ui->seasonCombo->currentIndex() != 0) {
763 QString dayval = m_ui->timeOfDayCombo->currentText().toLower();
764 opt->addOption("season", dayval.toStdString());
768 for (int i=0; i<m_ui->sceneryPathsList->count(); ++i) {
769 QString path = m_ui->sceneryPathsList->item(i)->text();
770 opt->addOption("fg-scenery", path.toStdString());
774 for (int i=0; i<m_ui->aircraftPathsList->count(); ++i) {
775 QString path = m_ui->aircraftPathsList->item(i)->text();
776 // can't use fg-aircraft for this, as it is processed before the launcher is run
777 globals->append_aircraft_path(path.toStdString());
780 // additional arguments
781 ArgumentsTokenizer tk;
782 Q_FOREACH(ArgumentsTokenizer::Arg a, tk.tokenize(m_ui->commandLineArgs->toPlainText())) {
783 if (a.arg.startsWith("prop:")) {
784 QString v = a.arg.mid(5) + "=" + a.value;
785 opt->addOption("prop", v.toStdString());
787 opt->addOption(a.arg.toStdString(), a.value.toStdString());
794 void QtLauncher::onQuit()
799 void QtLauncher::onSearchAirports()
801 QString search = m_ui->airportEdit->text();
802 m_airportsModel->setSearch(search);
804 if (m_airportsModel->isSearchActive()) {
805 m_ui->searchStatusText->setText(QString("Searching for '%1'").arg(search));
806 m_ui->locationStack->setCurrentIndex(2);
807 } else if (m_airportsModel->rowCount(QModelIndex()) == 1) {
808 QString ident = m_airportsModel->firstIdent();
809 setAirport(FGAirport::findByIdent(ident.toStdString()));
810 m_ui->locationStack->setCurrentIndex(0);
814 void QtLauncher::onAirportSearchComplete()
816 int numResults = m_airportsModel->rowCount(QModelIndex());
817 if (numResults == 0) {
818 m_ui->searchStatusText->setText(QString("No matching airports for '%1'").arg(m_ui->airportEdit->text()));
819 } else if (numResults == 1) {
820 QString ident = m_airportsModel->firstIdent();
821 setAirport(FGAirport::findByIdent(ident.toStdString()));
822 m_ui->locationStack->setCurrentIndex(0);
824 m_ui->locationStack->setCurrentIndex(1);
828 void QtLauncher::onAirportChanged()
830 m_ui->runwayCombo->setEnabled(m_selectedAirport);
831 m_ui->parkingCombo->setEnabled(m_selectedAirport);
832 m_ui->airportDiagram->setAirport(m_selectedAirport);
834 m_ui->runwayRadio->setChecked(true); // default back to runway mode
835 // unelss multiplayer is enabled ?
837 if (!m_selectedAirport) {
838 m_ui->airportDescription->setText(QString());
839 m_ui->airportDiagram->setEnabled(false);
843 m_ui->airportDiagram->setEnabled(true);
845 m_ui->runwayCombo->clear();
846 m_ui->runwayCombo->addItem("Automatic", -1);
847 for (unsigned int r=0; r<m_selectedAirport->numRunways(); ++r) {
848 FGRunwayRef rwy = m_selectedAirport->getRunwayByIndex(r);
849 // add runway with index as data role
850 m_ui->runwayCombo->addItem(QString::fromStdString(rwy->ident()), r);
852 m_ui->airportDiagram->addRunway(rwy);
855 m_ui->parkingCombo->clear();
856 FGAirportDynamics* dynamics = m_selectedAirport->getDynamics();
857 PositionedIDVec parkings = NavDataCache::instance()->airportItemsOfType(
858 m_selectedAirport->guid(),
859 FGPositioned::PARKING);
860 if (parkings.empty()) {
861 m_ui->parkingCombo->setEnabled(false);
862 m_ui->parkingRadio->setEnabled(false);
864 m_ui->parkingCombo->setEnabled(true);
865 m_ui->parkingRadio->setEnabled(true);
866 Q_FOREACH(PositionedID parking, parkings) {
867 FGParking* park = dynamics->getParking(parking);
868 m_ui->parkingCombo->addItem(QString::fromStdString(park->getName()),
869 static_cast<qlonglong>(parking));
871 m_ui->airportDiagram->addParking(park);
876 void QtLauncher::updateAirportDescription()
878 if (!m_selectedAirport) {
879 m_ui->airportDescription->setText(QString("No airport selected"));
883 QString ident = QString::fromStdString(m_selectedAirport->ident()),
884 name = QString::fromStdString(m_selectedAirport->name());
885 QString locationOnAirport;
886 if (m_ui->runwayRadio->isChecked()) {
887 bool onFinal = m_ui->onFinalCheckbox->isChecked();
888 QString runwayName = (m_ui->runwayCombo->currentIndex() == 0) ?
890 QString("runway %1").arg(m_ui->runwayCombo->currentText());
893 locationOnAirport = QString("on 10-mile final to %1").arg(runwayName);
895 locationOnAirport = QString("on %1").arg(runwayName);
897 } else if (m_ui->parkingRadio->isChecked()) {
898 locationOnAirport = QString("at parking position %1").arg(m_ui->parkingCombo->currentText());
901 m_ui->airportDescription->setText(QString("%2 (%1): %3").arg(ident).arg(name).arg(locationOnAirport));
904 void QtLauncher::onAirportChoiceSelected(const QModelIndex& index)
906 m_ui->locationStack->setCurrentIndex(0);
907 setAirport(FGPositioned::loadById<FGAirport>(index.data(Qt::UserRole).toULongLong()));
910 void QtLauncher::onAircraftSelected(const QModelIndex& index)
912 m_selectedAircraft = index.data(AircraftPathRole).toString();
913 updateSelectedAircraft();
916 void QtLauncher::updateSelectedAircraft()
919 QFileInfo info(m_selectedAircraft);
920 AircraftItem item(info.dir(), m_selectedAircraft);
921 m_ui->thumbnail->setPixmap(item.thumbnail());
922 m_ui->aircraftDescription->setText(item.description);
923 } catch (sg_exception& e) {
924 m_ui->thumbnail->setPixmap(QPixmap());
925 m_ui->aircraftDescription->setText("");
929 void QtLauncher::onPopupAirportHistory()
931 if (m_recentAirports.isEmpty()) {
936 Q_FOREACH(QString aptCode, m_recentAirports) {
937 FGAirportRef apt = FGAirport::findByIdent(aptCode.toStdString());
938 QString name = QString::fromStdString(apt->name());
939 QAction* act = m.addAction(QString("%1 - %2").arg(aptCode).arg(name));
940 act->setData(aptCode);
943 QPoint popupPos = m_ui->airportHistory->mapToGlobal(m_ui->airportHistory->rect().bottomLeft());
944 QAction* triggered = m.exec(popupPos);
946 FGAirportRef apt = FGAirport::findByIdent(triggered->data().toString().toStdString());
948 m_ui->airportEdit->clear();
949 m_ui->locationStack->setCurrentIndex(0);
953 QModelIndex QtLauncher::proxyIndexForAircraftPath(QString path) const
955 return m_aircraftProxy->mapFromSource(sourceIndexForAircraftPath(path));
958 QModelIndex QtLauncher::sourceIndexForAircraftPath(QString path) const
960 AircraftItemModel* sourceModel = qobject_cast<AircraftItemModel*>(m_aircraftProxy->sourceModel());
961 Q_ASSERT(sourceModel);
962 return sourceModel->indexOfAircraftPath(path);
965 void QtLauncher::onPopupAircraftHistory()
967 if (m_recentAircraft.isEmpty()) {
972 Q_FOREACH(QString path, m_recentAircraft) {
973 QModelIndex index = sourceIndexForAircraftPath(path);
974 if (!index.isValid()) {
978 QAction* act = m.addAction(index.data(Qt::DisplayRole).toString());
982 QPoint popupPos = m_ui->aircraftHistory->mapToGlobal(m_ui->aircraftHistory->rect().bottomLeft());
983 QAction* triggered = m.exec(popupPos);
985 m_selectedAircraft = triggered->data().toString();
986 QModelIndex index = proxyIndexForAircraftPath(m_selectedAircraft);
987 m_ui->aircraftList->selectionModel()->setCurrentIndex(index,
988 QItemSelectionModel::ClearAndSelect);
989 m_ui->aircraftFilter->clear();
990 updateSelectedAircraft();
994 void QtLauncher::setAirport(FGAirportRef ref)
996 if (m_selectedAirport == ref)
999 m_selectedAirport = ref;
1003 // maintain the recent airport list
1004 QString icao = QString::fromStdString(ref->ident());
1005 if (m_recentAirports.contains(icao)) {
1007 m_recentAirports.removeOne(icao);
1008 m_recentAirports.push_front(icao);
1010 // insert and trim list if necessary
1011 m_recentAirports.push_front(icao);
1012 if (m_recentAirports.size() > MAX_RECENT_AIRPORTS) {
1013 m_recentAirports.pop_back();
1018 updateAirportDescription();
1021 void QtLauncher::onOpenCustomAircraftDir()
1023 QFileInfo info(m_customAircraftDir);
1024 if (!info.exists()) {
1025 int result = QMessageBox::question(this, "Create folder?",
1026 "The custom aircraft folder does not exist, create it now?",
1027 QMessageBox::Yes | QMessageBox::No,
1029 if (result == QMessageBox::No) {
1033 QDir d(m_customAircraftDir);
1034 d.mkpath(m_customAircraftDir);
1037 QUrl u = QUrl::fromLocalFile(m_customAircraftDir);
1038 QDesktopServices::openUrl(u);
1041 void QtLauncher::onEditRatingsFilter()
1043 EditRatingsFilterDialog dialog(this);
1044 dialog.setRatings(m_ratingFilters);
1047 if (dialog.result() == QDialog::Accepted) {
1049 for (int i=0; i<4; ++i) {
1050 m_ratingFilters[i] = dialog.getRating(i);
1051 vl.append(m_ratingFilters[i]);
1053 m_aircraftProxy->setRatings(m_ratingFilters);
1056 settings.setValue("min-ratings", vl);
1060 void QtLauncher::updateSettingsSummary()
1062 QStringList summary;
1063 if (m_ui->timeOfDayCombo->currentIndex() > 0) {
1064 summary.append(QString(m_ui->timeOfDayCombo->currentText().toLower()));
1067 if (m_ui->seasonCombo->currentIndex() > 0) {
1068 summary.append(QString(m_ui->seasonCombo->currentText().toLower()));
1071 if (m_ui->rembrandtCheckbox->isChecked()) {
1072 summary.append("Rembrandt enabled");
1073 } else if (m_ui->msaaCheckbox->isChecked()) {
1074 summary.append("anti-aliasing");
1077 if (m_ui->fetchRealWxrCheckbox->isChecked()) {
1078 summary.append("live weather");
1081 if (m_ui->terrasyncCheck->isChecked()) {
1082 summary.append("automatic scenery downloads");
1085 if (m_ui->startPausedCheck->isChecked()) {
1086 summary.append("paused");
1089 QString s = summary.join(", ");
1090 s[0] = s[0].toUpper();
1091 m_ui->settingsDescription->setText(s);
1094 void QtLauncher::onAddSceneryPath()
1096 QString path = QFileDialog::getExistingDirectory(this, tr("Choose scenery folder"));
1097 if (!path.isEmpty()) {
1098 m_ui->sceneryPathsList->addItem(path);
1103 void QtLauncher::onRemoveSceneryPath()
1105 if (m_ui->sceneryPathsList->currentItem()) {
1106 delete m_ui->sceneryPathsList->currentItem();
1111 void QtLauncher::onAddAircraftPath()
1113 QString path = QFileDialog::getExistingDirectory(this, tr("Choose aircraft folder"));
1114 if (!path.isEmpty()) {
1115 m_ui->aircraftPathsList->addItem(path);
1118 // re-scan the aircraft list
1120 m_aircraftModel->setPaths(settings.value("aircraft-paths").toStringList());
1121 m_aircraftModel->scanDirs();
1125 void QtLauncher::onRemoveAircraftPath()
1127 if (m_ui->aircraftPathsList->currentItem()) {
1128 delete m_ui->aircraftPathsList->currentItem();
1133 void QtLauncher::onRembrandtToggled(bool b)
1135 // Rembrandt and multi-sample are exclusive
1136 m_ui->msaaCheckbox->setEnabled(!b);
1139 void QtLauncher::onSubsytemIdleTimeout()
1141 globals->get_subsystem_mgr()->update(0.0);
1144 void QtLauncher::onAddCatalog()
1149 void QtLauncher::onRemoveCatalog()
1154 #include "QtLauncher.moc"