From 8bcc9c487bfaf9de9ffa8c037e881adcc0e27d3c Mon Sep 17 00:00:00 2001 From: fourtf Date: Thu, 16 Aug 2018 00:16:33 +0200 Subject: [PATCH] Fixes #411, #516 --- src/common/Common.hpp | 5 ++++ src/messages/layouts/MessageLayout.cpp | 5 ++-- src/messages/layouts/MessageLayout.hpp | 4 ++- .../layouts/MessageLayoutContainer.cpp | 28 ++++++++++++------- .../layouts/MessageLayoutContainer.hpp | 3 +- src/providers/twitch/TwitchMessageBuilder.cpp | 10 +++---- src/widgets/helper/ChannelView.cpp | 13 +++++---- 7 files changed, 42 insertions(+), 26 deletions(-) diff --git a/src/common/Common.hpp b/src/common/Common.hpp index 46de4a7cb..7f22bf27d 100644 --- a/src/common/Common.hpp +++ b/src/common/Common.hpp @@ -41,4 +41,9 @@ std::weak_ptr weakOf(T *element) struct Message; using MessagePtr = std::shared_ptr; +enum class CopyMode { + Everything, + OnlyTextAndEmotes, +}; + } // namespace chatterino diff --git a/src/messages/layouts/MessageLayout.cpp b/src/messages/layouts/MessageLayout.cpp index c7ff02af9..5fbbe0bfa 100644 --- a/src/messages/layouts/MessageLayout.cpp +++ b/src/messages/layouts/MessageLayout.cpp @@ -284,9 +284,10 @@ int MessageLayout::getSelectionIndex(QPoint position) return this->container_->getSelectionIndex(position); } -void MessageLayout::addSelectionText(QString &str, int from, int to) +void MessageLayout::addSelectionText(QString &str, int from, int to, + CopyMode copymode) { - this->container_->addSelectionText(str, from, to); + this->container_->addSelectionText(str, from, to, copymode); } } // namespace chatterino diff --git a/src/messages/layouts/MessageLayout.hpp b/src/messages/layouts/MessageLayout.hpp index 2ab015856..671d30668 100644 --- a/src/messages/layouts/MessageLayout.hpp +++ b/src/messages/layouts/MessageLayout.hpp @@ -1,5 +1,6 @@ #pragma once +#include "common/Common.hpp" #include "common/FlagsEnum.hpp" #include @@ -54,7 +55,8 @@ public: const MessageLayoutElement *getElementAt(QPoint point); int getLastCharacterIndex() const; int getSelectionIndex(QPoint position); - void addSelectionText(QString &str, int from = 0, int to = INT_MAX); + void addSelectionText(QString &str, int from = 0, int to = INT_MAX, + CopyMode copymode = CopyMode::Everything); // Misc bool isDisabled() const; diff --git a/src/messages/layouts/MessageLayoutContainer.cpp b/src/messages/layouts/MessageLayoutContainer.cpp index 008a02d05..0aef73c6b 100644 --- a/src/messages/layouts/MessageLayoutContainer.cpp +++ b/src/messages/layouts/MessageLayoutContainer.cpp @@ -505,33 +505,41 @@ int MessageLayoutContainer::getLastCharacterIndex() const return this->lines_.back().endCharIndex; } -void MessageLayoutContainer::addSelectionText(QString &str, int from, int to) +void MessageLayoutContainer::addSelectionText(QString &str, int from, int to, + CopyMode copymode) { int index = 0; bool first = true; - for (std::unique_ptr &ele : this->elements_) { - int c = ele->getSelectionIndexCount(); + for (auto &element : this->elements_) { + if (copymode == CopyMode::OnlyTextAndEmotes) { + if (element->getCreator().getFlags().hasAny( + {MessageElementFlag::Timestamp, + MessageElementFlag::Username, MessageElementFlag::Badges})) + continue; + } + + auto indexCount = element->getSelectionIndexCount(); if (first) { - if (index + c > from) { - ele->addCopyTextToString(str, from - index, to - index); + if (index + indexCount > from) { + element->addCopyTextToString(str, from - index, to - index); first = false; - if (index + c > to) { + if (index + indexCount > to) { break; } } } else { - if (index + c > to) { - ele->addCopyTextToString(str, 0, to - index); + if (index + indexCount > to) { + element->addCopyTextToString(str, 0, to - index); break; } else { - ele->addCopyTextToString(str); + element->addCopyTextToString(str); } } - index += c; + index += indexCount; } } diff --git a/src/messages/layouts/MessageLayoutContainer.hpp b/src/messages/layouts/MessageLayoutContainer.hpp index cab849ed0..669986196 100644 --- a/src/messages/layouts/MessageLayoutContainer.hpp +++ b/src/messages/layouts/MessageLayoutContainer.hpp @@ -5,6 +5,7 @@ #include #include +#include "common/Common.hpp" #include "common/FlagsEnum.hpp" #include "messages/Selection.hpp" #include "messages/layouts/MessageLayoutElement.hpp" @@ -74,7 +75,7 @@ struct MessageLayoutContainer { // selection int getSelectionIndex(QPoint point); int getLastCharacterIndex() const; - void addSelectionText(QString &str, int from, int to); + void addSelectionText(QString &str, int from, int to, CopyMode copymode); bool isCollapsed(); diff --git a/src/providers/twitch/TwitchMessageBuilder.cpp b/src/providers/twitch/TwitchMessageBuilder.cpp index 26ea07dec..ca04e7e86 100644 --- a/src/providers/twitch/TwitchMessageBuilder.cpp +++ b/src/providers/twitch/TwitchMessageBuilder.cpp @@ -83,8 +83,6 @@ bool TwitchMessageBuilder::isIgnored() const MessagePtr TwitchMessageBuilder::build() { - auto app = getApp(); - // PARSING this->parseUsername(); @@ -437,7 +435,7 @@ void TwitchMessageBuilder::appendUsername() // IrcManager::getInstance().getUser().getUserName(); } else if (this->args.isReceivedWhisper) { // Sender username - this->emplace(usernameText, MessageElementFlag::Text, + this->emplace(usernameText, MessageElementFlag::Username, this->usernameColor_, FontStyle::ChatMediumBold) ->setLink({Link::UserInfo, this->userName}); @@ -445,7 +443,7 @@ void TwitchMessageBuilder::appendUsername() auto currentUser = app->accounts->twitch.getCurrent(); // Separator - this->emplace("->", MessageElementFlag::Text, + this->emplace("->", MessageElementFlag::Username, app->themes->messages.textColors.system, FontStyle::ChatMedium); @@ -456,14 +454,14 @@ void TwitchMessageBuilder::appendUsername() // Your own username this->emplace(currentUser->getUserName() + ":", - MessageElementFlag::Text, selfColor, + MessageElementFlag::Username, selfColor, FontStyle::ChatMediumBold); } else { if (!this->action_) { usernameText += ":"; } - this->emplace(usernameText, MessageElementFlag::Text, + this->emplace(usernameText, MessageElementFlag::Username, this->usernameColor_, FontStyle::ChatMediumBold) ->setLink({Link::UserInfo, this->userName}); diff --git a/src/widgets/helper/ChannelView.cpp b/src/widgets/helper/ChannelView.cpp index 18139219e..2afc42e75 100644 --- a/src/widgets/helper/ChannelView.cpp +++ b/src/widgets/helper/ChannelView.cpp @@ -1088,17 +1088,18 @@ void ChannelView::addContextMenuItems( menu->addAction("Copy message", [layout] { QString copyString; - layout->addSelectionText(copyString); + layout->addSelectionText(copyString, 0, INT_MAX, + CopyMode::OnlyTextAndEmotes); QGuiApplication::clipboard()->setText(copyString); }); - // menu->addAction("Quote message", [layout] { - // QString copyString; - // layout->addSelectionText(copyString); + menu->addAction("Copy full message", [layout] { + QString copyString; + layout->addSelectionText(copyString); - // // insert into input - // }); + QGuiApplication::clipboard()->setText(copyString); + }); menu->popup(QCursor::pos()); menu->raise();