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

Last change on this file since 117 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 8.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43#include "dirmodel.h"
44#include "mainwindow.h"
45
46//! [0]
47MainWindow::MainWindow(QWidget *parent)
48 : QMainWindow(parent), completer(0), lineEdit(0)
49{
50 createMenu();
51
52 QWidget *centralWidget = new QWidget;
53
54 QLabel *modelLabel = new QLabel;
55 modelLabel->setText(tr("Model"));
56
57 modelCombo = new QComboBox;
58 modelCombo->addItem(tr("QDirModel"));
59 modelCombo->addItem(tr("QDirModel that shows full path"));
60 modelCombo->addItem(tr("Country list"));
61 modelCombo->addItem(tr("Word list"));
62 modelCombo->setCurrentIndex(0);
63
64 QLabel *modeLabel = new QLabel;
65 modeLabel->setText(tr("Completion Mode"));
66 modeCombo = new QComboBox;
67 modeCombo->addItem(tr("Inline"));
68 modeCombo->addItem(tr("Filtered Popup"));
69 modeCombo->addItem(tr("Unfiltered Popup"));
70 modeCombo->setCurrentIndex(1);
71
72 QLabel *caseLabel = new QLabel;
73 caseLabel->setText(tr("Case Sensitivity"));
74 caseCombo = new QComboBox;
75 caseCombo->addItem(tr("Case Insensitive"));
76 caseCombo->addItem(tr("Case Sensitive"));
77 caseCombo->setCurrentIndex(0);
78//! [0]
79
80//! [1]
81 wrapCheckBox = new QCheckBox;
82 wrapCheckBox->setText(tr("Wrap around completions"));
83 wrapCheckBox->setChecked(true);
84//! [1]
85
86//! [2]
87 contentsLabel = new QLabel;
88 contentsLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
89
90 connect(modelCombo, SIGNAL(activated(int)), this, SLOT(changeModel()));
91 connect(modeCombo, SIGNAL(activated(int)), this, SLOT(changeMode(int)));
92 connect(caseCombo, SIGNAL(activated(int)), this, SLOT(changeCase(int)));
93//! [2]
94
95//! [3]
96 lineEdit = new QLineEdit;
97
98 QGridLayout *layout = new QGridLayout;
99 layout->addWidget(modelLabel, 0, 0); layout->addWidget(modelCombo, 0, 1);
100 layout->addWidget(modeLabel, 1, 0); layout->addWidget(modeCombo, 1, 1);
101 layout->addWidget(caseLabel, 2, 0); layout->addWidget(caseCombo, 2, 1);
102 layout->addWidget(wrapCheckBox, 3, 0);
103 layout->addWidget(contentsLabel, 4, 0, 1, 2);
104 layout->addWidget(lineEdit, 5, 0, 1, 2);
105 centralWidget->setLayout(layout);
106 setCentralWidget(centralWidget);
107
108 changeModel();
109
110 setWindowTitle(tr("Completer"));
111 lineEdit->setFocus();
112}
113//! [3]
114
115//! [4]
116void MainWindow::createMenu()
117{
118 QAction *exitAction = new QAction(tr("Exit"), this);
119 QAction *aboutAct = new QAction(tr("About"), this);
120 QAction *aboutQtAct = new QAction(tr("About Qt"), this);
121
122 connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
123 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
124 connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
125
126 QMenu* fileMenu = menuBar()->addMenu(tr("File"));
127 fileMenu->addAction(exitAction);
128
129 QMenu* helpMenu = menuBar()->addMenu(tr("About"));
130 helpMenu->addAction(aboutAct);
131 helpMenu->addAction(aboutQtAct);
132}
133//! [4]
134
135//! [5]
136QAbstractItemModel *MainWindow::modelFromFile(const QString& fileName)
137{
138 QFile file(fileName);
139 if (!file.open(QFile::ReadOnly))
140 return new QStringListModel(completer);
141//! [5]
142
143//! [6]
144#ifndef QT_NO_CURSOR
145 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
146#endif
147 QStringList words;
148
149 while (!file.atEnd()) {
150 QByteArray line = file.readLine();
151 if (!line.isEmpty())
152 words << line.trimmed();
153 }
154
155#ifndef QT_NO_CURSOR
156 QApplication::restoreOverrideCursor();
157#endif
158//! [6]
159
160//! [7]
161 if (!fileName.contains(QLatin1String("countries.txt")))
162 return new QStringListModel(words, completer);
163//! [7]
164
165 // The last two chars of the countries.txt file indicate the country
166 // symbol. We put that in column 2 of a standard item model
167//! [8]
168 QStandardItemModel *m = new QStandardItemModel(words.count(), 2, completer);
169//! [8] //! [9]
170 for (int i = 0; i < words.count(); ++i) {
171 QModelIndex countryIdx = m->index(i, 0);
172 QModelIndex symbolIdx = m->index(i, 1);
173 QString country = words[i].mid(0, words[i].length() - 2).trimmed();
174 QString symbol = words[i].right(2);
175 m->setData(countryIdx, country);
176 m->setData(symbolIdx, symbol);
177 }
178
179 return m;
180}
181//! [9]
182
183//! [10]
184void MainWindow::changeMode(int index)
185{
186 QCompleter::CompletionMode mode;
187 if (index == 0)
188 mode = QCompleter::InlineCompletion;
189 else if (index == 1)
190 mode = QCompleter::PopupCompletion;
191 else
192 mode = QCompleter::UnfilteredPopupCompletion;
193
194 completer->setCompletionMode(mode);
195}
196//! [10]
197
198void MainWindow::changeCase(int cs)
199{
200 completer->setCaseSensitivity(cs ? Qt::CaseSensitive : Qt::CaseInsensitive);
201}
202
203//! [11]
204void MainWindow::changeModel()
205{
206 delete completer;
207 completer = new QCompleter(this);
208
209 switch (modelCombo->currentIndex()) {
210 default:
211 case 0:
212 { // Unsorted QDirModel
213 QDirModel *dirModel = new QDirModel(completer);
214 completer->setModel(dirModel);
215 contentsLabel->setText(tr("Enter file path"));
216 }
217 break;
218//! [11] //! [12]
219 case 1:
220 { // DirModel that shows full paths
221 DirModel *dirModel = new DirModel(completer);
222 completer->setModel(dirModel);
223 contentsLabel->setText(tr("Enter file path"));
224 }
225 break;
226//! [12] //! [13]
227 case 2:
228 { // Country List
229 completer->setModel(modelFromFile(":/resources/countries.txt"));
230 QTreeView *treeView = new QTreeView;
231 completer->setPopup(treeView);
232 treeView->setRootIsDecorated(false);
233 treeView->header()->hide();
234 treeView->header()->setStretchLastSection(false);
235 treeView->header()->setResizeMode(0, QHeaderView::Stretch);
236 treeView->header()->setResizeMode(1, QHeaderView::ResizeToContents);
237 contentsLabel->setText(tr("Enter name of your country"));
238 }
239 break;
240//! [13] //! [14]
241 case 3:
242 { // Word list
243 completer->setModel(modelFromFile(":/resources/wordlist.txt"));
244 completer->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
245 contentsLabel->setText(tr("Enter a word"));
246 }
247 break;
248 }
249
250 changeMode(modeCombo->currentIndex());
251 changeCase(caseCombo->currentIndex());
252 completer->setWrapAround(wrapCheckBox->isChecked());
253 lineEdit->setCompleter(completer);
254 connect(wrapCheckBox, SIGNAL(clicked(bool)), completer, SLOT(setWrapAround(bool)));
255}
256//! [14]
257
258//! [15]
259void MainWindow::about()
260{
261 QMessageBox::about(this, tr("About"), tr("This example demonstrates the "
262 "different features of the QCompleter class."));
263}
264//! [15]
Note: See TracBrowser for help on using the repository browser.