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 //! [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 
setupTreeItems()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 
findItems()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 
insertItem()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 
removeItem()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 
sortAscending()208 void MainWindow::sortAscending()
209 {
210     treeWidget->sortItems(0, Qt::AscendingOrder);
211 }
212 
sortDescending()213 void MainWindow::sortDescending()
214 {
215     treeWidget->sortItems(0, Qt::DescendingOrder);
216 }
217 
updateMenus(QTreeWidgetItem * current)218 void MainWindow::updateMenus(QTreeWidgetItem *current)
219 {
220     insertAction->setEnabled(current != 0);
221     removeAction->setEnabled(current != 0);
222 }
223 
updateSortItems()224 void MainWindow::updateSortItems()
225 {
226     ascendingAction->setEnabled(!autoSortAction->isChecked());
227     descendingAction->setEnabled(!autoSortAction->isChecked());
228 
229     treeWidget->setSortingEnabled(autoSortAction->isChecked());
230 }
231