source: SMSSender/frontend/gui/ui/completers/bettercompleter.cpp @ 315:e6c8bb244ecd

separation-frontend-backend
Last change on this file since 315:e6c8bb244ecd was 315:e6c8bb244ecd, checked in by Sämy Zehnder <saemy.zehnder@…>, 6 years ago
  • Separates the frontend from the backend code.
  • At this point the schoolnet gateway is converted only.
  • Removes bc & da prefixes from the filenames.
File size: 5.6 KB
Line 
1/*
2 smssender - A frontend for fast and easy SMS sending over different gateways.
3 Copyright (C) 2007-2012, gorrión. See http://smssender.gorrion.ch
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18#include "bettercompleter.h"
19
20#include <QAbstractItemView>
21
22namespace UI {
23
24BetterCompletionModel::BetterCompletionModel(QObject* parent)
25    : QSortFilterProxyModel(parent)
26    , isValid_(false)
27    , searchMatcher_(QString(), Qt::CaseInsensitive, QRegExp::FixedString)
28{
29    setDynamicSortFilter(true);
30}
31
32QVariant BetterCompletionModel::data(const QModelIndex& index, int role) const {
33    // if we are valid, tell QCompleter that everything we have filtered matches
34    // what the user typed; if not, nothing matches
35    if (role == BetterCompletionRole && index.isValid()) {
36        if (isValid())
37            return QLatin1String("a");
38        else
39            return QLatin1String("b");
40    }
41
42    return QSortFilterProxyModel::data(index, role);
43}
44
45QRegExp BetterCompletionModel::searchMatcher() const {
46    return searchMatcher_;
47}
48
49QString BetterCompletionModel::searchString() const {
50    return searchString_;
51}
52
53void BetterCompletionModel::setFilterFixedString (const QString& pattern) {
54    QSortFilterProxyModel::setFilterFixedString(pattern);
55
56    setSearchString(pattern);
57}
58
59void BetterCompletionModel::setSearchString(const QString& str) {
60    if (str == searchString_)
61        return;
62
63    searchString_ = str;
64    searchMatcher_.setPattern(str);
65
66    invalidateFilter();
67}
68
69bool BetterCompletionModel::isValid() const {
70    return isValid_;
71}
72
73void BetterCompletionModel::setValid(bool b) {
74    if (b == isValid_)
75        return;
76
77    isValid_ = b;
78
79    // tell the AccountCompleter that we've changed
80    emit dataChanged(index(0, 0), index(columnCount() - 1, rowCount() - 1));
81}
82
83bool BetterCompletionModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const {
84    // do a case-insensitive substring match against the filterKeyColumns;
85    // we have also made sure that the user doesn't accidentally use regexp
86    // metacharacters
87    QList<QModelIndex> modelIndices;
88    int filterColumn = filterKeyColumn();
89    if (filterColumn == -1) {
90        for (int i = 0; i < sourceModel()->columnCount(source_parent); i++) {
91            modelIndices.append(sourceModel()->index(source_row, i, source_parent));
92        }
93    } else {
94        modelIndices.append(sourceModel()->index(source_row, filterColumn, source_parent));
95    }
96
97    foreach (QModelIndex index, modelIndices) {
98        QString field = sourceModel()->data(index, Qt::DisplayRole).toString();
99        if (searchMatcher_.indexIn(field) != -1)
100            return true;
101    }
102
103    return false;
104}
105
106
107
108
109BetterCompleter::BetterCompleter(QObject* parent)
110    : QCompleter(parent)
111{
112    init();
113}
114
115BetterCompleter::BetterCompleter(QAbstractItemModel* m, QObject* parent)
116    : QCompleter(m, parent)
117{
118    init();
119}
120
121void BetterCompleter::init() {
122    // we want to complete against our own faked role
123    setCompletionRole(BetterCompletionModel::BetterCompletionRole);
124
125    // and since we fake our completion role, we can take
126    // advantage of the sorted-model optimizations in QCompleter
127    setCaseSensitivity(Qt::CaseSensitive);
128    setModelSorting(QCompleter::CaseSensitivelySortedModel);
129
130    filterTimer_.setSingleShot(true);
131    connect(&filterTimer_, SIGNAL(timeout()), this, SLOT(updateFilter()));
132}
133
134QString BetterCompleter::pathFromIndex(const QModelIndex& index) const {
135    // we want to return the actual name from the account for the
136    // data the QCompleter finally returns
137    return model()->data(index, Qt::DisplayRole).toString();
138}
139
140QStringList BetterCompleter::splitPath(const QString& path) const {
141    if (path == searchString_)
142        return QStringList() << QLatin1String("a");
143
144    // queue an update to our search string
145    // We will wait a bit so that if the user is quickly typing,
146    // we don't try to complete until they pause.
147    if (filterTimer_.isActive())
148        filterTimer_.stop();
149    filterTimer_.start(150);
150
151    // if the previous search results are not a superset of
152    // the current search results, tell the model that it is not valid yet
153    if (!path.startsWith(searchString_)) {
154        BetterCompletionModel* completionModel = qobject_cast<BetterCompletionModel*>(model());
155        Q_ASSERT(completionModel);
156        completionModel->setValid(false);
157    }
158
159    searchString_ = path;
160
161    // the actual filtering is done by the BetterCompletionModel; we just
162    // return a short dummy here so that QCompleter thinks we match everything
163    return QStringList() << QLatin1String("a");
164}
165
166void BetterCompleter::updateFilter() {
167    BetterCompletionModel* completionModel = qobject_cast<BetterCompletionModel*>(model());
168    Q_ASSERT(completionModel);
169
170    // tell the BetterCompletionModel about the new search string
171    completionModel->setSearchString(searchString_);
172
173    // sort the model
174    completionModel->sort(0);
175
176    // mark it valid
177    completionModel->setValid(true);
178
179    // and now update the QCompleter widget
180    complete();
181}
182
183} // namespace UI
Note: See TracBrowser for help on using the repository browser.