| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the examples of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 10 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 15 | ** | 
|---|
| 16 | ** GNU Lesser General Public License Usage | 
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 20 | ** packaging of this file.  Please review the following information to | 
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 23 | ** | 
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional | 
|---|
| 25 | ** rights.  These rights are described in the Nokia Qt LGPL Exception | 
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include <QtGui> | 
|---|
| 43 | #include "treemodelcompleter.h" | 
|---|
| 44 | #include "mainwindow.h" | 
|---|
| 45 |  | 
|---|
| 46 | //! [0] | 
|---|
| 47 | MainWindow::MainWindow(QWidget *parent) | 
|---|
| 48 | : QMainWindow(parent), completer(0), lineEdit(0) | 
|---|
| 49 | { | 
|---|
| 50 | createMenu(); | 
|---|
| 51 |  | 
|---|
| 52 | completer = new TreeModelCompleter(this); | 
|---|
| 53 | completer->setModel(modelFromFile(":/resources/treemodel.txt")); | 
|---|
| 54 | completer->setSeparator(QLatin1String(".")); | 
|---|
| 55 | QObject::connect(completer, SIGNAL(highlighted(QModelIndex)), | 
|---|
| 56 | this, SLOT(highlight(QModelIndex))); | 
|---|
| 57 |  | 
|---|
| 58 | QWidget *centralWidget = new QWidget; | 
|---|
| 59 |  | 
|---|
| 60 | QLabel *modelLabel = new QLabel; | 
|---|
| 61 | modelLabel->setText(tr("Tree Model<br>(Double click items to edit)")); | 
|---|
| 62 |  | 
|---|
| 63 | QLabel *modeLabel = new QLabel; | 
|---|
| 64 | modeLabel->setText(tr("Completion Mode")); | 
|---|
| 65 | modeCombo = new QComboBox; | 
|---|
| 66 | modeCombo->addItem(tr("Inline")); | 
|---|
| 67 | modeCombo->addItem(tr("Filtered Popup")); | 
|---|
| 68 | modeCombo->addItem(tr("Unfiltered Popup")); | 
|---|
| 69 | modeCombo->setCurrentIndex(1); | 
|---|
| 70 |  | 
|---|
| 71 | QLabel *caseLabel = new QLabel; | 
|---|
| 72 | caseLabel->setText(tr("Case Sensitivity")); | 
|---|
| 73 | caseCombo = new QComboBox; | 
|---|
| 74 | caseCombo->addItem(tr("Case Insensitive")); | 
|---|
| 75 | caseCombo->addItem(tr("Case Sensitive")); | 
|---|
| 76 | caseCombo->setCurrentIndex(0); | 
|---|
| 77 | //! [0] | 
|---|
| 78 |  | 
|---|
| 79 | //! [1] | 
|---|
| 80 | QLabel *separatorLabel = new QLabel; | 
|---|
| 81 | separatorLabel->setText(tr("Tree Separator")); | 
|---|
| 82 |  | 
|---|
| 83 | QLineEdit *separatorLineEdit = new QLineEdit; | 
|---|
| 84 | separatorLineEdit->setText(completer->separator()); | 
|---|
| 85 | connect(separatorLineEdit, SIGNAL(textChanged(QString)), | 
|---|
| 86 | completer, SLOT(setSeparator(QString))); | 
|---|
| 87 |  | 
|---|
| 88 | QCheckBox *wrapCheckBox = new QCheckBox; | 
|---|
| 89 | wrapCheckBox->setText(tr("Wrap around completions")); | 
|---|
| 90 | wrapCheckBox->setChecked(completer->wrapAround()); | 
|---|
| 91 | connect(wrapCheckBox, SIGNAL(clicked(bool)), completer, SLOT(setWrapAround(bool))); | 
|---|
| 92 |  | 
|---|
| 93 | contentsLabel = new QLabel; | 
|---|
| 94 | contentsLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); | 
|---|
| 95 | connect(separatorLineEdit, SIGNAL(textChanged(QString)), | 
|---|
| 96 | this, SLOT(updateContentsLabel(QString))); | 
|---|
| 97 |  | 
|---|
| 98 | treeView = new QTreeView; | 
|---|
| 99 | treeView->setModel(completer->model()); | 
|---|
| 100 | treeView->header()->hide(); | 
|---|
| 101 | treeView->expandAll(); | 
|---|
| 102 | //! [1] | 
|---|
| 103 |  | 
|---|
| 104 | //! [2] | 
|---|
| 105 | connect(modeCombo, SIGNAL(activated(int)), this, SLOT(changeMode(int))); | 
|---|
| 106 | connect(caseCombo, SIGNAL(activated(int)), this, SLOT(changeCase(int))); | 
|---|
| 107 |  | 
|---|
| 108 | lineEdit = new QLineEdit; | 
|---|
| 109 | lineEdit->setCompleter(completer); | 
|---|
| 110 | //! [2] | 
|---|
| 111 |  | 
|---|
| 112 | //! [3] | 
|---|
| 113 | QGridLayout *layout = new QGridLayout; | 
|---|
| 114 | layout->addWidget(modelLabel, 0, 0); layout->addWidget(treeView, 0, 1); | 
|---|
| 115 | layout->addWidget(modeLabel, 1, 0);  layout->addWidget(modeCombo, 1, 1); | 
|---|
| 116 | layout->addWidget(caseLabel, 2, 0);  layout->addWidget(caseCombo, 2, 1); | 
|---|
| 117 | layout->addWidget(separatorLabel, 3, 0); layout->addWidget(separatorLineEdit, 3, 1); | 
|---|
| 118 | layout->addWidget(wrapCheckBox, 4, 0); | 
|---|
| 119 | layout->addWidget(contentsLabel, 5, 0, 1, 2); | 
|---|
| 120 | layout->addWidget(lineEdit, 6, 0, 1, 2); | 
|---|
| 121 | centralWidget->setLayout(layout); | 
|---|
| 122 | setCentralWidget(centralWidget); | 
|---|
| 123 |  | 
|---|
| 124 | changeCase(caseCombo->currentIndex()); | 
|---|
| 125 | changeMode(modeCombo->currentIndex()); | 
|---|
| 126 |  | 
|---|
| 127 | setWindowTitle(tr("Tree Model Completer")); | 
|---|
| 128 | lineEdit->setFocus(); | 
|---|
| 129 | } | 
|---|
| 130 | //! [3] | 
|---|
| 131 |  | 
|---|
| 132 | //! [4] | 
|---|
| 133 | void MainWindow::createMenu() | 
|---|
| 134 | { | 
|---|
| 135 | QAction *exitAction = new QAction(tr("Exit"), this); | 
|---|
| 136 | QAction *aboutAct = new QAction(tr("About"), this); | 
|---|
| 137 | QAction *aboutQtAct = new QAction(tr("About Qt"), this); | 
|---|
| 138 |  | 
|---|
| 139 | connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); | 
|---|
| 140 | connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); | 
|---|
| 141 | connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); | 
|---|
| 142 |  | 
|---|
| 143 | QMenu* fileMenu = menuBar()->addMenu(tr("File")); | 
|---|
| 144 | fileMenu->addAction(exitAction); | 
|---|
| 145 |  | 
|---|
| 146 | QMenu* helpMenu = menuBar()->addMenu(tr("About")); | 
|---|
| 147 | helpMenu->addAction(aboutAct); | 
|---|
| 148 | helpMenu->addAction(aboutQtAct); | 
|---|
| 149 | } | 
|---|
| 150 | //! [4] | 
|---|
| 151 |  | 
|---|
| 152 | //! [5] | 
|---|
| 153 | void MainWindow::changeMode(int index) | 
|---|
| 154 | { | 
|---|
| 155 | QCompleter::CompletionMode mode; | 
|---|
| 156 | if (index == 0) | 
|---|
| 157 | mode = QCompleter::InlineCompletion; | 
|---|
| 158 | else if (index == 1) | 
|---|
| 159 | mode = QCompleter::PopupCompletion; | 
|---|
| 160 | else | 
|---|
| 161 | mode = QCompleter::UnfilteredPopupCompletion; | 
|---|
| 162 |  | 
|---|
| 163 | completer->setCompletionMode(mode); | 
|---|
| 164 | } | 
|---|
| 165 | //! [5] | 
|---|
| 166 |  | 
|---|
| 167 | QAbstractItemModel *MainWindow::modelFromFile(const QString& fileName) | 
|---|
| 168 | { | 
|---|
| 169 | QFile file(fileName); | 
|---|
| 170 | if (!file.open(QFile::ReadOnly)) | 
|---|
| 171 | return new QStringListModel(completer); | 
|---|
| 172 |  | 
|---|
| 173 | #ifndef QT_NO_CURSOR | 
|---|
| 174 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); | 
|---|
| 175 | #endif | 
|---|
| 176 | QStringList words; | 
|---|
| 177 |  | 
|---|
| 178 | QStandardItemModel *model = new QStandardItemModel(completer); | 
|---|
| 179 | QVector<QStandardItem *> parents(10); | 
|---|
| 180 | parents[0] = model->invisibleRootItem(); | 
|---|
| 181 |  | 
|---|
| 182 | while (!file.atEnd()) { | 
|---|
| 183 | QString line = file.readLine(); | 
|---|
| 184 | QString trimmedLine = line.trimmed(); | 
|---|
| 185 | if (line.isEmpty() || trimmedLine.isEmpty()) | 
|---|
| 186 | continue; | 
|---|
| 187 |  | 
|---|
| 188 | QRegExp re("^\\s+"); | 
|---|
| 189 | int nonws = re.indexIn(line); | 
|---|
| 190 | int level = 0; | 
|---|
| 191 | if (nonws == -1) { | 
|---|
| 192 | level = 0; | 
|---|
| 193 | } else { | 
|---|
| 194 | if (line.startsWith("\t")) { | 
|---|
| 195 | level = re.cap(0).length(); | 
|---|
| 196 | } else { | 
|---|
| 197 | level = re.cap(0).length()/4; | 
|---|
| 198 | } | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | if (level+1 >= parents.size()) | 
|---|
| 202 | parents.resize(parents.size()*2); | 
|---|
| 203 |  | 
|---|
| 204 | QStandardItem *item = new QStandardItem; | 
|---|
| 205 | item->setText(trimmedLine); | 
|---|
| 206 | parents[level]->appendRow(item); | 
|---|
| 207 | parents[level+1] = item; | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 | #ifndef QT_NO_CURSOR | 
|---|
| 211 | QApplication::restoreOverrideCursor(); | 
|---|
| 212 | #endif | 
|---|
| 213 |  | 
|---|
| 214 | return model; | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 | void MainWindow::highlight(const QModelIndex &index) | 
|---|
| 218 | { | 
|---|
| 219 | QAbstractItemModel *completionModel = completer->completionModel(); | 
|---|
| 220 | QAbstractProxyModel *proxy = qobject_cast<QAbstractProxyModel *>(completionModel); | 
|---|
| 221 | if (!proxy) | 
|---|
| 222 | return; | 
|---|
| 223 | QModelIndex sourceIndex = proxy->mapToSource(index); | 
|---|
| 224 | treeView->selectionModel()->select(sourceIndex, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); | 
|---|
| 225 | treeView->scrollTo(index); | 
|---|
| 226 | } | 
|---|
| 227 |  | 
|---|
| 228 | //! [6] | 
|---|
| 229 | void MainWindow::about() | 
|---|
| 230 | { | 
|---|
| 231 | QMessageBox::about(this, tr("About"), tr("This example demonstrates how " | 
|---|
| 232 | "to use a QCompleter with a custom tree model.")); | 
|---|
| 233 | } | 
|---|
| 234 | //! [6] | 
|---|
| 235 |  | 
|---|
| 236 | //! [7] | 
|---|
| 237 | void MainWindow::changeCase(int cs) | 
|---|
| 238 | { | 
|---|
| 239 | completer->setCaseSensitivity(cs ? Qt::CaseSensitive : Qt::CaseInsensitive); | 
|---|
| 240 | } | 
|---|
| 241 | //! [7] | 
|---|
| 242 |  | 
|---|
| 243 | void MainWindow::updateContentsLabel(const QString& sep) | 
|---|
| 244 | { | 
|---|
| 245 | contentsLabel->setText(QString(tr("Type path from model above with items at each level separated by a '%1'")).arg(sep)); | 
|---|
| 246 | } | 
|---|
| 247 |  | 
|---|