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