]> git.sur5r.net Git - minitube/blob - src/autocomplete.cpp
Fixed bug with downloads not starting
[minitube] / src / autocomplete.cpp
1 #include "autocomplete.h"
2 #include "suggester.h"
3
4 AutoComplete::AutoComplete(QWidget *parent, QLineEdit *editor):
5     QObject(parent), buddy(parent), editor(editor), suggester(0) {
6
7     enabled = true;
8
9     popup = new QListWidget;
10     popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
11     popup->setMouseTracking(true);
12     popup->setWindowOpacity(.9);
13     popup->installEventFilter(this);
14     popup->setWindowFlags(Qt::Popup);
15     popup->setFocusPolicy(Qt::NoFocus);
16     popup->setFocusProxy(buddy);
17
18     connect(popup, SIGNAL(itemClicked(QListWidgetItem*)), SLOT(doneCompletion()));
19
20     // connect(popup, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
21     //    SLOT(currentItemChanged(QListWidgetItem *)));
22
23     // mouse hover
24     // connect(popup, SIGNAL(itemEntered(QListWidgetItem*)),
25     //    SLOT(currentItemChanged(QListWidgetItem *)));
26
27     timer = new QTimer(this);
28     timer->setSingleShot(true);
29     timer->setInterval(600);
30     connect(timer, SIGNAL(timeout()), SLOT(autoSuggest()));
31     connect(buddy, SIGNAL(textChanged(QString)), timer, SLOT(start()));
32
33 }
34
35 AutoComplete::~AutoComplete() {
36     delete popup;
37 }
38
39 bool AutoComplete::eventFilter(QObject *obj, QEvent *ev) {
40     if (obj != popup)
41         return false;
42
43     if (ev->type() == QEvent::FocusOut) {
44         popup->hide();
45         buddy->setFocus();
46         return true;
47     }
48
49     if (ev->type() == QEvent::MouseButtonPress) {
50         popup->hide();
51         buddy->setFocus();
52         editor->setText(originalText);
53         return true;
54     }
55
56     if (ev->type() == QEvent::KeyPress) {
57
58         bool consumed = false;
59
60         QKeyEvent *keyEvent = static_cast<QKeyEvent*>(ev);
61         int key = keyEvent->key();
62         // qDebug() << keyEvent->text();
63         switch (key) {
64         case Qt::Key_Enter:
65         case Qt::Key_Return:
66             if (popup->currentItem()) {
67                 doneCompletion();
68                 consumed = true;
69             } else {
70                 buddy->setFocus();
71                 editor->event(ev);
72                 popup->hide();
73             }
74             break;
75
76         case Qt::Key_Escape:
77             buddy->setFocus();
78             editor->setText(originalText);
79             popup->hide();
80             consumed = true;
81             break;
82
83         case Qt::Key_Up:
84         case Qt::Key_Down:
85         case Qt::Key_Home:
86         case Qt::Key_End:
87         case Qt::Key_PageUp:
88         case Qt::Key_PageDown:
89             break;
90
91         default:
92             // qDebug() << keyEvent->text();
93             buddy->setFocus();
94             editor->event(ev);
95             popup->hide();
96             break;
97         }
98
99         return consumed;
100     }
101
102     return false;
103 }
104
105 void AutoComplete::showCompletion(const QStringList &choices) {
106
107     if (choices.isEmpty())
108         return;
109
110     popup->setUpdatesEnabled(false);
111     popup->clear();
112     for (int i = 0; i < choices.count(); ++i) {
113         QListWidgetItem * item;
114         item = new QListWidgetItem(popup);
115         item->setText(choices[i]);
116     }
117     popup->setCurrentItem(0);
118     popup->adjustSize();
119     popup->setUpdatesEnabled(true);
120
121     int h = popup->sizeHintForRow(0) * choices.count() + 4;
122     popup->resize(buddy->width(), h);
123
124     popup->move(buddy->mapToGlobal(QPoint(0, buddy->height())));
125
126     popup->setFocus();
127     popup->show();
128 }
129
130 void AutoComplete::doneCompletion() {
131     timer->stop();
132     popup->hide();
133     buddy->setFocus();
134     QListWidgetItem *item = popup->currentItem();
135     if (item) {
136         editor->setText(item->text());
137         emit suggestionAccepted(item->text());
138     }
139 }
140
141 void AutoComplete::preventSuggest() {
142     // qDebug() << "preventSuggest";
143     timer->stop();
144     enabled = false;
145     popup->hide();
146 }
147
148 void AutoComplete::enableSuggest() {
149     // qDebug() << "enableSuggest";
150     enabled = true;
151 }
152
153 void AutoComplete::setSuggester(Suggester* suggester) {
154     if (this->suggester) this->suggester->disconnect();
155     this->suggester = suggester;
156     connect(suggester, SIGNAL(ready(QStringList)), SLOT(suggestionsReady(QStringList)));
157 }
158
159 void AutoComplete::autoSuggest() {
160     if (!enabled) return;
161     if (!buddy->hasFocus()) return;
162
163     QString query = editor->text();
164     originalText = query;
165     // qDebug() << "originalText" << originalText;
166     if (query.isEmpty()) {
167         popup->hide();
168         buddy->setFocus();
169         return;
170     }
171
172     if (suggester)
173         suggester->suggest(query);
174 }
175
176 void AutoComplete::suggestionsReady(QStringList suggestions) {
177     if (!enabled) return;
178     showCompletion(suggestions);
179 }
180
181 void AutoComplete::currentItemChanged(QListWidgetItem *current) {
182     if (current) {
183         // qDebug() << "current" << current->text();
184         current->setSelected(true);
185         editor->setText(current->text());
186         editor->setSelection(originalText.length(), editor->text().length());
187     }
188 }