1 #include "QtLauncher.hxx"
4 #include <QProgressDialog>
5 #include <QCoreApplication>
6 #include <QAbstractListModel>
15 #include <QMutexLocker>
19 #include <QSortFilterProxyModel>
21 #include <QDesktopServices>
24 #include <QStyledItemDelegate>
25 #include <QLinearGradient>
26 #include <QFileDialog>
27 #include <QMessageBox>
30 #include <simgear/timing/timestamp.hxx>
31 #include <simgear/props/props_io.hxx>
32 #include <simgear/structure/exception.hxx>
33 #include <simgear/misc/sg_path.hxx>
35 #include "ui_Launcher.h"
36 #include "EditRatingsFilterDialog.hxx"
38 #include <Main/globals.hxx>
39 #include <Navaids/NavDataCache.hxx>
40 #include <Airports/airport.hxx>
41 #include <Airports/dynamics.hxx> // for parking
42 #include <Main/options.hxx>
44 using namespace flightgear;
46 const int MAX_RECENT_AIRPORTS = 32;
47 const int MAX_RECENT_AIRCRAFT = 20;
49 namespace { // anonymous namespace
51 const int AircraftPathRole = Qt::UserRole + 1;
52 const int AircraftAuthorsRole = Qt::UserRole + 2;
53 const int AircraftRatingRole = Qt::UserRole + 100;
57 NavDataCache* cache = NavDataCache::instance();
58 if (cache->isRebuildRequired()) {
59 QProgressDialog rebuildProgress("Initialising navigation data, this may take several minutes",
60 QString() /* cancel text */,
62 rebuildProgress.setWindowModality(Qt::WindowModal);
63 rebuildProgress.show();
65 while (!cache->rebuild()) {
66 // sleep to give the rebuild thread more time
67 SGTimeStamp::sleepForMSec(50);
68 rebuildProgress.setValue(0);
69 QCoreApplication::processEvents();
77 // oh for C++11 initialisers
78 for (int i=0; i<4; ++i) ratings[i] = 0;
81 AircraftItem(QDir dir, QString filePath)
83 for (int i=0; i<4; ++i) ratings[i] = 0;
86 readProperties(filePath.toStdString(), &root);
88 if (!root.hasChild("sim")) {
89 throw sg_io_exception(std::string("Malformed -set.xml file"), filePath.toStdString());
92 SGPropertyNode_ptr sim = root.getNode("sim");
95 description = sim->getStringValue("description");
96 authors = sim->getStringValue("author");
98 if (sim->hasChild("rating")) {
99 parseRatings(sim->getNode("rating"));
102 if (dir.exists("thumbnail.jpg")) {
103 thumbnail.load(dir.filePath("thumbnail.jpg"));
104 // resize to the standard size
105 if (thumbnail.height() > 128) {
106 thumbnail = thumbnail.scaledToHeight(128);
119 void parseRatings(SGPropertyNode_ptr ratingsNode)
121 ratings[0] = ratingsNode->getIntValue("FDM");
122 ratings[1] = ratingsNode->getIntValue("systems");
123 ratings[2] = ratingsNode->getIntValue("cockpit");
124 ratings[3] = ratingsNode->getIntValue("model");
128 class AircraftScanThread : public QThread
132 AircraftScanThread(QStringList dirsToScan) :
139 /** thread-safe access to items already scanned */
140 QList<AircraftItem> items()
142 QList<AircraftItem> result;
143 QMutexLocker g(&m_lock);
144 result.swap(m_items);
159 Q_FOREACH(QString d, m_dirs) {
160 scanAircraftDir(QDir(d));
168 void scanAircraftDir(QDir path)
171 filters << "*-set.xml";
172 Q_FOREACH(QFileInfo child, path.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot)) {
173 QDir childDir(child.absoluteFilePath());
174 Q_FOREACH(QFileInfo xmlChild, childDir.entryInfoList(filters, QDir::Files)) {
176 AircraftItem item(childDir, xmlChild.absoluteFilePath());
177 // lock mutex whil we modify the items array
179 QMutexLocker g(&m_lock);
180 m_items.append(item);
182 } catch (sg_exception& e) {
189 } // of set.xml iteration
192 } // of subdir iteration
197 QList<AircraftItem> m_items;
201 class AircraftItemModel : public QAbstractListModel
205 AircraftItemModel(QObject* pr) :
206 QAbstractListModel(pr)
209 Q_FOREACH(std::string ap, globals->get_aircraft_paths()) {
210 dirs << QString::fromStdString(ap);
213 SGPath rootAircraft(globals->get_fg_root());
214 rootAircraft.append("Aircraft");
215 dirs << QString::fromStdString(rootAircraft.str());
217 m_scanThread = new AircraftScanThread(dirs);
218 connect(m_scanThread, &AircraftScanThread::finished, this,
219 &AircraftItemModel::onScanFinished);
220 connect(m_scanThread, &AircraftScanThread::addedItems,
221 this, &AircraftItemModel::onScanResults);
222 m_scanThread->start();
228 m_scanThread->setDone();
229 m_scanThread->wait(1000);
234 virtual int rowCount(const QModelIndex& parent) const
236 return m_items.size();
239 virtual QVariant data(const QModelIndex& index, int role) const
241 const AircraftItem& item(m_items.at(index.row()));
242 if (role == Qt::DisplayRole) {
243 return item.description;
244 } else if (role == Qt::DecorationRole) {
245 return item.thumbnail;
246 } else if (role == AircraftPathRole) {
248 } else if (role == AircraftAuthorsRole) {
250 } else if (role >= AircraftRatingRole) {
251 return item.ratings[role - AircraftRatingRole];
252 } else if (role == Qt::ToolTipRole) {
259 QModelIndex indexOfAircraftPath(QString path) const
261 for (int row=0; row <m_items.size(); ++row) {
262 const AircraftItem& item(m_items.at(row));
263 if (item.path == path) {
268 return QModelIndex();
274 QList<AircraftItem> newItems = m_scanThread->items();
275 if (newItems.isEmpty())
278 int firstRow = m_items.count();
279 int lastRow = firstRow + newItems.count() - 1;
280 beginInsertRows(QModelIndex(), firstRow, lastRow);
281 m_items.append(newItems);
285 void onScanFinished()
292 AircraftScanThread* m_scanThread;
293 QList<AircraftItem> m_items;
296 class AircraftItemDelegate : public QStyledItemDelegate
299 static const int MARGIN = 4;
301 virtual void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
303 // selection feedback rendering
304 if (option.state & QStyle::State_Selected) {
305 QLinearGradient grad(option.rect.topLeft(), option.rect.bottomLeft());
306 grad.setColorAt(0.0, QColor(152, 163, 180));
307 grad.setColorAt(1.0, QColor(90, 107, 131));
309 QBrush backgroundBrush(grad);
310 painter->fillRect(option.rect, backgroundBrush);
312 painter->setPen(QColor(90, 107, 131));
313 painter->drawLine(option.rect.topLeft(), option.rect.topRight());
317 QRect contentRect = option.rect.adjusted(MARGIN, MARGIN, -MARGIN, -MARGIN);
319 QPixmap thumbnail = index.data(Qt::DecorationRole).value<QPixmap>();
320 painter->drawPixmap(contentRect.topLeft(), thumbnail);
323 painter->setPen(QColor(0x7f, 0x7f, 0x7f));
324 painter->setBrush(Qt::NoBrush);
325 painter->drawRect(contentRect.left(), contentRect.top(), thumbnail.width(), thumbnail.height());
327 QString description = index.data(Qt::DisplayRole).toString();
328 contentRect.setLeft(contentRect.left() + MARGIN + thumbnail.width());
330 painter->setPen(Qt::black);
336 painter->drawText(contentRect, Qt::TextWordWrap, description, &actualBounds);
338 QString authors = index.data(AircraftAuthorsRole).toString();
343 QRect authorsRect = contentRect;
344 authorsRect.moveTop(actualBounds.bottom() + MARGIN);
345 painter->drawText(authorsRect, Qt::TextWordWrap,
346 QString("by: %1").arg(authors),
349 QRect r = contentRect;
350 r.setWidth(contentRect.width() / 2);
351 r.moveTop(actualBounds.bottom() + MARGIN);
354 drawRating(painter, "Flight model:", r, index.data(AircraftRatingRole).toInt());
355 r.moveTop(r.bottom());
356 drawRating(painter, "Systems:", r, index.data(AircraftRatingRole + 1).toInt());
358 r.moveTop(actualBounds.bottom() + MARGIN);
359 r.moveLeft(r.right());
360 drawRating(painter, "Cockpit:", r, index.data(AircraftRatingRole + 2).toInt());
361 r.moveTop(r.bottom());
362 drawRating(painter, "Exterior model:", r, index.data(AircraftRatingRole + 3).toInt());
367 virtual QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
369 return QSize(500, 128 + (MARGIN * 2));
373 void drawRating(QPainter* painter, QString label, const QRect& box, int value) const
375 const int DOT_SIZE = 10;
376 const int DOT_MARGIN = 4;
379 dotBox.setLeft(box.right() - (DOT_MARGIN * 6 + DOT_SIZE * 5));
381 painter->setPen(Qt::black);
383 textBox.setRight(dotBox.left() - DOT_MARGIN);
384 painter->drawText(textBox, Qt::AlignVCenter | Qt::AlignRight, label);
386 painter->setPen(Qt::NoPen);
387 QRect dot(dotBox.left() + DOT_MARGIN,
388 dotBox.center().y() - (DOT_SIZE / 2),
391 for (int i=0; i<5; ++i) {
392 painter->setBrush((i < value) ? QColor(0x3f, 0x3f, 0x3f) : QColor(0xaf, 0xaf, 0xaf));
393 painter->drawEllipse(dot);
394 dot.moveLeft(dot.right() + DOT_MARGIN);
399 class ArgumentsTokenizer
405 explicit Arg(QString k, QString v = QString()) : arg(k), value(v) {}
411 QList<Arg> tokenize(QString in) const
414 const int len = in.count();
420 for (; index < len; ++index) {
422 nc = index < (len - 1) ? in.at(index + 1) : QChar();
426 if (c == QChar('-')) {
427 if (nc == QChar('-')) {
432 // should we pemit single hyphen arguments?
433 // choosing to fail for now
436 } else if (c.isSpace()) {
442 if (c == QChar('=')) {
445 } else if (c.isSpace()) {
447 result.append(Arg(key));
449 // could check for illegal charatcers here
455 if (c == QChar('"')) {
457 } else if (c.isSpace()) {
459 result.append(Arg(key, value));
466 if (c == QChar('\\')) {
467 // check for escaped double-quote inside quoted value
468 if (nc == QChar('"')) {
471 } else if (c == QChar('"')) {
478 } // of character loop
480 // ensure last argument isn't lost
482 result.append(Arg(key));
483 } else if (state == Value) {
484 result.append(Arg(key, value));
499 } // of anonymous namespace
501 class AirportSearchModel : public QAbstractListModel
505 AirportSearchModel() :
506 m_searchActive(false)
510 void setSearch(QString t)
517 std::string term(t.toUpper().toStdString());
518 // try ICAO lookup first
519 FGAirportRef ref = FGAirport::findByIdent(term);
521 m_ids.push_back(ref->guid());
522 m_airports.push_back(ref);
524 m_search.reset(new NavDataCache::ThreadedAirportSearch(term));
525 QTimer::singleShot(100, this, SLOT(onSearchResultsPoll()));
526 m_searchActive = true;
532 bool isSearchActive() const
534 return m_searchActive;
537 virtual int rowCount(const QModelIndex&) const
539 // if empty, return 1 for special 'no matches'?
543 virtual QVariant data(const QModelIndex& index, int role) const
545 if (!index.isValid())
548 FGAirportRef apt = m_airports[index.row()];
550 apt = FGPositioned::loadById<FGAirport>(m_ids[index.row()]);
551 m_airports[index.row()] = apt;
554 if (role == Qt::DisplayRole) {
555 QString name = QString::fromStdString(apt->name());
556 return QString("%1: %2").arg(QString::fromStdString(apt->ident())).arg(name);
559 if (role == Qt::EditRole) {
560 return QString::fromStdString(apt->ident());
563 if (role == Qt::UserRole) {
564 return static_cast<qlonglong>(m_ids[index.row()]);
570 QString firstIdent() const
575 if (!m_airports.front().valid()) {
576 m_airports[0] = FGPositioned::loadById<FGAirport>(m_ids.front());
579 return QString::fromStdString(m_airports.front()->ident());
583 void searchComplete();
586 void onSearchResultsPoll()
588 PositionedIDVec newIds = m_search->results();
590 beginInsertRows(QModelIndex(), m_ids.size(), newIds.size() - 1);
591 for (unsigned int i=m_ids.size(); i < newIds.size(); ++i) {
592 m_ids.push_back(newIds[i]);
593 m_airports.push_back(FGAirportRef()); // null ref
597 if (m_search->isComplete()) {
598 m_searchActive = false;
600 emit searchComplete();
602 QTimer::singleShot(100, this, SLOT(onSearchResultsPoll()));
607 PositionedIDVec m_ids;
608 mutable std::vector<FGAirportRef> m_airports;
610 QScopedPointer<NavDataCache::ThreadedAirportSearch> m_search;
613 class AircraftProxyModel : public QSortFilterProxyModel
617 AircraftProxyModel(QObject* pr) :
618 QSortFilterProxyModel(pr),
619 m_ratingsFilter(true)
621 for (int i=0; i<4; ++i) {
626 void setRatings(int* ratings)
628 ::memcpy(m_ratings, ratings, sizeof(int) * 4);
633 void setRatingFilterEnabled(bool e)
635 if (e == m_ratingsFilter) {
644 bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
646 if (!QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent)) {
650 if (m_ratingsFilter) {
651 QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
652 for (int i=0; i<4; ++i) {
653 if (m_ratings[i] > index.data(AircraftRatingRole + i).toInt()) {
663 bool m_ratingsFilter;
667 QtLauncher::QtLauncher() :
671 m_ui.reset(new Ui::Launcher);
674 #if QT_VERSION >= 0x050300
675 // don't require Qt 5.3
676 m_ui->commandLineArgs->setPlaceholderText("--option=value --prop:/sim/name=value");
679 #if QT_VERSION >= 0x050200
680 m_ui->aircraftFilter->setClearButtonEnabled(true);
683 for (int i=0; i<4; ++i) {
684 m_ratingFilters[i] = 3;
687 m_airportsModel = new AirportSearchModel;
688 m_ui->searchList->setModel(m_airportsModel);
689 connect(m_ui->searchList, &QListView::clicked,
690 this, &QtLauncher::onAirportChoiceSelected);
691 connect(m_airportsModel, &AirportSearchModel::searchComplete,
692 this, &QtLauncher::onAirportSearchComplete);
694 SGPath p = SGPath::documents();
695 p.append("FlightGear");
696 p.append("Aircraft");
697 m_customAircraftDir = QString::fromStdString(p.str());
698 m_ui->customAircraftDirLabel->setText(QString("Custom aircraft folder: %1").arg(m_customAircraftDir));
700 globals->append_aircraft_path(m_customAircraftDir.toStdString());
702 // create and configure the proxy model
703 m_aircraftProxy = new AircraftProxyModel(this);
704 m_aircraftProxy->setSourceModel(new AircraftItemModel(this));
706 m_aircraftProxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
707 m_aircraftProxy->setSortCaseSensitivity(Qt::CaseInsensitive);
708 m_aircraftProxy->setSortRole(Qt::DisplayRole);
709 m_aircraftProxy->setDynamicSortFilter(true);
711 m_ui->aircraftList->setModel(m_aircraftProxy);
712 m_ui->aircraftList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
713 m_ui->aircraftList->setItemDelegate(new AircraftItemDelegate);
714 m_ui->aircraftList->setSelectionMode(QAbstractItemView::SingleSelection);
715 connect(m_ui->aircraftList, &QListView::clicked,
716 this, &QtLauncher::onAircraftSelected);
718 connect(m_ui->runwayCombo, SIGNAL(currentIndexChanged(int)),
719 this, SLOT(updateAirportDescription()));
720 connect(m_ui->parkingCombo, SIGNAL(currentIndexChanged(int)),
721 this, SLOT(updateAirportDescription()));
722 connect(m_ui->runwayRadio, SIGNAL(toggled(bool)),
723 this, SLOT(updateAirportDescription()));
724 connect(m_ui->parkingRadio, SIGNAL(toggled(bool)),
725 this, SLOT(updateAirportDescription()));
726 connect(m_ui->onFinalCheckbox, SIGNAL(toggled(bool)),
727 this, SLOT(updateAirportDescription()));
730 connect(m_ui->runButton, SIGNAL(clicked()), this, SLOT(onRun()));
731 connect(m_ui->quitButton, SIGNAL(clicked()), this, SLOT(onQuit()));
732 connect(m_ui->airportEdit, SIGNAL(returnPressed()),
733 this, SLOT(onSearchAirports()));
735 connect(m_ui->aircraftFilter, &QLineEdit::textChanged,
736 m_aircraftProxy, &QSortFilterProxyModel::setFilterFixedString);
738 connect(m_ui->airportHistory, &QPushButton::clicked,
739 this, &QtLauncher::onPopupAirportHistory);
740 connect(m_ui->aircraftHistory, &QPushButton::clicked,
741 this, &QtLauncher::onPopupAircraftHistory);
745 connect(m_ui->openAircraftDirButton, &QPushButton::clicked,
746 this, &QtLauncher::onOpenCustomAircraftDir);
748 QAction* qa = new QAction(this);
749 qa->setShortcut(QKeySequence("Ctrl+Q"));
750 connect(qa, &QAction::triggered, this, &QtLauncher::onQuit);
753 connect(m_ui->editRatingFilter, &QPushButton::clicked,
754 this, &QtLauncher::onEditRatingsFilter);
755 connect(m_ui->ratingsFilterCheck, &QAbstractButton::toggled,
756 m_aircraftProxy, &AircraftProxyModel::setRatingFilterEnabled);
758 QIcon historyIcon(":/history-icon");
759 m_ui->aircraftHistory->setIcon(historyIcon);
760 m_ui->airportHistory->setIcon(historyIcon);
762 m_ui->searchIcon->setPixmap(QPixmap(":/search-icon"));
764 connect(m_ui->timeOfDayCombo, SIGNAL(currentIndexChanged(int)),
765 this, SLOT(updateSettingsSummary()));
766 connect(m_ui->seasonCombo, SIGNAL(currentIndexChanged(int)),
767 this, SLOT(updateSettingsSummary()));
768 connect(m_ui->fetchRealWxrCheckbox, SIGNAL(toggled(bool)),
769 this, SLOT(updateSettingsSummary()));
770 connect(m_ui->rembrandtCheckbox, SIGNAL(toggled(bool)),
771 this, SLOT(updateSettingsSummary()));
772 connect(m_ui->terrasyncCheck, SIGNAL(toggled(bool)),
773 this, SLOT(updateSettingsSummary()));
774 connect(m_ui->startPausedCheck, SIGNAL(toggled(bool)),
775 this, SLOT(updateSettingsSummary()));
776 connect(m_ui->msaaCheckbox, SIGNAL(toggled(bool)),
777 this, SLOT(updateSettingsSummary()));
779 connect(m_ui->rembrandtCheckbox, SIGNAL(toggled(bool)),
780 this, SLOT(onRembrandtToggled(bool)));
782 updateSettingsSummary();
784 connect(m_ui->addSceneryPath, &QToolButton::clicked,
785 this, &QtLauncher::onAddSceneryPath);
786 connect(m_ui->removeSceneryPath, &QToolButton::clicked,
787 this, &QtLauncher::onRemoveSceneryPath);
790 QtLauncher::~QtLauncher()
795 bool QtLauncher::runLauncherDialog()
797 Q_INIT_RESOURCE(resources);
799 // startup the nav-cache now. This pre-empts normal startup of
800 // the cache, but no harm done. (Providing scenery paths are consistent)
804 // setup scenery paths now, especially TerraSync path for airport
805 // parking locations (after they're downloaded)
809 if (dlg.result() != QDialog::Accepted) {
816 void QtLauncher::restoreSettings()
819 m_ui->rembrandtCheckbox->setChecked(settings.value("enable-rembrandt", false).toBool());
820 m_ui->terrasyncCheck->setChecked(settings.value("enable-terrasync", true).toBool());
821 m_ui->fullScreenCheckbox->setChecked(settings.value("start-fullscreen", false).toBool());
822 m_ui->msaaCheckbox->setChecked(settings.value("enable-msaa", false).toBool());
823 m_ui->fetchRealWxrCheckbox->setChecked(settings.value("enable-realwx", true).toBool());
824 m_ui->startPausedCheck->setChecked(settings.value("start-paused", false).toBool());
825 m_ui->timeOfDayCombo->setCurrentIndex(settings.value("timeofday", 0).toInt());
826 m_ui->seasonCombo->setCurrentIndex(settings.value("season", 0).toInt());
828 // full paths to -set.xml files
829 m_recentAircraft = settings.value("recent-aircraft").toStringList();
831 if (!m_recentAircraft.empty()) {
832 m_selectedAircraft = m_recentAircraft.front();
834 // select the default C172p
837 updateSelectedAircraft();
840 m_recentAirports = settings.value("recent-airports").toStringList();
841 if (!m_recentAirports.empty()) {
842 setAirport(FGAirport::findByIdent(m_recentAirports.front().toStdString()));
844 updateAirportDescription();
847 m_ui->ratingsFilterCheck->setChecked(settings.value("ratings-filter", true).toBool());
849 Q_FOREACH(QVariant v, settings.value("min-ratings").toList()) {
850 m_ratingFilters[index++] = v.toInt();
853 m_aircraftProxy->setRatingFilterEnabled(m_ui->ratingsFilterCheck->isChecked());
854 m_aircraftProxy->setRatings(m_ratingFilters);
856 QStringList sceneryPaths = settings.value("scenery-paths").toStringList();
857 m_ui->sceneryPathsList->addItems(sceneryPaths);
859 m_ui->commandLineArgs->setPlainText(settings.value("additional-args").toString());
862 void QtLauncher::saveSettings()
865 settings.setValue("enable-rembrandt", m_ui->rembrandtCheckbox->isChecked());
866 settings.setValue("enable-terrasync", m_ui->terrasyncCheck->isChecked());
867 settings.setValue("enable-msaa", m_ui->msaaCheckbox->isChecked());
868 settings.setValue("start-fullscreen", m_ui->fullScreenCheckbox->isChecked());
869 settings.setValue("enable-realwx", m_ui->fetchRealWxrCheckbox->isChecked());
870 settings.setValue("start-paused", m_ui->startPausedCheck->isChecked());
871 settings.setValue("ratings-filter", m_ui->ratingsFilterCheck->isChecked());
872 settings.setValue("recent-aircraft", m_recentAircraft);
873 settings.setValue("recent-airports", m_recentAirports);
874 settings.setValue("timeofday", m_ui->timeOfDayCombo->currentIndex());
875 settings.setValue("season", m_ui->seasonCombo->currentIndex());
878 for (int i=0; i<m_ui->sceneryPathsList->count(); ++i) {
879 paths.append(m_ui->sceneryPathsList->item(i)->text());
882 settings.setValue("scenery-paths", paths);
883 settings.setValue("additional-args", m_ui->commandLineArgs->toPlainText());
886 void QtLauncher::setEnableDisableOptionFromCheckbox(QCheckBox* cbox, QString name) const
888 flightgear::Options* opt = flightgear::Options::sharedInstance();
889 std::string stdName(name.toStdString());
890 if (cbox->isChecked()) {
891 opt->addOption("enable-" + stdName, "");
893 opt->addOption("disable-" + stdName, "");
897 void QtLauncher::onRun()
901 flightgear::Options* opt = flightgear::Options::sharedInstance();
902 setEnableDisableOptionFromCheckbox(m_ui->terrasyncCheck, "terrasync");
903 setEnableDisableOptionFromCheckbox(m_ui->fetchRealWxrCheckbox, "real-weather-fetch");
904 setEnableDisableOptionFromCheckbox(m_ui->rembrandtCheckbox, "rembrandt");
905 setEnableDisableOptionFromCheckbox(m_ui->fullScreenCheckbox, "fullscreen");
906 setEnableDisableOptionFromCheckbox(m_ui->startPausedCheck, "freeze");
908 // MSAA is more complex
909 if (!m_ui->rembrandtCheckbox->isChecked()) {
910 if (m_ui->msaaCheckbox->isChecked()) {
911 globals->get_props()->setIntValue("/sim/rendering/multi-sample-buffers", 1);
912 globals->get_props()->setIntValue("/sim/rendering/multi-samples", 4);
914 globals->get_props()->setIntValue("/sim/rendering/multi-sample-buffers", 0);
919 if (!m_selectedAircraft.isEmpty()) {
920 QFileInfo setFileInfo(m_selectedAircraft);
921 opt->addOption("aircraft-dir", setFileInfo.dir().absolutePath().toStdString());
922 QString setFile = setFileInfo.fileName();
923 Q_ASSERT(setFile.endsWith("-set.xml"));
924 setFile.truncate(setFile.count() - 8); // drop the '-set.xml' portion
925 opt->addOption("aircraft", setFile.toStdString());
927 // manage aircraft history
928 if (m_recentAircraft.contains(m_selectedAircraft))
929 m_recentAircraft.removeOne(m_selectedAircraft);
930 m_recentAircraft.prepend(m_selectedAircraft);
931 if (m_recentAircraft.size() > MAX_RECENT_AIRCRAFT)
932 m_recentAircraft.pop_back();
935 // airport / location
936 if (m_selectedAirport) {
937 opt->addOption("airport", m_selectedAirport->ident());
940 if (m_ui->runwayRadio->isChecked()) {
941 int index = m_ui->runwayCombo->itemData(m_ui->runwayCombo->currentIndex()).toInt();
942 if ((index >= 0) && m_selectedAirport) {
943 // explicit runway choice
944 opt->addOption("runway", m_selectedAirport->getRunwayByIndex(index)->ident());
947 if (m_ui->onFinalCheckbox->isChecked()) {
948 opt->addOption("glideslope", "3.0");
949 opt->addOption("offset-distance", "10.0"); // in nautical miles
951 } else if (m_ui->parkingRadio->isChecked()) {
953 opt->addOption("parkpos", m_ui->parkingCombo->currentText().toStdString());
957 if (m_ui->timeOfDayCombo->currentIndex() != 0) {
958 QString dayval = m_ui->timeOfDayCombo->currentText().toLower();
959 opt->addOption("timeofday", dayval.toStdString());
962 if (m_ui->seasonCombo->currentIndex() != 0) {
963 QString dayval = m_ui->timeOfDayCombo->currentText().toLower();
964 opt->addOption("season", dayval.toStdString());
968 for (int i=0; i<m_ui->sceneryPathsList->count(); ++i) {
969 QString path = m_ui->sceneryPathsList->item(i)->text();
970 opt->addOption("fg-scenery", path.toStdString());
973 // additional arguments
974 ArgumentsTokenizer tk;
975 Q_FOREACH(ArgumentsTokenizer::Arg a, tk.tokenize(m_ui->commandLineArgs->toPlainText())) {
976 if (a.arg.startsWith("prop:")) {
977 QString v = a.arg.mid(5) + "=" + a.value;
978 opt->addOption("prop", v.toStdString());
980 opt->addOption(a.arg.toStdString(), a.value.toStdString());
987 void QtLauncher::onQuit()
992 void QtLauncher::onSearchAirports()
994 QString search = m_ui->airportEdit->text();
995 m_airportsModel->setSearch(search);
997 if (m_airportsModel->isSearchActive()) {
998 m_ui->searchStatusText->setText(QString("Searching for '%1'").arg(search));
999 m_ui->locationStack->setCurrentIndex(2);
1000 } else if (m_airportsModel->rowCount(QModelIndex()) == 1) {
1001 QString ident = m_airportsModel->firstIdent();
1002 setAirport(FGAirport::findByIdent(ident.toStdString()));
1003 m_ui->locationStack->setCurrentIndex(0);
1007 void QtLauncher::onAirportSearchComplete()
1009 int numResults = m_airportsModel->rowCount(QModelIndex());
1010 if (numResults == 0) {
1011 m_ui->searchStatusText->setText(QString("No matching airports for '%1'").arg(m_ui->airportEdit->text()));
1012 } else if (numResults == 1) {
1013 QString ident = m_airportsModel->firstIdent();
1014 setAirport(FGAirport::findByIdent(ident.toStdString()));
1015 m_ui->locationStack->setCurrentIndex(0);
1017 m_ui->locationStack->setCurrentIndex(1);
1021 void QtLauncher::onAirportChanged()
1023 m_ui->runwayCombo->setEnabled(m_selectedAirport);
1024 m_ui->parkingCombo->setEnabled(m_selectedAirport);
1025 m_ui->airportDiagram->setAirport(m_selectedAirport);
1027 m_ui->runwayRadio->setChecked(true); // default back to runway mode
1028 // unelss multiplayer is enabled ?
1030 if (!m_selectedAirport) {
1031 m_ui->airportDescription->setText(QString());
1032 m_ui->airportDiagram->setEnabled(false);
1036 m_ui->airportDiagram->setEnabled(true);
1038 m_ui->runwayCombo->clear();
1039 m_ui->runwayCombo->addItem("Automatic", -1);
1040 for (unsigned int r=0; r<m_selectedAirport->numRunways(); ++r) {
1041 FGRunwayRef rwy = m_selectedAirport->getRunwayByIndex(r);
1042 // add runway with index as data role
1043 m_ui->runwayCombo->addItem(QString::fromStdString(rwy->ident()), r);
1045 m_ui->airportDiagram->addRunway(rwy);
1048 m_ui->parkingCombo->clear();
1049 FGAirportDynamics* dynamics = m_selectedAirport->getDynamics();
1050 PositionedIDVec parkings = NavDataCache::instance()->airportItemsOfType(
1051 m_selectedAirport->guid(),
1052 FGPositioned::PARKING);
1053 if (parkings.empty()) {
1054 m_ui->parkingCombo->setEnabled(false);
1055 m_ui->parkingRadio->setEnabled(false);
1057 m_ui->parkingCombo->setEnabled(true);
1058 m_ui->parkingRadio->setEnabled(true);
1059 Q_FOREACH(PositionedID parking, parkings) {
1060 FGParking* park = dynamics->getParking(parking);
1061 m_ui->parkingCombo->addItem(QString::fromStdString(park->getName()),
1062 static_cast<qlonglong>(parking));
1064 m_ui->airportDiagram->addParking(park);
1069 void QtLauncher::updateAirportDescription()
1071 if (!m_selectedAirport) {
1072 m_ui->airportDescription->setText(QString("No airport selected"));
1076 QString ident = QString::fromStdString(m_selectedAirport->ident()),
1077 name = QString::fromStdString(m_selectedAirport->name());
1078 QString locationOnAirport;
1079 if (m_ui->runwayRadio->isChecked()) {
1080 bool onFinal = m_ui->onFinalCheckbox->isChecked();
1081 QString runwayName = (m_ui->runwayCombo->currentIndex() == 0) ?
1083 QString("runway %1").arg(m_ui->runwayCombo->currentText());
1086 locationOnAirport = QString("on 10-mile final to %1").arg(runwayName);
1088 locationOnAirport = QString("on %1").arg(runwayName);
1090 } else if (m_ui->parkingRadio->isChecked()) {
1091 locationOnAirport = QString("at parking position %1").arg(m_ui->parkingCombo->currentText());
1094 m_ui->airportDescription->setText(QString("%2 (%1): %3").arg(ident).arg(name).arg(locationOnAirport));
1097 void QtLauncher::onAirportChoiceSelected(const QModelIndex& index)
1099 m_ui->locationStack->setCurrentIndex(0);
1100 setAirport(FGPositioned::loadById<FGAirport>(index.data(Qt::UserRole).toULongLong()));
1103 void QtLauncher::onAircraftSelected(const QModelIndex& index)
1105 m_selectedAircraft = index.data(AircraftPathRole).toString();
1106 updateSelectedAircraft();
1109 void QtLauncher::updateSelectedAircraft()
1112 QFileInfo info(m_selectedAircraft);
1113 AircraftItem item(info.dir(), m_selectedAircraft);
1114 m_ui->thumbnail->setPixmap(item.thumbnail);
1115 m_ui->aircraftDescription->setText(item.description);
1116 } catch (sg_exception& e) {
1117 m_ui->thumbnail->setPixmap(QPixmap());
1118 m_ui->aircraftDescription->setText("");
1122 void QtLauncher::onPopupAirportHistory()
1124 if (m_recentAirports.isEmpty()) {
1129 Q_FOREACH(QString aptCode, m_recentAirports) {
1130 FGAirportRef apt = FGAirport::findByIdent(aptCode.toStdString());
1131 QString name = QString::fromStdString(apt->name());
1132 QAction* act = m.addAction(QString("%1 - %2").arg(aptCode).arg(name));
1133 act->setData(aptCode);
1136 QPoint popupPos = m_ui->airportHistory->mapToGlobal(m_ui->airportHistory->rect().bottomLeft());
1137 QAction* triggered = m.exec(popupPos);
1139 FGAirportRef apt = FGAirport::findByIdent(triggered->data().toString().toStdString());
1141 m_ui->airportEdit->clear();
1142 m_ui->locationStack->setCurrentIndex(0);
1146 QModelIndex QtLauncher::proxyIndexForAircraftPath(QString path) const
1148 return m_aircraftProxy->mapFromSource(sourceIndexForAircraftPath(path));
1151 QModelIndex QtLauncher::sourceIndexForAircraftPath(QString path) const
1153 AircraftItemModel* sourceModel = qobject_cast<AircraftItemModel*>(m_aircraftProxy->sourceModel());
1154 Q_ASSERT(sourceModel);
1155 return sourceModel->indexOfAircraftPath(path);
1158 void QtLauncher::onPopupAircraftHistory()
1160 if (m_recentAircraft.isEmpty()) {
1165 Q_FOREACH(QString path, m_recentAircraft) {
1166 QModelIndex index = sourceIndexForAircraftPath(path);
1167 if (!index.isValid()) {
1171 QAction* act = m.addAction(index.data(Qt::DisplayRole).toString());
1175 QPoint popupPos = m_ui->aircraftHistory->mapToGlobal(m_ui->aircraftHistory->rect().bottomLeft());
1176 QAction* triggered = m.exec(popupPos);
1178 m_selectedAircraft = triggered->data().toString();
1179 QModelIndex index = proxyIndexForAircraftPath(m_selectedAircraft);
1180 m_ui->aircraftList->selectionModel()->setCurrentIndex(index,
1181 QItemSelectionModel::ClearAndSelect);
1182 m_ui->aircraftFilter->clear();
1183 updateSelectedAircraft();
1187 void QtLauncher::setAirport(FGAirportRef ref)
1189 if (m_selectedAirport == ref)
1192 m_selectedAirport = ref;
1196 // maintain the recent airport list
1197 QString icao = QString::fromStdString(ref->ident());
1198 if (m_recentAirports.contains(icao)) {
1200 m_recentAirports.removeOne(icao);
1201 m_recentAirports.push_front(icao);
1203 // insert and trim list if necessary
1204 m_recentAirports.push_front(icao);
1205 if (m_recentAirports.size() > MAX_RECENT_AIRPORTS) {
1206 m_recentAirports.pop_back();
1211 updateAirportDescription();
1214 void QtLauncher::onOpenCustomAircraftDir()
1216 QFileInfo info(m_customAircraftDir);
1217 if (!info.exists()) {
1218 int result = QMessageBox::question(this, "Create folder?",
1219 "The custom aircraft folder does not exist, create it now?",
1220 QMessageBox::Yes | QMessageBox::No,
1222 if (result == QMessageBox::No) {
1226 QDir d(m_customAircraftDir);
1227 d.mkpath(m_customAircraftDir);
1230 QUrl u = QUrl::fromLocalFile(m_customAircraftDir);
1231 QDesktopServices::openUrl(u);
1234 void QtLauncher::onEditRatingsFilter()
1236 EditRatingsFilterDialog dialog(this);
1237 dialog.setRatings(m_ratingFilters);
1240 if (dialog.result() == QDialog::Accepted) {
1242 for (int i=0; i<4; ++i) {
1243 m_ratingFilters[i] = dialog.getRating(i);
1244 vl.append(m_ratingFilters[i]);
1246 m_aircraftProxy->setRatings(m_ratingFilters);
1249 settings.setValue("min-ratings", vl);
1253 void QtLauncher::updateSettingsSummary()
1255 QStringList summary;
1256 if (m_ui->timeOfDayCombo->currentIndex() > 0) {
1257 summary.append(QString(m_ui->timeOfDayCombo->currentText().toLower()));
1260 if (m_ui->seasonCombo->currentIndex() > 0) {
1261 summary.append(QString(m_ui->seasonCombo->currentText().toLower()));
1264 if (m_ui->rembrandtCheckbox->isChecked()) {
1265 summary.append("Rembrandt enabled");
1266 } else if (m_ui->msaaCheckbox->isChecked()) {
1267 summary.append("anti-aliasing");
1270 if (m_ui->fetchRealWxrCheckbox->isChecked()) {
1271 summary.append("live weather");
1274 if (m_ui->terrasyncCheck->isChecked()) {
1275 summary.append("automatic scenery downloads");
1278 if (m_ui->startPausedCheck->isChecked()) {
1279 summary.append("paused");
1282 QString s = summary.join(", ");
1283 s[0] = s[0].toUpper();
1284 m_ui->settingsDescription->setText(s);
1287 void QtLauncher::onAddSceneryPath()
1289 QString path = QFileDialog::getExistingDirectory(this, tr("Choose scenery folder"));
1290 if (!path.isEmpty()) {
1291 m_ui->sceneryPathsList->addItem(path);
1296 void QtLauncher::onRemoveSceneryPath()
1298 if (m_ui->sceneryPathsList->currentItem()) {
1299 delete m_ui->sceneryPathsList->currentItem();
1304 void QtLauncher::onRembrandtToggled(bool b)
1306 // Rembrandt and multi-sample are exclusive
1307 m_ui->msaaCheckbox->setEnabled(!b);
1310 #include "QtLauncher.moc"