From 47a813d5d6c6f8fbfad530feb12f8b98a56c28c2 Mon Sep 17 00:00:00 2001 From: fourtf Date: Fri, 5 Jan 2018 13:42:23 +0100 Subject: [PATCH] added search popup --- chatterino.pro | 6 +- src/messages/limitedqueuesnapshot.hpp | 1 + src/messages/message.cpp | 22 +++++++ src/messages/message.hpp | 3 +- src/widgets/helper/channelview.cpp | 2 +- src/widgets/helper/searchpopup.cpp | 87 +++++++++++++++++++++++++++ src/widgets/helper/searchpopup.hpp | 31 ++++++++++ src/widgets/split.cpp | 12 ++++ src/widgets/split.hpp | 3 + 9 files changed, 163 insertions(+), 4 deletions(-) create mode 100644 src/widgets/helper/searchpopup.cpp create mode 100644 src/widgets/helper/searchpopup.hpp diff --git a/chatterino.pro b/chatterino.pro index 6cd331ed..314090dd 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -114,7 +114,8 @@ SOURCES += \ src/singletons/helper/completionmodel.cpp \ src/singletons/resourcemanager.cpp \ src/singletons/helper/ircmessagehandler.cpp \ - src/singletons/pathmanager.cpp + src/singletons/pathmanager.cpp \ + src/widgets/helper/searchpopup.cpp HEADERS += \ src/precompiled_headers.hpp \ @@ -198,7 +199,8 @@ HEADERS += \ src/util/serialize-custom.hpp \ src/messages/highlightphrase.hpp \ src/messages/selection.hpp \ - src/singletons/pathmanager.hpp + src/singletons/pathmanager.hpp \ + src/widgets/helper/searchpopup.hpp PRECOMPILED_HEADER = diff --git a/src/messages/limitedqueuesnapshot.hpp b/src/messages/limitedqueuesnapshot.hpp index 3e1ece0c..84dc83b3 100644 --- a/src/messages/limitedqueuesnapshot.hpp +++ b/src/messages/limitedqueuesnapshot.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include diff --git a/src/messages/message.cpp b/src/messages/message.cpp index 3003c584..28149984 100644 --- a/src/messages/message.cpp +++ b/src/messages/message.cpp @@ -38,6 +38,10 @@ int Message::getTimeoutCount() const const QString &Message::getContent() const { + if (this->content.isNull()) { + this->updateContent(); + } + return this->content; } @@ -86,6 +90,24 @@ void Message::setDisableCompactEmotes(bool value) this->disableCompactEmotes = value; } +void Message::updateContent() const +{ + QString _content(""); + + bool first; + + for (const Word &word : this->words) { + if (!first) { + _content += ""; + } + + _content += word.getCopyText(); + first = false; + } + + this->content = _content; +} + namespace { void AddCurrentTimestamp(Message *message) diff --git a/src/messages/message.hpp b/src/messages/message.hpp index db767cf4..c6f40522 100644 --- a/src/messages/message.hpp +++ b/src/messages/message.hpp @@ -32,6 +32,7 @@ public: void setCollapsedDefault(bool value); bool getDisableCompactEmotes() const; void setDisableCompactEmotes(bool value); + void updateContent() const; QString loginName; QString displayName; @@ -68,7 +69,7 @@ private: std::chrono::time_point parseTime; - QString content; + mutable QString content; QString id = ""; std::vector words; diff --git a/src/widgets/helper/channelview.cpp b/src/widgets/helper/channelview.cpp index a1232bbd..16351f3b 100644 --- a/src/widgets/helper/channelview.cpp +++ b/src/widgets/helper/channelview.cpp @@ -26,7 +26,6 @@ #define LAYOUT_WIDTH \ (this->width() - (this->scrollBar.isVisible() ? 16 : 4) * this->getDpiMultiplier()) -#define PAUSE_TIME 1000 using namespace chatterino::messages; @@ -443,6 +442,7 @@ void ChannelView::setChannel(std::shared_ptr newChannel) this->channel = newChannel; this->userPopupWidget.setChannel(newChannel); + this->layoutMessages(); } void ChannelView::detachChannel() diff --git a/src/widgets/helper/searchpopup.cpp b/src/widgets/helper/searchpopup.cpp new file mode 100644 index 00000000..1a3a9a29 --- /dev/null +++ b/src/widgets/helper/searchpopup.cpp @@ -0,0 +1,87 @@ +#include "searchpopup.hpp" + +#include +#include +#include + +#include "channel.hpp" +#include "widgets/helper/channelview.hpp" + +namespace chatterino { +namespace widgets { +SearchPopup::SearchPopup() +{ + this->initAsWindow(); + this->initLayout(); + this->resize(400, 600); +} + +void SearchPopup::initLayout() +{ + // VBOX + { + QVBoxLayout *layout1 = new QVBoxLayout(this); + layout1->setMargin(0); + + // HBOX + { + QHBoxLayout *layout2 = new QHBoxLayout(this); + layout2->setMargin(6); + + // SEARCH INPUT + { + this->searchInput = new QLineEdit(this); + layout2->addWidget(this->searchInput); + QObject::connect(this->searchInput, &QLineEdit::returnPressed, + [this] { this->performSearch(); }); + } + + // SEARCH BUTTON + { + QPushButton *searchButton = new QPushButton(this); + searchButton->setText("Search"); + layout2->addWidget(searchButton); + QObject::connect(searchButton, &QPushButton::clicked, + [this] { this->performSearch(); }); + } + + layout1->addLayout(layout2); + } + + // CHANNELVIEW + { + this->channelView = new ChannelView(this); + + layout1->addWidget(this->channelView); + } + + this->setLayout(layout1); + } +} + +void SearchPopup::setChannel(std::shared_ptr channel) +{ + this->snapshot = channel->getMessageSnapshot(); + this->performSearch(); + + this->setWindowTitle("Searching in " + channel->name + "s history"); +} + +void SearchPopup::performSearch() +{ + QString text = searchInput->text(); + + std::shared_ptr channel(new Channel("search")); + + for (size_t i = 0; i < this->snapshot.getLength(); i++) { + messages::SharedMessage message = this->snapshot[i]; + + if (text.isEmpty() || message->getContent().indexOf(this->searchInput->text()) != -1) { + channel->addMessage(message); + } + } + + this->channelView->setChannel(channel); +} +} +} diff --git a/src/widgets/helper/searchpopup.hpp b/src/widgets/helper/searchpopup.hpp new file mode 100644 index 00000000..71d7db24 --- /dev/null +++ b/src/widgets/helper/searchpopup.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include + +#include "messages/limitedqueuesnapshot.hpp" +#include "messages/message.hpp" +#include "widgets/basewidget.hpp" + +class QLineEdit; +namespace chatterino { +class Channel; +namespace widgets { +class ChannelView; + +class SearchPopup : public BaseWidget +{ +public: + SearchPopup(); + + void setChannel(std::shared_ptr channel); + +private: + messages::LimitedQueueSnapshot snapshot; + QLineEdit *searchInput; + ChannelView *channelView; + + void initLayout(); + void performSearch(); +}; +} +} diff --git a/src/widgets/split.cpp b/src/widgets/split.cpp index 1a3b94d8..4d1797e3 100644 --- a/src/widgets/split.cpp +++ b/src/widgets/split.cpp @@ -5,6 +5,7 @@ #include "singletons/windowmanager.hpp" #include "twitch/twitchmessagebuilder.hpp" #include "util/urlfetch.hpp" +#include "widgets/helper/searchpopup.hpp" #include "widgets/qualitypopup.hpp" #include "widgets/splitcontainer.hpp" #include "widgets/textinputdialog.hpp" @@ -76,6 +77,9 @@ Split::Split(SplitContainer *parent, const std::string &_uuid) // CTRL+R: Change Channel ezShortcut(this, "CTRL+R", &Split::doChangeChannel); + // CTRL+F: Search + ezShortcut(this, "CTRL+F", &Split::doSearch); + // xd // ezShortcut(this, "ALT+SHIFT+RIGHT", &Split::doIncFlexX); // ezShortcut(this, "ALT+SHIFT+LEFT", &Split::doDecFlexX); @@ -473,6 +477,14 @@ void Split::doCopy() QApplication::clipboard()->setText(this->view.getSelectedText()); } +void Split::doSearch() +{ + SearchPopup *popup = new SearchPopup(); + + popup->setChannel(this->getChannel()); + popup->show(); +} + template static Iter select_randomly(Iter start, Iter end, RandomGenerator &g) { diff --git a/src/widgets/split.hpp b/src/widgets/split.hpp index 98861744..09f3fd98 100644 --- a/src/widgets/split.hpp +++ b/src/widgets/split.hpp @@ -118,6 +118,9 @@ public slots: // Copy text from chat void doCopy(); + // Open a search popup + void doSearch(); + // Open viewer list of the channel void doOpenViewerList();