From fed8cca10eb8026e3b20703fc7766ee7910e88ae Mon Sep 17 00:00:00 2001 From: Cranken Date: Sun, 30 Sep 2018 18:18:30 +0200 Subject: [PATCH] Added initial double clicking to select word functionality. --- src/messages/MessageElement.cpp | 8 ++- src/messages/MessageElement.hpp | 4 +- src/messages/layouts/MessageLayoutElement.cpp | 23 +++++--- src/messages/layouts/MessageLayoutElement.hpp | 19 ++++--- src/widgets/helper/ChannelView.cpp | 54 ++++++++++++------- 5 files changed, 72 insertions(+), 36 deletions(-) diff --git a/src/messages/MessageElement.cpp b/src/messages/MessageElement.cpp index ede398daa..434b944a1 100644 --- a/src/messages/MessageElement.cpp +++ b/src/messages/MessageElement.cpp @@ -29,6 +29,12 @@ MessageElement *MessageElement::setLink(const Link &link) return this; } +MessageElement *MessageElement::setText(const QString &text) +{ + this->text_ = text; + return this; +} + MessageElement *MessageElement::setTooltip(const QString &tooltip) { this->tooltip_ = tooltip; @@ -156,7 +162,7 @@ void TextElement::addToContainer(MessageLayoutContainer &container, auto e = (new TextLayoutElement( *this, text, QSize(width, metrics.height()), color, this->style_, container.getScale())) - ->setLink(this->getLink()); + ->setText(text); e->setTrailingSpace(trailingSpace); // If URL link was changed, diff --git a/src/messages/MessageElement.hpp b/src/messages/MessageElement.hpp index e82bee5e0..e9032f958 100644 --- a/src/messages/MessageElement.hpp +++ b/src/messages/MessageElement.hpp @@ -11,8 +11,8 @@ #include #include #include -#include #include +#include namespace chatterino { class Channel; @@ -123,6 +123,7 @@ public: virtual ~MessageElement(); MessageElement *setLink(const Link &link); + MessageElement *setText(const QString &text); MessageElement *setTooltip(const QString &tooltip); MessageElement *setTrailingSpace(bool value); const QString &getTooltip() const; @@ -140,6 +141,7 @@ protected: pajlada::Signals::NoArgSignal linkChanged; private: + QString text_; Link link_; QString tooltip_; MessageElementFlags flags_; diff --git a/src/messages/layouts/MessageLayoutElement.cpp b/src/messages/layouts/MessageLayoutElement.cpp index 09bc2936c..84b50f104 100644 --- a/src/messages/layouts/MessageLayoutElement.cpp +++ b/src/messages/layouts/MessageLayoutElement.cpp @@ -58,11 +58,22 @@ MessageLayoutElement *MessageLayoutElement::setLink(const Link &_link) return this; } +MessageLayoutElement *MessageLayoutElement::setText(const QString &_text) +{ + this->text_ = _text; + return this; +} + const Link &MessageLayoutElement::getLink() const { return this->link_; } +const QString &MessageLayoutElement::getText() const +{ + return this->text_; +} + // // IMAGE // @@ -88,7 +99,7 @@ void ImageLayoutElement::addCopyTextToString(QString &str, int from, } } -int ImageLayoutElement::getSelectionIndexCount() +int ImageLayoutElement::getSelectionIndexCount() const { return this->trailingSpace ? 2 : 1; } @@ -121,7 +132,7 @@ void ImageLayoutElement::paintAnimated(QPainter &painter, int yOffset) } } -int ImageLayoutElement::getMouseOverIndex(const QPoint &abs) +int ImageLayoutElement::getMouseOverIndex(const QPoint &abs) const { return 0; } @@ -163,7 +174,7 @@ void TextLayoutElement::addCopyTextToString(QString &str, int from, } } -int TextLayoutElement::getSelectionIndexCount() +int TextLayoutElement::getSelectionIndexCount() const { return this->text.length() + (this->trailingSpace ? 1 : 0); } @@ -185,7 +196,7 @@ void TextLayoutElement::paintAnimated(QPainter &, int) { } -int TextLayoutElement::getMouseOverIndex(const QPoint &abs) +int TextLayoutElement::getMouseOverIndex(const QPoint &abs) const { if (abs.x() < this->getRect().left()) { return 0; @@ -246,7 +257,7 @@ void TextIconLayoutElement::addCopyTextToString(QString &str, int from, { } -int TextIconLayoutElement::getSelectionIndexCount() +int TextIconLayoutElement::getSelectionIndexCount() const { return this->trailingSpace ? 2 : 1; } @@ -281,7 +292,7 @@ void TextIconLayoutElement::paintAnimated(QPainter &painter, int yOffset) { } -int TextIconLayoutElement::getMouseOverIndex(const QPoint &abs) +int TextIconLayoutElement::getMouseOverIndex(const QPoint &abs) const { return 0; } diff --git a/src/messages/layouts/MessageLayoutElement.hpp b/src/messages/layouts/MessageLayoutElement.hpp index 7167adaa9..48a43e97e 100644 --- a/src/messages/layouts/MessageLayoutElement.hpp +++ b/src/messages/layouts/MessageLayoutElement.hpp @@ -30,20 +30,23 @@ public: MessageLayoutElement *setTrailingSpace(bool value); MessageLayoutElement *setLink(const Link &link_); + MessageLayoutElement *setText(const QString &text_); virtual void addCopyTextToString(QString &str, int from = 0, int to = INT_MAX) const = 0; - virtual int getSelectionIndexCount() = 0; + virtual int getSelectionIndexCount() const = 0; virtual void paint(QPainter &painter) = 0; virtual void paintAnimated(QPainter &painter, int yOffset) = 0; - virtual int getMouseOverIndex(const QPoint &abs) = 0; + virtual int getMouseOverIndex(const QPoint &abs) const = 0; virtual int getXFromIndex(int index) = 0; const Link &getLink() const; + const QString &getText() const; protected: bool trailingSpace = true; private: + QString text_; QRect rect_; Link link_; MessageElement &creator_; @@ -59,10 +62,10 @@ public: protected: void addCopyTextToString(QString &str, int from = 0, int to = INT_MAX) const override; - int getSelectionIndexCount() override; + int getSelectionIndexCount() const override; void paint(QPainter &painter) override; void paintAnimated(QPainter &painter, int yOffset) override; - int getMouseOverIndex(const QPoint &abs) override; + int getMouseOverIndex(const QPoint &abs) const override; int getXFromIndex(int index) override; private: @@ -80,10 +83,10 @@ public: protected: void addCopyTextToString(QString &str, int from = 0, int to = INT_MAX) const override; - int getSelectionIndexCount() override; + int getSelectionIndexCount() const override; void paint(QPainter &painter) override; void paintAnimated(QPainter &painter, int yOffset) override; - int getMouseOverIndex(const QPoint &abs) override; + int getMouseOverIndex(const QPoint &abs) const override; int getXFromIndex(int index) override; private: @@ -104,10 +107,10 @@ public: protected: void addCopyTextToString(QString &str, int from = 0, int to = INT_MAX) const override; - int getSelectionIndexCount() override; + int getSelectionIndexCount() const override; void paint(QPainter &painter) override; void paintAnimated(QPainter &painter, int yOffset) override; - int getMouseOverIndex(const QPoint &abs) override; + int getMouseOverIndex(const QPoint &abs) const override; int getXFromIndex(int index) override; private: diff --git a/src/widgets/helper/ChannelView.cpp b/src/widgets/helper/ChannelView.cpp index 0f4701974..e8739c4d6 100644 --- a/src/widgets/helper/ChannelView.cpp +++ b/src/widgets/helper/ChannelView.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -1162,27 +1163,40 @@ void ChannelView::addContextMenuItems( void ChannelView::mouseDoubleClickEvent(QMouseEvent *event) { + std::shared_ptr layout; + QPoint relativePos; + int messageIndex; + + if (!tryGetMessageAt(event->pos(), layout, relativePos, messageIndex)) { + return; + } + + // message under cursor is collapsed + if (layout->flags.has(MessageLayoutFlag::Collapsed)) { + return; + } + + const MessageLayoutElement *hoverLayoutElement = + layout->getElementAt(relativePos); + + if (hoverLayoutElement == nullptr) { + return; + } + if (!this->isMouseDown_) { + this->isMouseDown_ = true; + + const int mouseInWordIndex = + hoverLayoutElement->getMouseOverIndex(relativePos); + const int wordStart = + layout->getSelectionIndex(relativePos) - mouseInWordIndex; + const int wordEnd = wordStart + hoverLayoutElement->getText().length(); + + SelectionItem wordMin(messageIndex, wordStart); + SelectionItem wordMax(messageIndex, wordEnd); + this->setSelection(wordMin, wordMax); + } + if (getSettings()->linksDoubleClickOnly) { - std::shared_ptr layout; - QPoint relativePos; - int messageIndex; - - if (!tryGetMessageAt(event->pos(), layout, relativePos, messageIndex)) { - return; - } - - // message under cursor is collapsed - if (layout->flags.has(MessageLayoutFlag::Collapsed)) { - return; - } - - const MessageLayoutElement *hoverLayoutElement = - layout->getElementAt(relativePos); - - if (hoverLayoutElement == nullptr) { - return; - } - auto &link = hoverLayoutElement->getLink(); this->handleLinkClick(event, link, layout.get()); }