From: Manuel Nickschas Date: Sat, 15 Nov 2008 19:51:25 +0000 (+0100) Subject: Make double and triple click selection work X-Git-Tag: 0.4.0~429 X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=commitdiff_plain;h=0125cf23d570d2b8ee4d3fd5407364cf6c5769df;hp=f64ded9b73ed4eb09d3e2c14cddc0ead1e269185 Make double and triple click selection work --- diff --git a/src/qtui/chatitem.cpp b/src/qtui/chatitem.cpp index 53bee456..ed24bbaf 100644 --- a/src/qtui/chatitem.cpp +++ b/src/qtui/chatitem.cpp @@ -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); diff --git a/src/qtui/chatitem.h b/src/qtui/chatitem.h index 0710b6af..3cb9c3d7 100644 --- a/src/qtui/chatitem.h +++ b/src/qtui/chatitem.h @@ -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 additionalFormats() const { return QVector(); } + 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 { diff --git a/src/qtui/chatscene.cpp b/src/qtui/chatscene.cpp index 313d80eb..393058b8 100644 --- a/src/qtui/chatscene.cpp +++ b/src/qtui/chatscene.cpp @@ -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);