From 5c2cdce516d1ab5ed4ad9166e7066325197dfff3 Mon Sep 17 00:00:00 2001 From: Cranken Date: Tue, 2 Oct 2018 12:56:10 +0200 Subject: [PATCH] Added triple clicking to select the whole message. --- src/messages/layouts/MessageLayout.cpp | 5 +++++ src/messages/layouts/MessageLayout.hpp | 1 + .../layouts/MessageLayoutContainer.cpp | 20 ++++++++++++++++++ .../layouts/MessageLayoutContainer.hpp | 1 + src/messages/layouts/MessageLayoutElement.cpp | 21 ++++++++++++------- src/messages/layouts/MessageLayoutElement.hpp | 4 +++- src/widgets/helper/ChannelView.cpp | 21 +++++++++++++++++-- src/widgets/helper/ChannelView.hpp | 3 ++- 8 files changed, 64 insertions(+), 12 deletions(-) diff --git a/src/messages/layouts/MessageLayout.cpp b/src/messages/layouts/MessageLayout.cpp index 357a52f0e..c39590934 100644 --- a/src/messages/layouts/MessageLayout.cpp +++ b/src/messages/layouts/MessageLayout.cpp @@ -278,6 +278,11 @@ int MessageLayout::getLastCharacterIndex() const return this->container_->getLastCharacterIndex(); } +int MessageLayout::getFirstMessageCharacterIndex() const +{ + return this->container_->getFirstMessageCharacterIndex(); +} + int MessageLayout::getSelectionIndex(QPoint position) { return this->container_->getSelectionIndex(position); diff --git a/src/messages/layouts/MessageLayout.hpp b/src/messages/layouts/MessageLayout.hpp index ff0b17d0a..00b944a96 100644 --- a/src/messages/layouts/MessageLayout.hpp +++ b/src/messages/layouts/MessageLayout.hpp @@ -54,6 +54,7 @@ public: // Elements const MessageLayoutElement *getElementAt(QPoint point); int getLastCharacterIndex() const; + int getFirstMessageCharacterIndex() const; int getSelectionIndex(QPoint position); void addSelectionText(QString &str, int from = 0, int to = INT_MAX, CopyMode copymode = CopyMode::Everything); diff --git a/src/messages/layouts/MessageLayoutContainer.cpp b/src/messages/layouts/MessageLayoutContainer.cpp index bdd161e67..31bb8577f 100644 --- a/src/messages/layouts/MessageLayoutContainer.cpp +++ b/src/messages/layouts/MessageLayoutContainer.cpp @@ -505,6 +505,26 @@ int MessageLayoutContainer::getLastCharacterIndex() const return this->lines_.back().endCharIndex; } +int MessageLayoutContainer::getFirstMessageCharacterIndex() const +{ + static FlagsEnum flags; + flags.set(MessageElementFlag::Username); + flags.set(MessageElementFlag::Timestamp); + flags.set(MessageElementFlag::Badges); + + // Get the index of the first character of the real message + // (no badges/timestamps/username) + int index = 0; + for (auto &element : this->elements_) { + if (element.get()->getFlags().hasAny(flags)) { + index += element.get()->getSelectionIndexCount(); + } else { + break; + } + } + return index; +} + void MessageLayoutContainer::addSelectionText(QString &str, int from, int to, CopyMode copymode) { diff --git a/src/messages/layouts/MessageLayoutContainer.hpp b/src/messages/layouts/MessageLayoutContainer.hpp index 669986196..e248038a3 100644 --- a/src/messages/layouts/MessageLayoutContainer.hpp +++ b/src/messages/layouts/MessageLayoutContainer.hpp @@ -75,6 +75,7 @@ struct MessageLayoutContainer { // selection int getSelectionIndex(QPoint point); int getLastCharacterIndex() const; + int getFirstMessageCharacterIndex() const; void addSelectionText(QString &str, int from, int to, CopyMode copymode); bool isCollapsed(); diff --git a/src/messages/layouts/MessageLayoutElement.cpp b/src/messages/layouts/MessageLayoutElement.cpp index 84b50f104..3b9bfc4e4 100644 --- a/src/messages/layouts/MessageLayoutElement.cpp +++ b/src/messages/layouts/MessageLayoutElement.cpp @@ -74,6 +74,11 @@ const QString &MessageLayoutElement::getText() const return this->text_; } +FlagsEnum MessageLayoutElement::getFlags() const +{ + return this->creator_.getFlags(); +} + // // IMAGE // @@ -157,17 +162,17 @@ TextLayoutElement::TextLayoutElement(MessageElement &_creator, QString &_text, const QSize &_size, QColor _color, FontStyle _style, float _scale) : MessageLayoutElement(_creator, _size) - , text(_text) , color(_color) , style(_style) , scale(_scale) { + this->setText(_text); } void TextLayoutElement::addCopyTextToString(QString &str, int from, int to) const { - str += this->text.mid(from, to - from); + str += this->getText().mid(from, to - from); if (this->hasTrailingSpace()) { str += " "; @@ -176,7 +181,7 @@ void TextLayoutElement::addCopyTextToString(QString &str, int from, int TextLayoutElement::getSelectionIndexCount() const { - return this->text.length() + (this->trailingSpace ? 1 : 0); + return this->getText().length() + (this->trailingSpace ? 1 : 0); } void TextLayoutElement::paint(QPainter &painter) @@ -189,7 +194,7 @@ void TextLayoutElement::paint(QPainter &painter) painter.drawText( QRectF(this->getRect().x(), this->getRect().y(), 10000, 10000), - this->text, QTextOption(Qt::AlignLeft | Qt::AlignTop)); + this->getText(), QTextOption(Qt::AlignLeft | Qt::AlignTop)); } void TextLayoutElement::paintAnimated(QPainter &, int) @@ -208,8 +213,8 @@ int TextLayoutElement::getMouseOverIndex(const QPoint &abs) const int x = this->getRect().left(); - for (int i = 0; i < this->text.size(); i++) { - int w = metrics.width(this->text[i]); + for (int i = 0; i < this->getText().size(); i++) { + int w = metrics.width(this->getText()[i]); if (x + w > abs.x()) { return i; @@ -229,10 +234,10 @@ int TextLayoutElement::getXFromIndex(int index) if (index <= 0) { return this->getRect().left(); - } else if (index < this->text.size()) { + } else if (index < this->getText().size()) { int x = 0; for (int i = 0; i < index; i++) { - x += metrics.width(this->text[i]); + x += metrics.width(this->getText()[i]); } return x + this->getRect().left(); } else { diff --git a/src/messages/layouts/MessageLayoutElement.hpp b/src/messages/layouts/MessageLayoutElement.hpp index 48a43e97e..bdf490437 100644 --- a/src/messages/layouts/MessageLayoutElement.hpp +++ b/src/messages/layouts/MessageLayoutElement.hpp @@ -6,8 +6,10 @@ #include #include +#include "common/FlagsEnum.hpp" #include "messages/Link.hpp" #include "messages/MessageColor.hpp" +#include "messages/MessageElement.hpp" class QPainter; @@ -41,6 +43,7 @@ public: virtual int getXFromIndex(int index) = 0; const Link &getLink() const; const QString &getText() const; + FlagsEnum getFlags() const; protected: bool trailingSpace = true; @@ -90,7 +93,6 @@ protected: int getXFromIndex(int index) override; private: - QString text; QColor color; FontStyle style; float scale; diff --git a/src/widgets/helper/ChannelView.cpp b/src/widgets/helper/ChannelView.cpp index e8739c4d6..b2e210c37 100644 --- a/src/widgets/helper/ChannelView.cpp +++ b/src/widgets/helper/ChannelView.cpp @@ -120,6 +120,10 @@ ChannelView::ChannelView(BaseWidget *parent) QObject::connect(shortcut, &QShortcut::activated, [this] { QGuiApplication::clipboard()->setText(this->getSelectedText()); }); + + this->clickTimer_ = new QTimer(this); + this->clickTimer_->setSingleShot(true); + this->clickTimer_->setInterval(500); } void ChannelView::initializeLayout() @@ -1023,12 +1027,13 @@ void ChannelView::mouseReleaseEvent(QMouseEvent *event) } // handle the click - this->handleMouseClick(event, hoverLayoutElement, layout.get()); + this->handleMouseClick(event, hoverLayoutElement, layout.get(), + messageIndex); } void ChannelView::handleMouseClick(QMouseEvent *event, const MessageLayoutElement *hoveredElement, - MessageLayout *layout) + MessageLayout *layout, int &messageIndex) { switch (event->button()) { case Qt::LeftButton: { @@ -1038,6 +1043,16 @@ void ChannelView::handleMouseClick(QMouseEvent *event, this->showingLatestMessages_ = false; } + if (this->clickTimer_->isActive()) { + auto firstRealCharacterIndex = + layout->getFirstMessageCharacterIndex(); + SelectionItem msgStart(messageIndex, + firstRealCharacterIndex); + SelectionItem msgEnd(messageIndex, + layout->getLastCharacterIndex()); + this->setSelection(msgStart, msgEnd); + } + // this->pausedBySelection = false; this->selecting_ = false; // this->pauseTimeout.stop(); @@ -1191,6 +1206,8 @@ void ChannelView::mouseDoubleClickEvent(QMouseEvent *event) layout->getSelectionIndex(relativePos) - mouseInWordIndex; const int wordEnd = wordStart + hoverLayoutElement->getText().length(); + this->clickTimer_->start(); + SelectionItem wordMin(messageIndex, wordStart); SelectionItem wordMax(messageIndex, wordEnd); this->setSelection(wordMin, wordMax); diff --git a/src/widgets/helper/ChannelView.hpp b/src/widgets/helper/ChannelView.hpp index 48d7712d0..1b665ef65 100644 --- a/src/widgets/helper/ChannelView.hpp +++ b/src/widgets/helper/ChannelView.hpp @@ -108,7 +108,7 @@ private: void handleMouseClick(QMouseEvent *event, const MessageLayoutElement *hoverLayoutElement, - MessageLayout *layout); + MessageLayout *layout, int &messageIndex); void addContextMenuItems(const MessageLayoutElement *hoveredElement, MessageLayout *layout); int getLayoutWidth() const; @@ -149,6 +149,7 @@ private: bool isRightMouseDown_ = false; QPointF lastPressPosition_; QPointF lastRightPressPosition_; + QTimer *clickTimer_; Selection selection_; bool selecting_ = false;