Added initial double clicking to select word functionality.

This commit is contained in:
Cranken 2018-09-30 18:18:30 +02:00 committed by pajlada
parent 5dce212cb4
commit fed8cca10e
5 changed files with 72 additions and 36 deletions

View file

@ -29,6 +29,12 @@ MessageElement *MessageElement::setLink(const Link &link)
return this; return this;
} }
MessageElement *MessageElement::setText(const QString &text)
{
this->text_ = text;
return this;
}
MessageElement *MessageElement::setTooltip(const QString &tooltip) MessageElement *MessageElement::setTooltip(const QString &tooltip)
{ {
this->tooltip_ = tooltip; this->tooltip_ = tooltip;
@ -156,7 +162,7 @@ void TextElement::addToContainer(MessageLayoutContainer &container,
auto e = (new TextLayoutElement( auto e = (new TextLayoutElement(
*this, text, QSize(width, metrics.height()), *this, text, QSize(width, metrics.height()),
color, this->style_, container.getScale())) color, this->style_, container.getScale()))
->setLink(this->getLink()); ->setText(text);
e->setTrailingSpace(trailingSpace); e->setTrailingSpace(trailingSpace);
// If URL link was changed, // If URL link was changed,

View file

@ -11,8 +11,8 @@
#include <boost/noncopyable.hpp> #include <boost/noncopyable.hpp>
#include <cstdint> #include <cstdint>
#include <memory> #include <memory>
#include <vector>
#include <pajlada/signals/signalholder.hpp> #include <pajlada/signals/signalholder.hpp>
#include <vector>
namespace chatterino { namespace chatterino {
class Channel; class Channel;
@ -123,6 +123,7 @@ public:
virtual ~MessageElement(); virtual ~MessageElement();
MessageElement *setLink(const Link &link); MessageElement *setLink(const Link &link);
MessageElement *setText(const QString &text);
MessageElement *setTooltip(const QString &tooltip); MessageElement *setTooltip(const QString &tooltip);
MessageElement *setTrailingSpace(bool value); MessageElement *setTrailingSpace(bool value);
const QString &getTooltip() const; const QString &getTooltip() const;
@ -140,6 +141,7 @@ protected:
pajlada::Signals::NoArgSignal linkChanged; pajlada::Signals::NoArgSignal linkChanged;
private: private:
QString text_;
Link link_; Link link_;
QString tooltip_; QString tooltip_;
MessageElementFlags flags_; MessageElementFlags flags_;

View file

@ -58,11 +58,22 @@ MessageLayoutElement *MessageLayoutElement::setLink(const Link &_link)
return this; return this;
} }
MessageLayoutElement *MessageLayoutElement::setText(const QString &_text)
{
this->text_ = _text;
return this;
}
const Link &MessageLayoutElement::getLink() const const Link &MessageLayoutElement::getLink() const
{ {
return this->link_; return this->link_;
} }
const QString &MessageLayoutElement::getText() const
{
return this->text_;
}
// //
// IMAGE // IMAGE
// //
@ -88,7 +99,7 @@ void ImageLayoutElement::addCopyTextToString(QString &str, int from,
} }
} }
int ImageLayoutElement::getSelectionIndexCount() int ImageLayoutElement::getSelectionIndexCount() const
{ {
return this->trailingSpace ? 2 : 1; 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; 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); 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()) { if (abs.x() < this->getRect().left()) {
return 0; 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; 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; return 0;
} }

View file

@ -30,20 +30,23 @@ public:
MessageLayoutElement *setTrailingSpace(bool value); MessageLayoutElement *setTrailingSpace(bool value);
MessageLayoutElement *setLink(const Link &link_); MessageLayoutElement *setLink(const Link &link_);
MessageLayoutElement *setText(const QString &text_);
virtual void addCopyTextToString(QString &str, int from = 0, virtual void addCopyTextToString(QString &str, int from = 0,
int to = INT_MAX) const = 0; int to = INT_MAX) const = 0;
virtual int getSelectionIndexCount() = 0; virtual int getSelectionIndexCount() const = 0;
virtual void paint(QPainter &painter) = 0; virtual void paint(QPainter &painter) = 0;
virtual void paintAnimated(QPainter &painter, int yOffset) = 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; virtual int getXFromIndex(int index) = 0;
const Link &getLink() const; const Link &getLink() const;
const QString &getText() const;
protected: protected:
bool trailingSpace = true; bool trailingSpace = true;
private: private:
QString text_;
QRect rect_; QRect rect_;
Link link_; Link link_;
MessageElement &creator_; MessageElement &creator_;
@ -59,10 +62,10 @@ public:
protected: protected:
void addCopyTextToString(QString &str, int from = 0, void addCopyTextToString(QString &str, int from = 0,
int to = INT_MAX) const override; int to = INT_MAX) const override;
int getSelectionIndexCount() override; int getSelectionIndexCount() const override;
void paint(QPainter &painter) override; void paint(QPainter &painter) override;
void paintAnimated(QPainter &painter, int yOffset) 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; int getXFromIndex(int index) override;
private: private:
@ -80,10 +83,10 @@ public:
protected: protected:
void addCopyTextToString(QString &str, int from = 0, void addCopyTextToString(QString &str, int from = 0,
int to = INT_MAX) const override; int to = INT_MAX) const override;
int getSelectionIndexCount() override; int getSelectionIndexCount() const override;
void paint(QPainter &painter) override; void paint(QPainter &painter) override;
void paintAnimated(QPainter &painter, int yOffset) 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; int getXFromIndex(int index) override;
private: private:
@ -104,10 +107,10 @@ public:
protected: protected:
void addCopyTextToString(QString &str, int from = 0, void addCopyTextToString(QString &str, int from = 0,
int to = INT_MAX) const override; int to = INT_MAX) const override;
int getSelectionIndexCount() override; int getSelectionIndexCount() const override;
void paint(QPainter &painter) override; void paint(QPainter &painter) override;
void paintAnimated(QPainter &painter, int yOffset) 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; int getXFromIndex(int index) override;
private: private:

View file

@ -26,6 +26,7 @@
#include <QDebug> #include <QDebug>
#include <QDesktopServices> #include <QDesktopServices>
#include <QGraphicsBlurEffect> #include <QGraphicsBlurEffect>
#include <QMessageBox>
#include <QPainter> #include <QPainter>
#include <algorithm> #include <algorithm>
@ -1162,7 +1163,6 @@ void ChannelView::addContextMenuItems(
void ChannelView::mouseDoubleClickEvent(QMouseEvent *event) void ChannelView::mouseDoubleClickEvent(QMouseEvent *event)
{ {
if (getSettings()->linksDoubleClickOnly) {
std::shared_ptr<MessageLayout> layout; std::shared_ptr<MessageLayout> layout;
QPoint relativePos; QPoint relativePos;
int messageIndex; int messageIndex;
@ -1182,7 +1182,21 @@ void ChannelView::mouseDoubleClickEvent(QMouseEvent *event)
if (hoverLayoutElement == nullptr) { if (hoverLayoutElement == nullptr) {
return; 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) {
auto &link = hoverLayoutElement->getLink(); auto &link = hoverLayoutElement->getLink();
this->handleLinkClick(event, link, layout.get()); this->handleLinkClick(event, link, layout.get());
} }