mirror-chatterino2/src/widgets/helper/searchpopup.cpp

90 lines
2.3 KiB
C++
Raw Normal View History

2018-01-05 13:42:23 +01:00
#include "searchpopup.hpp"
#include <QHBoxLayout>
#include <QLineEdit>
#include <QVBoxLayout>
#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(SharedChannel channel)
2018-01-05 13:42:23 +01:00
{
this->snapshot = channel->getMessageSnapshot();
this->performSearch();
this->setWindowTitle("Searching in " + channel->name + "s history");
}
void SearchPopup::performSearch()
{
QString text = searchInput->text();
SharedChannel channel(new Channel("search"));
2018-01-05 13:42:23 +01:00
for (size_t i = 0; i < this->snapshot.getLength(); i++) {
messages::MessagePtr message = this->snapshot[i];
2018-01-05 13:42:23 +01:00
2018-01-07 00:05:32 +01:00
if (text.isEmpty() ||
message->getSearchText().indexOf(this->searchInput->text(), 0, Qt::CaseInsensitive) !=
2018-01-07 00:05:32 +01:00
-1) {
2018-01-05 13:42:23 +01:00
channel->addMessage(message);
}
}
this->channelView->setChannel(channel);
}
}
}