|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 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 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
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 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <QtGui> |
|
43 #include "dirmodel.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 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 QLabel *maxVisibleLabel = new QLabel; |
|
82 maxVisibleLabel->setText(tr("Max Visible Items")); |
|
83 maxVisibleSpinBox = new QSpinBox; |
|
84 maxVisibleSpinBox->setRange(3,25); |
|
85 maxVisibleSpinBox->setValue(10); |
|
86 |
|
87 wrapCheckBox = new QCheckBox; |
|
88 wrapCheckBox->setText(tr("Wrap around completions")); |
|
89 wrapCheckBox->setChecked(true); |
|
90 //! [1] |
|
91 |
|
92 //! [2] |
|
93 contentsLabel = new QLabel; |
|
94 contentsLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
|
95 |
|
96 connect(modelCombo, SIGNAL(activated(int)), this, SLOT(changeModel())); |
|
97 connect(modeCombo, SIGNAL(activated(int)), this, SLOT(changeMode(int))); |
|
98 connect(caseCombo, SIGNAL(activated(int)), this, SLOT(changeCase(int))); |
|
99 connect(maxVisibleSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeMaxVisible(int))); |
|
100 //! [2] |
|
101 |
|
102 //! [3] |
|
103 lineEdit = new QLineEdit; |
|
104 |
|
105 QGridLayout *layout = new QGridLayout; |
|
106 layout->addWidget(modelLabel, 0, 0); layout->addWidget(modelCombo, 0, 1); |
|
107 layout->addWidget(modeLabel, 1, 0); layout->addWidget(modeCombo, 1, 1); |
|
108 layout->addWidget(caseLabel, 2, 0); layout->addWidget(caseCombo, 2, 1); |
|
109 layout->addWidget(maxVisibleLabel, 3, 0); layout->addWidget(maxVisibleSpinBox, 3, 1); |
|
110 layout->addWidget(wrapCheckBox, 4, 0); |
|
111 layout->addWidget(contentsLabel, 5, 0, 1, 2); |
|
112 layout->addWidget(lineEdit, 6, 0, 1, 2); |
|
113 centralWidget->setLayout(layout); |
|
114 setCentralWidget(centralWidget); |
|
115 |
|
116 changeModel(); |
|
117 |
|
118 setWindowTitle(tr("Completer")); |
|
119 lineEdit->setFocus(); |
|
120 } |
|
121 //! [3] |
|
122 |
|
123 //! [4] |
|
124 void MainWindow::createMenu() |
|
125 { |
|
126 QAction *exitAction = new QAction(tr("Exit"), this); |
|
127 QAction *aboutAct = new QAction(tr("About"), this); |
|
128 QAction *aboutQtAct = new QAction(tr("About Qt"), this); |
|
129 |
|
130 connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); |
|
131 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); |
|
132 connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); |
|
133 |
|
134 QMenu* fileMenu = menuBar()->addMenu(tr("File")); |
|
135 fileMenu->addAction(exitAction); |
|
136 |
|
137 QMenu* helpMenu = menuBar()->addMenu(tr("About")); |
|
138 helpMenu->addAction(aboutAct); |
|
139 helpMenu->addAction(aboutQtAct); |
|
140 } |
|
141 //! [4] |
|
142 |
|
143 //! [5] |
|
144 QAbstractItemModel *MainWindow::modelFromFile(const QString& fileName) |
|
145 { |
|
146 QFile file(fileName); |
|
147 if (!file.open(QFile::ReadOnly)) |
|
148 return new QStringListModel(completer); |
|
149 //! [5] |
|
150 |
|
151 //! [6] |
|
152 #ifndef QT_NO_CURSOR |
|
153 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
|
154 #endif |
|
155 QStringList words; |
|
156 |
|
157 while (!file.atEnd()) { |
|
158 QByteArray line = file.readLine(); |
|
159 if (!line.isEmpty()) |
|
160 words << line.trimmed(); |
|
161 } |
|
162 |
|
163 #ifndef QT_NO_CURSOR |
|
164 QApplication::restoreOverrideCursor(); |
|
165 #endif |
|
166 //! [6] |
|
167 |
|
168 //! [7] |
|
169 if (!fileName.contains(QLatin1String("countries.txt"))) |
|
170 return new QStringListModel(words, completer); |
|
171 //! [7] |
|
172 |
|
173 // The last two chars of the countries.txt file indicate the country |
|
174 // symbol. We put that in column 2 of a standard item model |
|
175 //! [8] |
|
176 QStandardItemModel *m = new QStandardItemModel(words.count(), 2, completer); |
|
177 //! [8] //! [9] |
|
178 for (int i = 0; i < words.count(); ++i) { |
|
179 QModelIndex countryIdx = m->index(i, 0); |
|
180 QModelIndex symbolIdx = m->index(i, 1); |
|
181 QString country = words[i].mid(0, words[i].length() - 2).trimmed(); |
|
182 QString symbol = words[i].right(2); |
|
183 m->setData(countryIdx, country); |
|
184 m->setData(symbolIdx, symbol); |
|
185 } |
|
186 |
|
187 return m; |
|
188 } |
|
189 //! [9] |
|
190 |
|
191 //! [10] |
|
192 void MainWindow::changeMode(int index) |
|
193 { |
|
194 QCompleter::CompletionMode mode; |
|
195 if (index == 0) |
|
196 mode = QCompleter::InlineCompletion; |
|
197 else if (index == 1) |
|
198 mode = QCompleter::PopupCompletion; |
|
199 else |
|
200 mode = QCompleter::UnfilteredPopupCompletion; |
|
201 |
|
202 completer->setCompletionMode(mode); |
|
203 } |
|
204 //! [10] |
|
205 |
|
206 void MainWindow::changeCase(int cs) |
|
207 { |
|
208 completer->setCaseSensitivity(cs ? Qt::CaseSensitive : Qt::CaseInsensitive); |
|
209 } |
|
210 |
|
211 //! [11] |
|
212 void MainWindow::changeModel() |
|
213 { |
|
214 delete completer; |
|
215 completer = new QCompleter(this); |
|
216 completer->setMaxVisibleItems(maxVisibleSpinBox->value()); |
|
217 |
|
218 switch (modelCombo->currentIndex()) { |
|
219 default: |
|
220 case 0: |
|
221 { // Unsorted QDirModel |
|
222 QDirModel *dirModel = new QDirModel(completer); |
|
223 completer->setModel(dirModel); |
|
224 contentsLabel->setText(tr("Enter file path")); |
|
225 } |
|
226 break; |
|
227 //! [11] //! [12] |
|
228 case 1: |
|
229 { // DirModel that shows full paths |
|
230 DirModel *dirModel = new DirModel(completer); |
|
231 completer->setModel(dirModel); |
|
232 contentsLabel->setText(tr("Enter file path")); |
|
233 } |
|
234 break; |
|
235 //! [12] //! [13] |
|
236 case 2: |
|
237 { // Country List |
|
238 completer->setModel(modelFromFile(":/resources/countries.txt")); |
|
239 QTreeView *treeView = new QTreeView; |
|
240 completer->setPopup(treeView); |
|
241 treeView->setRootIsDecorated(false); |
|
242 treeView->header()->hide(); |
|
243 treeView->header()->setStretchLastSection(false); |
|
244 treeView->header()->setResizeMode(0, QHeaderView::Stretch); |
|
245 treeView->header()->setResizeMode(1, QHeaderView::ResizeToContents); |
|
246 contentsLabel->setText(tr("Enter name of your country")); |
|
247 } |
|
248 break; |
|
249 //! [13] //! [14] |
|
250 case 3: |
|
251 { // Word list |
|
252 completer->setModel(modelFromFile(":/resources/wordlist.txt")); |
|
253 completer->setModelSorting(QCompleter::CaseInsensitivelySortedModel); |
|
254 contentsLabel->setText(tr("Enter a word")); |
|
255 } |
|
256 break; |
|
257 } |
|
258 |
|
259 changeMode(modeCombo->currentIndex()); |
|
260 changeCase(caseCombo->currentIndex()); |
|
261 completer->setWrapAround(wrapCheckBox->isChecked()); |
|
262 lineEdit->setCompleter(completer); |
|
263 connect(wrapCheckBox, SIGNAL(clicked(bool)), completer, SLOT(setWrapAround(bool))); |
|
264 } |
|
265 //! [14] |
|
266 |
|
267 //! [15] |
|
268 void MainWindow::changeMaxVisible(int max) |
|
269 { |
|
270 completer->setMaxVisibleItems(max); |
|
271 } |
|
272 //! [15] |
|
273 |
|
274 //! [16] |
|
275 void MainWindow::about() |
|
276 { |
|
277 QMessageBox::about(this, tr("About"), tr("This example demonstrates the " |
|
278 "different features of the QCompleter class.")); |
|
279 } |
|
280 //! [16] |