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

103 lines
2.5 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 {
2018-06-26 16:37:59 +02:00
2018-01-05 13:42:23 +01:00
SearchPopup::SearchPopup()
{
this->initLayout();
this->resize(400, 600);
}
void SearchPopup::setChannel(ChannelPtr channel)
{
this->snapshot_ = channel->getMessageSnapshot();
this->performSearch();
this->setWindowTitle("Searching in " + channel->getName() + "s history");
}
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);
// HBOX
{
QHBoxLayout *layout2 = new QHBoxLayout(this);
layout2->setMargin(6);
// 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,
2018-01-05 13:42:23 +01:00
[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
{
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);
}
}
void SearchPopup::performSearch()
{
2018-07-06 19:23:47 +02:00
QString text = searchInput_->text();
2018-01-05 13:42:23 +01:00
2018-07-06 17:30:12 +02:00
ChannelPtr channel(new Channel("search", Channel::Type::None));
2018-01-05 13:42:23 +01:00
2018-11-03 21:26:57 +01:00
for (size_t i = 0; i < this->snapshot_.size(); i++)
2018-10-21 13:43:02 +02:00
{
2018-07-06 19:23:47 +02:00
MessagePtr message = this->snapshot_[i];
2018-01-05 13:42:23 +01:00
2018-01-28 03:29:42 +01:00
if (text.isEmpty() ||
2018-08-06 21:17:03 +02:00
message->searchText.indexOf(this->searchInput_->text(), 0,
2018-10-21 13:43:02 +02:00
Qt::CaseInsensitive) != -1)
{
2018-01-05 13:42:23 +01:00
channel->addMessage(message);
}
}
2018-07-06 19:23:47 +02:00
this->channelView_->setChannel(channel);
2018-01-05 13:42:23 +01:00
}
2018-06-26 16:37:59 +02:00
} // namespace chatterino