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 "treemodelcompleter.h"
53
54#include <QAbstractProxyModel>
55#include <QAction>
56#include <QApplication>
57#include <QCheckBox>
58#include <QComboBox>
59#include <QFile>
60#include <QGridLayout>
61#include <QHeaderView>
62#include <QLabel>
63#include <QLineEdit>
64#include <QMenuBar>
65#include <QMessageBox>
66#include <QStandardItemModel>
67#include <QStringListModel>
68#include <QTreeView>
69
70//! [0]
71MainWindow::MainWindow(QWidget *parent)
72 : QMainWindow(parent)
73{
74 createMenu();
75
76 completer = new TreeModelCompleter(this);
77 completer->setModel(modelFromFile(":/resources/treemodel.txt"));
78 completer->setSeparator(QLatin1String("."));
79 QObject::connect(completer, QOverload<const QModelIndex &>::of(&TreeModelCompleter::highlighted),
80 this, &MainWindow::highlight);
81
82 QWidget *centralWidget = new QWidget;
83
84 QLabel *modelLabel = new QLabel;
85 modelLabel->setText(tr("Tree Model<br>(Double click items to edit)"));
86
87 QLabel *modeLabel = new QLabel;
88 modeLabel->setText(tr("Completion Mode"));
89 modeCombo = new QComboBox;
90 modeCombo->addItem(tr("Inline"));
91 modeCombo->addItem(tr("Filtered Popup"));
92 modeCombo->addItem(tr("Unfiltered Popup"));
93 modeCombo->setCurrentIndex(1);
94
95 QLabel *caseLabel = new QLabel;
96 caseLabel->setText(tr("Case Sensitivity"));
97 caseCombo = new QComboBox;
98 caseCombo->addItem(tr("Case Insensitive"));
99 caseCombo->addItem(tr("Case Sensitive"));
100 caseCombo->setCurrentIndex(0);
101//! [0]
102
103//! [1]
104 QLabel *separatorLabel = new QLabel;
105 separatorLabel->setText(tr("Tree Separator"));
106
107 QLineEdit *separatorLineEdit = new QLineEdit;
108 separatorLineEdit->setText(completer->separator());
109 connect(separatorLineEdit, &QLineEdit::textChanged,
110 completer, &TreeModelCompleter::setSeparator);
111
112 QCheckBox *wrapCheckBox = new QCheckBox;
113 wrapCheckBox->setText(tr("Wrap around completions"));
114 wrapCheckBox->setChecked(completer->wrapAround());
115 connect(wrapCheckBox, &QAbstractButton::clicked, completer, &QCompleter::setWrapAround);
116
117 contentsLabel = new QLabel;
118 contentsLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
119 connect(separatorLineEdit, &QLineEdit::textChanged,
120 this, &MainWindow::updateContentsLabel);
121
122 treeView = new QTreeView;
123 treeView->setModel(completer->model());
124 treeView->header()->hide();
125 treeView->expandAll();
126//! [1]
127
128//! [2]
129 connect(modeCombo, &QComboBox::activated,
130 this, &MainWindow::changeMode);
131 connect(caseCombo, &QComboBox::activated,
132 this, &MainWindow::changeMode);
133
134 lineEdit = new QLineEdit;
135 lineEdit->setCompleter(completer);
136//! [2]
137
138//! [3]
139 QGridLayout *layout = new QGridLayout;
140 layout->addWidget(modelLabel, 0, 0); layout->addWidget(treeView, 0, 1);
141 layout->addWidget(modeLabel, 1, 0); layout->addWidget(modeCombo, 1, 1);
142 layout->addWidget(caseLabel, 2, 0); layout->addWidget(caseCombo, 2, 1);
143 layout->addWidget(separatorLabel, 3, 0); layout->addWidget(separatorLineEdit, 3, 1);
144 layout->addWidget(wrapCheckBox, 4, 0);
145 layout->addWidget(contentsLabel, 5, 0, 1, 2);
146 layout->addWidget(lineEdit, 6, 0, 1, 2);
147 centralWidget->setLayout(layout);
148 setCentralWidget(centralWidget);
149
150 changeCase(caseCombo->currentIndex());
151 changeMode(modeCombo->currentIndex());
152
153 setWindowTitle(tr("Tree Model Completer"));
154 lineEdit->setFocus();
155}
156//! [3]
157
158//! [4]
159void MainWindow::createMenu()
160{
161 QAction *exitAction = new QAction(tr("Exit"), this);
162 QAction *aboutAct = new QAction(tr("About"), this);
163 QAction *aboutQtAct = new QAction(tr("About Qt"), this);
164
165 connect(exitAction, &QAction::triggered, qApp, &QApplication::quit);
166 connect(aboutAct, &QAction::triggered, this, &MainWindow::about);
167 connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt);
168
169 QMenu *fileMenu = menuBar()->addMenu(tr("File"));
170 fileMenu->addAction(exitAction);
171
172 QMenu *helpMenu = menuBar()->addMenu(tr("About"));
173 helpMenu->addAction(aboutAct);
174 helpMenu->addAction(aboutQtAct);
175}
176//! [4]
177
178//! [5]
179void MainWindow::changeMode(int index)
180{
181 QCompleter::CompletionMode mode;
182 if (index == 0)
183 mode = QCompleter::InlineCompletion;
184 else if (index == 1)
185 mode = QCompleter::PopupCompletion;
186 else
187 mode = QCompleter::UnfilteredPopupCompletion;
188
189 completer->setCompletionMode(mode);
190}
191//! [5]
192
193QAbstractItemModel *MainWindow::modelFromFile(const QString &fileName)
194{
195 QFile file(fileName);
196 if (!file.open(QFile::ReadOnly))
197 return new QStringListModel(completer);
198
199#ifndef QT_NO_CURSOR
200 QGuiApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
201#endif
202
203 QStandardItemModel *model = new QStandardItemModel(completer);
204 QList<QStandardItem *> parents(10);
205 parents[0] = model->invisibleRootItem();
206
207 QRegularExpression re("^\\s+");
208 while (!file.atEnd()) {
209 const QString line = QString::fromUtf8(file.readLine()).trimmed();
210 const QString trimmedLine = line.trimmed();
211 if (trimmedLine.isEmpty())
212 continue;
213
214 const QRegularExpressionMatch match = re.match(line);
215 int nonws = match.capturedStart();
216 int level = 0;
217 if (nonws == -1) {
218 level = 0;
219 } else {
220 const int capLen = match.capturedLength();
221 level = line.startsWith(QLatin1Char('\t')) ? capLen / 4 : capLen;
222 }
223
224 if (level + 1 >= parents.size())
225 parents.resize(parents.size() * 2);
226
227 QStandardItem *item = new QStandardItem;
228 item->setText(trimmedLine);
229 parents[level]->appendRow(item);
230 parents[level + 1] = item;
231 }
232
233#ifndef QT_NO_CURSOR
234 QGuiApplication::restoreOverrideCursor();
235#endif
236
237 return model;
238}
239
240void MainWindow::highlight(const QModelIndex &index)
241{
242 QAbstractItemModel *completionModel = completer->completionModel();
243 QAbstractProxyModel *proxy = qobject_cast<QAbstractProxyModel *>(completionModel);
244 if (!proxy)
245 return;
246 QModelIndex sourceIndex = proxy->mapToSource(index);
247 treeView->selectionModel()->select(sourceIndex, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
248 treeView->scrollTo(sourceIndex);
249}
250
251//! [6]
252void MainWindow::about()
253{
254 QMessageBox::about(this, tr("About"), tr("This example demonstrates how "
255 "to use a QCompleter with a custom tree model."));
256}
257//! [6]
258
259//! [7]
260void MainWindow::changeCase(int cs)
261{
262 completer->setCaseSensitivity(cs ? Qt::CaseSensitive : Qt::CaseInsensitive);
263}
264//! [7]
265
266void MainWindow::updateContentsLabel(const QString &sep)
267{
268 contentsLabel->setText(tr("Type path from model above with items at each level separated by a '%1'").arg(sep));
269}
270
271