Make double and triple click selection work
authorManuel Nickschas <sputnick@quassel-irc.org>
Sat, 15 Nov 2008 19:51:25 +0000 (20:51 +0100)
committerManuel Nickschas <sputnick@quassel-irc.org>
Thu, 20 Nov 2008 14:14:53 +0000 (15:14 +0100)
src/qtui/chatitem.cpp
src/qtui/chatitem.h
src/qtui/chatscene.cpp

index 53bee45..ed24bba 100644 (file)
@@ -165,6 +165,13 @@ QString ChatItem::selection() const {
   return QString();
 }
 
+void ChatItem::setSelection(SelectionMode mode, qint16 start, qint16 end) {
+  _selectionMode = mode;
+  _selectionStart = start;
+  _selectionEnd = end;
+  update();
+}
+
 void ChatItem::setFullSelection() {
   if(_selectionMode != FullSelection) {
     _selectionMode = FullSelection;
@@ -173,8 +180,10 @@ void ChatItem::setFullSelection() {
 }
 
 void ChatItem::clearSelection() {
-  _selectionMode = NoSelection;
-  update();
+  if(_selectionMode != NoSelection) {
+    _selectionMode = NoSelection;
+    update();
+  }
 }
 
 void ChatItem::continueSelecting(const QPointF &pos) {
@@ -249,11 +258,6 @@ void ChatItem::handleClick(const QPointF &pos, ChatScene::ClickMode clickMode) {
     _selectionStart = _selectionEnd = posToCursor(pos);
     _selectionMode = NoSelection; // will be set to PartialSelection by mouseMoveEvent
     update();
-  } else if(clickMode == ChatScene::DoubleClick) {
-    //_selectionMode = PartialSelection;
-
-  } else if(clickMode == ChatScene::TripleClick) {
-
   }
 }
 
@@ -277,23 +281,20 @@ void ChatItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
 }
 
 void ChatItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
-  if(event->buttons() == Qt::LeftButton) {
+  if(event->buttons() == Qt::LeftButton)
     event->accept();
-  } else {
+  else
     event->ignore();
-  }
 }
 
 void ChatItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
   if(_selectionMode != NoSelection && !event->buttons() & Qt::LeftButton) {
-    _selectionEnd = posToCursor(event->pos());
     QString selection
         = data(MessageModel::DisplayRole).toString().mid(qMin(_selectionStart, _selectionEnd), qAbs(_selectionStart - _selectionEnd));
     chatScene()->putToClipboard(selection);
     event->accept();
-  } else {
+  } else
     event->ignore();
-  }
 }
 
 // ************************************************************
@@ -498,6 +499,26 @@ void ContentsChatItem::handleClick(const QPointF &pos, ChatScene::ClickMode clic
           break;
       }
     }
+  } else if(clickMode == ChatScene::DoubleClick) {
+    chatScene()->setSelectingItem(this);
+    setSelectionMode(PartialSelection);
+    Clickable click = privateData()->currentClickable;
+    if(click.isValid()) {
+      setSelectionStart(click.start);
+      setSelectionEnd(click.start + click.length);
+    } else {
+      // find word boundary
+      QString str = data(ChatLineModel::DisplayRole).toString();
+      qint16 cursor = posToCursor(pos);
+      qint16 start = str.lastIndexOf(QRegExp("\\W"), cursor) + 1;
+      qint16 end = qMin(str.indexOf(QRegExp("\\W"), cursor), str.length());
+      if(end < 0) end = str.length();
+      setSelectionStart(start);
+      setSelectionEnd(end);
+    }
+    update();
+  } else if(clickMode == ChatScene::TripleClick) {
+    setSelection(PartialSelection, 0, data(ChatLineModel::DisplayRole).toString().length());
   }
   ChatItem::handleClick(pos, clickMode);
 }
@@ -524,7 +545,7 @@ void ContentsChatItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
         showWebPreview(click);
       } else if(click.type == Clickable::Channel) {
         // TODO: don't make clickable if it's our own name
-        //onClickable = true; //FIXME disabled for now
+        // onClickable = true; //FIXME disabled for now
       }
       if(onClickable) {
         setCursor(Qt::PointingHandCursor);
index 0710b6a..3cb9c3d 100644 (file)
@@ -70,6 +70,12 @@ public:
   virtual void handleClick(const QPointF &pos, ChatScene::ClickMode);
 
 protected:
+  enum SelectionMode {
+    NoSelection,
+    PartialSelection,
+    FullSelection
+  };
+
   virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
   virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
   virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
@@ -79,6 +85,14 @@ protected:
   virtual QTextLayout::FormatRange selectionFormat() const;
   virtual inline QVector<QTextLayout::FormatRange> additionalFormats() const { return QVector<QTextLayout::FormatRange>(); }
 
+  inline qint16 selectionStart() const { return _selectionStart; }
+  inline void setSelectionStart(qint16 start) { _selectionStart = start; }
+  inline qint16 selectionEnd() const { return _selectionEnd; }
+  inline void setSelectionEnd(qint16 end) { _selectionEnd = end; }
+  inline SelectionMode selectionMode() const { return _selectionMode; }
+  inline void setSelectionMode(SelectionMode mode) { _selectionMode = mode; }
+  void setSelection(SelectionMode mode, qint16 selectionStart, qint16 selectionEnd);
+
   qint16 posToCursor(const QPointF &pos) const;
 
   inline bool hasPrivateData() const { return (bool)_data; }
@@ -106,7 +120,6 @@ private:
   ChatItemPrivate *_data;
   QRectF _boundingRect;
 
-  enum SelectionMode { NoSelection, PartialSelection, FullSelection };
   SelectionMode _selectionMode;
   qint16 _selectionStart, _selectionEnd;
 
@@ -229,7 +242,9 @@ struct ContentsChatItemPrivate : ChatItemPrivate {
 };
 
 //inlines regarding ContentsChatItemPrivate
-ChatItemPrivate *ContentsChatItem::newPrivateData() { return new ContentsChatItemPrivate(createLayout(QTextOption::WrapAnywhere), findClickables(), this); }
+ChatItemPrivate *ContentsChatItem::newPrivateData() {
+  return new ContentsChatItemPrivate(createLayout(QTextOption::WrapAnywhere), findClickables(), this);
+}
 ContentsChatItemPrivate *ContentsChatItem::privateData() const { return (ContentsChatItemPrivate *)ChatItem::privateData(); }
 
 class ContentsChatItem::WrapColumnFinder {
index 313d80e..393058b 100644 (file)
@@ -623,7 +623,8 @@ void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
   if(!event->buttons() & Qt::LeftButton) {
     _leftButtonPressed = false;
     if(_clickMode != NoClick) {
-      clearSelection();
+      if(_clickMode == SingleClick)
+        clearSelection();
       event->accept();
       if(!_clickTimer.isActive())
         handleClick(Qt::LeftButton, _clickPos);