0
|
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 documentation 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 |
|
|
44 |
#include "mainwindow.h"
|
|
45 |
|
|
46 |
MainWindow::MainWindow()
|
|
47 |
{
|
|
48 |
QMenu *fileMenu = new QMenu(tr("&File"));
|
|
49 |
|
|
50 |
QAction *quitAction = fileMenu->addAction(tr("E&xit"));
|
|
51 |
quitAction->setShortcut(tr("Ctrl+Q"));
|
|
52 |
|
|
53 |
QMenu *itemsMenu = new QMenu(tr("&Items"));
|
|
54 |
|
|
55 |
insertAction = itemsMenu->addAction(tr("&Insert Item"));
|
|
56 |
removeAction = itemsMenu->addAction(tr("&Remove Item"));
|
|
57 |
removeAction->setEnabled(false);
|
|
58 |
itemsMenu->addSeparator();
|
|
59 |
ascendingAction = itemsMenu->addAction(tr("Sort in &Ascending Order"));
|
|
60 |
descendingAction = itemsMenu->addAction(tr("Sort in &Descending Order"));
|
|
61 |
autoSortAction = itemsMenu->addAction(tr("&Automatically Sort Items"));
|
|
62 |
autoSortAction->setCheckable(true);
|
|
63 |
itemsMenu->addSeparator();
|
|
64 |
QAction *findItemsAction = itemsMenu->addAction(tr("&Find Items"));
|
|
65 |
findItemsAction->setShortcut(tr("Ctrl+F"));
|
|
66 |
|
|
67 |
menuBar()->addMenu(fileMenu);
|
|
68 |
menuBar()->addMenu(itemsMenu);
|
|
69 |
|
|
70 |
/* For convenient quoting:
|
|
71 |
//! [0]
|
|
72 |
QTreeWidget *treeWidget = new QTreeWidget(this);
|
|
73 |
//! [0]
|
|
74 |
*/
|
|
75 |
treeWidget = new QTreeWidget(this);
|
|
76 |
//! [1]
|
|
77 |
treeWidget->setColumnCount(2);
|
|
78 |
//! [1] //! [2]
|
|
79 |
QStringList headers;
|
|
80 |
headers << tr("Subject") << tr("Default");
|
|
81 |
treeWidget->setHeaderLabels(headers);
|
|
82 |
//! [2]
|
|
83 |
|
|
84 |
connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));
|
|
85 |
connect(ascendingAction, SIGNAL(triggered()), this, SLOT(sortAscending()));
|
|
86 |
connect(autoSortAction, SIGNAL(triggered()), this, SLOT(updateSortItems()));
|
|
87 |
connect(descendingAction, SIGNAL(triggered()), this, SLOT(sortDescending()));
|
|
88 |
connect(findItemsAction, SIGNAL(triggered()), this, SLOT(findItems()));
|
|
89 |
connect(insertAction, SIGNAL(triggered()), this, SLOT(insertItem()));
|
|
90 |
connect(removeAction, SIGNAL(triggered()), this, SLOT(removeItem()));
|
|
91 |
connect(treeWidget,
|
|
92 |
SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
|
|
93 |
this, SLOT(updateMenus(QTreeWidgetItem *)));
|
|
94 |
|
|
95 |
setupTreeItems();
|
|
96 |
updateMenus(treeWidget->currentItem());
|
|
97 |
|
|
98 |
setCentralWidget(treeWidget);
|
|
99 |
setWindowTitle(tr("Tree Widget"));
|
|
100 |
}
|
|
101 |
|
|
102 |
void MainWindow::setupTreeItems()
|
|
103 |
{
|
|
104 |
//! [3]
|
|
105 |
QTreeWidgetItem *cities = new QTreeWidgetItem(treeWidget);
|
|
106 |
cities->setText(0, tr("Cities"));
|
|
107 |
QTreeWidgetItem *osloItem = new QTreeWidgetItem(cities);
|
|
108 |
osloItem->setText(0, tr("Oslo"));
|
|
109 |
osloItem->setText(1, tr("Yes"));
|
|
110 |
//! [3]
|
|
111 |
|
|
112 |
(new QTreeWidgetItem(cities))->setText(0, tr("Stockholm"));
|
|
113 |
(new QTreeWidgetItem(cities))->setText(0, tr("Helsinki"));
|
|
114 |
(new QTreeWidgetItem(cities))->setText(0, tr("Copenhagen"));
|
|
115 |
|
|
116 |
//! [4] //! [5]
|
|
117 |
QTreeWidgetItem *planets = new QTreeWidgetItem(treeWidget, cities);
|
|
118 |
//! [4]
|
|
119 |
planets->setText(0, tr("Planets"));
|
|
120 |
//! [5]
|
|
121 |
(new QTreeWidgetItem(planets))->setText(0, tr("Mercury"));
|
|
122 |
(new QTreeWidgetItem(planets))->setText(0, tr("Venus"));
|
|
123 |
|
|
124 |
QTreeWidgetItem *earthItem = new QTreeWidgetItem(planets);
|
|
125 |
earthItem->setText(0, tr("Earth"));
|
|
126 |
earthItem->setText(1, tr("Yes"));
|
|
127 |
|
|
128 |
(new QTreeWidgetItem(planets))->setText(0, tr("Mars"));
|
|
129 |
(new QTreeWidgetItem(planets))->setText(0, tr("Jupiter"));
|
|
130 |
(new QTreeWidgetItem(planets))->setText(0, tr("Saturn"));
|
|
131 |
(new QTreeWidgetItem(planets))->setText(0, tr("Uranus"));
|
|
132 |
(new QTreeWidgetItem(planets))->setText(0, tr("Neptune"));
|
|
133 |
(new QTreeWidgetItem(planets))->setText(0, tr("Pluto"));
|
|
134 |
}
|
|
135 |
|
|
136 |
void MainWindow::findItems()
|
|
137 |
{
|
|
138 |
QString itemText = QInputDialog::getText(this, tr("Find Items"),
|
|
139 |
tr("Text to find (including wildcards):"));
|
|
140 |
|
|
141 |
if (itemText.isEmpty())
|
|
142 |
return;
|
|
143 |
|
|
144 |
//! [6]
|
|
145 |
QTreeWidgetItem *item;
|
|
146 |
//! [6]
|
|
147 |
foreach (item, treeWidget->selectedItems())
|
|
148 |
treeWidget->setItemSelected(item, false);
|
|
149 |
|
|
150 |
//! [7]
|
|
151 |
QList<QTreeWidgetItem *> found = treeWidget->findItems(
|
|
152 |
itemText, Qt::MatchWildcard);
|
|
153 |
|
|
154 |
foreach (item, found) {
|
|
155 |
treeWidget->setItemSelected(item, true);
|
|
156 |
// Show the item->text(0) for each item.
|
|
157 |
}
|
|
158 |
//! [7]
|
|
159 |
}
|
|
160 |
|
|
161 |
void MainWindow::insertItem()
|
|
162 |
{
|
|
163 |
QTreeWidgetItem *currentItem = treeWidget->currentItem();
|
|
164 |
|
|
165 |
if (!currentItem)
|
|
166 |
return;
|
|
167 |
|
|
168 |
QString itemText = QInputDialog::getText(this, tr("Insert Item"),
|
|
169 |
tr("Input text for the new item:"));
|
|
170 |
|
|
171 |
if (itemText.isEmpty())
|
|
172 |
return;
|
|
173 |
|
|
174 |
//! [8]
|
|
175 |
QTreeWidgetItem *parent = currentItem->parent();
|
|
176 |
QTreeWidgetItem *newItem;
|
|
177 |
if (parent)
|
|
178 |
newItem = new QTreeWidgetItem(parent, treeWidget->currentItem());
|
|
179 |
else
|
|
180 |
//! [8] //! [9]
|
|
181 |
newItem = new QTreeWidgetItem(treeWidget, treeWidget->currentItem());
|
|
182 |
//! [9]
|
|
183 |
|
|
184 |
newItem->setText(0, itemText);
|
|
185 |
}
|
|
186 |
|
|
187 |
void MainWindow::removeItem()
|
|
188 |
{
|
|
189 |
QTreeWidgetItem *currentItem = treeWidget->currentItem();
|
|
190 |
|
|
191 |
if (!currentItem)
|
|
192 |
return;
|
|
193 |
|
|
194 |
//! [10]
|
|
195 |
QTreeWidgetItem *parent = currentItem->parent();
|
|
196 |
int index;
|
|
197 |
|
|
198 |
if (parent) {
|
|
199 |
index = parent->indexOfChild(treeWidget->currentItem());
|
|
200 |
delete parent->takeChild(index);
|
|
201 |
} else {
|
|
202 |
index = treeWidget->indexOfTopLevelItem(treeWidget->currentItem());
|
|
203 |
delete treeWidget->takeTopLevelItem(index);
|
|
204 |
//! [10] //! [11]
|
|
205 |
}
|
|
206 |
//! [11]
|
|
207 |
}
|
|
208 |
|
|
209 |
void MainWindow::sortAscending()
|
|
210 |
{
|
|
211 |
treeWidget->sortItems(0, Qt::AscendingOrder);
|
|
212 |
}
|
|
213 |
|
|
214 |
void MainWindow::sortDescending()
|
|
215 |
{
|
|
216 |
treeWidget->sortItems(0, Qt::DescendingOrder);
|
|
217 |
}
|
|
218 |
|
|
219 |
void MainWindow::updateMenus(QTreeWidgetItem *current)
|
|
220 |
{
|
|
221 |
insertAction->setEnabled(current != 0);
|
|
222 |
removeAction->setEnabled(current != 0);
|
|
223 |
}
|
|
224 |
|
|
225 |
void MainWindow::updateSortItems()
|
|
226 |
{
|
|
227 |
ascendingAction->setEnabled(!autoSortAction->isChecked());
|
|
228 |
descendingAction->setEnabled(!autoSortAction->isChecked());
|
|
229 |
|
|
230 |
treeWidget->setSortingEnabled(autoSortAction->isChecked());
|
|
231 |
}
|