| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the examples of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include "mainwindow.h" |
| 52 | #include "fsmodel.h" |
| 53 | |
| 54 | #include <QAction> |
| 55 | #include <QApplication> |
| 56 | #include <QCheckBox> |
| 57 | #include <QComboBox> |
| 58 | #include <QCompleter> |
| 59 | #include <QGridLayout> |
| 60 | #include <QHeaderView> |
| 61 | #include <QLabel> |
| 62 | #include <QLineEdit> |
| 63 | #include <QMenuBar> |
| 64 | #include <QMessageBox> |
| 65 | #include <QSpinBox> |
| 66 | #include <QStandardItemModel> |
| 67 | #include <QStringListModel> |
| 68 | #include <QTreeView> |
| 69 | |
| 70 | //! [0] |
| 71 | MainWindow::MainWindow(QWidget *parent) |
| 72 | : QMainWindow(parent) |
| 73 | { |
| 74 | createMenu(); |
| 75 | |
| 76 | QWidget *centralWidget = new QWidget; |
| 77 | |
| 78 | QLabel *modelLabel = new QLabel; |
| 79 | modelLabel->setText(tr("Model" )); |
| 80 | |
| 81 | modelCombo = new QComboBox; |
| 82 | modelCombo->addItem(tr("QFileSystemModel" )); |
| 83 | modelCombo->addItem(tr("QFileSystemModel that shows full path" )); |
| 84 | modelCombo->addItem(tr("Country list" )); |
| 85 | modelCombo->addItem(tr("Word list" )); |
| 86 | modelCombo->setCurrentIndex(0); |
| 87 | |
| 88 | QLabel *modeLabel = new QLabel; |
| 89 | modeLabel->setText(tr("Completion Mode" )); |
| 90 | modeCombo = new QComboBox; |
| 91 | modeCombo->addItem(tr("Inline" )); |
| 92 | modeCombo->addItem(tr("Filtered Popup" )); |
| 93 | modeCombo->addItem(tr("Unfiltered Popup" )); |
| 94 | modeCombo->setCurrentIndex(1); |
| 95 | |
| 96 | QLabel *caseLabel = new QLabel; |
| 97 | caseLabel->setText(tr("Case Sensitivity" )); |
| 98 | caseCombo = new QComboBox; |
| 99 | caseCombo->addItem(tr("Case Insensitive" )); |
| 100 | caseCombo->addItem(tr("Case Sensitive" )); |
| 101 | caseCombo->setCurrentIndex(0); |
| 102 | //! [0] |
| 103 | |
| 104 | //! [1] |
| 105 | QLabel *maxVisibleLabel = new QLabel; |
| 106 | maxVisibleLabel->setText(tr("Max Visible Items" )); |
| 107 | maxVisibleSpinBox = new QSpinBox; |
| 108 | maxVisibleSpinBox->setRange(3,25); |
| 109 | maxVisibleSpinBox->setValue(10); |
| 110 | |
| 111 | wrapCheckBox = new QCheckBox; |
| 112 | wrapCheckBox->setText(tr("Wrap around completions" )); |
| 113 | wrapCheckBox->setChecked(true); |
| 114 | //! [1] |
| 115 | |
| 116 | //! [2] |
| 117 | contentsLabel = new QLabel; |
| 118 | contentsLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 119 | |
| 120 | connect(modelCombo, &QComboBox::activated, |
| 121 | this, &MainWindow::changeModel); |
| 122 | connect(modeCombo, &QComboBox::activated, |
| 123 | this, &MainWindow::changeMode); |
| 124 | connect(caseCombo, &QComboBox::activated, |
| 125 | this, &MainWindow::changeCase); |
| 126 | connect(maxVisibleSpinBox, &QSpinBox::valueChanged, |
| 127 | this, &MainWindow::changeMaxVisible); |
| 128 | //! [2] |
| 129 | |
| 130 | //! [3] |
| 131 | lineEdit = new QLineEdit; |
| 132 | |
| 133 | QGridLayout *layout = new QGridLayout; |
| 134 | layout->addWidget(modelLabel, 0, 0); layout->addWidget(modelCombo, 0, 1); |
| 135 | layout->addWidget(modeLabel, 1, 0); layout->addWidget(modeCombo, 1, 1); |
| 136 | layout->addWidget(caseLabel, 2, 0); layout->addWidget(caseCombo, 2, 1); |
| 137 | layout->addWidget(maxVisibleLabel, 3, 0); layout->addWidget(maxVisibleSpinBox, 3, 1); |
| 138 | layout->addWidget(wrapCheckBox, 4, 0); |
| 139 | layout->addWidget(contentsLabel, 5, 0, 1, 2); |
| 140 | layout->addWidget(lineEdit, 6, 0, 1, 2); |
| 141 | centralWidget->setLayout(layout); |
| 142 | setCentralWidget(centralWidget); |
| 143 | |
| 144 | changeModel(); |
| 145 | |
| 146 | setWindowTitle(tr("Completer" )); |
| 147 | lineEdit->setFocus(); |
| 148 | } |
| 149 | //! [3] |
| 150 | |
| 151 | //! [4] |
| 152 | void MainWindow::createMenu() |
| 153 | { |
| 154 | QAction *exitAction = new QAction(tr("Exit" ), this); |
| 155 | QAction *aboutAct = new QAction(tr("About" ), this); |
| 156 | QAction *aboutQtAct = new QAction(tr("About Qt" ), this); |
| 157 | |
| 158 | connect(exitAction, &QAction::triggered, qApp, &QApplication::quit); |
| 159 | connect(aboutAct, &QAction::triggered, this, &MainWindow::about); |
| 160 | connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt); |
| 161 | |
| 162 | QMenu * = menuBar()->addMenu(tr("File" )); |
| 163 | fileMenu->addAction(exitAction); |
| 164 | |
| 165 | QMenu * = menuBar()->addMenu(tr("About" )); |
| 166 | helpMenu->addAction(aboutAct); |
| 167 | helpMenu->addAction(aboutQtAct); |
| 168 | } |
| 169 | //! [4] |
| 170 | |
| 171 | //! [5] |
| 172 | QAbstractItemModel *MainWindow::modelFromFile(const QString &fileName) |
| 173 | { |
| 174 | QFile file(fileName); |
| 175 | if (!file.open(QFile::ReadOnly)) |
| 176 | return new QStringListModel(completer); |
| 177 | //! [5] |
| 178 | |
| 179 | //! [6] |
| 180 | #ifndef QT_NO_CURSOR |
| 181 | QGuiApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
| 182 | #endif |
| 183 | QStringList words; |
| 184 | |
| 185 | while (!file.atEnd()) { |
| 186 | QByteArray line = file.readLine(); |
| 187 | if (!line.isEmpty()) |
| 188 | words << QString::fromUtf8(line.trimmed()); |
| 189 | } |
| 190 | |
| 191 | #ifndef QT_NO_CURSOR |
| 192 | QGuiApplication::restoreOverrideCursor(); |
| 193 | #endif |
| 194 | //! [6] |
| 195 | |
| 196 | //! [7] |
| 197 | if (!fileName.contains(QLatin1String("countries.txt" ))) |
| 198 | return new QStringListModel(words, completer); |
| 199 | //! [7] |
| 200 | |
| 201 | // The last two chars of the countries.txt file indicate the country |
| 202 | // symbol. We put that in column 2 of a standard item model |
| 203 | //! [8] |
| 204 | QStandardItemModel *m = new QStandardItemModel(words.count(), 2, completer); |
| 205 | //! [8] //! [9] |
| 206 | for (int i = 0; i < words.count(); ++i) { |
| 207 | QModelIndex countryIdx = m->index(i, 0); |
| 208 | QModelIndex symbolIdx = m->index(i, 1); |
| 209 | QString country = words.at(i).mid(0, words[i].length() - 2).trimmed(); |
| 210 | QString symbol = words.at(i).right(2); |
| 211 | m->setData(countryIdx, country); |
| 212 | m->setData(symbolIdx, symbol); |
| 213 | } |
| 214 | |
| 215 | return m; |
| 216 | } |
| 217 | //! [9] |
| 218 | |
| 219 | //! [10] |
| 220 | void MainWindow::changeMode(int index) |
| 221 | { |
| 222 | QCompleter::CompletionMode mode; |
| 223 | if (index == 0) |
| 224 | mode = QCompleter::InlineCompletion; |
| 225 | else if (index == 1) |
| 226 | mode = QCompleter::PopupCompletion; |
| 227 | else |
| 228 | mode = QCompleter::UnfilteredPopupCompletion; |
| 229 | |
| 230 | completer->setCompletionMode(mode); |
| 231 | } |
| 232 | //! [10] |
| 233 | |
| 234 | void MainWindow::changeCase(int cs) |
| 235 | { |
| 236 | completer->setCaseSensitivity(cs ? Qt::CaseSensitive : Qt::CaseInsensitive); |
| 237 | } |
| 238 | |
| 239 | //! [11] |
| 240 | void MainWindow::changeModel() |
| 241 | { |
| 242 | delete completer; |
| 243 | completer = new QCompleter(this); |
| 244 | completer->setMaxVisibleItems(maxVisibleSpinBox->value()); |
| 245 | |
| 246 | switch (modelCombo->currentIndex()) { |
| 247 | default: |
| 248 | case 0: |
| 249 | { // Unsorted QFileSystemModel |
| 250 | QFileSystemModel *fsModel = new QFileSystemModel(completer); |
| 251 | fsModel->setRootPath(QString()); |
| 252 | completer->setModel(fsModel); |
| 253 | contentsLabel->setText(tr("Enter file path" )); |
| 254 | } |
| 255 | break; |
| 256 | //! [11] //! [12] |
| 257 | case 1: |
| 258 | { // FileSystemModel that shows full paths |
| 259 | FileSystemModel *fsModel = new FileSystemModel(completer); |
| 260 | completer->setModel(fsModel); |
| 261 | fsModel->setRootPath(QString()); |
| 262 | contentsLabel->setText(tr("Enter file path" )); |
| 263 | } |
| 264 | break; |
| 265 | //! [12] //! [13] |
| 266 | case 2: |
| 267 | { // Country List |
| 268 | completer->setModel(modelFromFile(":/resources/countries.txt" )); |
| 269 | QTreeView *treeView = new QTreeView; |
| 270 | completer->setPopup(treeView); |
| 271 | treeView->setRootIsDecorated(false); |
| 272 | treeView->header()->hide(); |
| 273 | treeView->header()->setStretchLastSection(false); |
| 274 | treeView->header()->setSectionResizeMode(0, QHeaderView::Stretch); |
| 275 | treeView->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents); |
| 276 | contentsLabel->setText(tr("Enter name of your country" )); |
| 277 | } |
| 278 | break; |
| 279 | //! [13] //! [14] |
| 280 | case 3: |
| 281 | { // Word list |
| 282 | completer->setModel(modelFromFile(":/resources/wordlist.txt" )); |
| 283 | completer->setModelSorting(QCompleter::CaseInsensitivelySortedModel); |
| 284 | contentsLabel->setText(tr("Enter a word" )); |
| 285 | } |
| 286 | break; |
| 287 | } |
| 288 | |
| 289 | changeMode(modeCombo->currentIndex()); |
| 290 | changeCase(caseCombo->currentIndex()); |
| 291 | completer->setWrapAround(wrapCheckBox->isChecked()); |
| 292 | lineEdit->setCompleter(completer); |
| 293 | connect(wrapCheckBox, &QAbstractButton::clicked, completer, &QCompleter::setWrapAround); |
| 294 | } |
| 295 | //! [14] |
| 296 | |
| 297 | //! [15] |
| 298 | void MainWindow::changeMaxVisible(int max) |
| 299 | { |
| 300 | completer->setMaxVisibleItems(max); |
| 301 | } |
| 302 | //! [15] |
| 303 | |
| 304 | //! [16] |
| 305 | void MainWindow::about() |
| 306 | { |
| 307 | QMessageBox::about(this, tr("About" ), tr("This example demonstrates the " |
| 308 | "different features of the QCompleter class." )); |
| 309 | } |
| 310 | //! [16] |
| 311 | |