]> git.mxchange.org Git - flightgear.git/commitdiff
Custom MP server support.
authorJames Turner <zakalawe@mac.com>
Fri, 8 Jul 2016 08:36:42 +0000 (09:36 +0100)
committerRoland Haeder <roland@mxchange.org>
Thu, 22 Sep 2016 21:27:47 +0000 (23:27 +0200)
src/GUI/CMakeLists.txt
src/GUI/EditCustomMPServerDialog.cxx [new file with mode: 0644]
src/GUI/EditCustomMPServerDialog.hxx [new file with mode: 0644]
src/GUI/EditCustomMPServerDialog.ui [new file with mode: 0644]
src/GUI/QtLauncher.cxx
src/GUI/QtLauncher_private.hxx

index c9afa8e12d8902dd667189cf4650381f4b7b6587..d129fde494e04f910f50a69affc7de82979301b7 100644 (file)
@@ -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 (file)
index 0000000..f7599ad
--- /dev/null
@@ -0,0 +1,48 @@
+#include "EditCustomMPServerDialog.hxx"
+#include "ui_EditCustomMPServerDialog.h"
+
+#include <QSettings>
+#include <QComboBox>
+
+#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 (file)
index 0000000..e8fda64
--- /dev/null
@@ -0,0 +1,29 @@
+#ifndef EDITCUSTOMMPSERVERDIALOG_HXX
+#define EDITCUSTOMMPSERVERDIALOG_HXX
+
+#include <QDialog>
+
+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 (file)
index 0000000..ba1f25b
--- /dev/null
@@ -0,0 +1,114 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>EditCustomMPServerDialog</class>
+ <widget class="QDialog" name="EditCustomMPServerDialog">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>369</width>
+    <height>172</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Enter custom server</string>
+  </property>
+  <layout class="QGridLayout" name="gridLayout" columnstretch="1,1">
+   <item row="0" column="0" colspan="2">
+    <widget class="QLabel" name="label_3">
+     <property name="text">
+      <string>Enter the host name and optional port of the multi-player server you wish to connect to.</string>
+     </property>
+     <property name="alignment">
+      <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
+     </property>
+     <property name="wordWrap">
+      <bool>true</bool>
+     </property>
+    </widget>
+   </item>
+   <item row="1" column="0">
+    <widget class="QLabel" name="label">
+     <property name="text">
+      <string>Server:</string>
+     </property>
+     <property name="alignment">
+      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+     </property>
+    </widget>
+   </item>
+   <item row="1" column="1">
+    <widget class="QLineEdit" name="mpServer">
+     <property name="placeholderText">
+      <string>flightgear.example.com</string>
+     </property>
+    </widget>
+   </item>
+   <item row="2" column="0">
+    <widget class="QLabel" name="label_2">
+     <property name="text">
+      <string>Port:</string>
+     </property>
+     <property name="alignment">
+      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+     </property>
+    </widget>
+   </item>
+   <item row="2" column="1">
+    <widget class="QLineEdit" name="port">
+     <property name="text">
+      <string>5000</string>
+     </property>
+     <property name="placeholderText">
+      <string>5000</string>
+     </property>
+    </widget>
+   </item>
+   <item row="3" column="0" colspan="2">
+    <widget class="QDialogButtonBox" name="buttonBox">
+     <property name="orientation">
+      <enum>Qt::Horizontal</enum>
+     </property>
+     <property name="standardButtons">
+      <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
+     </property>
+    </widget>
+   </item>
+  </layout>
+ </widget>
+ <resources/>
+ <connections>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>accepted()</signal>
+   <receiver>EditCustomMPServerDialog</receiver>
+   <slot>accept()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>248</x>
+     <y>254</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>157</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>rejected()</signal>
+   <receiver>EditCustomMPServerDialog</receiver>
+   <slot>reject()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>316</x>
+     <y>260</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>286</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+ </connections>
+</ui>
index f4a164804878fb7e7e8a51aa5bdc9256231bb881..fa63c190cff7f9efd0d309015aacad452bff62da 100644 (file)
@@ -65,6 +65,7 @@
 #include "AircraftItemDelegate.hxx"
 #include "AircraftModel.hxx"
 #include "PathsDialog.hxx"
+#include "EditCustomMPServerDialog.hxx"
 
 #include <Main/globals.hxx>
 #include <Main/fg_props.hxx>
@@ -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<FGHTTPClient>();
-    
+
     // 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; i<targetnode->nChildren(); ++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::restoreMPServerSelection()
+{
+    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::onMPServerEdited(QString text)
+void QtLauncher::onMPServerActivated(int index)
 {
-    // parse as server hostname + optional URL
+    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)
index f56daffea0e07d5a6adac3f044e503a2406baccd..301c7abd806c63f15f4944dfe783b842d76aa5ae 100644 (file)
@@ -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.