Introduce Clickable::activate() to put handling clicks in a single place
authorManuel Nickschas <sputnick@quassel-irc.org>
Tue, 25 Aug 2009 15:30:35 +0000 (17:30 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Tue, 25 Aug 2009 19:50:50 +0000 (21:50 +0200)
Moved out of ChatItem.

src/client/buffermodel.cpp
src/client/buffermodel.h
src/qtui/chatitem.cpp
src/uisupport/clickable.cpp
src/uisupport/clickable.h

index 21141ca..1ae0c2e 100644 (file)
@@ -102,6 +102,17 @@ void BufferModel::switchToBufferIndex(const QModelIndex &bufferIdx) {
   qWarning() << "BufferModel::switchToBufferIndex(const QModelIndex &):" << bufferIdx << "does not belong to BufferModel or NetworkModel";
 }
 
   qWarning() << "BufferModel::switchToBufferIndex(const QModelIndex &):" << bufferIdx << "does not belong to BufferModel or NetworkModel";
 }
 
+void BufferModel::switchToOrJoinBuffer(NetworkId networkId, const QString &name) {
+  BufferId bufId = Client::networkModel()->bufferId(networkId, name);
+  if(bufId.isValid()) {
+    QModelIndex targetIdx = Client::networkModel()->bufferIndex(bufId);
+    switchToBuffer(bufId);
+    if(!targetIdx.data(NetworkModel::ItemActiveRole).toBool())
+      Client::userInput(BufferInfo::fakeStatusBuffer(networkId), QString("/JOIN %1").arg(name));
+  } else
+    Client::userInput(BufferInfo::fakeStatusBuffer(networkId), QString("/JOIN %1").arg(name));
+}
+
 void BufferModel::debug_currentChanged(QModelIndex current, QModelIndex previous) {
   Q_UNUSED(previous);
   qDebug() << "Switched current Buffer: " << current << current.data().toString() << "Buffer:" << current.data(NetworkModel::BufferIdRole).value<BufferId>();
 void BufferModel::debug_currentChanged(QModelIndex current, QModelIndex previous) {
   Q_UNUSED(previous);
   qDebug() << "Switched current Buffer: " << current << current.data().toString() << "Buffer:" << current.data(NetworkModel::BufferIdRole).value<BufferId>();
index 578481b..9a94fbe 100644 (file)
@@ -38,10 +38,10 @@ public:
   BufferModel(NetworkModel *parent = 0);
 
   bool filterAcceptsRow(int sourceRow, const QModelIndex &parent) const;
   BufferModel(NetworkModel *parent = 0);
 
   bool filterAcceptsRow(int sourceRow, const QModelIndex &parent) const;
-  
+
   inline const SelectionModelSynchronizer *selectionModelSynchronizer() const { return &_selectionModelSynchronizer; }
   inline QItemSelectionModel *standardSelectionModel() const { return _selectionModelSynchronizer.selectionModel(); }
   inline const SelectionModelSynchronizer *selectionModelSynchronizer() const { return &_selectionModelSynchronizer; }
   inline QItemSelectionModel *standardSelectionModel() const { return _selectionModelSynchronizer.selectionModel(); }
-  
+
   inline void synchronizeSelectionModel(QItemSelectionModel *selectionModel) { _selectionModelSynchronizer.synchronizeSelectionModel(selectionModel); }
   void synchronizeView(QAbstractItemView *view);
 
   inline void synchronizeSelectionModel(QItemSelectionModel *selectionModel) { _selectionModelSynchronizer.synchronizeSelectionModel(selectionModel); }
   void synchronizeView(QAbstractItemView *view);
 
@@ -51,12 +51,13 @@ public slots:
   void setCurrentIndex(const QModelIndex &newCurrent);
   void switchToBuffer(const BufferId &bufferId);
   void switchToBufferIndex(const QModelIndex &bufferIdx);
   void setCurrentIndex(const QModelIndex &newCurrent);
   void switchToBuffer(const BufferId &bufferId);
   void switchToBufferIndex(const QModelIndex &bufferIdx);
-                                                       
+  void switchToOrJoinBuffer(NetworkId network, const QString &bufferName);
+
 private slots:
   void debug_currentChanged(QModelIndex current, QModelIndex previous);
   void newNetwork(NetworkId id);
   void networkConnectionChanged(Network::ConnectionState state);
 private slots:
   void debug_currentChanged(QModelIndex current, QModelIndex previous);
   void newNetwork(NetworkId id);
   void networkConnectionChanged(Network::ConnectionState state);
-    
+
 private:
   SelectionModelSynchronizer _selectionModelSynchronizer;
 };
 private:
   SelectionModelSynchronizer _selectionModelSynchronizer;
 };
index 499ae77..8ff441c 100644 (file)
@@ -458,13 +458,7 @@ void ContentsChatItem::doLayout(QTextLayout *layout) const {
 }
 
 Clickable ContentsChatItem::clickableAt(const QPointF &pos) const {
 }
 
 Clickable ContentsChatItem::clickableAt(const QPointF &pos) const {
-  qint16 idx = posToCursor(pos);
-  for(int i = 0; i < privateData()->clickables.count(); i++) {
-    Clickable click = privateData()->clickables.at(i);
-    if(idx >= click.start() && idx < click.start() + click.length())
-      return click;
-  }
-  return Clickable();
+  return privateData()->clickables.atCursorPos(posToCursor(pos));
 }
 
 UiStyle::FormatList ContentsChatItem::formatList() const {
 }
 
 UiStyle::FormatList ContentsChatItem::formatList() const {
@@ -505,30 +499,12 @@ void ContentsChatItem::endHoverMode() {
 
 void ContentsChatItem::handleClick(const QPointF &pos, ChatScene::ClickMode clickMode) {
   if(clickMode == ChatScene::SingleClick) {
 
 void ContentsChatItem::handleClick(const QPointF &pos, ChatScene::ClickMode clickMode) {
   if(clickMode == ChatScene::SingleClick) {
-    Clickable click = clickableAt(pos);
-    if(click.isValid()) {
-      QString str = data(ChatLineModel::DisplayRole).toString().mid(click.start(), click.length());
-      switch(click.type()) {
-        case Clickable::Url:
-          if(!str.contains("://"))
-            str = "http://" + str;
-          QDesktopServices::openUrl(QUrl::fromEncoded(str.toUtf8(), QUrl::TolerantMode));
-          break;
-        case Clickable::Channel: {
-          NetworkId networkId = Client::networkModel()->networkId(data(MessageModel::BufferIdRole).value<BufferId>());
-          BufferId bufId = Client::networkModel()->bufferId(networkId, str);
-          if(bufId.isValid()) {
-            QModelIndex targetIdx = Client::networkModel()->bufferIndex(bufId);
-            Client::bufferModel()->switchToBuffer(bufId);
-            if(!targetIdx.data(NetworkModel::ItemActiveRole).toBool())
-              Client::userInput(BufferInfo::fakeStatusBuffer(networkId), QString("/JOIN %1").arg(str));
-          } else
-              Client::userInput(BufferInfo::fakeStatusBuffer(networkId), QString("/JOIN %1").arg(str));
-          break;
-        }
-        default:
-          break;
-      }
+    qint16 idx = posToCursor(pos);
+    Clickable foo = privateData()->clickables.atCursorPos(idx);
+    if(foo.isValid()) {
+      NetworkId networkId = Client::networkModel()->networkId(data(MessageModel::BufferIdRole).value<BufferId>());
+      QString text = data(ChatLineModel::DisplayRole).toString();
+      foo.activate(networkId, text);
     }
   } else if(clickMode == ChatScene::DoubleClick) {
     chatScene()->setSelectingItem(this);
     }
   } else if(clickMode == ChatScene::DoubleClick) {
     chatScene()->setSelectingItem(this);
index d19083c..54fa9b8 100644 (file)
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
 
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
 
+#include <QDesktopServices>
+#include <QModelIndex>
+#include <QUrl>
+
+#include "buffermodel.h"
 #include "clickable.h"
 #include "clickable.h"
+#include "client.h"
+
+void Clickable::activate(NetworkId networkId, const QString &text) const {
+  if(!isValid())
+    return;
+
+  QString str = text.mid(start(), length());
+
+  switch(type()) {
+    case Clickable::Url:
+      if(!str.contains("://"))
+        str = "http://" + str;
+      QDesktopServices::openUrl(QUrl::fromEncoded(str.toUtf8(), QUrl::TolerantMode));
+      break;
+    case Clickable::Channel:
+      Client::bufferModel()->switchToOrJoinBuffer(networkId, str);
+      break;
+    default:
+      break;
+  }
+}
 
 // NOTE: This method is not threadsafe and not reentrant!
 //       (RegExps are not constant while matching, and they are static here for efficiency)
 
 // NOTE: This method is not threadsafe and not reentrant!
 //       (RegExps are not constant while matching, and they are static here for efficiency)
@@ -84,3 +110,11 @@ ClickableList ClickableList::fromString(const QString &str) {
   } while(type >= 0);
   return result;
 }
   } while(type >= 0);
   return result;
 }
+
+Clickable ClickableList::atCursorPos(int idx) {
+  foreach(const Clickable &click, *this) {
+    if(idx >= click.start() && idx < click.start() + click.length())
+      return click;
+  }
+  return Clickable();
+}
index ce8d9a6..eb89a36 100644 (file)
 
 #include <QStackedWidget>
 
 
 #include <QStackedWidget>
 
+#include "types.h"
+
+class QModelIndex;
+
 class Clickable {
 
 public:
 class Clickable {
 
 public:
@@ -44,6 +48,8 @@ public:
 
   inline bool isValid() const { return _type != Invalid; }
 
 
   inline bool isValid() const { return _type != Invalid; }
 
+  void activate(NetworkId networkId, const QString &bufferName) const;
+
 private:
   Type _type;
   quint16 _start;
 private:
   Type _type;
   quint16 _start;
@@ -55,6 +61,8 @@ class ClickableList : public QList<Clickable> {
 public:
   static ClickableList fromString(const QString &);
 
 public:
   static ClickableList fromString(const QString &);
 
+  Clickable atCursorPos(int idx);
+
 };
 
 #endif // CLICKABLE_H_
 };
 
 #endif // CLICKABLE_H_