| 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] |
| 71 | MainWindow::MainWindow(QWidget *parent) |
| 72 | : QMainWindow(parent) |
| 73 | { |
| 74 | createMenu(); |
| 75 | |
| 76 | completer = new TreeModelCompleter(this); |
| 77 | completer->setModel(modelFromFile(fileName: ":/resources/treemodel.txt" )); |
| 78 | completer->setSeparator(QLatin1String("." )); |
| 79 | QObject::connect(sender: completer, signal: QOverload<const QModelIndex &>::of(ptr: &TreeModelCompleter::highlighted), |
| 80 | receiver: this, slot: &MainWindow::highlight); |
| 81 | |
| 82 | QWidget *centralWidget = new QWidget; |
| 83 | |
| 84 | QLabel *modelLabel = new QLabel; |
| 85 | modelLabel->setText(tr(s: "Tree Model<br>(Double click items to edit)" )); |
| 86 | |
| 87 | QLabel *modeLabel = new QLabel; |
| 88 | modeLabel->setText(tr(s: "Completion Mode" )); |
| 89 | modeCombo = new QComboBox; |
| 90 | modeCombo->addItem(atext: tr(s: "Inline" )); |
| 91 | modeCombo->addItem(atext: tr(s: "Filtered Popup" )); |
| 92 | modeCombo->addItem(atext: tr(s: "Unfiltered Popup" )); |
| 93 | modeCombo->setCurrentIndex(1); |
| 94 | |
| 95 | QLabel *caseLabel = new QLabel; |
| 96 | caseLabel->setText(tr(s: "Case Sensitivity" )); |
| 97 | caseCombo = new QComboBox; |
| 98 | caseCombo->addItem(atext: tr(s: "Case Insensitive" )); |
| 99 | caseCombo->addItem(atext: tr(s: "Case Sensitive" )); |
| 100 | caseCombo->setCurrentIndex(0); |
| 101 | //! [0] |
| 102 | |
| 103 | //! [1] |
| 104 | QLabel *separatorLabel = new QLabel; |
| 105 | separatorLabel->setText(tr(s: "Tree Separator" )); |
| 106 | |
| 107 | QLineEdit *separatorLineEdit = new QLineEdit; |
| 108 | separatorLineEdit->setText(completer->separator()); |
| 109 | connect(sender: separatorLineEdit, signal: &QLineEdit::textChanged, |
| 110 | receiver: completer, slot: &TreeModelCompleter::setSeparator); |
| 111 | |
| 112 | QCheckBox *wrapCheckBox = new QCheckBox; |
| 113 | wrapCheckBox->setText(tr(s: "Wrap around completions" )); |
| 114 | wrapCheckBox->setChecked(completer->wrapAround()); |
| 115 | connect(sender: wrapCheckBox, signal: &QAbstractButton::clicked, receiver: completer, slot: &QCompleter::setWrapAround); |
| 116 | |
| 117 | contentsLabel = new QLabel; |
| 118 | contentsLabel->setSizePolicy(hor: QSizePolicy::Fixed, ver: QSizePolicy::Fixed); |
| 119 | connect(sender: separatorLineEdit, signal: &QLineEdit::textChanged, |
| 120 | receiver: this, slot: &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(sender: modeCombo, signal: QOverload<int>::of(ptr: &QComboBox::activated), |
| 130 | receiver: this, slot: &MainWindow::changeMode); |
| 131 | connect(sender: caseCombo, signal: QOverload<int>::of(ptr: &QComboBox::activated), |
| 132 | receiver: this, slot: &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, row: 0, column: 0); layout->addWidget(treeView, row: 0, column: 1); |
| 141 | layout->addWidget(modeLabel, row: 1, column: 0); layout->addWidget(modeCombo, row: 1, column: 1); |
| 142 | layout->addWidget(caseLabel, row: 2, column: 0); layout->addWidget(caseCombo, row: 2, column: 1); |
| 143 | layout->addWidget(separatorLabel, row: 3, column: 0); layout->addWidget(separatorLineEdit, row: 3, column: 1); |
| 144 | layout->addWidget(wrapCheckBox, row: 4, column: 0); |
| 145 | layout->addWidget(contentsLabel, row: 5, column: 0, rowSpan: 1, columnSpan: 2); |
| 146 | layout->addWidget(lineEdit, row: 6, column: 0, rowSpan: 1, columnSpan: 2); |
| 147 | centralWidget->setLayout(layout); |
| 148 | setCentralWidget(centralWidget); |
| 149 | |
| 150 | changeCase(caseCombo->currentIndex()); |
| 151 | changeMode(modeCombo->currentIndex()); |
| 152 | |
| 153 | setWindowTitle(tr(s: "Tree Model Completer" )); |
| 154 | lineEdit->setFocus(); |
| 155 | } |
| 156 | //! [3] |
| 157 | |
| 158 | //! [4] |
| 159 | void MainWindow::createMenu() |
| 160 | { |
| 161 | QAction *exitAction = new QAction(tr(s: "Exit" ), this); |
| 162 | QAction *aboutAct = new QAction(tr(s: "About" ), this); |
| 163 | QAction *aboutQtAct = new QAction(tr(s: "About Qt" ), this); |
| 164 | |
| 165 | connect(sender: exitAction, signal: &QAction::triggered, qApp, slot: &QApplication::quit); |
| 166 | connect(sender: aboutAct, signal: &QAction::triggered, receiver: this, slot: &MainWindow::about); |
| 167 | connect(sender: aboutQtAct, signal: &QAction::triggered, qApp, slot: &QApplication::aboutQt); |
| 168 | |
| 169 | QMenu * = menuBar()->addMenu(title: tr(s: "File" )); |
| 170 | fileMenu->addAction(action: exitAction); |
| 171 | |
| 172 | QMenu * = menuBar()->addMenu(title: tr(s: "About" )); |
| 173 | helpMenu->addAction(action: aboutAct); |
| 174 | helpMenu->addAction(action: aboutQtAct); |
| 175 | } |
| 176 | //! [4] |
| 177 | |
| 178 | //! [5] |
| 179 | void 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 | |
| 193 | QAbstractItemModel *MainWindow::modelFromFile(const QString &fileName) |
| 194 | { |
| 195 | QFile file(fileName); |
| 196 | if (!file.open(flags: 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 | QVector<QStandardItem *> parents(10); |
| 205 | parents[0] = model->invisibleRootItem(); |
| 206 | |
| 207 | QRegularExpression re("^\\s+" ); |
| 208 | while (!file.atEnd()) { |
| 209 | const QString line = QString::fromUtf8(str: file.readLine()); |
| 210 | const QString trimmedLine = line.trimmed(); |
| 211 | if (trimmedLine.isEmpty()) |
| 212 | continue; |
| 213 | |
| 214 | const QRegularExpressionMatch match = re.match(subject: 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 = capLen / 4; |
| 222 | } |
| 223 | |
| 224 | if (level + 1 >= parents.size()) |
| 225 | parents.resize(asize: parents.size() * 2); |
| 226 | |
| 227 | QStandardItem *item = new QStandardItem; |
| 228 | item->setText(trimmedLine); |
| 229 | parents[level]->appendRow(aitem: item); |
| 230 | parents[level + 1] = item; |
| 231 | } |
| 232 | |
| 233 | #ifndef QT_NO_CURSOR |
| 234 | QGuiApplication::restoreOverrideCursor(); |
| 235 | #endif |
| 236 | |
| 237 | return model; |
| 238 | } |
| 239 | |
| 240 | void MainWindow::highlight(const QModelIndex &index) |
| 241 | { |
| 242 | QAbstractItemModel *completionModel = completer->completionModel(); |
| 243 | QAbstractProxyModel *proxy = qobject_cast<QAbstractProxyModel *>(object: completionModel); |
| 244 | if (!proxy) |
| 245 | return; |
| 246 | QModelIndex sourceIndex = proxy->mapToSource(proxyIndex: index); |
| 247 | treeView->selectionModel()->select(index: sourceIndex, command: QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); |
| 248 | treeView->scrollTo(index: sourceIndex); |
| 249 | } |
| 250 | |
| 251 | //! [6] |
| 252 | void MainWindow::about() |
| 253 | { |
| 254 | QMessageBox::about(parent: this, title: tr(s: "About" ), text: tr(s: "This example demonstrates how " |
| 255 | "to use a QCompleter with a custom tree model." )); |
| 256 | } |
| 257 | //! [6] |
| 258 | |
| 259 | //! [7] |
| 260 | void MainWindow::changeCase(int cs) |
| 261 | { |
| 262 | completer->setCaseSensitivity(cs ? Qt::CaseSensitive : Qt::CaseInsensitive); |
| 263 | } |
| 264 | //! [7] |
| 265 | |
| 266 | void MainWindow::updateContentsLabel(const QString &sep) |
| 267 | { |
| 268 | contentsLabel->setText(tr(s: "Type path from model above with items at each level separated by a '%1'" ).arg(a: sep)); |
| 269 | } |
| 270 | |