Add more fancy dialog for channel join
authorManuel Nickschas <sputnick@quassel-irc.org>
Thu, 5 Feb 2009 08:03:08 +0000 (09:03 +0100)
committerManuel Nickschas <sputnick@quassel-irc.org>
Fri, 6 Feb 2009 00:26:32 +0000 (01:26 +0100)
src/uisupport/networkmodelcontroller.cpp
src/uisupport/networkmodelcontroller.h

index 3d56b1a..626d495 100644 (file)
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
 
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
 
+#include <QComboBox>
+#include <QDialogButtonBox>
+#include <QGridLayout>
+#include <QLabel>
+#include <QLineEdit>
 #include <QInputDialog>
 #include <QMessageBox>
 #include <QInputDialog>
 #include <QMessageBox>
+#include <QPushButton>
 
 #include "networkmodelcontroller.h"
 
 
 #include "networkmodelcontroller.h"
 
@@ -45,11 +51,11 @@ NetworkModelController::~NetworkModelController() {
 
 }
 
 
 }
 
-void NetworkModelController::registerAction(ActionType type, const QString &text, bool checkable) {
-  registerAction(type, QPixmap(), text, checkable);
+Action * NetworkModelController::registerAction(ActionType type, const QString &text, bool checkable) {
+  return registerAction(type, QPixmap(), text, checkable);
 }
 
 }
 
-void NetworkModelController::registerAction(ActionType type, const QPixmap &icon, const QString &text, bool checkable) {
+Action * NetworkModelController::registerAction(ActionType type, const QPixmap &icon, const QString &text, bool checkable) {
   Action *act;
   if(icon.isNull())
     act = new Action(text, this);
   Action *act;
   if(icon.isNull())
     act = new Action(text, this);
@@ -61,6 +67,7 @@ void NetworkModelController::registerAction(ActionType type, const QPixmap &icon
 
   _actionCollection->addAction(QString::number(type, 16), act);
   _actionByType[type] = act;
 
   _actionCollection->addAction(QString::number(type, 16), act);
   _actionByType[type] = act;
+  return act;
 }
 
 /******** Helper Functions ***********************************************************************/
 }
 
 /******** Helper Functions ***********************************************************************/
@@ -189,8 +196,20 @@ void NetworkModelController::actionTriggered(QAction *action) {
 }
 
 void NetworkModelController::handleNetworkAction(ActionType type, QAction *) {
 }
 
 void NetworkModelController::handleNetworkAction(ActionType type, QAction *) {
+  if(type == NetworkConnectAll || type == NetworkDisconnectAll) {
+    foreach(NetworkId id, Client::networkIds()) {
+      const Network *net = Client::network(id);
+      if(type == NetworkConnectAll && net->connectionState() == Network::Disconnected)
+        net->requestConnect();
+      if(type == NetworkDisconnectAll && net->connectionState() != Network::Disconnected)
+        net->requestDisconnect();
+    }
+    return;
+  }
+
   if(!indexList().count())
     return;
   if(!indexList().count())
     return;
+
   const Network *network = Client::network(indexList().at(0).data(NetworkModel::NetworkIdRole).value<NetworkId>());
   Q_CHECK_PTR(network);
   if(!network)
   const Network *network = Client::network(indexList().at(0).data(NetworkModel::NetworkIdRole).value<NetworkId>());
   Q_CHECK_PTR(network);
   if(!network)
@@ -305,10 +324,11 @@ void NetworkModelController::handleGeneralAction(ActionType type, QAction *actio
     case JoinChannel: {
       QString channelName = contextItem();
       if(channelName.isEmpty()) {
     case JoinChannel: {
       QString channelName = contextItem();
       if(channelName.isEmpty()) {
-        bool ok;
-        channelName = QInputDialog::getText(0, tr("Join Channel"), tr("Input channel name:"), QLineEdit::Normal, QString(), &ok);
-        if(!ok)
-          return;
+        JoinDlg dlg(networkId);
+        if(dlg.exec() == QDialog::Accepted) {
+          channelName = dlg.channelName();
+          networkId = dlg.networkId();
+        }
       }
       if(!channelName.isEmpty()) {
         Client::instance()->userInput(BufferInfo::fakeStatusBuffer(networkId), QString("/JOIN %1").arg(channelName));
       }
       if(!channelName.isEmpty()) {
         Client::instance()->userInput(BufferInfo::fakeStatusBuffer(networkId), QString("/JOIN %1").arg(channelName));
@@ -387,3 +407,55 @@ void NetworkModelController::handleNickAction(ActionType type, QAction *) {
     }
   }
 }
     }
   }
 }
+
+/***************************************************************************************************************
+ * JoinDlg
+ ***************************************************************************************************************/
+
+NetworkModelController::JoinDlg::JoinDlg(NetworkId defaultId, QWidget *parent) : QDialog(parent) {
+  setWindowIcon(SmallIcon("irc-join-channel"));
+  setWindowTitle(tr("Join Channel"));
+
+  QGridLayout *layout = new QGridLayout(this);
+  layout->addWidget(new QLabel(tr("Network:")), 0, 0);
+  layout->addWidget(networks = new QComboBox, 0, 1);
+  layout->addWidget(new QLabel(tr("Channel:")), 1, 0);
+  layout->addWidget(channel = new QLineEdit, 1, 1);
+  layout->addWidget(buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel), 2, 0, 1, 2);
+  setLayout(layout);
+
+  channel->setFocus();
+  buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
+  networks->setInsertPolicy(QComboBox::InsertAlphabetically);
+
+  connect(buttonBox, SIGNAL(accepted()), SLOT(accept()));
+  connect(buttonBox, SIGNAL(rejected()), SLOT(reject()));
+  connect(channel, SIGNAL(textChanged(QString)), SLOT(on_channel_textChanged(QString)));
+
+
+  QString defaultName;
+  foreach(NetworkId id, Client::networkIds()) {
+    const Network *net = Client::network(id);
+    if(net->isConnected()) {
+      networks->addItem(net->networkName(), QVariant::fromValue<NetworkId>(id));
+      if(id == defaultId)
+        defaultName = net->networkName();
+    }
+  }
+
+  if(!defaultName.isEmpty())
+    networks->setCurrentIndex(networks->findText(defaultName));
+
+}
+
+NetworkId NetworkModelController::JoinDlg::networkId() const {
+  return networks->itemData(networks->currentIndex()).value<NetworkId>();
+}
+
+QString NetworkModelController::JoinDlg::channelName() const {
+  return channel->text();
+}
+
+void NetworkModelController::JoinDlg::on_channel_textChanged(const QString &text) {
+  buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!text.isEmpty());
+}
index 459d387..4333a0c 100644 (file)
 #ifndef NETWORKMODELCONTROLLER_H_
 #define NETWORKMODELCONTROLLER_H_
 
 #ifndef NETWORKMODELCONTROLLER_H_
 #define NETWORKMODELCONTROLLER_H_
 
+#include <QDialog>
+
 #include "action.h"
 #include "actioncollection.h"
 #include "messagefilter.h"
 
 #include "action.h"
 #include "actioncollection.h"
 #include "messagefilter.h"
 
+class QComboBox;
+class QDialogButtonBox;
+class QLineEdit;
+
 class NetworkModelController : public QObject {
   Q_OBJECT
 
 class NetworkModelController : public QObject {
   Q_OBJECT
 
@@ -38,6 +44,8 @@ public:
     NetworkMask = 0x0f,
     NetworkConnect = 0x01,
     NetworkDisconnect = 0x02,
     NetworkMask = 0x0f,
     NetworkConnect = 0x01,
     NetworkDisconnect = 0x02,
+    NetworkConnectAll = 0x03,
+    NetworkDisconnectAll = 0x04,
 
     // Buffer actions
     BufferMask = 0xf0,
 
     // Buffer actions
     BufferMask = 0xf0,
@@ -110,8 +118,8 @@ protected:
   void setContextItem(const QString &);
   void setSlot(QObject *receiver, const char *method);
 
   void setContextItem(const QString &);
   void setSlot(QObject *receiver, const char *method);
 
-  void registerAction(ActionType type, const QString &text, bool checkable = false);
-  void registerAction(ActionType type, const QPixmap &icon, const QString &text, bool checkable = false);
+  Action * registerAction(ActionType type, const QString &text, bool checkable = false);
+  Action * registerAction(ActionType type, const QPixmap &icon, const QString &text, bool checkable = false);
   bool checkRequirements(const QModelIndex &index, ItemActiveStates requiredActiveState = QFlags<ItemActiveState>(ActiveState | InactiveState));
 
   QString nickName(const QModelIndex &index) const;
   bool checkRequirements(const QModelIndex &index, ItemActiveStates requiredActiveState = QFlags<ItemActiveState>(ActiveState | InactiveState));
 
   QString nickName(const QModelIndex &index) const;
@@ -126,14 +134,17 @@ signals:
   void showChannelList(NetworkId);
   void showIgnoreList(NetworkId);
 
   void showChannelList(NetworkId);
   void showIgnoreList(NetworkId);
 
-private:
-  void handleNetworkAction(ActionType, QAction *);
-  void handleBufferAction(ActionType, QAction *);
-  void handleHideAction(ActionType, QAction *);
-  void handleNickAction(ActionType, QAction *);
-  void handleGeneralAction(ActionType, QAction *);
-  void handleExternalAction(ActionType, QAction *);
+protected:
+  virtual void handleNetworkAction(ActionType, QAction *);
+  virtual void handleBufferAction(ActionType, QAction *);
+  virtual void handleHideAction(ActionType, QAction *);
+  virtual void handleNickAction(ActionType, QAction *);
+  virtual void handleGeneralAction(ActionType, QAction *);
+  virtual void handleExternalAction(ActionType, QAction *);
 
 
+  class JoinDlg;
+
+private:
   NetworkModel *_model;
 
   ActionCollection *_actionCollection;
   NetworkModel *_model;
 
   ActionCollection *_actionCollection;
@@ -146,6 +157,26 @@ private:
   const char *_method;
 };
 
   const char *_method;
 };
 
+//! Input dialog for joining a channel
+class NetworkModelController::JoinDlg : public QDialog {
+  Q_OBJECT
+
+public:
+  JoinDlg(NetworkId id, QWidget *parent = 0);
+
+  QString channelName() const;
+  NetworkId networkId() const;
+
+private slots:
+  void on_channel_textChanged(const QString &);
+
+private:
+  QComboBox *networks;
+  QLineEdit *channel;
+  QDialogButtonBox *buttonBox;
+};
+
+
 // inlines
 ActionCollection *NetworkModelController::actionCollection() const { return _actionCollection; }
 Action *NetworkModelController::action(ActionType type) const { return _actionByType.value(type, 0); }
 // inlines
 ActionCollection *NetworkModelController::actionCollection() const { return _actionCollection; }
 Action *NetworkModelController::action(ActionType type) const { return _actionByType.value(type, 0); }