1 #include "completermodel.h"
2 #include "iconmanager.h"
3 #include "common/unused.h"
4 #include "completerview.h"
5 #include <QIcon>
6 #include <QVariant>
7 
CompleterModel(QObject * parent)8 CompleterModel::CompleterModel(QObject *parent) :
9     QAbstractItemModel(parent)
10 {
11 }
12 
13 
index(int row,int column,const QModelIndex & parent) const14 QModelIndex CompleterModel::index(int row, int column, const QModelIndex& parent) const
15 {
16     if (parent.isValid())
17         return QModelIndex(); // no childrens
18 
19     return createIndex(row, column);
20 }
21 
parent(const QModelIndex & child) const22 QModelIndex CompleterModel::parent(const QModelIndex& child) const
23 {
24     UNUSED(child);
25     return QModelIndex();
26 }
27 
rowCount(const QModelIndex & parent) const28 int CompleterModel::rowCount(const QModelIndex& parent) const
29 {
30     UNUSED(parent);
31     return tokens.size();
32 }
33 
columnCount(const QModelIndex & parent) const34 int CompleterModel::columnCount(const QModelIndex& parent) const
35 {
36     UNUSED(parent);
37     return 1;
38 }
39 
data(const QModelIndex & index,int role) const40 QVariant CompleterModel::data(const QModelIndex& index, int role) const
41 {
42     int row = index.row();
43     if (row < 0 || row >= tokens.size())
44         return QVariant();
45 
46     ExpectedTokenPtr token = tokens[row];
47     switch (role)
48     {
49         case Qt::DisplayRole:
50         case VALUE:
51             return token->value;
52         case CONTEXT:
53             return token->contextInfo;
54         case LABEL:
55             return token->label;
56         case PREFIX:
57             return token->prefix;
58         case TYPE:
59             return (int)token->type;
60         case Qt::DecorationRole:
61             return getIcon(token->type);
62     }
63 
64     return QVariant();
65 }
66 
setCompleterView(CompleterView * view)67 void CompleterModel::setCompleterView(CompleterView* view)
68 {
69     completerView = view;
70 }
71 
setData(const QList<ExpectedTokenPtr> & data)72 void CompleterModel::setData(const QList<ExpectedTokenPtr>& data)
73 {
74     clear();
75     beginInsertRows(QModelIndex(), 0, data.size()-1);
76     tokens = data;
77     endInsertRows();
78 }
79 
setFilter(const QString & filter)80 void CompleterModel::setFilter(const QString& filter)
81 {
82     this->filter = filter;
83     applyFilter();
84 }
85 
getFilter() const86 QString CompleterModel::getFilter() const
87 {
88     return filter;
89 }
90 
applyFilter()91 void CompleterModel::applyFilter()
92 {
93     bool empty = filter.isEmpty();
94     QModelIndex idx;
95     QString value;
96     QString prefix;
97     bool matched = empty;
98     for (int i = 0; i < rowCount(QModelIndex()); i++)
99     {
100         if (!empty)
101         {
102             idx = index(i, 0, QModelIndex());
103             value = idx.data(VALUE).toString();
104             prefix = idx.data(PREFIX).toString();
105             if (!prefix.isEmpty())
106                 value.prepend(prefix+".");
107 
108             matched = value.startsWith(filter, Qt::CaseInsensitive);
109         }
110 
111         completerView->setRowHidden(i, !matched);
112     }
113 }
114 
115 
clear()116 void CompleterModel::clear()
117 {
118     beginResetModel();
119     tokens.clear();
120     endResetModel();
121 }
122 
getToken(int index) const123 ExpectedTokenPtr CompleterModel::getToken(int index) const
124 {
125     if (index < 0 || index >= tokens.size())
126         return ExpectedTokenPtr();
127 
128     return tokens[index];
129 }
130 
getIcon(ExpectedToken::Type type) const131 QIcon CompleterModel::getIcon(ExpectedToken::Type type) const
132 {
133     switch (type)
134     {
135         case ExpectedToken::COLUMN:
136             return ICONS.COLUMN;
137         case ExpectedToken::TABLE:
138             return ICONS.TABLE;
139         case ExpectedToken::INDEX:
140             return ICONS.INDEX;
141         case ExpectedToken::TRIGGER:
142             return ICONS.TRIGGER;
143         case ExpectedToken::VIEW:
144             return ICONS.VIEW;
145         case ExpectedToken::DATABASE:
146             return ICONS.DATABASE;
147         case ExpectedToken::OTHER:
148             return ICONS.COMPLETER_OTHER;
149         case ExpectedToken::KEYWORD:
150             return ICONS.KEYWORD;
151         case ExpectedToken::FUNCTION:
152             return ICONS.FUNCTION;
153         case ExpectedToken::OPERATOR:
154             return ICONS.COMPLETER_OPERATOR;
155         case ExpectedToken::STRING:
156             return ICONS.COMPLETER_STRING;
157         case ExpectedToken::NUMBER:
158             return ICONS.COMPLETER_NUMBER;
159         case ExpectedToken::BLOB:
160             return ICONS.COMPLETER_BLOB;
161         case ExpectedToken::COLLATION:
162             return ICONS.CONSTRAINT_COLLATION;
163         case ExpectedToken::PRAGMA:
164             return ICONS.COMPLETER_PRAGMA;
165         case ExpectedToken::NO_VALUE:
166             return ICONS.COMPLETER_NO_VALUE;
167     }
168 
169     return ICONS.COMPLETER_NO_VALUE;
170 }
171