From 866e6e6338767086cef8f97bc4e7b38e78b83651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Mercille?= Date: Fri, 17 Jul 2009 14:46:29 +0200 Subject: Lets the size of the completer be configurable in a way similar to QComboBox. Merge-request: 884 Reviewed-by: Olivier Goffart --- examples/tools/completer/mainwindow.cpp | 24 +++++++++-- examples/tools/completer/mainwindow.h | 3 ++ src/gui/util/qcompleter.cpp | 30 ++++++++++++-- src/gui/util/qcompleter.h | 4 ++ src/gui/util/qcompleter_p.h | 1 + tests/auto/qcompleter/tst_qcompleter.cpp | 68 ++++++++++++++++++++++++++++++++ 6 files changed, 123 insertions(+), 7 deletions(-) diff --git a/examples/tools/completer/mainwindow.cpp b/examples/tools/completer/mainwindow.cpp index 8ea1c39..06f16de 100644 --- a/examples/tools/completer/mainwindow.cpp +++ b/examples/tools/completer/mainwindow.cpp @@ -75,6 +75,12 @@ MainWindow::MainWindow(QWidget *parent) caseCombo->addItem(tr("Case Insensitive")); caseCombo->addItem(tr("Case Sensitive")); caseCombo->setCurrentIndex(0); + + QLabel *maxVisibleLabel = new QLabel; + maxVisibleLabel->setText(tr("Max Visible Items")); + maxVisibleSpinBox = new QSpinBox; + maxVisibleSpinBox->setRange(3,25); + maxVisibleSpinBox->setValue(10); //! [0] //! [1] @@ -90,6 +96,7 @@ MainWindow::MainWindow(QWidget *parent) connect(modelCombo, SIGNAL(activated(int)), this, SLOT(changeModel())); connect(modeCombo, SIGNAL(activated(int)), this, SLOT(changeMode(int))); connect(caseCombo, SIGNAL(activated(int)), this, SLOT(changeCase(int))); + connect(maxVisibleSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeMaxVisible(int))); //! [2] //! [3] @@ -99,9 +106,10 @@ MainWindow::MainWindow(QWidget *parent) layout->addWidget(modelLabel, 0, 0); layout->addWidget(modelCombo, 0, 1); layout->addWidget(modeLabel, 1, 0); layout->addWidget(modeCombo, 1, 1); layout->addWidget(caseLabel, 2, 0); layout->addWidget(caseCombo, 2, 1); - layout->addWidget(wrapCheckBox, 3, 0); - layout->addWidget(contentsLabel, 4, 0, 1, 2); - layout->addWidget(lineEdit, 5, 0, 1, 2); + layout->addWidget(maxVisibleLabel, 3, 0); layout->addWidget(maxVisibleSpinBox, 3, 1); + layout->addWidget(wrapCheckBox, 4, 0); + layout->addWidget(contentsLabel, 5, 0, 1, 2); + layout->addWidget(lineEdit, 6, 0, 1, 2); centralWidget->setLayout(layout); setCentralWidget(centralWidget); @@ -205,6 +213,7 @@ void MainWindow::changeModel() { delete completer; completer = new QCompleter(this); + completer->setMaxVisibleItems(maxVisibleSpinBox->value()); switch (modelCombo->currentIndex()) { default: @@ -256,9 +265,16 @@ void MainWindow::changeModel() //! [14] //! [15] +void MainWindow::changeMaxVisible(int max) +{ + completer->setMaxVisibleItems(max); +} +//! [15] + +//! [16] void MainWindow::about() { QMessageBox::about(this, tr("About"), tr("This example demonstrates the " "different features of the QCompleter class.")); } -//! [15] +//! [16] diff --git a/examples/tools/completer/mainwindow.h b/examples/tools/completer/mainwindow.h index f6c962b..30b8d26 100644 --- a/examples/tools/completer/mainwindow.h +++ b/examples/tools/completer/mainwindow.h @@ -52,6 +52,7 @@ class QLabel; class QLineEdit; class QProgressBar; class QCheckBox; +class QSpinBox; QT_END_NAMESPACE //! [0] @@ -67,6 +68,7 @@ private slots: void changeCase(int); void changeMode(int); void changeModel(); + void changeMaxVisible(int); //! [0] //! [1] @@ -77,6 +79,7 @@ private: QComboBox *caseCombo; QComboBox *modeCombo; QComboBox *modelCombo; + QSpinBox *maxVisibleSpinBox; QCheckBox *wrapCheckBox; QCompleter *completer; QLabel *contentsLabel; diff --git a/src/gui/util/qcompleter.cpp b/src/gui/util/qcompleter.cpp index d68e309..bf1fa6a 100644 --- a/src/gui/util/qcompleter.cpp +++ b/src/gui/util/qcompleter.cpp @@ -134,7 +134,7 @@ To provide completions, QCompleter needs to know the path from an index. This is provided by pathFromIndex(). The default implementation of - pathFromIndex(), returns the data for the \l{Qt::EditRole}{edit role} + pathFromIndex(), returns the data for the \l{Qt::EditRole}{edit role} for list models and the absolute file path if the mode is a QDirModel. \sa QAbstractItemModel, QLineEdit, QComboBox, {Completer Example} @@ -772,7 +772,7 @@ QMatchData QUnsortedModelEngine::filter(const QString& part, const QModelIndex& /////////////////////////////////////////////////////////////////////////////// QCompleterPrivate::QCompleterPrivate() : widget(0), proxy(0), popup(0), cs(Qt::CaseSensitive), role(Qt::EditRole), column(0), - sorting(QCompleter::UnsortedModel), wrap(true), eatFocusOut(true) + sorting(QCompleter::UnsortedModel), wrap(true), maxVisibleItems(7), eatFocusOut(true) { } @@ -861,7 +861,7 @@ void QCompleterPrivate::showPopup(const QRect& rect) Qt::LayoutDirection dir = widget->layoutDirection(); QPoint pos; int rw, rh, w; - int h = (popup->sizeHintForRow(0) * qMin(7, popup->model()->rowCount()) + 3) + 3; + int h = (popup->sizeHintForRow(0) * qMin(maxVisibleItems, popup->model()->rowCount()) + 3) + 3; QScrollBar *hsb = popup->horizontalScrollBar(); if (hsb && hsb->isVisible()) h += popup->horizontalScrollBar()->sizeHint().height(); @@ -1510,6 +1510,30 @@ bool QCompleter::wrapAround() const } /*! + \property QCompleter::maxVisibleItems + \brief the maximum allowed size on screen of the completer, measured in items + \since 4.6 + + By default, this property has a value of 7. +*/ +int QCompleter::maxVisibleItems() const +{ + Q_D(const QCompleter); + return d->maxVisibleItems; +} + +void QCompleter::setMaxVisibleItems(int maxItems) +{ + Q_D(QCompleter); + if (maxItems < 0) { + qWarning("QCompleter::setMaxVisibleItems: " + "Invalid max visible items (%d) must be >= 0", maxItems); + return; + } + d->maxVisibleItems = maxItems; +} + +/*! \property QCompleter::caseSensitivity \brief the case sensitivity of the matching diff --git a/src/gui/util/qcompleter.h b/src/gui/util/qcompleter.h index c1169ef..a419154 100644 --- a/src/gui/util/qcompleter.h +++ b/src/gui/util/qcompleter.h @@ -69,6 +69,7 @@ class Q_GUI_EXPORT QCompleter : public QObject Q_PROPERTY(CompletionMode completionMode READ completionMode WRITE setCompletionMode) Q_PROPERTY(int completionColumn READ completionColumn WRITE setCompletionColumn) Q_PROPERTY(int completionRole READ completionRole WRITE setCompletionRole) + Q_PROPERTY(int maxVisibleItems READ maxVisibleItems WRITE setMaxVisibleItems) Q_PROPERTY(Qt::CaseSensitivity caseSensitivity READ caseSensitivity WRITE setCaseSensitivity) Q_PROPERTY(bool wrapAround READ wrapAround WRITE setWrapAround) @@ -118,6 +119,9 @@ public: bool wrapAround() const; + int maxVisibleItems() const; + void setMaxVisibleItems(int maxItems); + int completionCount() const; bool setCurrentRow(int row); int currentRow() const; diff --git a/src/gui/util/qcompleter_p.h b/src/gui/util/qcompleter_p.h index dc4189f..288f531 100644 --- a/src/gui/util/qcompleter_p.h +++ b/src/gui/util/qcompleter_p.h @@ -87,6 +87,7 @@ public: Qt::CaseSensitivity cs; int role; int column; + int maxVisibleItems; QCompleter::ModelSorting sorting; bool wrap; diff --git a/tests/auto/qcompleter/tst_qcompleter.cpp b/tests/auto/qcompleter/tst_qcompleter.cpp index fb03e1a..0a9c16a 100644 --- a/tests/auto/qcompleter/tst_qcompleter.cpp +++ b/tests/auto/qcompleter/tst_qcompleter.cpp @@ -102,6 +102,8 @@ public: ~tst_QCompleter(); private slots: + void getSetCheck(); + void multipleWidgets(); void focusIn(); @@ -268,6 +270,72 @@ void tst_QCompleter::filter() QCOMPARE(completer->currentCompletion(), completionText); } +// Testing get/set functions +void tst_QCompleter::getSetCheck() +{ + QStandardItemModel model(3,3); + QCompleter completer(&model); + + // QString QCompleter::completionPrefix() + // void QCompleter::setCompletionPrefix(QString) + completer.setCompletionPrefix(QString("te")); + QCOMPARE(completer.completionPrefix(), QString("te")); + completer.setCompletionPrefix(QString()); + QCOMPARE(completer.completionPrefix(), QString()); + + // ModelSorting QCompleter::modelSorting() + // void QCompleter::setModelSorting(ModelSorting) + completer.setModelSorting(QCompleter::CaseSensitivelySortedModel); + QCOMPARE(completer.modelSorting(), QCompleter::CaseSensitivelySortedModel); + completer.setModelSorting(QCompleter::CaseInsensitivelySortedModel); + QCOMPARE(completer.modelSorting(), QCompleter::CaseInsensitivelySortedModel); + completer.setModelSorting(QCompleter::UnsortedModel); + QCOMPARE(completer.modelSorting(), QCompleter::UnsortedModel); + + // CompletionMode QCompleter::completionMode() + // void QCompleter::setCompletionMode(CompletionMode) + QCOMPARE(completer.completionMode(), QCompleter::PopupCompletion); // default value + completer.setCompletionMode(QCompleter::UnfilteredPopupCompletion); + QCOMPARE(completer.completionMode(), QCompleter::UnfilteredPopupCompletion); + completer.setCompletionMode(QCompleter::InlineCompletion); + QCOMPARE(completer.completionMode(), QCompleter::InlineCompletion); + + // int QCompleter::completionColumn() + // void QCompleter::setCompletionColumn(int) + completer.setCompletionColumn(2); + QCOMPARE(completer.completionColumn(), 2); + completer.setCompletionColumn(1); + QCOMPARE(completer.completionColumn(), 1); + + // int QCompleter::completionRole() + // void QCompleter::setCompletionRole(int) + QCOMPARE(completer.completionRole(), static_cast(Qt::EditRole)); // default value + completer.setCompletionRole(Qt::DisplayRole); + QCOMPARE(completer.completionRole(), static_cast(Qt::DisplayRole)); + + // int QCompleter::maxVisibleItems() + // void QCompleter::setMaxVisibleItems(int) + QCOMPARE(completer.maxVisibleItems(), 7); // default value + completer.setMaxVisibleItems(10); + QCOMPARE(completer.maxVisibleItems(), 10); + QTest::ignoreMessage(QtWarningMsg, "QCompleter::setMaxVisibleItems: " + "Invalid max visible items (-2147483648) must be >= 0"); + completer.setMaxVisibleItems(INT_MIN); + QCOMPARE(completer.maxVisibleItems(), 10); // Cannot be set to something negative => old value + + // Qt::CaseSensitivity QCompleter::caseSensitivity() + // void QCompleter::setCaseSensitivity(Qt::CaseSensitivity) + QCOMPARE(completer.caseSensitivity(), Qt::CaseSensitive); // default value + completer.setCaseSensitivity(Qt::CaseInsensitive); + QCOMPARE(completer.caseSensitivity(), Qt::CaseInsensitive); + + // bool QCompleter::wrapAround() + // void QCompleter::setWrapAround(bool) + QCOMPARE(completer.wrapAround(), true); // default value + completer.setWrapAround(false); + QCOMPARE(completer.wrapAround(), false); +} + void tst_QCompleter::csMatchingOnCsSortedModel_data() { delete completer; -- cgit v0.12