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]
71MainWindow::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(s: "Model"));
80
81 modelCombo = new QComboBox;
82 modelCombo->addItem(atext: tr(s: "QFileSystemModel"));
83 modelCombo->addItem(atext: tr(s: "QFileSystemModel that shows full path"));
84 modelCombo->addItem(atext: tr(s: "Country list"));
85 modelCombo->addItem(atext: tr(s: "Word list"));
86 modelCombo->setCurrentIndex(0);
87
88 QLabel *modeLabel = new QLabel;
89 modeLabel->setText(tr(s: "Completion Mode"));
90 modeCombo = new QComboBox;
91 modeCombo->addItem(atext: tr(s: "Inline"));
92 modeCombo->addItem(atext: tr(s: "Filtered Popup"));
93 modeCombo->addItem(atext: tr(s: "Unfiltered Popup"));
94 modeCombo->setCurrentIndex(1);
95
96 QLabel *caseLabel = new QLabel;
97 caseLabel->setText(tr(s: "Case Sensitivity"));
98 caseCombo = new QComboBox;
99 caseCombo->addItem(atext: tr(s: "Case Insensitive"));
100 caseCombo->addItem(atext: tr(s: "Case Sensitive"));
101 caseCombo->setCurrentIndex(0);
102//! [0]
103
104//! [1]
105 QLabel *maxVisibleLabel = new QLabel;
106 maxVisibleLabel->setText(tr(s: "Max Visible Items"));
107 maxVisibleSpinBox = new QSpinBox;
108 maxVisibleSpinBox->setRange(min: 3,max: 25);
109 maxVisibleSpinBox->setValue(10);
110
111 wrapCheckBox = new QCheckBox;
112 wrapCheckBox->setText(tr(s: "Wrap around completions"));
113 wrapCheckBox->setChecked(true);
114//! [1]
115
116//! [2]
117 contentsLabel = new QLabel;
118 contentsLabel->setSizePolicy(hor: QSizePolicy::Fixed, ver: QSizePolicy::Fixed);
119
120 connect(sender: modelCombo, signal: QOverload<int>::of(ptr: &QComboBox::activated),
121 receiver: this, slot: &MainWindow::changeModel);
122 connect(sender: modeCombo, signal: QOverload<int>::of(ptr: &QComboBox::activated),
123 receiver: this, slot: &MainWindow::changeMode);
124 connect(sender: caseCombo, signal: QOverload<int>::of(ptr: &QComboBox::activated),
125 receiver: this, slot: &MainWindow::changeCase);
126 connect(sender: maxVisibleSpinBox, signal: QOverload<int>::of(ptr: &QSpinBox::valueChanged),
127 receiver: this, slot: &MainWindow::changeMaxVisible);
128//! [2]
129
130//! [3]
131 lineEdit = new QLineEdit;
132
133 QGridLayout *layout = new QGridLayout;
134 layout->addWidget(modelLabel, row: 0, column: 0); layout->addWidget(modelCombo, row: 0, column: 1);
135 layout->addWidget(modeLabel, row: 1, column: 0); layout->addWidget(modeCombo, row: 1, column: 1);
136 layout->addWidget(caseLabel, row: 2, column: 0); layout->addWidget(caseCombo, row: 2, column: 1);
137 layout->addWidget(maxVisibleLabel, row: 3, column: 0); layout->addWidget(maxVisibleSpinBox, row: 3, column: 1);
138 layout->addWidget(wrapCheckBox, row: 4, column: 0);
139 layout->addWidget(contentsLabel, row: 5, column: 0, rowSpan: 1, columnSpan: 2);
140 layout->addWidget(lineEdit, row: 6, column: 0, rowSpan: 1, columnSpan: 2);
141 centralWidget->setLayout(layout);
142 setCentralWidget(centralWidget);
143
144 changeModel();
145
146 setWindowTitle(tr(s: "Completer"));
147 lineEdit->setFocus();
148}
149//! [3]
150
151//! [4]
152void MainWindow::createMenu()
153{
154 QAction *exitAction = new QAction(tr(s: "Exit"), this);
155 QAction *aboutAct = new QAction(tr(s: "About"), this);
156 QAction *aboutQtAct = new QAction(tr(s: "About Qt"), this);
157
158 connect(sender: exitAction, signal: &QAction::triggered, qApp, slot: &QApplication::quit);
159 connect(sender: aboutAct, signal: &QAction::triggered, receiver: this, slot: &MainWindow::about);
160 connect(sender: aboutQtAct, signal: &QAction::triggered, qApp, slot: &QApplication::aboutQt);
161
162 QMenu *fileMenu = menuBar()->addMenu(title: tr(s: "File"));
163 fileMenu->addAction(action: exitAction);
164
165 QMenu *helpMenu = menuBar()->addMenu(title: tr(s: "About"));
166 helpMenu->addAction(action: aboutAct);
167 helpMenu->addAction(action: aboutQtAct);
168}
169//! [4]
170
171//! [5]
172QAbstractItemModel *MainWindow::modelFromFile(const QString &fileName)
173{
174 QFile file(fileName);
175 if (!file.open(flags: 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(str: line.trimmed());
189 }
190
191#ifndef QT_NO_CURSOR
192 QGuiApplication::restoreOverrideCursor();
193#endif
194//! [6]
195
196//! [7]
197 if (!fileName.contains(s: 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(row: i, column: 0);
208 QModelIndex symbolIdx = m->index(row: i, column: 1);
209 QString country = words.at(i).mid(position: 0, n: words[i].length() - 2).trimmed();
210 QString symbol = words.at(i).right(n: 2);
211 m->setData(index: countryIdx, value: country);
212 m->setData(index: symbolIdx, value: symbol);
213 }
214
215 return m;
216}
217//! [9]
218
219//! [10]
220void 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
234void MainWindow::changeCase(int cs)
235{
236 completer->setCaseSensitivity(cs ? Qt::CaseSensitive : Qt::CaseInsensitive);
237}
238
239//! [11]
240void 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(s: "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(s: "Enter file path"));
263 }
264 break;
265//! [12] //! [13]
266 case 2:
267 { // Country List
268 completer->setModel(modelFromFile(fileName: ":/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(logicalIndex: 0, mode: QHeaderView::Stretch);
275 treeView->header()->setSectionResizeMode(logicalIndex: 1, mode: QHeaderView::ResizeToContents);
276 contentsLabel->setText(tr(s: "Enter name of your country"));
277 }
278 break;
279//! [13] //! [14]
280 case 3:
281 { // Word list
282 completer->setModel(modelFromFile(fileName: ":/resources/wordlist.txt"));
283 completer->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
284 contentsLabel->setText(tr(s: "Enter a word"));
285 }
286 break;
287 }
288
289 changeMode(index: modeCombo->currentIndex());
290 changeCase(cs: caseCombo->currentIndex());
291 completer->setWrapAround(wrapCheckBox->isChecked());
292 lineEdit->setCompleter(completer);
293 connect(sender: wrapCheckBox, signal: &QAbstractButton::clicked, receiver: completer, slot: &QCompleter::setWrapAround);
294}
295//! [14]
296
297//! [15]
298void MainWindow::changeMaxVisible(int max)
299{
300 completer->setMaxVisibleItems(max);
301}
302//! [15]
303
304//! [16]
305void MainWindow::about()
306{
307 QMessageBox::about(parent: this, title: tr(s: "About"), text: tr(s: "This example demonstrates the "
308 "different features of the QCompleter class."));
309}
310//! [16]
311

source code of qtbase/examples/widgets/tools/completer/mainwindow.cpp