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

117 lines
2.9 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "SearchPopup.hpp"
2018-01-05 13:42:23 +01:00
#include <QHBoxLayout>
#include <QLineEdit>
#include <QPushButton>
2018-01-05 13:42:23 +01:00
#include <QVBoxLayout>
2018-06-26 15:33:51 +02:00
#include "common/Channel.hpp"
#include "messages/Message.hpp"
2018-06-26 14:09:39 +02:00
#include "widgets/helper/ChannelView.hpp"
2018-01-05 13:42:23 +01:00
namespace chatterino {
2019-09-04 00:29:58 +02:00
namespace {
ChannelPtr filter(const QString &text, const QString &channelName,
const LimitedQueueSnapshot<MessagePtr> &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
2018-06-26 16:37:59 +02:00
2018-01-05 13:42:23 +01:00
SearchPopup::SearchPopup()
{
this->initLayout();
this->resize(400, 600);
}
2019-09-04 00:29:58 +02:00
void SearchPopup::setChannel(const ChannelPtr &channel)
{
this->channelName_ = channel->getName();
this->snapshot_ = channel->getMessageSnapshot();
2019-09-04 00:29:58 +02:00
this->search();
this->updateWindowTitle();
}
void SearchPopup::updateWindowTitle()
{
this->setWindowTitle("Searching in " + this->channelName_ + "s history");
}
2019-09-04 00:29:58 +02:00
void SearchPopup::search()
{
this->channelView_->setChannel(filter(this->searchInput_->text(),
this->channelName_, this->snapshot_));
}
void SearchPopup::keyPressEvent(QKeyEvent *e)
{
if (e->key() == Qt::Key_Escape)
{
this->close();
return;
}
BaseWidget::keyPressEvent(e);
}
2018-01-05 13:42:23 +01:00
void SearchPopup::initLayout()
{
// VBOX
{
QVBoxLayout *layout1 = new QVBoxLayout(this);
layout1->setMargin(0);
2019-09-04 00:29:58 +02:00
layout1->setSpacing(0);
2018-01-05 13:42:23 +01:00
// HBOX
{
QHBoxLayout *layout2 = new QHBoxLayout(this);
2019-09-04 00:29:58 +02:00
layout2->setMargin(8);
layout2->setSpacing(8);
2018-01-05 13:42:23 +01:00
// SEARCH INPUT
{
2018-07-06 19:23:47 +02:00
this->searchInput_ = new QLineEdit(this);
layout2->addWidget(this->searchInput_);
QObject::connect(this->searchInput_, &QLineEdit::returnPressed,
2019-09-04 00:29:58 +02:00
[this] { this->search(); });
2018-01-05 13:42:23 +01:00
}
// SEARCH BUTTON
{
QPushButton *searchButton = new QPushButton(this);
searchButton->setText("Search");
layout2->addWidget(searchButton);
QObject::connect(searchButton, &QPushButton::clicked,
2019-09-04 00:29:58 +02:00
[this] { this->search(); });
2018-01-05 13:42:23 +01:00
}
layout1->addLayout(layout2);
}
// CHANNELVIEW
{
2018-07-06 19:23:47 +02:00
this->channelView_ = new ChannelView(this);
2018-01-05 13:42:23 +01:00
2018-07-06 19:23:47 +02:00
layout1->addWidget(this->channelView_);
2018-01-05 13:42:23 +01:00
}
this->setLayout(layout1);
}
}
} // namespace chatterino