mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
Added triple clicking to select the whole message.
This commit is contained in:
parent
7c367b73e6
commit
5c2cdce516
8 changed files with 64 additions and 12 deletions
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -505,6 +505,26 @@ int MessageLayoutContainer::getLastCharacterIndex() const
|
|||
return this->lines_.back().endCharIndex;
|
||||
}
|
||||
|
||||
int MessageLayoutContainer::getFirstMessageCharacterIndex() const
|
||||
{
|
||||
static FlagsEnum<MessageElementFlag> 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)
|
||||
{
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -74,6 +74,11 @@ const QString &MessageLayoutElement::getText() const
|
|||
return this->text_;
|
||||
}
|
||||
|
||||
FlagsEnum<MessageElementFlag> 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 {
|
||||
|
|
|
@ -6,8 +6,10 @@
|
|||
#include <boost/noncopyable.hpp>
|
||||
#include <climits>
|
||||
|
||||
#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<MessageElementFlag> 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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue