~ubuntu-branches/ubuntu/trusty/minitube/trusty

« back to all changes in this revision

Viewing changes to src/autocomplete.cpp

  • Committer: Package Import Robot
  • Author(s): Jakob Haufe
  • Date: 2012-09-29 02:43:53 UTC
  • mfrom: (2.1.10)
  • Revision ID: package-import@ubuntu.com-20120929024353-cvsvqeewq4p93pb4
Tags: 1.9-1
* New upstream version (Closes: #673696).
* Refresh disable-update-check.
* Refresh proper-tempfiles.
* Use hardening-wrapper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include "autocomplete.h"
2
2
#include "suggester.h"
3
 
 
4
 
AutoComplete::AutoComplete(QWidget *parent, QLineEdit *editor):
5
 
    QObject(parent), buddy(parent), editor(editor), suggester(0) {
6
 
 
 
3
#ifdef APP_MAC
 
4
#include "searchlineedit_mac.h"
 
5
#else
 
6
#include "searchlineedit.h"
 
7
#endif
 
8
 
 
9
AutoComplete::AutoComplete(SearchLineEdit *parent, QLineEdit *editor):
 
10
    QObject(parent), editor(editor), suggester(0) {
 
11
 
 
12
    buddy = parent;
7
13
    enabled = true;
8
14
 
9
15
    popup = new QListWidget;
13
19
    popup->installEventFilter(this);
14
20
    popup->setWindowFlags(Qt::Popup);
15
21
    popup->setFocusPolicy(Qt::NoFocus);
16
 
    popup->setFocusProxy(parent);
 
22
    popup->setFocusProxy(buddy);
17
23
 
18
24
    connect(popup, SIGNAL(itemClicked(QListWidgetItem*)), SLOT(doneCompletion()));
19
25
 
28
34
    timer->setSingleShot(true);
29
35
    timer->setInterval(600);
30
36
    connect(timer, SIGNAL(timeout()), SLOT(autoSuggest()));
31
 
#ifdef APP_MAC
32
 
    connect(parent, SIGNAL(textChanged(QString)), timer, SLOT(start()));
33
 
#else
34
 
    connect(editor, SIGNAL(textEdited(QString)), timer, SLOT(start()));
35
 
#endif
 
37
    connect(buddy, SIGNAL(textChanged(QString)), timer, SLOT(start()));
36
38
 
37
39
}
38
40
 
44
46
    if (obj != popup)
45
47
        return false;
46
48
 
 
49
    if (ev->type() == QEvent::FocusOut) {
 
50
        popup->hide();
 
51
        buddy->setFocus();
 
52
        return true;
 
53
    }
 
54
 
47
55
    if (ev->type() == QEvent::MouseButtonPress) {
48
56
        popup->hide();
49
 
        editor->setFocus();
50
 
        editor->setText(originalText);
 
57
        buddy->setFocus();
 
58
        buddy->setText(originalText);
51
59
        return true;
52
60
    }
53
61
 
65
73
                doneCompletion();
66
74
                consumed = true;
67
75
            } else {
68
 
                editor->setFocus();
 
76
                buddy->setFocus();
69
77
                editor->event(ev);
70
78
                popup->hide();
71
79
            }
72
80
            break;
73
81
 
74
82
        case Qt::Key_Escape:
75
 
            editor->setFocus();
 
83
            buddy->setFocus();
76
84
            editor->setText(originalText);
77
85
            popup->hide();
78
86
            consumed = true;
88
96
 
89
97
        default:
90
98
            // qDebug() << keyEvent->text();
91
 
            editor->setFocus();
 
99
            buddy->setFocus();
92
100
            editor->event(ev);
93
101
            popup->hide();
94
102
            break;
121
129
 
122
130
    popup->move(buddy->mapToGlobal(QPoint(0, buddy->height())));
123
131
 
124
 
    // popup->setFocus();
 
132
    popup->setFocus();
125
133
    popup->show();
126
134
}
127
135
 
128
136
void AutoComplete::doneCompletion() {
129
137
    timer->stop();
130
138
    popup->hide();
131
 
    editor->setFocus();
 
139
    buddy->setFocus();
132
140
    QListWidgetItem *item = popup->currentItem();
133
141
    if (item) {
134
 
        editor->setText(item->text());
 
142
        buddy->setText(item->text());
135
143
        emit suggestionAccepted(item->text());
136
144
    }
137
145
}
156
164
 
157
165
void AutoComplete::autoSuggest() {
158
166
    if (!enabled) return;
 
167
    if (!buddy->hasFocus()) return;
159
168
 
160
169
    QString query = editor->text();
161
170
    originalText = query;
162
171
    // qDebug() << "originalText" << originalText;
163
172
    if (query.isEmpty()) {
164
173
        popup->hide();
 
174
        buddy->setFocus();
165
175
        return;
166
176
    }
167
177
 
178
188
    if (current) {
179
189
        // qDebug() << "current" << current->text();
180
190
        current->setSelected(true);
181
 
        editor->setText(current->text());
 
191
        buddy->setText(current->text());
182
192
        editor->setSelection(originalText.length(), editor->text().length());
183
193
    }
184
194
}