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");
678 for (int i=0; i<4; ++i) {
679 m_ratingFilters[i] = 3;
682 m_airportsModel = new AirportSearchModel;
683 m_ui->searchList->setModel(m_airportsModel);
684 connect(m_ui->searchList, &QListView::clicked,
685 this, &QtLauncher::onAirportChoiceSelected);
686 connect(m_airportsModel, &AirportSearchModel::searchComplete,
687 this, &QtLauncher::onAirportSearchComplete);
689 SGPath p = SGPath::documents();
690 p.append("FlightGear");
691 p.append("Aircraft");
692 m_customAircraftDir = QString::fromStdString(p.str());
693 m_ui->customAircraftDirLabel->setText(QString("Custom aircraft folder: %1").arg(m_customAircraftDir));
695 globals->append_aircraft_path(m_customAircraftDir.toStdString());
697 // create and configure the proxy model
698 m_aircraftProxy = new AircraftProxyModel(this);
699 m_aircraftProxy->setSourceModel(new AircraftItemModel(this));
701 m_aircraftProxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
702 m_aircraftProxy->setSortCaseSensitivity(Qt::CaseInsensitive);
703 m_aircraftProxy->setSortRole(Qt::DisplayRole);
704 m_aircraftProxy->setDynamicSortFilter(true);
706 m_ui->aircraftList->setModel(m_aircraftProxy);
707 m_ui->aircraftList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
708 m_ui->aircraftList->setItemDelegate(new AircraftItemDelegate);
709 m_ui->aircraftList->setSelectionMode(QAbstractItemView::SingleSelection);
710 connect(m_ui->aircraftList, &QListView::clicked,
711 this, &QtLauncher::onAircraftSelected);
713 connect(m_ui->runwayCombo, SIGNAL(currentIndexChanged(int)),
714 this, SLOT(updateAirportDescription()));
715 connect(m_ui->parkingCombo, SIGNAL(currentIndexChanged(int)),
716 this, SLOT(updateAirportDescription()));
717 connect(m_ui->runwayRadio, SIGNAL(toggled(bool)),
718 this, SLOT(updateAirportDescription()));
719 connect(m_ui->parkingRadio, SIGNAL(toggled(bool)),
720 this, SLOT(updateAirportDescription()));
721 connect(m_ui->onFinalCheckbox, SIGNAL(toggled(bool)),
722 this, SLOT(updateAirportDescription()));
725 connect(m_ui->runButton, SIGNAL(clicked()), this, SLOT(onRun()));
726 connect(m_ui->quitButton, SIGNAL(clicked()), this, SLOT(onQuit()));
727 connect(m_ui->airportEdit, SIGNAL(returnPressed()),
728 this, SLOT(onSearchAirports()));
730 connect(m_ui->aircraftFilter, &QLineEdit::textChanged,
731 m_aircraftProxy, &QSortFilterProxyModel::setFilterFixedString);
733 connect(m_ui->airportHistory, &QPushButton::clicked,
734 this, &QtLauncher::onPopupAirportHistory);
735 connect(m_ui->aircraftHistory, &QPushButton::clicked,
736 this, &QtLauncher::onPopupAircraftHistory);
740 connect(m_ui->openAircraftDirButton, &QPushButton::clicked,
741 this, &QtLauncher::onOpenCustomAircraftDir);
743 QAction* qa = new QAction(this);
744 qa->setShortcut(QKeySequence("Ctrl+Q"));
745 connect(qa, &QAction::triggered, this, &QtLauncher::onQuit);
748 connect(m_ui->editRatingFilter, &QPushButton::clicked,
749 this, &QtLauncher::onEditRatingsFilter);
750 connect(m_ui->ratingsFilterCheck, &QAbstractButton::toggled,
751 m_aircraftProxy, &AircraftProxyModel::setRatingFilterEnabled);
753 QIcon historyIcon(":/history-icon");
754 m_ui->aircraftHistory->setIcon(historyIcon);
755 m_ui->airportHistory->setIcon(historyIcon);
757 m_ui->searchIcon->setPixmap(QPixmap(":/search-icon"));
759 connect(m_ui->timeOfDayCombo, SIGNAL(currentIndexChanged(int)),
760 this, SLOT(updateSettingsSummary()));
761 connect(m_ui->seasonCombo, SIGNAL(currentIndexChanged(int)),
762 this, SLOT(updateSettingsSummary()));
763 connect(m_ui->fetchRealWxrCheckbox, SIGNAL(toggled(bool)),
764 this, SLOT(updateSettingsSummary()));
765 connect(m_ui->rembrandtCheckbox, SIGNAL(toggled(bool)),
766 this, SLOT(updateSettingsSummary()));
767 connect(m_ui->terrasyncCheck, SIGNAL(toggled(bool)),
768 this, SLOT(updateSettingsSummary()));
769 connect(m_ui->startPausedCheck, SIGNAL(toggled(bool)),
770 this, SLOT(updateSettingsSummary()));
771 connect(m_ui->msaaCheckbox, SIGNAL(toggled(bool)),
772 this, SLOT(updateSettingsSummary()));
774 connect(m_ui->rembrandtCheckbox, SIGNAL(toggled(bool)),
775 this, SLOT(onRembrandtToggled(bool)));
777 updateSettingsSummary();
779 connect(m_ui->addSceneryPath, &QToolButton::clicked,
780 this, &QtLauncher::onAddSceneryPath);
781 connect(m_ui->removeSceneryPath, &QToolButton::clicked,
782 this, &QtLauncher::onRemoveSceneryPath);
785 QtLauncher::~QtLauncher()
790 bool QtLauncher::runLauncherDialog()
792 Q_INIT_RESOURCE(resources);
794 // startup the nav-cache now. This pre-empts normal startup of
795 // the cache, but no harm done. (Providing scenery paths are consistent)
799 // setup scenery paths now, especially TerraSync path for airport
800 // parking locations (after they're downloaded)
804 if (dlg.result() != QDialog::Accepted) {
811 void QtLauncher::restoreSettings()
814 m_ui->rembrandtCheckbox->setChecked(settings.value("enable-rembrandt", false).toBool());
815 m_ui->terrasyncCheck->setChecked(settings.value("enable-terrasync", true).toBool());
816 m_ui->fullScreenCheckbox->setChecked(settings.value("start-fullscreen", false).toBool());
817 m_ui->msaaCheckbox->setChecked(settings.value("enable-msaa", false).toBool());
818 m_ui->fetchRealWxrCheckbox->setChecked(settings.value("enable-realwx", true).toBool());
819 m_ui->startPausedCheck->setChecked(settings.value("start-paused", false).toBool());
820 m_ui->timeOfDayCombo->setCurrentIndex(settings.value("timeofday", 0).toInt());
821 m_ui->seasonCombo->setCurrentIndex(settings.value("season", 0).toInt());
823 // full paths to -set.xml files
824 m_recentAircraft = settings.value("recent-aircraft").toStringList();
826 if (!m_recentAircraft.empty()) {
827 m_selectedAircraft = m_recentAircraft.front();
829 // select the default C172p
832 updateSelectedAircraft();
835 m_recentAirports = settings.value("recent-airports").toStringList();
836 if (!m_recentAirports.empty()) {
837 setAirport(FGAirport::findByIdent(m_recentAirports.front().toStdString()));
839 updateAirportDescription();
842 m_ui->ratingsFilterCheck->setChecked(settings.value("ratings-filter", true).toBool());
844 Q_FOREACH(QVariant v, settings.value("min-ratings").toList()) {
845 m_ratingFilters[index++] = v.toInt();
848 m_aircraftProxy->setRatingFilterEnabled(m_ui->ratingsFilterCheck->isChecked());
849 m_aircraftProxy->setRatings(m_ratingFilters);
851 QStringList sceneryPaths = settings.value("scenery-paths").toStringList();
852 m_ui->sceneryPathsList->addItems(sceneryPaths);
854 m_ui->commandLineArgs->setPlainText(settings.value("additional-args").toString());
857 void QtLauncher::saveSettings()
860 settings.setValue("enable-rembrandt", m_ui->rembrandtCheckbox->isChecked());
861 settings.setValue("enable-terrasync", m_ui->terrasyncCheck->isChecked());
862 settings.setValue("enable-msaa", m_ui->msaaCheckbox->isChecked());
863 settings.setValue("start-fullscreen", m_ui->fullScreenCheckbox->isChecked());
864 settings.setValue("enable-realwx", m_ui->fetchRealWxrCheckbox->isChecked());
865 settings.setValue("start-paused", m_ui->startPausedCheck->isChecked());
866 settings.setValue("ratings-filter", m_ui->ratingsFilterCheck->isChecked());
867 settings.setValue("recent-aircraft", m_recentAircraft);
868 settings.setValue("recent-airports", m_recentAirports);
869 settings.setValue("timeofday", m_ui->timeOfDayCombo->currentIndex());
870 settings.setValue("season", m_ui->seasonCombo->currentIndex());
873 for (int i=0; i<m_ui->sceneryPathsList->count(); ++i) {
874 paths.append(m_ui->sceneryPathsList->item(i)->text());
877 settings.setValue("scenery-paths", paths);
878 settings.setValue("additional-args", m_ui->commandLineArgs->toPlainText());
881 void QtLauncher::setEnableDisableOptionFromCheckbox(QCheckBox* cbox, QString name) const
883 flightgear::Options* opt = flightgear::Options::sharedInstance();
884 std::string stdName(name.toStdString());
885 if (cbox->isChecked()) {
886 opt->addOption("enable-" + stdName, "");
888 opt->addOption("disable-" + stdName, "");
892 void QtLauncher::onRun()
896 flightgear::Options* opt = flightgear::Options::sharedInstance();
897 setEnableDisableOptionFromCheckbox(m_ui->terrasyncCheck, "terrasync");
898 setEnableDisableOptionFromCheckbox(m_ui->fetchRealWxrCheckbox, "real-weather-fetch");
899 setEnableDisableOptionFromCheckbox(m_ui->rembrandtCheckbox, "rembrandt");
900 setEnableDisableOptionFromCheckbox(m_ui->fullScreenCheckbox, "fullscreen");
901 setEnableDisableOptionFromCheckbox(m_ui->startPausedCheck, "freeze");
903 // MSAA is more complex
904 if (!m_ui->rembrandtCheckbox->isChecked()) {
905 if (m_ui->msaaCheckbox->isChecked()) {
906 globals->get_props()->setIntValue("/sim/rendering/multi-sample-buffers", 1);
907 globals->get_props()->setIntValue("/sim/rendering/multi-samples", 4);
909 globals->get_props()->setIntValue("/sim/rendering/multi-sample-buffers", 0);
914 if (!m_selectedAircraft.isEmpty()) {
915 QFileInfo setFileInfo(m_selectedAircraft);
916 opt->addOption("aircraft-dir", setFileInfo.dir().absolutePath().toStdString());
917 QString setFile = setFileInfo.fileName();
918 Q_ASSERT(setFile.endsWith("-set.xml"));
919 setFile.truncate(setFile.count() - 8); // drop the '-set.xml' portion
920 opt->addOption("aircraft", setFile.toStdString());
922 // manage aircraft history
923 if (m_recentAircraft.contains(m_selectedAircraft))
924 m_recentAircraft.removeOne(m_selectedAircraft);
925 m_recentAircraft.prepend(m_selectedAircraft);
926 if (m_recentAircraft.size() > MAX_RECENT_AIRCRAFT)
927 m_recentAircraft.pop_back();
930 // airport / location
931 if (m_selectedAirport) {
932 opt->addOption("airport", m_selectedAirport->ident());
935 if (m_ui->runwayRadio->isChecked()) {
936 int index = m_ui->runwayCombo->currentData().toInt();
937 if ((index >= 0) && m_selectedAirport) {
938 // explicit runway choice
939 opt->addOption("runway", m_selectedAirport->getRunwayByIndex(index)->ident());
942 if (m_ui->onFinalCheckbox->isChecked()) {
943 opt->addOption("glideslope", "3.0");
944 opt->addOption("offset-distance", "10.0"); // in nautical miles
946 } else if (m_ui->parkingRadio->isChecked()) {
948 opt->addOption("parkpos", m_ui->parkingCombo->currentText().toStdString());
952 if (m_ui->timeOfDayCombo->currentIndex() != 0) {
953 QString dayval = m_ui->timeOfDayCombo->currentText().toLower();
954 opt->addOption("timeofday", dayval.toStdString());
957 if (m_ui->seasonCombo->currentIndex() != 0) {
958 QString dayval = m_ui->timeOfDayCombo->currentText().toLower();
959 opt->addOption("season", dayval.toStdString());
963 for (int i=0; i<m_ui->sceneryPathsList->count(); ++i) {
964 QString path = m_ui->sceneryPathsList->item(i)->text();
965 opt->addOption("fg-scenery", path.toStdString());
968 // additional arguments
969 ArgumentsTokenizer tk;
970 Q_FOREACH(ArgumentsTokenizer::Arg a, tk.tokenize(m_ui->commandLineArgs->toPlainText())) {
971 if (a.arg.startsWith("prop:")) {
972 QString v = a.arg.mid(5) + "=" + a.value;
973 opt->addOption("prop", v.toStdString());
975 opt->addOption(a.arg.toStdString(), a.value.toStdString());
982 void QtLauncher::onQuit()
987 void QtLauncher::onSearchAirports()
989 QString search = m_ui->airportEdit->text();
990 m_airportsModel->setSearch(search);
992 if (m_airportsModel->isSearchActive()) {
993 m_ui->searchStatusText->setText(QString("Searching for '%1'").arg(search));
994 m_ui->locationStack->setCurrentIndex(2);
995 } else if (m_airportsModel->rowCount(QModelIndex()) == 1) {
996 QString ident = m_airportsModel->firstIdent();
997 setAirport(FGAirport::findByIdent(ident.toStdString()));
998 m_ui->locationStack->setCurrentIndex(0);
1002 void QtLauncher::onAirportSearchComplete()
1004 int numResults = m_airportsModel->rowCount(QModelIndex());
1005 if (numResults == 0) {
1006 m_ui->searchStatusText->setText(QString("No matching airports for '%1'").arg(m_ui->airportEdit->text()));
1007 } else if (numResults == 1) {
1008 QString ident = m_airportsModel->firstIdent();
1009 setAirport(FGAirport::findByIdent(ident.toStdString()));
1010 m_ui->locationStack->setCurrentIndex(0);
1012 m_ui->locationStack->setCurrentIndex(1);
1016 void QtLauncher::onAirportChanged()
1018 m_ui->runwayCombo->setEnabled(m_selectedAirport);
1019 m_ui->parkingCombo->setEnabled(m_selectedAirport);
1020 m_ui->airportDiagram->setAirport(m_selectedAirport);
1022 m_ui->runwayRadio->setChecked(true); // default back to runway mode
1023 // unelss multiplayer is enabled ?
1025 if (!m_selectedAirport) {
1026 m_ui->airportDescription->setText(QString());
1027 m_ui->airportDiagram->setEnabled(false);
1031 m_ui->airportDiagram->setEnabled(true);
1033 m_ui->runwayCombo->clear();
1034 m_ui->runwayCombo->addItem("Automatic", -1);
1035 for (unsigned int r=0; r<m_selectedAirport->numRunways(); ++r) {
1036 FGRunwayRef rwy = m_selectedAirport->getRunwayByIndex(r);
1037 // add runway with index as data role
1038 m_ui->runwayCombo->addItem(QString::fromStdString(rwy->ident()), r);
1040 m_ui->airportDiagram->addRunway(rwy);
1043 m_ui->parkingCombo->clear();
1044 FGAirportDynamics* dynamics = m_selectedAirport->getDynamics();
1045 PositionedIDVec parkings = NavDataCache::instance()->airportItemsOfType(
1046 m_selectedAirport->guid(),
1047 FGPositioned::PARKING);
1048 if (parkings.empty()) {
1049 m_ui->parkingCombo->setEnabled(false);
1050 m_ui->parkingRadio->setEnabled(false);
1052 m_ui->parkingCombo->setEnabled(true);
1053 m_ui->parkingRadio->setEnabled(true);
1054 Q_FOREACH(PositionedID parking, parkings) {
1055 FGParking* park = dynamics->getParking(parking);
1056 m_ui->parkingCombo->addItem(QString::fromStdString(park->getName()),
1057 static_cast<qlonglong>(parking));
1059 m_ui->airportDiagram->addParking(park);
1064 void QtLauncher::updateAirportDescription()
1066 if (!m_selectedAirport) {
1067 m_ui->airportDescription->setText(QString("No airport selected"));
1071 QString ident = QString::fromStdString(m_selectedAirport->ident()),
1072 name = QString::fromStdString(m_selectedAirport->name());
1073 QString locationOnAirport;
1074 if (m_ui->runwayRadio->isChecked()) {
1075 bool onFinal = m_ui->onFinalCheckbox->isChecked();
1076 QString runwayName = (m_ui->runwayCombo->currentIndex() == 0) ?
1078 QString("runway %1").arg(m_ui->runwayCombo->currentText());
1081 locationOnAirport = QString("on 10-mile final to %1").arg(runwayName);
1083 locationOnAirport = QString("on %1").arg(runwayName);
1085 } else if (m_ui->parkingRadio->isChecked()) {
1086 locationOnAirport = QString("at parking position %1").arg(m_ui->parkingCombo->currentText());
1089 m_ui->airportDescription->setText(QString("%2 (%1): %3").arg(ident).arg(name).arg(locationOnAirport));
1092 void QtLauncher::onAirportChoiceSelected(const QModelIndex& index)
1094 m_ui->locationStack->setCurrentIndex(0);
1095 setAirport(FGPositioned::loadById<FGAirport>(index.data(Qt::UserRole).toULongLong()));
1098 void QtLauncher::onAircraftSelected(const QModelIndex& index)
1100 m_selectedAircraft = index.data(AircraftPathRole).toString();
1101 updateSelectedAircraft();
1104 void QtLauncher::updateSelectedAircraft()
1107 QFileInfo info(m_selectedAircraft);
1108 AircraftItem item(info.dir(), m_selectedAircraft);
1109 m_ui->thumbnail->setPixmap(item.thumbnail);
1110 m_ui->aircraftDescription->setText(item.description);
1111 } catch (sg_exception& e) {
1112 m_ui->thumbnail->setPixmap(QPixmap());
1113 m_ui->aircraftDescription->setText("");
1117 void QtLauncher::onPopupAirportHistory()
1119 if (m_recentAirports.isEmpty()) {
1124 Q_FOREACH(QString aptCode, m_recentAirports) {
1125 FGAirportRef apt = FGAirport::findByIdent(aptCode.toStdString());
1126 QString name = QString::fromStdString(apt->name());
1127 QAction* act = m.addAction(QString("%1 - %2").arg(aptCode).arg(name));
1128 act->setData(aptCode);
1131 QPoint popupPos = m_ui->airportHistory->mapToGlobal(m_ui->airportHistory->rect().bottomLeft());
1132 QAction* triggered = m.exec(popupPos);
1134 FGAirportRef apt = FGAirport::findByIdent(triggered->data().toString().toStdString());
1136 m_ui->airportEdit->clear();
1137 m_ui->locationStack->setCurrentIndex(0);
1141 QModelIndex QtLauncher::proxyIndexForAircraftPath(QString path) const
1143 return m_aircraftProxy->mapFromSource(sourceIndexForAircraftPath(path));
1146 QModelIndex QtLauncher::sourceIndexForAircraftPath(QString path) const
1148 AircraftItemModel* sourceModel = qobject_cast<AircraftItemModel*>(m_aircraftProxy->sourceModel());
1149 Q_ASSERT(sourceModel);
1150 return sourceModel->indexOfAircraftPath(path);
1153 void QtLauncher::onPopupAircraftHistory()
1155 if (m_recentAircraft.isEmpty()) {
1160 Q_FOREACH(QString path, m_recentAircraft) {
1161 QModelIndex index = sourceIndexForAircraftPath(path);
1162 if (!index.isValid()) {
1166 QAction* act = m.addAction(index.data(Qt::DisplayRole).toString());
1170 QPoint popupPos = m_ui->aircraftHistory->mapToGlobal(m_ui->aircraftHistory->rect().bottomLeft());
1171 QAction* triggered = m.exec(popupPos);
1173 m_selectedAircraft = triggered->data().toString();
1174 QModelIndex index = proxyIndexForAircraftPath(m_selectedAircraft);
1175 m_ui->aircraftList->selectionModel()->setCurrentIndex(index,
1176 QItemSelectionModel::ClearAndSelect);
1177 m_ui->aircraftFilter->clear();
1178 updateSelectedAircraft();
1182 void QtLauncher::setAirport(FGAirportRef ref)
1184 if (m_selectedAirport == ref)
1187 m_selectedAirport = ref;
1191 // maintain the recent airport list
1192 QString icao = QString::fromStdString(ref->ident());
1193 if (m_recentAirports.contains(icao)) {
1195 m_recentAirports.removeOne(icao);
1196 m_recentAirports.push_front(icao);
1198 // insert and trim list if necessary
1199 m_recentAirports.push_front(icao);
1200 if (m_recentAirports.size() > MAX_RECENT_AIRPORTS) {
1201 m_recentAirports.pop_back();
1206 updateAirportDescription();
1209 void QtLauncher::onOpenCustomAircraftDir()
1211 QFileInfo info(m_customAircraftDir);
1212 if (!info.exists()) {
1213 int result = QMessageBox::question(this, "Create folder?",
1214 "The custom aircraft folder does not exist, create it now?",
1215 QMessageBox::Yes | QMessageBox::No,
1217 if (result == QMessageBox::No) {
1221 QDir d(m_customAircraftDir);
1222 d.mkpath(m_customAircraftDir);
1225 QUrl u = QUrl::fromLocalFile(m_customAircraftDir);
1226 QDesktopServices::openUrl(u);
1229 void QtLauncher::onEditRatingsFilter()
1231 EditRatingsFilterDialog dialog(this);
1232 dialog.setRatings(m_ratingFilters);
1235 if (dialog.result() == QDialog::Accepted) {
1237 for (int i=0; i<4; ++i) {
1238 m_ratingFilters[i] = dialog.getRating(i);
1239 vl.append(m_ratingFilters[i]);
1241 m_aircraftProxy->setRatings(m_ratingFilters);
1244 settings.setValue("min-ratings", vl);
1248 void QtLauncher::updateSettingsSummary()
1250 QStringList summary;
1251 if (m_ui->timeOfDayCombo->currentIndex() > 0) {
1252 summary.append(QString(m_ui->timeOfDayCombo->currentText().toLower()));
1255 if (m_ui->seasonCombo->currentIndex() > 0) {
1256 summary.append(QString(m_ui->seasonCombo->currentText().toLower()));
1259 if (m_ui->rembrandtCheckbox->isChecked()) {
1260 summary.append("Rembrandt enabled");
1261 } else if (m_ui->msaaCheckbox->isChecked()) {
1262 summary.append("anti-aliasing");
1265 if (m_ui->fetchRealWxrCheckbox->isChecked()) {
1266 summary.append("live weather");
1269 if (m_ui->terrasyncCheck->isChecked()) {
1270 summary.append("automatic scenery downloads");
1273 if (m_ui->startPausedCheck->isChecked()) {
1274 summary.append("paused");
1277 QString s = summary.join(", ");
1278 s[0] = s[0].toUpper();
1279 m_ui->settingsDescription->setText(s);
1282 void QtLauncher::onAddSceneryPath()
1284 QString path = QFileDialog::getExistingDirectory(this, tr("Choose scenery folder"));
1285 if (!path.isEmpty()) {
1286 m_ui->sceneryPathsList->addItem(path);
1291 void QtLauncher::onRemoveSceneryPath()
1293 if (m_ui->sceneryPathsList->currentItem()) {
1294 delete m_ui->sceneryPathsList->currentItem();
1299 void QtLauncher::onRembrandtToggled(bool b)
1301 // Rembrandt and multi-sample are exclusive
1302 m_ui->msaaCheckbox->setEnabled(!b);
1305 #include "QtLauncher.moc"