Changeset View
Changeset View
Standalone View
Standalone View
src/main/KexiSearchLineEdit.cpp
Show First 20 Lines • Show All 46 Lines • ▼ Show 20 Line(s) | |||||
47 | { | 47 | { | ||
48 | Q_OBJECT | 48 | Q_OBJECT | ||
49 | public: | 49 | public: | ||
50 | explicit KexiSearchLineEditCompleterPopupModel(QObject *parent = 0); | 50 | explicit KexiSearchLineEditCompleterPopupModel(QObject *parent = 0); | ||
51 | ~KexiSearchLineEditCompleterPopupModel(); | 51 | ~KexiSearchLineEditCompleterPopupModel(); | ||
52 | virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; | 52 | virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; | ||
53 | virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; | 53 | virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; | ||
54 | virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; | 54 | virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; | ||
55 | | ||||
56 | public Q_SLOTS: | ||||
57 | //! Adds a new model or updates information (model items) about existing one | ||||
55 | void addSearchableModel(KexiSearchableModel *model); | 58 | void addSearchableModel(KexiSearchableModel *model); | ||
59 | | ||||
60 | //! Removes existing model | ||||
61 | void removeSearchableModel(KexiSearchableModel *model); | ||||
62 | | ||||
56 | private: | 63 | private: | ||
57 | class Private; | 64 | class Private; | ||
58 | Private * const d; | 65 | Private * const d; | ||
59 | }; | 66 | }; | ||
60 | 67 | | |||
61 | class Q_DECL_HIDDEN KexiSearchLineEditCompleterPopupModel::Private | 68 | class Q_DECL_HIDDEN KexiSearchLineEditCompleterPopupModel::Private | ||
62 | { | 69 | { | ||
63 | public: | 70 | public: | ||
64 | Private() | 71 | Private() | ||
65 | : cachedCount(-1) | 72 | : cachedCount(-1) | ||
66 | { | 73 | { | ||
67 | } | 74 | } | ||
68 | ~Private() { | 75 | ~Private() { | ||
69 | qDeleteAll(searchableObjects); | 76 | qDeleteAll(searchableObjects); | ||
70 | } | 77 | } | ||
71 | void updateCachedCount() { | 78 | void removeSearchableModel(KexiSearchableModel *model) { | ||
72 | if (searchableModels.isEmpty()) { | 79 | if (searchableModels.removeAll(model) == 0) { | ||
73 | return; | 80 | return; | ||
74 | } | 81 | } | ||
82 | QMutableMapIterator<int, SearchableObject *> it(searchableObjects); | ||||
83 | while (it.hasNext()) { | ||||
84 | it.next(); | ||||
85 | if (it.value()->model == model) { | ||||
86 | it.remove(); | ||||
87 | } | ||||
88 | } | ||||
89 | } | ||||
90 | void updateCachedCount() { | ||||
75 | cachedCount = 0; | 91 | cachedCount = 0; | ||
76 | foreach (KexiSearchableModel* searchableModel, searchableModels) { | 92 | foreach (KexiSearchableModel* searchableModel, searchableModels) { | ||
77 | cachedCount += searchableModel->searchableObjectCount(); | 93 | cachedCount += searchableModel->searchableObjectCount(); | ||
78 | } | 94 | } | ||
79 | } | 95 | } | ||
80 | int cachedCount; | 96 | int cachedCount; | ||
81 | QList<KexiSearchableModel*> searchableModels; | 97 | QList<KexiSearchableModel*> searchableModels; | ||
82 | QMap<int, SearchableObject*> searchableObjects; | 98 | QMap<int, SearchableObject*> searchableObjects; | ||
▲ Show 20 Lines • Show All 58 Lines • ▼ Show 20 Line(s) | 133 | { | |||
141 | if (!sobject) { | 157 | if (!sobject) { | ||
142 | return QModelIndex(); | 158 | return QModelIndex(); | ||
143 | } | 159 | } | ||
144 | return createIndex(row, column, sobject); | 160 | return createIndex(row, column, sobject); | ||
145 | } | 161 | } | ||
146 | 162 | | |||
147 | void KexiSearchLineEditCompleterPopupModel::addSearchableModel(KexiSearchableModel *model) | 163 | void KexiSearchLineEditCompleterPopupModel::addSearchableModel(KexiSearchableModel *model) | ||
148 | { | 164 | { | ||
149 | d->searchableModels.removeAll(model); | 165 | if (!model) { | ||
166 | return; | ||||
167 | } | ||||
168 | beginResetModel(); | ||||
169 | d->removeSearchableModel(model); | ||||
150 | d->searchableModels.append(model); | 170 | d->searchableModels.append(model); | ||
171 | connect(model->deleteNotifier(), &KexiSearchableModelDeleteNotifier::aboutToDelete, this, | ||||
172 | &KexiSearchLineEditCompleterPopupModel::removeSearchableModel, Qt::UniqueConnection); | ||||
151 | d->updateCachedCount(); | 173 | d->updateCachedCount(); | ||
174 | endResetModel(); | ||||
175 | } | ||||
176 | | ||||
177 | void KexiSearchLineEditCompleterPopupModel::removeSearchableModel(KexiSearchableModel *model) | ||||
178 | { | ||||
179 | if (!model || !d->searchableModels.contains(model)) { | ||||
180 | return; | ||||
181 | } | ||||
182 | beginResetModel(); | ||||
183 | d->removeSearchableModel(model); | ||||
184 | d->updateCachedCount(); | ||||
185 | endResetModel(); | ||||
152 | } | 186 | } | ||
153 | 187 | | |||
154 | // ---- | 188 | // ---- | ||
155 | 189 | | |||
156 | class KexiSearchLineEditCompleter : public KexiCompleter | 190 | class KexiSearchLineEditCompleter : public KexiCompleter | ||
157 | { | 191 | { | ||
158 | Q_OBJECT | 192 | Q_OBJECT | ||
159 | public: | 193 | public: | ||
▲ Show 20 Lines • Show All 279 Lines • ▼ Show 20 Line(s) | 463 | { | |||
439 | } | 473 | } | ||
440 | } | 474 | } | ||
441 | 475 | | |||
442 | void KexiSearchLineEdit::addSearchableModel(KexiSearchableModel *model) | 476 | void KexiSearchLineEdit::addSearchableModel(KexiSearchableModel *model) | ||
443 | { | 477 | { | ||
444 | d->model->addSearchableModel(model); | 478 | d->model->addSearchableModel(model); | ||
445 | } | 479 | } | ||
446 | 480 | | |||
481 | void KexiSearchLineEdit::removeSearchableModel(KexiSearchableModel *model) | ||||
482 | { | ||||
483 | d->model->removeSearchableModel(model); | ||||
484 | } | ||||
485 | | ||||
447 | QPair<QModelIndex, KexiSearchableModel*> KexiSearchLineEdit::mapCompletionIndexToSource(const QModelIndex &index) const | 486 | QPair<QModelIndex, KexiSearchableModel*> KexiSearchLineEdit::mapCompletionIndexToSource(const QModelIndex &index) const | ||
448 | { | 487 | { | ||
449 | QModelIndex realIndex | 488 | QModelIndex realIndex | ||
450 | = qobject_cast<QAbstractProxyModel*>(d->completer->completionModel())->mapToSource(index); | 489 | = qobject_cast<QAbstractProxyModel*>(d->completer->completionModel())->mapToSource(index); | ||
451 | if (!realIndex.isValid()) { | 490 | if (!realIndex.isValid()) { | ||
452 | return qMakePair(QModelIndex(), static_cast<KexiSearchableModel*>(0)); | 491 | return qMakePair(QModelIndex(), static_cast<KexiSearchableModel*>(0)); | ||
453 | } | 492 | } | ||
454 | SearchableObject *object = static_cast<SearchableObject*>(realIndex.internalPointer()); | 493 | SearchableObject *object = static_cast<SearchableObject*>(realIndex.internalPointer()); | ||
▲ Show 20 Lines • Show All 390 Lines • Show Last 20 Lines |