summaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/guiSQLiteStudio/completer/completerview.cpp
blob: a209c423ad766bb4a957dc30fe9ff1b144f5e619 (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
#include "completerview.h"
#include "completeritemdelegate.h"
#include "sqleditor.h"
#include <QKeySequence>
#include <QMouseEvent>
#include <QDebug>

CompleterView::CompleterView(QWidget *parent) :
    QListView(parent)
{
    setItemDelegate(new CompleterItemDelegate(this));
}

void CompleterView::selectFirstVisible()
{
    QModelIndex idx;
    for (int i = 0; i < model()->rowCount(); i++)
    {
        if (isRowHidden(i))
            continue;

        idx = model()->index(i, 0, QModelIndex());
        selectionModel()->setCurrentIndex(idx, QItemSelectionModel::ClearAndSelect);
        break;
    }
}

bool CompleterView::hasVisibleItem() const
{
    return countVisibleItem() > 0;
}

int CompleterView::countVisibleItem() const
{
    int cnt = 0;
    for (int i = 0; i < model()->rowCount(); i++)
    {
        if (!isRowHidden(i))
            cnt++;
    }
    return cnt;
}

void CompleterView::focusOutEvent(QFocusEvent* e)
{
    emit focusOut();
    QListView::focusOutEvent(e);
}

void CompleterView::keyPressEvent(QKeyEvent* e)
{
    QKeySequence hotkey = GET_SHORTCUTS_CATEGORY(SqlEditor).COMPLETE.get();
    QKeySequence theKey = QKeySequence(e->key() | e->modifiers());
    if (hotkey == theKey)
        return;

    QString txt = e->text();
    if (!txt.isEmpty() && txt[0].isPrint())
    {
        emit textTyped(txt);
        return;
    }

    switch (e->key())
    {
        case Qt::Key_Backspace:
            emit backspace();
            return;
        case Qt::Key_Left:
            emit left();
            return;
        case Qt::Key_Right:
            emit right();
            return;
    }

    QListView::keyPressEvent(e);
}