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;
}
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,

View file

@ -11,8 +11,8 @@
#include <boost/noncopyable.hpp>
#include <cstdint>
#include <memory>
#include <vector>
#include <pajlada/signals/signalholder.hpp>
#include <vector>
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_;

View file

@ -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;
}

View file

@ -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:

View file

@ -26,6 +26,7 @@
#include <QDebug>
#include <QDesktopServices>
#include <QGraphicsBlurEffect>
#include <QMessageBox>
#include <QPainter>
#include <algorithm>
@ -1162,27 +1163,40 @@ void ChannelView::addContextMenuItems(
void ChannelView::mouseDoubleClickEvent(QMouseEvent *event)
{
std::shared_ptr<MessageLayout> 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<MessageLayout> 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());
}