|
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 QTreeWidget *treeWidget = new QTreeWidget(this); |
|
72 */ |
|
73 treeWidget = new QTreeWidget(this); |
|
74 treeWidget->setColumnCount(2); |
|
75 QStringList headers; |
|
76 headers << tr("Subject") << tr("Default"); |
|
77 treeWidget->setHeaderLabels(headers); |
|
78 |
|
79 connect(quitAction, SIGNAL(triggered()), this, SLOT(close())); |
|
80 connect(ascendingAction, SIGNAL(triggered()), this, SLOT(sortAscending())); |
|
81 connect(autoSortAction, SIGNAL(triggered()), this, SLOT(updateSortItems())); |
|
82 connect(descendingAction, SIGNAL(triggered()), this, SLOT(sortDescending())); |
|
83 connect(findItemsAction, SIGNAL(triggered()), this, SLOT(findItems())); |
|
84 connect(insertAction, SIGNAL(triggered()), this, SLOT(insertItem())); |
|
85 connect(removeAction, SIGNAL(triggered()), this, SLOT(removeItem())); |
|
86 connect(treeWidget, |
|
87 SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)), |
|
88 this, SLOT(updateMenus(QTreeWidgetItem *))); |
|
89 |
|
90 setupTreeItems(); |
|
91 updateMenus(treeWidget->currentItem()); |
|
92 |
|
93 setCentralWidget(treeWidget); |
|
94 setWindowTitle(tr("Tree Widget")); |
|
95 } |
|
96 |
|
97 void MainWindow::setupTreeItems() |
|
98 { |
|
99 QTreeWidgetItem *planets = new QTreeWidgetItem(treeWidget); |
|
100 planets->setText(0, tr("Planets")); |
|
101 (new QTreeWidgetItem(planets))->setText(0, tr("Mercury")); |
|
102 (new QTreeWidgetItem(planets))->setText(0, tr("Venus")); |
|
103 |
|
104 QTreeWidgetItem *earthItem = new QTreeWidgetItem(planets); |
|
105 earthItem->setText(0, tr("Earth")); |
|
106 earthItem->setText(1, tr("Yes")); |
|
107 |
|
108 (new QTreeWidgetItem(planets))->setText(0, tr("Mars")); |
|
109 (new QTreeWidgetItem(planets))->setText(0, tr("Jupiter")); |
|
110 (new QTreeWidgetItem(planets))->setText(0, tr("Saturn")); |
|
111 (new QTreeWidgetItem(planets))->setText(0, tr("Uranus")); |
|
112 (new QTreeWidgetItem(planets))->setText(0, tr("Neptune")); |
|
113 (new QTreeWidgetItem(planets))->setText(0, tr("Pluto")); |
|
114 } |
|
115 |
|
116 void MainWindow::findItems() |
|
117 { |
|
118 QString itemText = QInputDialog::getText(this, tr("Find Items"), |
|
119 tr("Text to find:")); |
|
120 |
|
121 if (itemText.isEmpty()) |
|
122 return; |
|
123 |
|
124 //! [0] |
|
125 QTreeWidgetItemIterator it(treeWidget); |
|
126 while (*it) { |
|
127 if ((*it)->text(0) == itemText) |
|
128 (*it)->setSelected(true); |
|
129 ++it; |
|
130 } |
|
131 //! [0] |
|
132 } |
|
133 |
|
134 void MainWindow::insertItem() |
|
135 { |
|
136 QTreeWidgetItem *currentItem = treeWidget->currentItem(); |
|
137 |
|
138 if (!currentItem) |
|
139 return; |
|
140 |
|
141 QString itemText = QInputDialog::getText(this, tr("Insert Item"), |
|
142 tr("Input text for the new item:")); |
|
143 |
|
144 if (itemText.isEmpty()) |
|
145 return; |
|
146 |
|
147 QTreeWidgetItem *parent = currentItem->parent(); |
|
148 QTreeWidgetItem *newItem; |
|
149 if (parent) |
|
150 newItem = new QTreeWidgetItem(parent, treeWidget->currentItem()); |
|
151 else |
|
152 newItem = new QTreeWidgetItem(treeWidget, treeWidget->currentItem()); |
|
153 |
|
154 newItem->setText(0, itemText); |
|
155 } |
|
156 |
|
157 void MainWindow::removeItem() |
|
158 { |
|
159 QTreeWidgetItem *currentItem = treeWidget->currentItem(); |
|
160 |
|
161 if (!currentItem) |
|
162 return; |
|
163 |
|
164 QTreeWidgetItem *parent = currentItem->parent(); |
|
165 int index; |
|
166 |
|
167 if (parent) { |
|
168 index = parent->indexOfChild(treeWidget->currentItem()); |
|
169 delete parent->takeChild(index); |
|
170 } else { |
|
171 index = treeWidget->indexOfTopLevelItem(treeWidget->currentItem()); |
|
172 delete treeWidget->takeTopLevelItem(index); |
|
173 } |
|
174 } |
|
175 |
|
176 void MainWindow::sortAscending() |
|
177 { |
|
178 treeWidget->sortItems(0, Qt::AscendingOrder); |
|
179 } |
|
180 |
|
181 void MainWindow::sortDescending() |
|
182 { |
|
183 treeWidget->sortItems(0, Qt::DescendingOrder); |
|
184 } |
|
185 |
|
186 void MainWindow::updateMenus(QTreeWidgetItem *current) |
|
187 { |
|
188 insertAction->setEnabled(current != 0); |
|
189 removeAction->setEnabled(current != 0); |
|
190 } |
|
191 |
|
192 void MainWindow::updateSortItems() |
|
193 { |
|
194 ascendingAction->setEnabled(!autoSortAction->isChecked()); |
|
195 descendingAction->setEnabled(!autoSortAction->isChecked()); |
|
196 |
|
197 treeWidget->setSortingEnabled(autoSortAction->isChecked()); |
|
198 } |