1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
19 ** distribution.
20 ** * Neither the name of The Qt Company Ltd nor the names of its
21 ** contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include <QtGui>
42
43 #include "mainwindow.h"
44
MainWindow()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 QTreeWidget *treeWidget = new QTreeWidget(this);
71 */
72 treeWidget = new QTreeWidget(this);
73 treeWidget->setColumnCount(2);
74 QStringList headers;
75 headers << tr("Subject") << tr("Default");
76 treeWidget->setHeaderLabels(headers);
77
78 connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));
79 connect(ascendingAction, SIGNAL(triggered()), this, SLOT(sortAscending()));
80 connect(autoSortAction, SIGNAL(triggered()), this, SLOT(updateSortItems()));
81 connect(descendingAction, SIGNAL(triggered()), this, SLOT(sortDescending()));
82 connect(findItemsAction, SIGNAL(triggered()), this, SLOT(findItems()));
83 connect(insertAction, SIGNAL(triggered()), this, SLOT(insertItem()));
84 connect(removeAction, SIGNAL(triggered()), this, SLOT(removeItem()));
85 connect(treeWidget,
86 SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
87 this, SLOT(updateMenus(QTreeWidgetItem *)));
88
89 setupTreeItems();
90 updateMenus(treeWidget->currentItem());
91
92 setCentralWidget(treeWidget);
93 setWindowTitle(tr("Tree Widget"));
94 }
95
setupTreeItems()96 void MainWindow::setupTreeItems()
97 {
98 QTreeWidgetItem *planets = new QTreeWidgetItem(treeWidget);
99 planets->setText(0, tr("Planets"));
100 (new QTreeWidgetItem(planets))->setText(0, tr("Mercury"));
101 (new QTreeWidgetItem(planets))->setText(0, tr("Venus"));
102
103 QTreeWidgetItem *earthItem = new QTreeWidgetItem(planets);
104 earthItem->setText(0, tr("Earth"));
105 earthItem->setText(1, tr("Yes"));
106
107 (new QTreeWidgetItem(planets))->setText(0, tr("Mars"));
108 (new QTreeWidgetItem(planets))->setText(0, tr("Jupiter"));
109 (new QTreeWidgetItem(planets))->setText(0, tr("Saturn"));
110 (new QTreeWidgetItem(planets))->setText(0, tr("Uranus"));
111 (new QTreeWidgetItem(planets))->setText(0, tr("Neptune"));
112 (new QTreeWidgetItem(planets))->setText(0, tr("Pluto"));
113 }
114
findItems()115 void MainWindow::findItems()
116 {
117 QString itemText = QInputDialog::getText(this, tr("Find Items"),
118 tr("Text to find:"));
119
120 if (itemText.isEmpty())
121 return;
122
123 //! [0]
124 QTreeWidgetItemIterator it(treeWidget);
125 while (*it) {
126 if ((*it)->text(0) == itemText)
127 (*it)->setSelected(true);
128 ++it;
129 }
130 //! [0]
131 }
132
insertItem()133 void MainWindow::insertItem()
134 {
135 QTreeWidgetItem *currentItem = treeWidget->currentItem();
136
137 if (!currentItem)
138 return;
139
140 QString itemText = QInputDialog::getText(this, tr("Insert Item"),
141 tr("Input text for the new item:"));
142
143 if (itemText.isEmpty())
144 return;
145
146 QTreeWidgetItem *parent = currentItem->parent();
147 QTreeWidgetItem *newItem;
148 if (parent)
149 newItem = new QTreeWidgetItem(parent, treeWidget->currentItem());
150 else
151 newItem = new QTreeWidgetItem(treeWidget, treeWidget->currentItem());
152
153 newItem->setText(0, itemText);
154 }
155
removeItem()156 void MainWindow::removeItem()
157 {
158 QTreeWidgetItem *currentItem = treeWidget->currentItem();
159
160 if (!currentItem)
161 return;
162
163 QTreeWidgetItem *parent = currentItem->parent();
164 int index;
165
166 if (parent) {
167 index = parent->indexOfChild(treeWidget->currentItem());
168 delete parent->takeChild(index);
169 } else {
170 index = treeWidget->indexOfTopLevelItem(treeWidget->currentItem());
171 delete treeWidget->takeTopLevelItem(index);
172 }
173 }
174
sortAscending()175 void MainWindow::sortAscending()
176 {
177 treeWidget->sortItems(0, Qt::AscendingOrder);
178 }
179
sortDescending()180 void MainWindow::sortDescending()
181 {
182 treeWidget->sortItems(0, Qt::DescendingOrder);
183 }
184
updateMenus(QTreeWidgetItem * current)185 void MainWindow::updateMenus(QTreeWidgetItem *current)
186 {
187 insertAction->setEnabled(current != 0);
188 removeAction->setEnabled(current != 0);
189 }
190
updateSortItems()191 void MainWindow::updateSortItems()
192 {
193 ascendingAction->setEnabled(!autoSortAction->isChecked());
194 descendingAction->setEnabled(!autoSortAction->isChecked());
195
196 treeWidget->setSortingEnabled(autoSortAction->isChecked());
197 }
198