From a268abfd4c49c76ad6e9e6edf23c00af20bd2bbb Mon Sep 17 00:00:00 2001 From: fourtf Date: Wed, 4 Sep 2019 00:29:58 +0200 Subject: [PATCH] Allow searching in LogsPopup --- chatterino.pro | 1 + src/ForwardDecl.hpp | 10 ++++ src/PrecompiledHeader.hpp | 2 + src/messages/LimitedQueueSnapshot.hpp | 2 +- src/widgets/dialogs/LogsPopup.cpp | 49 ++++++++++---------- src/widgets/dialogs/LogsPopup.hpp | 24 ++++------ src/widgets/helper/SearchPopup.cpp | 66 ++++++++++++++++----------- src/widgets/helper/SearchPopup.hpp | 19 ++++---- 8 files changed, 93 insertions(+), 80 deletions(-) create mode 100644 src/ForwardDecl.hpp diff --git a/chatterino.pro b/chatterino.pro index d2db3dbc9..bf6c49159 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -222,6 +222,7 @@ SOURCES += \ HEADERS += \ src/Application.hpp \ + src/ForwardDecl.hpp \ src/autogenerated/ResourcesAutogen.hpp \ src/BrowserExtension.hpp \ src/common/Aliases.hpp \ diff --git a/src/ForwardDecl.hpp b/src/ForwardDecl.hpp new file mode 100644 index 000000000..204c61ae2 --- /dev/null +++ b/src/ForwardDecl.hpp @@ -0,0 +1,10 @@ +#pragma once + +namespace chatterino { +class Channel; +class ChannelView; +using ChannelPtr = std::shared_ptr; + +struct Message; +using MessagePtr = std::shared_ptr; +} // namespace chatterino diff --git a/src/PrecompiledHeader.hpp b/src/PrecompiledHeader.hpp index 07b9c4605..a989dc7c5 100644 --- a/src/PrecompiledHeader.hpp +++ b/src/PrecompiledHeader.hpp @@ -1,3 +1,5 @@ +#pragma once + #ifdef __cplusplus # include # include diff --git a/src/messages/LimitedQueueSnapshot.hpp b/src/messages/LimitedQueueSnapshot.hpp index 293f31ef3..e7d62ae19 100644 --- a/src/messages/LimitedQueueSnapshot.hpp +++ b/src/messages/LimitedQueueSnapshot.hpp @@ -22,7 +22,7 @@ public: { } - std::size_t size() + std::size_t size() const { return this->length_; } diff --git a/src/widgets/dialogs/LogsPopup.cpp b/src/widgets/dialogs/LogsPopup.cpp index 7c840468d..c9b8b0599 100644 --- a/src/widgets/dialogs/LogsPopup.cpp +++ b/src/widgets/dialogs/LogsPopup.cpp @@ -4,6 +4,7 @@ #include "common/Channel.hpp" #include "common/NetworkRequest.hpp" #include "debug/Log.hpp" +#include "messages/Message.hpp" #include "providers/twitch/PartialTwitchUser.hpp" #include "providers/twitch/TwitchChannel.hpp" #include "providers/twitch/TwitchMessageBuilder.hpp" @@ -20,34 +21,30 @@ namespace chatterino { LogsPopup::LogsPopup() : channel_(Channel::getEmpty()) { - this->initLayout(); this->resize(400, 600); } -void LogsPopup::initLayout() -{ - QVBoxLayout *layout = new QVBoxLayout(this); - layout->setMargin(0); - - this->channelView_ = new ChannelView(this); - layout->addWidget(this->channelView_); - - this->setLayout(layout); -} - -void LogsPopup::setChannelName(QString channelName) -{ - this->channelName_ = channelName; -} - -void LogsPopup::setChannel(std::shared_ptr channel) +void LogsPopup::setChannel(const ChannelPtr &channel) { this->channel_ = channel; + this->updateWindowTitle(); } -void LogsPopup::setTargetUserName(QString userName) +void LogsPopup::setChannelName(const QString &channelName) +{ + this->channelName_ = channelName; + this->updateWindowTitle(); +} + +void LogsPopup::setTargetUserName(const QString &userName) { this->userName_ = userName; + this->updateWindowTitle(); +} + +void LogsPopup::updateWindowTitle() +{ + this->setWindowTitle(this->userName_ + "'s logs in #" + this->channelName_); } void LogsPopup::getLogs() @@ -60,8 +57,6 @@ void LogsPopup::getLogs() this->channelName_ = twitchChannel->getName(); this->getLogviewerLogs(twitchChannel->roomId()); - this->setWindowTitle(this->userName_ + "'s logs in #" + - this->channelName_); return; } } @@ -83,7 +78,7 @@ void LogsPopup::setMessages(std::vector &messages) ChannelPtr logsChannel(new Channel("logs", Channel::Type::Misc)); logsChannel->addMessagesAtStart(messages); - this->channelView_->setChannel(logsChannel); + SearchPopup::setChannel(logsChannel); } void LogsPopup::getLogviewerLogs(const QString &roomID) @@ -121,6 +116,8 @@ void LogsPopup::getLogviewerLogs(const QString &roomID) static_cast(ircMessage); TwitchMessageBuilder builder(this->channel_.get(), privMsg, args); + builder.message().searchText = message; + messages.push_back(builder.build()); } @@ -165,6 +162,7 @@ void LogsPopup::getOverrustleLogs() for (auto i : dataMessages) { QJsonObject singleMessage = i.toObject(); + auto text = singleMessage.value("text").toString(); QTime timeStamp = QDateTime::fromSecsSinceEpoch( singleMessage.value("timestamp").toInt()) @@ -175,9 +173,9 @@ void LogsPopup::getOverrustleLogs() builder.emplace(this->userName_, MessageElementFlag::Username, MessageColor::System); - builder.emplace( - singleMessage.value("text").toString(), - MessageElementFlag::Text, MessageColor::Text); + builder.emplace(text, MessageElementFlag::Text, + MessageColor::Text); + builder.message().searchText = text; messages.push_back(builder.release()); } } @@ -191,4 +189,5 @@ void LogsPopup::getOverrustleLogs() }) .execute(); } + } // namespace chatterino diff --git a/src/widgets/dialogs/LogsPopup.hpp b/src/widgets/dialogs/LogsPopup.hpp index e66061d99..66bf19acc 100644 --- a/src/widgets/dialogs/LogsPopup.hpp +++ b/src/widgets/dialogs/LogsPopup.hpp @@ -1,37 +1,29 @@ #pragma once -#include "widgets/BaseWindow.hpp" +#include "widgets/helper/SearchPopup.hpp" namespace chatterino { -class Channel; -class ChannelView; - -class Channel; -using ChannelPtr = std::shared_ptr; - -struct Message; -using MessagePtr = std::shared_ptr; - -class LogsPopup : public BaseWindow +class LogsPopup : public SearchPopup { public: LogsPopup(); - void setChannelName(QString channelName); - void setChannel(std::shared_ptr channel); - void setTargetUserName(QString userName); + void setChannel(const ChannelPtr &channel) override; + void setChannelName(const QString &channelName); + void setTargetUserName(const QString &userName); void getLogs(); +protected: + void updateWindowTitle() override; + private: - ChannelView *channelView_ = nullptr; ChannelPtr channel_; QString userName_; QString channelName_; - void initLayout(); void setMessages(std::vector &messages); void getOverrustleLogs(); void getLogviewerLogs(const QString &roomID); diff --git a/src/widgets/helper/SearchPopup.cpp b/src/widgets/helper/SearchPopup.cpp index 426e9c6e3..8c2904899 100644 --- a/src/widgets/helper/SearchPopup.cpp +++ b/src/widgets/helper/SearchPopup.cpp @@ -10,6 +10,26 @@ #include "widgets/helper/ChannelView.hpp" namespace chatterino { +namespace { + ChannelPtr filter(const QString &text, const QString &channelName, + const LimitedQueueSnapshot &snapshot) + { + ChannelPtr channel(new Channel(channelName, Channel::Type::None)); + + for (size_t i = 0; i < snapshot.size(); i++) + { + MessagePtr message = snapshot[i]; + + if (text.isEmpty() || + message->searchText.indexOf(text, 0, Qt::CaseInsensitive) != -1) + { + channel->addMessage(message); + } + } + + return channel; + } +} // namespace SearchPopup::SearchPopup() { @@ -17,13 +37,24 @@ SearchPopup::SearchPopup() this->resize(400, 600); } -void SearchPopup::setChannel(ChannelPtr channel) +void SearchPopup::setChannel(const ChannelPtr &channel) { this->channelName_ = channel->getName(); this->snapshot_ = channel->getMessageSnapshot(); - this->performSearch(); + this->search(); - this->setWindowTitle("Searching in " + channel->getName() + "s history"); + this->updateWindowTitle(); +} + +void SearchPopup::updateWindowTitle() +{ + this->setWindowTitle("Searching in " + this->channelName_ + "s history"); +} + +void SearchPopup::search() +{ + this->channelView_->setChannel(filter(this->searchInput_->text(), + this->channelName_, this->snapshot_)); } void SearchPopup::keyPressEvent(QKeyEvent *e) @@ -43,18 +74,20 @@ void SearchPopup::initLayout() { QVBoxLayout *layout1 = new QVBoxLayout(this); layout1->setMargin(0); + layout1->setSpacing(0); // HBOX { QHBoxLayout *layout2 = new QHBoxLayout(this); - layout2->setMargin(6); + layout2->setMargin(8); + layout2->setSpacing(8); // SEARCH INPUT { this->searchInput_ = new QLineEdit(this); layout2->addWidget(this->searchInput_); QObject::connect(this->searchInput_, &QLineEdit::returnPressed, - [this] { this->performSearch(); }); + [this] { this->search(); }); } // SEARCH BUTTON @@ -63,7 +96,7 @@ void SearchPopup::initLayout() searchButton->setText("Search"); layout2->addWidget(searchButton); QObject::connect(searchButton, &QPushButton::clicked, - [this] { this->performSearch(); }); + [this] { this->search(); }); } layout1->addLayout(layout2); @@ -80,25 +113,4 @@ void SearchPopup::initLayout() } } -void SearchPopup::performSearch() -{ - QString text = searchInput_->text(); - - ChannelPtr channel(new Channel(this->channelName_, Channel::Type::None)); - - for (size_t i = 0; i < this->snapshot_.size(); i++) - { - MessagePtr message = this->snapshot_[i]; - - if (text.isEmpty() || - message->searchText.indexOf(this->searchInput_->text(), 0, - Qt::CaseInsensitive) != -1) - { - channel->addMessage(message); - } - } - - this->channelView_->setChannel(channel); -} - } // namespace chatterino diff --git a/src/widgets/helper/SearchPopup.hpp b/src/widgets/helper/SearchPopup.hpp index fd5dba915..07e6dba3c 100644 --- a/src/widgets/helper/SearchPopup.hpp +++ b/src/widgets/helper/SearchPopup.hpp @@ -1,5 +1,6 @@ #pragma once +#include "ForwardDecl.hpp" #include "messages/LimitedQueueSnapshot.hpp" #include "widgets/BaseWindow.hpp" @@ -9,30 +10,26 @@ class QLineEdit; namespace chatterino { -class Channel; -class ChannelView; - -struct Message; -using MessagePtr = std::shared_ptr; - class SearchPopup : public BaseWindow { public: SearchPopup(); - void setChannel(std::shared_ptr channel); + virtual void setChannel(const ChannelPtr &channel); protected: void keyPressEvent(QKeyEvent *e) override; + virtual void updateWindowTitle(); + private: void initLayout(); - void performSearch(); + void search(); LimitedQueueSnapshot snapshot_; - QLineEdit *searchInput_; - ChannelView *channelView_; - QString channelName_; + QLineEdit *searchInput_{}; + ChannelView *channelView_{}; + QString channelName_{}; }; } // namespace chatterino