diff options
author | Alexander Kornilov <Alexander.Kornilov@Teleca.com> | 2010-11-26 16:31:45 +0300 |
---|---|---|
committer | Pekka Vuorela <pekka.ta.vuorela@nokia.com> | 2010-12-03 15:50:23 +0200 |
commit | 4f030360f31248364c2de742da34a466f38bdae8 (patch) | |
tree | cb56f951d538d7cbf954224e09a15bb824b9f939 | |
parent | 033d8b60f6f4e938dd64525c718d02d4b129c0fb (diff) |
Changes: The signals handlers was implemented for async models.
RevBy: Ning Chi
-rw-r--r-- | src/corelib/widgets/mcompleter.cpp | 120 | ||||
-rw-r--r-- | src/corelib/widgets/mcompleter.h | 3 | ||||
-rw-r--r-- | src/corelib/widgets/mcompleter_p.h | 7 |
3 files changed, 99 insertions, 31 deletions
diff --git a/src/corelib/widgets/mcompleter.cpp b/src/corelib/widgets/mcompleter.cpp index f7320f73..d4122617 100644 --- a/src/corelib/widgets/mcompleter.cpp +++ b/src/corelib/widgets/mcompleter.cpp @@ -38,7 +38,7 @@ M_REGISTER_WIDGET(MCompleter) namespace { //! default delimiters are comma and semicolon. - const QString DefaultDelimiters(",;"); + const QString DefaultDelimiters(QString(",;") + QString(QChar::ObjectReplacementCharacter)); //! default match regular expression const QString DefaultMatchRegularExpression("(%1.*)||(.*\\W%1.*)"); //! default maximum hits is 10 @@ -225,34 +225,7 @@ void MCompleterPrivate::_q_complete() emit q->startCompleting(q->model()->completionPrefix(), completionModel->index(q->model()->matchedIndex(), q->valueColumnIndex())); - - // TODO #1: remove check for one of those signals once they have been merged. - // TODO #2: Change the "<= 1" back to "<= 0", because this is only needed for - // the current signal forwarding! - if ((q->receivers(SIGNAL(startCompleting(QString))) <= 0) && - (q->receivers(SIGNAL(startCompleting(QString, QModelIndex))) <= 1)) { - //if no customized match slot, then use default match rule - //limit the max hits to DefaultMaximumHits + 1 (+1 allow it to be larger than DefaultMaximumHits) - match(DefaultMaximumHits + 1); - } else { - //if customized, then think all items inside model are matched - matchedIndexList.clear(); - const int rowCount = completionModel->rowCount(); - for (int i = 0; i < rowCount; ++i) - matchedIndexList << i; - matchedModel->setMatchedList(matchedIndexList); - } - - if (matchedModel->rowCount() > 0) { - q->model()->setMatchedIndex(0); - q->model()->setActive(true); - q->widget()->sceneManager()->appearSceneWindow(q); - emit q->shown(); - } else if (q->isActive()) { - reset(); - q->hideCompleter(); - resetFocus(); - } + pollModel(true); } QString MCompleterPrivate::trim(const QString &s) @@ -284,8 +257,7 @@ void MCompleterPrivate::setCompletionModel(QAbstractItemModel *m, bool own) return; if (completionModel) { - QObject::disconnect(completionModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), - q, SLOT(_q_modelUpdate())); + QObject::disconnect(completionModel, 0, q, 0); } if (hasOwnModel && completionModel) { matchedIndexList.clear(); @@ -300,15 +272,101 @@ void MCompleterPrivate::setCompletionModel(QAbstractItemModel *m, bool own) q->model()->setValueColumnIndex(completionModel->columnCount() - 1); QObject::connect(completionModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), q, SLOT(_q_modelUpdate())); + QObject::connect(completionModel, SIGNAL(modelAboutToBeReset()), + q, SLOT(_q_modelAboutToBeReset())); + QObject::connect(completionModel, SIGNAL(modelReset()), + q, SLOT(_q_pollModel())); + QObject::connect(completionModel, SIGNAL(rowsInserted(QModelIndex, int, int)), + q, SLOT(_q_pollModel())); + QObject::connect(completionModel, SIGNAL(rowsRemoved(QModelIndex, int, int)), + q, SLOT(_q_rowsRemoved(QModelIndex, int, int))); + _q_modelUpdate(); } +void MCompleterPrivate::pollModel(bool isResetFocus) +{ + Q_Q(MCompleter); + + // TODO #1: remove check for one of those signals once they have been merged. + // TODO #2: Change the "<= 1" back to "<= 0", because this is only needed for + // the current signal forwarding! + if ((q->receivers(SIGNAL(startCompleting(QString))) <= 0) && + (q->receivers(SIGNAL(startCompleting(QString, QModelIndex))) <= 1)) { + //if no customized match slot, then use default match rule + //limit the max hits to DefaultMaximumHits + 1 (+1 allow it to be larger than DefaultMaximumHits) + match(DefaultMaximumHits + 1); + } else { + //if customized, then think all items inside model are matched + matchedIndexList.clear(); + const int rowCount = completionModel->rowCount(); + for (int i = 0; i < rowCount; ++i) + matchedIndexList << i; + matchedModel->setMatchedList(matchedIndexList); + } + updateScene(isResetFocus); +} + +void MCompleterPrivate::updateScene(bool isResetFocus) +{ + Q_Q(MCompleter); + + if (matchedModel->rowCount() > 0) { + q->model()->setMatchedIndex(0); + q->model()->setActive(true); + q->widget()->sceneManager()->appearSceneWindow(q); + emit q->shown(); + } else if (q->isActive()) { + reset(); + q->hideCompleter(); + if (isResetFocus) + resetFocus(); + } +} + void MCompleterPrivate::_q_modelUpdate() { + Q_Q(MCompleter); if (!completionModel) return; matchedIndexList.clear(); matchedModel->setMatchedList(matchedIndexList); + if (q->isActive()) + pollModel(false); +} + +void MCompleterPrivate::_q_pollModel() +{ + pollModel(false); +} + +void MCompleterPrivate::_q_modelAboutToBeReset() +{ + Q_Q(MCompleter); + reset(); + q->hideCompleter(); +} + +void MCompleterPrivate::_q_rowsRemoved(const QModelIndex& parent, int start, int end) +{ + Q_UNUSED(parent); + Q_Q(MCompleter); + if (start < 0 || end < 0 || start > end) { + qDebug() << "[MCompleterPrivate::onRowsRemoved] Invalid parameters!"; + return; + } + + for (int i = start; i <= end; i++) { + for (QList<int>::iterator it = matchedIndexList.begin(); it != matchedIndexList.end(); ++it) { + if (*it == i) { + matchedIndexList.erase(it); + break; + } + } + } + matchedModel->setMatchedList(matchedIndexList); + if (q->isActive()) + updateScene(true); } void MCompleterPrivate::resetFocus() diff --git a/src/corelib/widgets/mcompleter.h b/src/corelib/widgets/mcompleter.h index 964c2a24..2330fc8b 100644 --- a/src/corelib/widgets/mcompleter.h +++ b/src/corelib/widgets/mcompleter.h @@ -331,6 +331,9 @@ private: Q_PRIVATE_SLOT(d_func(), void _q_modelUpdate()) Q_PRIVATE_SLOT(d_func(), void _q_complete()) + Q_PRIVATE_SLOT(d_func(), void _q_pollModel()) + Q_PRIVATE_SLOT(d_func(), void _q_modelAboutToBeReset()) + Q_PRIVATE_SLOT(d_func(), void _q_rowsRemoved(const QModelIndex &parent, int start, int end)) #ifdef UNIT_TEST friend class Ut_MCompleter; diff --git a/src/corelib/widgets/mcompleter_p.h b/src/corelib/widgets/mcompleter_p.h index 602eb92d..83131556 100644 --- a/src/corelib/widgets/mcompleter_p.h +++ b/src/corelib/widgets/mcompleter_p.h @@ -46,6 +46,9 @@ public: void _q_modelUpdate(); void _q_complete(); + void _q_pollModel(); + void _q_modelAboutToBeReset(); + void _q_rowsRemoved(const QModelIndex &parent, int start, int end); protected: void fetchPrefix(); @@ -62,6 +65,10 @@ protected: void resetFocus(); + void pollModel(bool isResetFocus); + + void updateScene(bool isResetFocus); + private: QString delimiters; bool hasOwnModel; |