matrixion/src/QuickSwitcher.cc

120 lines
2.7 KiB
C++
Raw Normal View History

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 <QAbstractItemView>
#include <QCompleter>
#include <QDebug>
#include <QStringListModel>
#include <QTimer>
#include "QuickSwitcher.h"
2017-08-20 13:47:22 +03:00
RoomSearchInput::RoomSearchInput(QWidget *parent)
: TextField(parent)
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
{
Q_UNUSED(next);
return false;
}
2017-08-20 13:47:22 +03:00
void
RoomSearchInput::keyPressEvent(QKeyEvent *event)
2017-08-15 21:06:27 +03:00
{
if (event->key() == Qt::Key_Tab) {
auto completer = this->completer();
if (completer) {
// Enable the current item if its valid.
completer->popup()->setCurrentIndex(completer->currentIndex());
if (!completer->setCurrentRow(completer->currentRow() + 1))
completer->setCurrentRow(0);
}
event->accept();
return;
}
TextField::keyPressEvent(event);
}
2017-08-20 13:47:22 +03:00
QuickSwitcher::QuickSwitcher(QWidget *parent)
: QFrame(parent)
2017-08-15 21:06:27 +03:00
{
setMaximumWidth(400);
setStyleSheet("background-color: #f9f9f9");
QFont font;
font.setPixelSize(20);
roomSearch_ = new RoomSearchInput(this);
roomSearch_->setFont(font);
roomSearch_->setPlaceholderText(tr("Find a room..."));
QStringList wordList;
2017-08-20 13:47:22 +03:00
QCompleter *completer = new QCompleter(wordList, this);
2017-08-15 21:06:27 +03:00
completer->setCaseSensitivity(Qt::CaseInsensitive);
roomSearch_->setCompleter(completer);
topLayout_ = new QVBoxLayout(this);
topLayout_->setMargin(20);
topLayout_->setSpacing(0);
topLayout_->addWidget(roomSearch_);
connect(roomSearch_, &QLineEdit::returnPressed, this, [=]() {
emit closing();
emit roomSelected(rooms_[this->roomSearch_->text().trimmed()]);
roomSearch_->clear();
});
}
2017-08-20 13:47:22 +03:00
void
QuickSwitcher::setRoomList(const QMap<QString, QString> &rooms)
2017-08-15 21:06:27 +03:00
{
rooms_ = rooms;
QStringList search_items = rooms.keys();
if (!roomSearch_->completer())
return;
roomSearch_->completer()->setModel(new QStringListModel(search_items));
}
2017-08-20 13:47:22 +03:00
void
QuickSwitcher::showEvent(QShowEvent *)
2017-08-15 21:06:27 +03:00
{
roomSearch_->setFocus();
}
2017-08-20 13:47:22 +03:00
void
QuickSwitcher::keyPressEvent(QKeyEvent *event)
2017-08-15 21:06:27 +03:00
{
if (event->key() == Qt::Key_Escape) {
roomSearch_->clear();
event->accept();
emit closing();
}
}