From cdc740c5d12cb7a5a133839e9fcd7e7812ca44c3 Mon Sep 17 00:00:00 2001 From: jerry Date: Tue, 26 Aug 2014 14:49:27 +0000 Subject: [PATCH] issue #65 - Auto config feature using Zeroconf/Bonjour [no-build] added massage box to ask user where to add client --- src/gui/res/MainWindowBase.ui | 2 +- src/gui/src/MainWindow.cpp | 10 ++-- src/gui/src/ServerConfig.cpp | 99 ++++++++++++++++++++++++++++++++--- src/gui/src/ServerConfig.h | 12 +++-- 4 files changed, 107 insertions(+), 16 deletions(-) diff --git a/src/gui/res/MainWindowBase.ui b/src/gui/res/MainWindowBase.ui index b6489244..02fdc723 100644 --- a/src/gui/res/MainWindowBase.ui +++ b/src/gui/res/MainWindowBase.ui @@ -291,7 +291,7 @@ - + Auto connect diff --git a/src/gui/src/MainWindow.cpp b/src/gui/src/MainWindow.cpp index fb884275..5f515145 100644 --- a/src/gui/src/MainWindow.cpp +++ b/src/gui/src/MainWindow.cpp @@ -65,7 +65,7 @@ MainWindow::MainWindow(QSettings& settings, AppConfig& appConfig) : m_AppConfig(appConfig), m_pSynergy(NULL), m_SynergyState(synergyDisconnected), - m_ServerConfig(&m_Settings, 5, 3, m_AppConfig.screenName()), + m_ServerConfig(&m_Settings, 5, 3, m_AppConfig.screenName(), this), m_pTempConfigFile(NULL), m_pTrayIcon(NULL), m_pTrayIconMenu(NULL), @@ -880,15 +880,17 @@ void MainWindow::autoAddScreen(const QString name) int r = m_ServerConfig.autoAddScreen(name); if (r != kAutoAddScreenOk) { switch (r) { - case kAutoAddScreenNoServer: + case kAutoAddScreenManualServer: showConfigureServer( tr("Please add the server (%1) to the grid.") .arg(appConfig().screenName())); break; - case kAutoAddScreenNoSpace: + case kAutoAddScreenManualClient: showConfigureServer( - tr("Please add the client (%1) to the grid.").arg(name)); + tr("Please drag the new client screen (%1) " + "to the desired position on the grid.") + .arg(name)); break; } } diff --git a/src/gui/src/ServerConfig.cpp b/src/gui/src/ServerConfig.cpp index 04174b3b..b9e2a2e5 100644 --- a/src/gui/src/ServerConfig.cpp +++ b/src/gui/src/ServerConfig.cpp @@ -18,8 +18,12 @@ #include "ServerConfig.h" #include "Hotkey.h" +#include "MainWindow.h" #include +#include +#include +#include static const struct { @@ -35,14 +39,23 @@ static const struct }; +enum { + kAddClientRight, + kAddClientLeft, + kAddClientOther, + kAddClientIgnore +}; + const int serverDefaultIndex = 7; -ServerConfig::ServerConfig(QSettings* settings, int numColumns, int numRows , QString serverName) : +ServerConfig::ServerConfig(QSettings* settings, int numColumns, int numRows , + QString serverName, MainWindow* mainWindow) : m_pSettings(settings), m_Screens(), m_NumColumns(numColumns), m_NumRows(numRows), - m_ServerName(serverName) + m_ServerName(serverName), + m_pMainWindow(mainWindow) { Q_ASSERT(m_pSettings); @@ -273,7 +286,7 @@ int ServerConfig::autoAddScreen(const QString name) int targetIndex = -1; if (!findScreenName(m_ServerName, serverIndex)) { if (!fixNoServer(m_ServerName, serverIndex)) { - return kAutoAddScreenNoServer; + return kAutoAddScreenManualServer; } } if (findScreenName(name, targetIndex)) { @@ -281,18 +294,44 @@ int ServerConfig::autoAddScreen(const QString name) return kAutoAddScreenOk; } + int result = showAddClientMsgBox(name); + + if (result == kAddClientIgnore) { + return kAutoAddScreenIgnore; + } + + if (result == kAddClientOther) { + addToFirstEmptyGrid(name); + return kAutoAddScreenManualClient; + } + bool success = false; - for (unsigned int i = 0; i < sizeof(neighbourDirs) / sizeof(neighbourDirs[0]); i++) { - int idx = adjacentScreenIndex(serverIndex, neighbourDirs[i].x, neighbourDirs[i].y); - if (idx != -1 && screens()[idx].isNull()) { + int startIndex = serverIndex; + int offset = 1; + int dirIndex = 0; + + if (result == kAddClientLeft) { + offset = -1; + dirIndex = 1; + } + + int idx = adjacentScreenIndex(startIndex, neighbourDirs[dirIndex].x, + neighbourDirs[dirIndex].y); + while (idx != -1) { + if (screens()[idx].isNull()) { m_Screens[idx].setName(name); success = true; break; } + + startIndex += offset; + idx = adjacentScreenIndex(startIndex, neighbourDirs[dirIndex].x, + neighbourDirs[dirIndex].y); } if (!success) { - return kAutoAddScreenNoSpace; + addToFirstEmptyGrid(name); + return kAutoAddScreenManualClient; } saveSettings(); @@ -303,7 +342,6 @@ bool ServerConfig::findScreenName(const QString& name, int& index) { bool found = false; for (int i = 0; i < screens().size(); i++) { - QString test = screens()[i].name(); if (!screens()[i].isNull() && screens()[i].name().compare(name) == 0) { index = i; @@ -325,3 +363,48 @@ bool ServerConfig::fixNoServer(const QString& name, int& index) return fixed; } + +int ServerConfig::showAddClientMsgBox(const QString& clientName) +{ + int result = kAddClientIgnore; + QWidget* w = dynamic_cast(m_pMainWindow); + QMessageBox msgBox(w); + msgBox.setIcon(QMessageBox::Question); + msgBox.setWindowTitle(QObject::tr("Incoming client")); + msgBox.setText(QObject::tr( + "A new client wants to connect. Which side\n" + "of this screen is the client (%1) located?") + .arg(clientName)); + + QPushButton* left = msgBox.addButton(QObject::tr("Left"), QMessageBox::AcceptRole); + QPushButton* right = msgBox.addButton(QObject::tr("Right"), QMessageBox::AcceptRole); + QPushButton* other = msgBox.addButton(QObject::tr("Other"), QMessageBox::AcceptRole); + QPushButton* ignore = msgBox.addButton(QObject::tr("Ignore"), QMessageBox::RejectRole); + msgBox.setDefaultButton(ignore); + + msgBox.exec(); + + QAbstractButton* button = msgBox.clickedButton(); + QPushButton* clickedButton = dynamic_cast(button); + if (clickedButton == right) { + result = kAddClientRight; + } + else if (clickedButton == left) { + result = kAddClientLeft; + } + else if (clickedButton == other) { + result = kAddClientOther; + } + + return result; +} + +void::ServerConfig::addToFirstEmptyGrid(const QString &clientName) +{ + for (int i = 0; i < screens().size(); i++) { + if (screens()[i].isNull()) { + m_Screens[i].setName(clientName); + break; + } + } +} diff --git a/src/gui/src/ServerConfig.h b/src/gui/src/ServerConfig.h index b3a48a4f..25d75d97 100644 --- a/src/gui/src/ServerConfig.h +++ b/src/gui/src/ServerConfig.h @@ -31,6 +31,7 @@ class QSettings; class QString; class QFile; class ServerConfigDialog; +class MainWindow; class ServerConfig : public BaseConfig { @@ -38,7 +39,8 @@ class ServerConfig : public BaseConfig friend QTextStream& operator<<(QTextStream& outStream, const ServerConfig& config); public: - ServerConfig(QSettings* settings, int numColumns, int numRows, QString serverName); + ServerConfig(QSettings* settings, int numColumns, int numRows, + QString serverName, MainWindow* mainWindow); ~ServerConfig(); public: @@ -93,6 +95,8 @@ class ServerConfig : public BaseConfig private: bool findScreenName(const QString& name, int& index); bool fixNoServer(const QString& name, int& index); + int showAddClientMsgBox(const QString& clientName); + void addToFirstEmptyGrid(const QString& clientName); private: QSettings* m_pSettings; @@ -112,14 +116,16 @@ class ServerConfig : public BaseConfig QList m_SwitchCorners; HotkeyList m_Hotkeys; QString m_ServerName; + MainWindow* m_pMainWindow; }; QTextStream& operator<<(QTextStream& outStream, const ServerConfig& config); enum { kAutoAddScreenOk, - kAutoAddScreenNoServer, - kAutoAddScreenNoSpace + kAutoAddScreenManualServer, + kAutoAddScreenManualClient, + kAutoAddScreenIgnore }; #endif