source: trunk/examples/tools/treemodelcompleter/mainwindow.cpp@ 1147

Last change on this file since 1147 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 8.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 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:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include <QtGui>
42#include "treemodelcompleter.h"
43#include "mainwindow.h"
44
45//! [0]
46MainWindow::MainWindow(QWidget *parent)
47 : QMainWindow(parent), completer(0), lineEdit(0)
48{
49 createMenu();
50
51 completer = new TreeModelCompleter(this);
52 completer->setModel(modelFromFile(":/resources/treemodel.txt"));
53 completer->setSeparator(QLatin1String("."));
54 QObject::connect(completer, SIGNAL(highlighted(QModelIndex)),
55 this, SLOT(highlight(QModelIndex)));
56
57 QWidget *centralWidget = new QWidget;
58
59 QLabel *modelLabel = new QLabel;
60 modelLabel->setText(tr("Tree Model<br>(Double click items to edit)"));
61
62 QLabel *modeLabel = new QLabel;
63 modeLabel->setText(tr("Completion Mode"));
64 modeCombo = new QComboBox;
65 modeCombo->addItem(tr("Inline"));
66 modeCombo->addItem(tr("Filtered Popup"));
67 modeCombo->addItem(tr("Unfiltered Popup"));
68 modeCombo->setCurrentIndex(1);
69
70 QLabel *caseLabel = new QLabel;
71 caseLabel->setText(tr("Case Sensitivity"));
72 caseCombo = new QComboBox;
73 caseCombo->addItem(tr("Case Insensitive"));
74 caseCombo->addItem(tr("Case Sensitive"));
75 caseCombo->setCurrentIndex(0);
76//! [0]
77
78//! [1]
79 QLabel *separatorLabel = new QLabel;
80 separatorLabel->setText(tr("Tree Separator"));
81
82 QLineEdit *separatorLineEdit = new QLineEdit;
83 separatorLineEdit->setText(completer->separator());
84 connect(separatorLineEdit, SIGNAL(textChanged(QString)),
85 completer, SLOT(setSeparator(QString)));
86
87 QCheckBox *wrapCheckBox = new QCheckBox;
88 wrapCheckBox->setText(tr("Wrap around completions"));
89 wrapCheckBox->setChecked(completer->wrapAround());
90 connect(wrapCheckBox, SIGNAL(clicked(bool)), completer, SLOT(setWrapAround(bool)));
91
92 contentsLabel = new QLabel;
93 contentsLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
94 connect(separatorLineEdit, SIGNAL(textChanged(QString)),
95 this, SLOT(updateContentsLabel(QString)));
96
97 treeView = new QTreeView;
98 treeView->setModel(completer->model());
99 treeView->header()->hide();
100 treeView->expandAll();
101//! [1]
102
103//! [2]
104 connect(modeCombo, SIGNAL(activated(int)), this, SLOT(changeMode(int)));
105 connect(caseCombo, SIGNAL(activated(int)), this, SLOT(changeCase(int)));
106
107 lineEdit = new QLineEdit;
108 lineEdit->setCompleter(completer);
109//! [2]
110
111//! [3]
112 QGridLayout *layout = new QGridLayout;
113 layout->addWidget(modelLabel, 0, 0); layout->addWidget(treeView, 0, 1);
114 layout->addWidget(modeLabel, 1, 0); layout->addWidget(modeCombo, 1, 1);
115 layout->addWidget(caseLabel, 2, 0); layout->addWidget(caseCombo, 2, 1);
116 layout->addWidget(separatorLabel, 3, 0); layout->addWidget(separatorLineEdit, 3, 1);
117 layout->addWidget(wrapCheckBox, 4, 0);
118 layout->addWidget(contentsLabel, 5, 0, 1, 2);
119 layout->addWidget(lineEdit, 6, 0, 1, 2);
120 centralWidget->setLayout(layout);
121 setCentralWidget(centralWidget);
122
123 changeCase(caseCombo->currentIndex());
124 changeMode(modeCombo->currentIndex());
125
126 setWindowTitle(tr("Tree Model Completer"));
127 lineEdit->setFocus();
128}
129//! [3]
130
131//! [4]
132void MainWindow::createMenu()
133{
134 QAction *exitAction = new QAction(tr("Exit"), this);
135 QAction *aboutAct = new QAction(tr("About"), this);
136 QAction *aboutQtAct = new QAction(tr("About Qt"), this);
137
138 connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
139 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
140 connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
141
142 QMenu* fileMenu = menuBar()->addMenu(tr("File"));
143 fileMenu->addAction(exitAction);
144
145 QMenu* helpMenu = menuBar()->addMenu(tr("About"));
146 helpMenu->addAction(aboutAct);
147 helpMenu->addAction(aboutQtAct);
148}
149//! [4]
150
151//! [5]
152void MainWindow::changeMode(int index)
153{
154 QCompleter::CompletionMode mode;
155 if (index == 0)
156 mode = QCompleter::InlineCompletion;
157 else if (index == 1)
158 mode = QCompleter::PopupCompletion;
159 else
160 mode = QCompleter::UnfilteredPopupCompletion;
161
162 completer->setCompletionMode(mode);
163}
164//! [5]
165
166QAbstractItemModel *MainWindow::modelFromFile(const QString& fileName)
167{
168 QFile file(fileName);
169 if (!file.open(QFile::ReadOnly))
170 return new QStringListModel(completer);
171
172#ifndef QT_NO_CURSOR
173 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
174#endif
175 QStringList words;
176
177 QStandardItemModel *model = new QStandardItemModel(completer);
178 QVector<QStandardItem *> parents(10);
179 parents[0] = model->invisibleRootItem();
180
181 while (!file.atEnd()) {
182 QString line = file.readLine();
183 QString trimmedLine = line.trimmed();
184 if (line.isEmpty() || trimmedLine.isEmpty())
185 continue;
186
187 QRegExp re("^\\s+");
188 int nonws = re.indexIn(line);
189 int level = 0;
190 if (nonws == -1) {
191 level = 0;
192 } else {
193 if (line.startsWith("\t")) {
194 level = re.cap(0).length();
195 } else {
196 level = re.cap(0).length()/4;
197 }
198 }
199
200 if (level+1 >= parents.size())
201 parents.resize(parents.size()*2);
202
203 QStandardItem *item = new QStandardItem;
204 item->setText(trimmedLine);
205 parents[level]->appendRow(item);
206 parents[level+1] = item;
207 }
208
209#ifndef QT_NO_CURSOR
210 QApplication::restoreOverrideCursor();
211#endif
212
213 return model;
214}
215
216void MainWindow::highlight(const QModelIndex &index)
217{
218 QAbstractItemModel *completionModel = completer->completionModel();
219 QAbstractProxyModel *proxy = qobject_cast<QAbstractProxyModel *>(completionModel);
220 if (!proxy)
221 return;
222 QModelIndex sourceIndex = proxy->mapToSource(index);
223 treeView->selectionModel()->select(sourceIndex, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
224 treeView->scrollTo(index);
225}
226
227//! [6]
228void MainWindow::about()
229{
230 QMessageBox::about(this, tr("About"), tr("This example demonstrates how "
231 "to use a QCompleter with a custom tree model."));
232}
233//! [6]
234
235//! [7]
236void MainWindow::changeCase(int cs)
237{
238 completer->setCaseSensitivity(cs ? Qt::CaseSensitive : Qt::CaseInsensitive);
239}
240//! [7]
241
242void MainWindow::updateContentsLabel(const QString& sep)
243{
244 contentsLabel->setText(tr("Type path from model above with items at each level separated by a '%1'").arg(sep));
245}
246
Note: See TracBrowser for help on using the repository browser.