source: trunk/examples/tools/completer/mainwindow.cpp@ 983

Last change on this file since 983 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: 9.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
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 "fsmodel.h"
43#include "mainwindow.h"
44
45//! [0]
46MainWindow::MainWindow(QWidget *parent)
47 : QMainWindow(parent), completer(0), lineEdit(0)
48{
49 createMenu();
50
51 QWidget *centralWidget = new QWidget;
52
53 QLabel *modelLabel = new QLabel;
54 modelLabel->setText(tr("Model"));
55
56 modelCombo = new QComboBox;
57 modelCombo->addItem(tr("QFileSytemModel"));
58 modelCombo->addItem(tr("QFileSytemModel that shows full path"));
59 modelCombo->addItem(tr("Country list"));
60 modelCombo->addItem(tr("Word list"));
61 modelCombo->setCurrentIndex(0);
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 *maxVisibleLabel = new QLabel;
81 maxVisibleLabel->setText(tr("Max Visible Items"));
82 maxVisibleSpinBox = new QSpinBox;
83 maxVisibleSpinBox->setRange(3,25);
84 maxVisibleSpinBox->setValue(10);
85
86 wrapCheckBox = new QCheckBox;
87 wrapCheckBox->setText(tr("Wrap around completions"));
88 wrapCheckBox->setChecked(true);
89//! [1]
90
91//! [2]
92 contentsLabel = new QLabel;
93 contentsLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
94
95 connect(modelCombo, SIGNAL(activated(int)), this, SLOT(changeModel()));
96 connect(modeCombo, SIGNAL(activated(int)), this, SLOT(changeMode(int)));
97 connect(caseCombo, SIGNAL(activated(int)), this, SLOT(changeCase(int)));
98 connect(maxVisibleSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeMaxVisible(int)));
99//! [2]
100
101//! [3]
102 lineEdit = new QLineEdit;
103
104 QGridLayout *layout = new QGridLayout;
105 layout->addWidget(modelLabel, 0, 0); layout->addWidget(modelCombo, 0, 1);
106 layout->addWidget(modeLabel, 1, 0); layout->addWidget(modeCombo, 1, 1);
107 layout->addWidget(caseLabel, 2, 0); layout->addWidget(caseCombo, 2, 1);
108 layout->addWidget(maxVisibleLabel, 3, 0); layout->addWidget(maxVisibleSpinBox, 3, 1);
109 layout->addWidget(wrapCheckBox, 4, 0);
110 layout->addWidget(contentsLabel, 5, 0, 1, 2);
111 layout->addWidget(lineEdit, 6, 0, 1, 2);
112 centralWidget->setLayout(layout);
113 setCentralWidget(centralWidget);
114
115 changeModel();
116
117 setWindowTitle(tr("Completer"));
118 lineEdit->setFocus();
119}
120//! [3]
121
122//! [4]
123void MainWindow::createMenu()
124{
125 QAction *exitAction = new QAction(tr("Exit"), this);
126 QAction *aboutAct = new QAction(tr("About"), this);
127 QAction *aboutQtAct = new QAction(tr("About Qt"), this);
128
129 connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
130 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
131 connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
132
133 QMenu* fileMenu = menuBar()->addMenu(tr("File"));
134 fileMenu->addAction(exitAction);
135
136 QMenu* helpMenu = menuBar()->addMenu(tr("About"));
137 helpMenu->addAction(aboutAct);
138 helpMenu->addAction(aboutQtAct);
139}
140//! [4]
141
142//! [5]
143QAbstractItemModel *MainWindow::modelFromFile(const QString& fileName)
144{
145 QFile file(fileName);
146 if (!file.open(QFile::ReadOnly))
147 return new QStringListModel(completer);
148//! [5]
149
150//! [6]
151#ifndef QT_NO_CURSOR
152 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
153#endif
154 QStringList words;
155
156 while (!file.atEnd()) {
157 QByteArray line = file.readLine();
158 if (!line.isEmpty())
159 words << line.trimmed();
160 }
161
162#ifndef QT_NO_CURSOR
163 QApplication::restoreOverrideCursor();
164#endif
165//! [6]
166
167//! [7]
168 if (!fileName.contains(QLatin1String("countries.txt")))
169 return new QStringListModel(words, completer);
170//! [7]
171
172 // The last two chars of the countries.txt file indicate the country
173 // symbol. We put that in column 2 of a standard item model
174//! [8]
175 QStandardItemModel *m = new QStandardItemModel(words.count(), 2, completer);
176//! [8] //! [9]
177 for (int i = 0; i < words.count(); ++i) {
178 QModelIndex countryIdx = m->index(i, 0);
179 QModelIndex symbolIdx = m->index(i, 1);
180 QString country = words[i].mid(0, words[i].length() - 2).trimmed();
181 QString symbol = words[i].right(2);
182 m->setData(countryIdx, country);
183 m->setData(symbolIdx, symbol);
184 }
185
186 return m;
187}
188//! [9]
189
190//! [10]
191void MainWindow::changeMode(int index)
192{
193 QCompleter::CompletionMode mode;
194 if (index == 0)
195 mode = QCompleter::InlineCompletion;
196 else if (index == 1)
197 mode = QCompleter::PopupCompletion;
198 else
199 mode = QCompleter::UnfilteredPopupCompletion;
200
201 completer->setCompletionMode(mode);
202}
203//! [10]
204
205void MainWindow::changeCase(int cs)
206{
207 completer->setCaseSensitivity(cs ? Qt::CaseSensitive : Qt::CaseInsensitive);
208}
209
210//! [11]
211void MainWindow::changeModel()
212{
213 delete completer;
214 completer = new QCompleter(this);
215 completer->setMaxVisibleItems(maxVisibleSpinBox->value());
216
217 switch (modelCombo->currentIndex()) {
218 default:
219 case 0:
220 { // Unsorted QFileSystemModel
221 QFileSystemModel *fsModel = new QFileSystemModel(completer);
222 fsModel->setRootPath("");
223 completer->setModel(fsModel);
224 contentsLabel->setText(tr("Enter file path"));
225 }
226 break;
227//! [11] //! [12]
228 case 1:
229 { // FileSystemModel that shows full paths
230 FileSystemModel *fsModel = new FileSystemModel(completer);
231 completer->setModel(fsModel);
232 fsModel->setRootPath("");
233 contentsLabel->setText(tr("Enter file path"));
234 }
235 break;
236//! [12] //! [13]
237 case 2:
238 { // Country List
239 completer->setModel(modelFromFile(":/resources/countries.txt"));
240 QTreeView *treeView = new QTreeView;
241 completer->setPopup(treeView);
242 treeView->setRootIsDecorated(false);
243 treeView->header()->hide();
244 treeView->header()->setStretchLastSection(false);
245 treeView->header()->setResizeMode(0, QHeaderView::Stretch);
246 treeView->header()->setResizeMode(1, QHeaderView::ResizeToContents);
247 contentsLabel->setText(tr("Enter name of your country"));
248 }
249 break;
250//! [13] //! [14]
251 case 3:
252 { // Word list
253 completer->setModel(modelFromFile(":/resources/wordlist.txt"));
254 completer->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
255 contentsLabel->setText(tr("Enter a word"));
256 }
257 break;
258 }
259
260 changeMode(modeCombo->currentIndex());
261 changeCase(caseCombo->currentIndex());
262 completer->setWrapAround(wrapCheckBox->isChecked());
263 lineEdit->setCompleter(completer);
264 connect(wrapCheckBox, SIGNAL(clicked(bool)), completer, SLOT(setWrapAround(bool)));
265}
266//! [14]
267
268//! [15]
269void MainWindow::changeMaxVisible(int max)
270{
271 completer->setMaxVisibleItems(max);
272}
273//! [15]
274
275//! [16]
276void MainWindow::about()
277{
278 QMessageBox::about(this, tr("About"), tr("This example demonstrates the "
279 "different features of the QCompleter class."));
280}
281//! [16]
Note: See TracBrowser for help on using the repository browser.