2017-08-15 21:06:27 +03: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 <QStringListModel>
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
#include "QuickSwitcher.h"
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
RoomSearchInput::RoomSearchInput(QWidget *parent)
|
|
|
|
: TextField(parent)
|
2017-10-15 22:08:51 +03:00
|
|
|
{}
|
2017-08-15 21:06:27 +03:00
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
bool
|
|
|
|
RoomSearchInput::focusNextPrevChild(bool next)
|
2017-08-15 21:06:27 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
Q_UNUSED(next);
|
2017-08-15 21:06:27 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
return false;
|
2017-08-15 21:06:27 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
RoomSearchInput::keyPressEvent(QKeyEvent *event)
|
2017-08-15 21:06:27 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
if (event->key() == Qt::Key_Tab || event->key() == Qt::Key_Down) {
|
|
|
|
emit selectNextCompletion();
|
|
|
|
event->accept();
|
|
|
|
return;
|
|
|
|
} else if (event->key() == Qt::Key_Up) {
|
|
|
|
emit selectPreviousCompletion();
|
|
|
|
event->accept();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextField::keyPressEvent(event);
|
2017-08-15 21:06:27 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 21:13:00 +03:00
|
|
|
void
|
|
|
|
RoomSearchInput::hideEvent(QHideEvent *event)
|
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
emit hiding();
|
|
|
|
TextField::hideEvent(event);
|
2017-08-20 21:13:00 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
QuickSwitcher::QuickSwitcher(QWidget *parent)
|
|
|
|
: QFrame(parent)
|
2017-08-15 21:06:27 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
setMaximumWidth(450);
|
|
|
|
setStyleSheet("background-color: white");
|
|
|
|
|
|
|
|
QFont font;
|
|
|
|
font.setPixelSize(20);
|
|
|
|
|
|
|
|
roomSearch_ = new RoomSearchInput(this);
|
|
|
|
roomSearch_->setFont(font);
|
|
|
|
roomSearch_->setPlaceholderText(tr("Find a room..."));
|
|
|
|
|
|
|
|
completer_ = new QCompleter();
|
|
|
|
completer_->setCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
completer_->setCompletionMode(QCompleter::PopupCompletion);
|
|
|
|
completer_->setWidget(this);
|
|
|
|
|
|
|
|
topLayout_ = new QVBoxLayout(this);
|
|
|
|
topLayout_->addWidget(roomSearch_);
|
|
|
|
|
|
|
|
connect(completer_, SIGNAL(highlighted(QString)), roomSearch_, SLOT(setText(QString)));
|
|
|
|
connect(roomSearch_, &QLineEdit::textEdited, this, [=](const QString &prefix) {
|
|
|
|
if (prefix.isEmpty()) {
|
|
|
|
completer_->popup()->hide();
|
|
|
|
selection_ = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prefix != completer_->completionPrefix()) {
|
|
|
|
completer_->setCompletionPrefix(prefix);
|
|
|
|
selection_ = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
completer_->popup()->setWindowFlags(completer_->popup()->windowFlags() |
|
|
|
|
Qt::ToolTip | Qt::NoDropShadowWindowHint);
|
|
|
|
completer_->popup()->setAttribute(Qt::WA_ShowWithoutActivating);
|
|
|
|
completer_->complete();
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(roomSearch_, &RoomSearchInput::selectNextCompletion, this, [=]() {
|
|
|
|
selection_ += 1;
|
|
|
|
|
|
|
|
if (!completer_->setCurrentRow(selection_)) {
|
|
|
|
selection_ = 0;
|
|
|
|
completer_->setCurrentRow(selection_);
|
|
|
|
}
|
|
|
|
|
|
|
|
completer_->popup()->setCurrentIndex(completer_->currentIndex());
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(roomSearch_, &RoomSearchInput::selectPreviousCompletion, this, [=]() {
|
|
|
|
selection_ -= 1;
|
|
|
|
|
|
|
|
if (!completer_->setCurrentRow(selection_)) {
|
|
|
|
selection_ = completer_->completionCount() - 1;
|
|
|
|
completer_->setCurrentRow(selection_);
|
|
|
|
}
|
|
|
|
|
|
|
|
completer_->popup()->setCurrentIndex(completer_->currentIndex());
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(
|
|
|
|
roomSearch_, &RoomSearchInput::hiding, this, [=]() { completer_->popup()->hide(); });
|
|
|
|
connect(roomSearch_, &QLineEdit::returnPressed, this, [=]() {
|
|
|
|
emit closing();
|
2017-11-03 09:54:17 +03:00
|
|
|
|
|
|
|
QString text("");
|
|
|
|
|
|
|
|
if (selection_ == -1) {
|
|
|
|
completer_->setCurrentRow(0);
|
|
|
|
text = completer_->currentCompletion();
|
|
|
|
} else {
|
|
|
|
text = this->roomSearch_->text().trimmed();
|
|
|
|
}
|
|
|
|
emit roomSelected(rooms_[text]);
|
2017-09-10 12:59:21 +03:00
|
|
|
|
|
|
|
roomSearch_->clear();
|
|
|
|
});
|
2017-08-15 21:06:27 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2018-01-24 21:46:37 +03:00
|
|
|
QuickSwitcher::setRoomList(const std::map<QString, QString> &rooms)
|
2017-08-15 21:06:27 +03:00
|
|
|
{
|
2018-01-24 21:46:37 +03:00
|
|
|
rooms_ = rooms;
|
|
|
|
|
|
|
|
QStringList items;
|
|
|
|
for (const auto &room : rooms)
|
|
|
|
items << room.first;
|
2017-08-15 21:06:27 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
completer_->setModel(new QStringListModel(items));
|
2017-08-15 21:06:27 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
QuickSwitcher::showEvent(QShowEvent *)
|
2017-08-15 21:06:27 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
roomSearch_->setFocus();
|
2017-08-15 21:06:27 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
QuickSwitcher::keyPressEvent(QKeyEvent *event)
|
2017-08-15 21:06:27 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
if (event->key() == Qt::Key_Escape) {
|
|
|
|
roomSearch_->clear();
|
|
|
|
event->accept();
|
|
|
|
emit closing();
|
|
|
|
}
|
2017-08-15 21:06:27 +03:00
|
|
|
}
|