summary refs log tree commit diff
path: root/src/QuickSwitcher.cc
blob: 29876f996ef442c7d37b2f9b1cbea6cd5d0475bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * 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 <QDebug>
#include <QStringListModel>
#include <QTimer>

#include "QuickSwitcher.h"

RoomSearchInput::RoomSearchInput(QWidget *parent)
  : TextField(parent)
{
}

bool
RoomSearchInput::focusNextPrevChild(bool next)
{
        Q_UNUSED(next);

        return false;
}

void
RoomSearchInput::keyPressEvent(QKeyEvent *event)
{
        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);
}

void
RoomSearchInput::hideEvent(QHideEvent *event)
{
        emit hiding();
        TextField::hideEvent(event);
}

QuickSwitcher::QuickSwitcher(QWidget *parent)
  : QFrame(parent)
{
        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();
                emit roomSelected(rooms_[this->roomSearch_->text().trimmed()]);

                roomSearch_->clear();
        });
}

void
QuickSwitcher::setRoomList(const QMap<QString, QString> &rooms)
{
        rooms_            = rooms;
        QStringList items = rooms.keys();

        completer_->setModel(new QStringListModel(items));
}

void
QuickSwitcher::showEvent(QShowEvent *)
{
        roomSearch_->setFocus();
}

void
QuickSwitcher::keyPressEvent(QKeyEvent *event)
{
        if (event->key() == Qt::Key_Escape) {
                roomSearch_->clear();
                event->accept();
                emit closing();
        }
}