diff --git a/src/GUI/CMakeLists.txt b/src/GUI/CMakeLists.txt index c9afa8e12..d129fde49 100644 --- a/src/GUI/CMakeLists.txt +++ b/src/GUI/CMakeLists.txt @@ -78,6 +78,7 @@ if (HAVE_QT) LocationWidget.ui NoOfficialHangar.ui InstallSceneryDialog.ui + EditCustomMPServerDialog.ui ) qt5_add_resources(qrc_sources resources.qrc) @@ -113,6 +114,8 @@ if (HAVE_QT) QtFileDialog.hxx InstallSceneryDialog.hxx InstallSceneryDialog.cxx + EditCustomMPServerDialog.cxx + EditCustomMPServerDialog.hxx ${uic_sources} ${qrc_sources}) diff --git a/src/GUI/EditCustomMPServerDialog.cxx b/src/GUI/EditCustomMPServerDialog.cxx new file mode 100644 index 000000000..f7599ad6a --- /dev/null +++ b/src/GUI/EditCustomMPServerDialog.cxx @@ -0,0 +1,48 @@ +#include "EditCustomMPServerDialog.hxx" +#include "ui_EditCustomMPServerDialog.h" + +#include +#include + +#include "Main/fg_props.hxx" + +EditCustomMPServerDialog::EditCustomMPServerDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::EditCustomMPServerDialog) +{ + ui->setupUi(this); + QSettings settings; + ui->mpServer->setText(settings.value("mp-custom-host").toString()); + ui->port->setText(settings.value("mp-custom-port").toString()); +} + +EditCustomMPServerDialog::~EditCustomMPServerDialog() +{ + delete ui; +} + +QString EditCustomMPServerDialog::hostname() const +{ + return ui->mpServer->text(); +} + +void EditCustomMPServerDialog::accept() +{ + QSettings settings; + settings.setValue("mp-custom-host", ui->mpServer->text()); + settings.setValue("mp-custom-port", ui->port->text()); + QDialog::accept(); +} + +void EditCustomMPServerDialog::addCustomItem(QComboBox* combo) +{ + QSettings settings; + QString customMPHost = settings.value("mp-custom-host").toString(); + + if (customMPHost.isEmpty()) { + combo->addItem(tr("Custom server..."), "custom"); + return; + } + + combo->addItem(tr("Custom - %1").arg(customMPHost), "custom"); +} \ No newline at end of file diff --git a/src/GUI/EditCustomMPServerDialog.hxx b/src/GUI/EditCustomMPServerDialog.hxx new file mode 100644 index 000000000..e8fda64ba --- /dev/null +++ b/src/GUI/EditCustomMPServerDialog.hxx @@ -0,0 +1,29 @@ +#ifndef EDITCUSTOMMPSERVERDIALOG_HXX +#define EDITCUSTOMMPSERVERDIALOG_HXX + +#include + +namespace Ui { +class EditCustomMPServerDialog; +} + +class QComboBox; + +class EditCustomMPServerDialog : public QDialog +{ + Q_OBJECT + +public: + explicit EditCustomMPServerDialog(QWidget *parent = 0); + ~EditCustomMPServerDialog(); + + QString hostname() const; + + virtual void accept(); + + static void addCustomItem(QComboBox* combo); +private: + Ui::EditCustomMPServerDialog *ui; +}; + +#endif // EDITCUSTOMMPSERVERDIALOG_HXX diff --git a/src/GUI/EditCustomMPServerDialog.ui b/src/GUI/EditCustomMPServerDialog.ui new file mode 100644 index 000000000..ba1f25bf2 --- /dev/null +++ b/src/GUI/EditCustomMPServerDialog.ui @@ -0,0 +1,114 @@ + + + EditCustomMPServerDialog + + + + 0 + 0 + 369 + 172 + + + + Enter custom server + + + + + + Enter the host name and optional port of the multi-player server you wish to connect to. + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + true + + + + + + + Server: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + flightgear.example.com + + + + + + + Port: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + 5000 + + + 5000 + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + EditCustomMPServerDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + EditCustomMPServerDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/src/GUI/QtLauncher.cxx b/src/GUI/QtLauncher.cxx index f4a164804..fa63c190c 100644 --- a/src/GUI/QtLauncher.cxx +++ b/src/GUI/QtLauncher.cxx @@ -65,6 +65,7 @@ #include "AircraftItemDelegate.hxx" #include "AircraftModel.hxx" #include "PathsDialog.hxx" +#include "EditCustomMPServerDialog.hxx" #include
#include
@@ -308,7 +309,7 @@ protected: if (status == NoOfficialCatalogMessage) { return true; } - + if (!QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent)) { return false; } @@ -454,7 +455,7 @@ bool runLauncherDialog() // startup the HTTP system now since packages needs it FGHTTPClient* http = globals->add_new_subsystem(); - + // we guard against re-init in the global phase; bind and postinit // will happen as normal http->init(); @@ -577,6 +578,9 @@ QtLauncher::QtLauncher() : this, &QtLauncher::onToggleTerrasync); updateSettingsSummary(); + connect(m_ui->mpServerCombo, SIGNAL(activated(int)), + this, SLOT(onMPServerActivated(int))); + m_aircraftModel = new AircraftItemModel(this); m_aircraftProxy->setSourceModel(m_aircraftModel); @@ -846,8 +850,15 @@ void QtLauncher::onRun() if (m_ui->mpBox->isChecked()) { opt->addOption("callsign", m_ui->mpCallsign->text().toStdString()); QString host = m_ui->mpServerCombo->currentData().toString(); + int port = 5000; + if (host == "custom") { + QSettings settings; + host = settings.value("mp-custom-host").toString(); + port = settings.value("mp-custom-port").toInt(); + } else { + port = findMPServerPort(host.toStdString()); + } globals->get_props()->setStringValue("/sim/multiplay/txhost", host.toStdString()); - int port = findMPServerPort(host.toStdString()); globals->get_props()->setIntValue("/sim/multiplay/txport", port); } @@ -1240,7 +1251,7 @@ void QtLauncher::onDownloadDirChanged() m_aircraftModel->scanDirs(); checkOfficialCatalogMessage(); - + // re-set scenery dirs setSceneryPaths(); } @@ -1302,8 +1313,6 @@ void QtLauncher::onRefreshMPServersDone(simgear::HTTP::Request*) { // parse the properties SGPropertyNode *targetnode = fgGetNode("/sim/multiplay/server-list", true); - - m_ui->mpServerCombo->clear(); for (int i=0; inChildren(); ++i) { @@ -1318,13 +1327,8 @@ void QtLauncher::onRefreshMPServersDone(simgear::HTTP::Request*) m_ui->mpServerCombo->addItem(tr("%1 - %2").arg(name,loc), host); } - if (m_doRestoreMPServer) { - QSettings settings; - int index = m_ui->mpServerCombo->findData(settings.value("mp-server")); - if (index >= 0) { - m_ui->mpServerCombo->setCurrentIndex(index); - } - } + EditCustomMPServerDialog::addCustomItem(m_ui->mpServerCombo); + restoreMPServerSelection(); m_mpServerRequest.clear(); } @@ -1333,11 +1337,31 @@ void QtLauncher::onRefreshMPServersFailed(simgear::HTTP::Request*) { qWarning() << "refreshing MP servers failed:" << QString::fromStdString(m_mpServerRequest->responseReason()); m_mpServerRequest.clear(); + EditCustomMPServerDialog::addCustomItem(m_ui->mpServerCombo); + restoreMPServerSelection(); } -void QtLauncher::onMPServerEdited(QString text) +void QtLauncher::restoreMPServerSelection() { - // parse as server hostname + optional URL + if (m_doRestoreMPServer) { + QSettings settings; + int index = m_ui->mpServerCombo->findData(settings.value("mp-server")); + if (index >= 0) { + m_ui->mpServerCombo->setCurrentIndex(index); + } + m_doRestoreMPServer = false; + } +} + +void QtLauncher::onMPServerActivated(int index) +{ + if (m_ui->mpServerCombo->itemData(index) == "custom") { + EditCustomMPServerDialog dlg(this); + dlg.exec(); + if (dlg.result() == QDialog::Accepted) { + m_ui->mpServerCombo->setItemText(index, tr("Custom - %1").arg(dlg.hostname())); + } + } } int QtLauncher::findMPServerPort(const std::string& host) diff --git a/src/GUI/QtLauncher_private.hxx b/src/GUI/QtLauncher_private.hxx index f56daffea..301c7abd8 100644 --- a/src/GUI/QtLauncher_private.hxx +++ b/src/GUI/QtLauncher_private.hxx @@ -67,7 +67,7 @@ private slots: // apply is used in-app, where we must set properties and trigger // a reset; setting command line options won't help us. void onApply(); - + void onQuit(); @@ -99,7 +99,7 @@ private slots: void onDownloadDirChanged(); void onRefreshMPServers(); - void onMPServerEdited(QString text); + void onMPServerActivated(int index); private: @@ -127,6 +127,7 @@ private: void onRefreshMPServersDone(simgear::HTTP::Request*); void onRefreshMPServersFailed(simgear::HTTP::Request*); int findMPServerPort(const std::string& host); + void restoreMPServerSelection(); // need to wait after a model reset before restoring selection and // scrolling, to give the view time it seems.