nheko/src/QuickSwitcher.cc

139 lines
4.3 KiB
C++
Raw Normal View History

2017-08-15 20:06:27 +02:00
/*
* nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QCompleter>
#include <QPainter>
2017-08-15 20:06:27 +02:00
#include <QStringListModel>
#include <QStyleOption>
2017-08-15 20:06:27 +02:00
#include <QTimer>
2018-04-27 00:57:46 +02:00
#include <QtConcurrent>
2017-08-15 20:06:27 +02:00
#include "QuickSwitcher.h"
2017-08-20 12:47:22 +02:00
RoomSearchInput::RoomSearchInput(QWidget *parent)
: TextField(parent)
{}
2017-08-15 20:06:27 +02:00
2017-08-20 12:47:22 +02:00
void
RoomSearchInput::keyPressEvent(QKeyEvent *event)
2017-08-15 20:06:27 +02:00
{
switch (event->key()) {
case Qt::Key_Tab:
case Qt::Key_Down: {
2017-09-10 11:59:21 +02:00
emit selectNextCompletion();
event->accept();
break;
}
case Qt::Key_Backtab:
case Qt::Key_Up: {
2017-09-10 11:59:21 +02:00
emit selectPreviousCompletion();
event->accept();
break;
}
default:
TextField::keyPressEvent(event);
2017-09-10 11:59:21 +02:00
}
2017-08-15 20:06:27 +02:00
}
void
RoomSearchInput::hideEvent(QHideEvent *event)
{
2017-09-10 11:59:21 +02:00
emit hiding();
TextField::hideEvent(event);
}
2018-04-27 00:57:46 +02:00
QuickSwitcher::QuickSwitcher(QSharedPointer<Cache> cache, QWidget *parent)
: QWidget(parent)
2018-04-27 00:57:46 +02:00
, cache_{cache}
2017-08-15 20:06:27 +02:00
{
2018-04-27 00:57:46 +02:00
qRegisterMetaType<std::vector<RoomSearchResult>>();
2017-09-10 11:59:21 +02:00
setMaximumWidth(450);
QFont font;
font.setPixelSize(20);
roomSearch_ = new RoomSearchInput(this);
roomSearch_->setFont(font);
2018-04-27 00:57:46 +02:00
roomSearch_->setPlaceholderText(tr("Search for a room..."));
2017-09-10 11:59:21 +02:00
topLayout_ = new QVBoxLayout(this);
topLayout_->addWidget(roomSearch_);
2018-04-27 00:57:46 +02:00
connect(this,
&QuickSwitcher::queryResults,
this,
[this](const std::vector<RoomSearchResult> &rooms) {
auto pos = mapToGlobal(roomSearch_->geometry().bottomLeft());
popup_.setFixedWidth(width());
popup_.addRooms(rooms);
popup_.move(pos.x() - topLayout_->margin(), pos.y() + topLayout_->margin());
popup_.show();
});
connect(roomSearch_, &QLineEdit::textEdited, this, [this](const QString &query) {
if (query.isEmpty()) {
popup_.hide();
2017-09-10 11:59:21 +02:00
return;
}
2018-04-27 00:57:46 +02:00
QtConcurrent::run([this, query = query.toLower()]() {
try {
emit queryResults(cache_->searchRooms(query.toStdString()));
} catch (const lmdb::error &e) {
qWarning() << "room search failed:" << e.what();
}
});
2017-09-10 11:59:21 +02:00
});
2018-04-27 00:57:46 +02:00
connect(roomSearch_,
&RoomSearchInput::selectNextCompletion,
&popup_,
&SuggestionsPopup::selectNextSuggestion);
connect(roomSearch_,
&RoomSearchInput::selectPreviousCompletion,
&popup_,
&SuggestionsPopup::selectPreviousSuggestion);
2018-04-27 13:04:13 +02:00
connect(&popup_, &SuggestionsPopup::itemSelected, this, [this](const QString &room_id) {
reset();
emit roomSelected(room_id);
});
2018-04-27 00:57:46 +02:00
connect(roomSearch_, &RoomSearchInput::hiding, this, [this]() { popup_.hide(); });
connect(roomSearch_, &QLineEdit::returnPressed, this, [this]() {
2018-04-27 13:04:13 +02:00
reset();
2018-04-27 00:57:46 +02:00
popup_.selectHoveredSuggestion<RoomItem>();
2017-09-10 11:59:21 +02:00
});
2017-08-15 20:06:27 +02:00
}
2017-08-20 12:47:22 +02:00
void
QuickSwitcher::paintEvent(QPaintEvent *)
2017-08-15 20:06:27 +02:00
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
2017-08-15 20:06:27 +02:00
}
2017-08-20 12:47:22 +02:00
void
QuickSwitcher::keyPressEvent(QKeyEvent *event)
2017-08-15 20:06:27 +02:00
{
2017-09-10 11:59:21 +02:00
if (event->key() == Qt::Key_Escape) {
event->accept();
2018-04-27 13:04:13 +02:00
reset();
2017-09-10 11:59:21 +02:00
}
2017-08-15 20:06:27 +02:00
}