1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50
51 #include <QtWidgets>
52
53 #include "mainwindow.h"
54
MainWindow()55 MainWindow::MainWindow()
56 {
57 QMenu *fileMenu = new QMenu(tr("&File"));
58
59 QAction *quitAction = fileMenu->addAction(tr("E&xit"));
60 quitAction->setShortcut(tr("Ctrl+Q"));
61
62 QMenu *itemsMenu = new QMenu(tr("&Items"));
63
64 insertAction = itemsMenu->addAction(tr("&Insert Item"));
65 removeAction = itemsMenu->addAction(tr("&Remove Item"));
66 removeAction->setEnabled(false);
67 itemsMenu->addSeparator();
68 ascendingAction = itemsMenu->addAction(tr("Sort in &Ascending Order"));
69 descendingAction = itemsMenu->addAction(tr("Sort in &Descending Order"));
70 autoSortAction = itemsMenu->addAction(tr("&Automatically Sort Items"));
71 autoSortAction->setCheckable(true);
72 itemsMenu->addSeparator();
73 QAction *findItemsAction = itemsMenu->addAction(tr("&Find Items"));
74 findItemsAction->setShortcut(tr("Ctrl+F"));
75
76 menuBar()->addMenu(fileMenu);
77 menuBar()->addMenu(itemsMenu);
78
79 /* For convenient quoting:
80 //! [0]
81 QTreeWidget *treeWidget = new QTreeWidget(this);
82 //! [0]
83 */
84 treeWidget = new QTreeWidget(this);
85 //! [1]
86 treeWidget->setColumnCount(2);
87 //! [1] //! [2]
88 QStringList headers;
89 headers << tr("Subject") << tr("Default");
90 treeWidget->setHeaderLabels(headers);
91 //! [2]
92
93 connect(quitAction, &QAction::triggered, this, &QWidget::close);
94 connect(ascendingAction, &QAction::triggered, this, &MainWindow::sortAscending);
95 connect(autoSortAction, &QAction::triggered, this, &MainWindow::updateSortItems);
96 connect(descendingAction, &QAction::triggered, this, &MainWindow::sortDescending);
97 connect(findItemsAction, &QAction::triggered, this, &MainWindow::findItems);
98 connect(insertAction, &QAction::triggered, this, &MainWindow::insertItem);
99 connect(removeAction, &QAction::triggered, this, &MainWindow::removeItem);
100 connect(treeWidget, &QTreeWidget::currentItemChanged,
101 this, &MainWindow::updateMenus);
102
103 setupTreeItems();
104 updateMenus(treeWidget->currentItem());
105
106 setCentralWidget(treeWidget);
107 setWindowTitle(tr("Tree Widget"));
108 }
109
setupTreeItems()110 void MainWindow::setupTreeItems()
111 {
112 //! [3]
113 QTreeWidgetItem *cities = new QTreeWidgetItem(treeWidget);
114 cities->setText(0, tr("Cities"));
115 QTreeWidgetItem *osloItem = new QTreeWidgetItem(cities);
116 osloItem->setText(0, tr("Oslo"));
117 osloItem->setText(1, tr("Yes"));
118 //! [3]
119
120 (new QTreeWidgetItem(cities))->setText(0, tr("Stockholm"));
121 (new QTreeWidgetItem(cities))->setText(0, tr("Helsinki"));
122 (new QTreeWidgetItem(cities))->setText(0, tr("Copenhagen"));
123
124 //! [4] //! [5]
125 QTreeWidgetItem *planets = new QTreeWidgetItem(treeWidget, cities);
126 //! [4]
127 planets->setText(0, tr("Planets"));
128 //! [5]
129 (new QTreeWidgetItem(planets))->setText(0, tr("Mercury"));
130 (new QTreeWidgetItem(planets))->setText(0, tr("Venus"));
131
132 QTreeWidgetItem *earthItem = new QTreeWidgetItem(planets);
133 earthItem->setText(0, tr("Earth"));
134 earthItem->setText(1, tr("Yes"));
135
136 (new QTreeWidgetItem(planets))->setText(0, tr("Mars"));
137 (new QTreeWidgetItem(planets))->setText(0, tr("Jupiter"));
138 (new QTreeWidgetItem(planets))->setText(0, tr("Saturn"));
139 (new QTreeWidgetItem(planets))->setText(0, tr("Uranus"));
140 (new QTreeWidgetItem(planets))->setText(0, tr("Neptune"));
141 (new QTreeWidgetItem(planets))->setText(0, tr("Pluto"));
142 }
143
findItems()144 void MainWindow::findItems()
145 {
146 QString itemText = QInputDialog::getText(this, tr("Find Items"),
147 tr("Text to find (including wildcards):"));
148
149 if (itemText.isEmpty())
150 return;
151
152 const QList<QTreeWidgetItem *> items = treeWidget->selectedItems();
153 for (QTreeWidgetItem *item : items)
154 item->setSelected(false);
155
156 //! [7]
157 const QList<QTreeWidgetItem *> found = treeWidget->findItems(
158 itemText, Qt::MatchWildcard);
159
160 for (QTreeWidgetItem *item : found) {
161 item->setSelected(true);
162 // Show the item->text(0) for each item.
163 }
164 //! [7]
165 }
166
insertItem()167 void MainWindow::insertItem()
168 {
169 QTreeWidgetItem *currentItem = treeWidget->currentItem();
170
171 if (!currentItem)
172 return;
173
174 QString itemText = QInputDialog::getText(this, tr("Insert Item"),
175 tr("Input text for the new item:"));
176
177 if (itemText.isEmpty())
178 return;
179
180 //! [8]
181 QTreeWidgetItem *parent = currentItem->parent();
182 QTreeWidgetItem *newItem;
183 if (parent)
184 newItem = new QTreeWidgetItem(parent, treeWidget->currentItem());
185 else
186 //! [8] //! [9]
187 newItem = new QTreeWidgetItem(treeWidget, treeWidget->currentItem());
188 //! [9]
189
190 newItem->setText(0, itemText);
191 }
192
removeItem()193 void MainWindow::removeItem()
194 {
195 QTreeWidgetItem *currentItem = treeWidget->currentItem();
196
197 if (!currentItem)
198 return;
199
200 //! [10]
201 QTreeWidgetItem *parent = currentItem->parent();
202 int index;
203
204 if (parent) {
205 index = parent->indexOfChild(treeWidget->currentItem());
206 delete parent->takeChild(index);
207 } else {
208 index = treeWidget->indexOfTopLevelItem(treeWidget->currentItem());
209 delete treeWidget->takeTopLevelItem(index);
210 //! [10] //! [11]
211 }
212 //! [11]
213 }
214
sortAscending()215 void MainWindow::sortAscending()
216 {
217 treeWidget->sortItems(0, Qt::AscendingOrder);
218 }
219
sortDescending()220 void MainWindow::sortDescending()
221 {
222 treeWidget->sortItems(0, Qt::DescendingOrder);
223 }
224
updateMenus(QTreeWidgetItem * current)225 void MainWindow::updateMenus(QTreeWidgetItem *current)
226 {
227 insertAction->setEnabled(current != 0);
228 removeAction->setEnabled(current != 0);
229 }
230
updateSortItems()231 void MainWindow::updateSortItems()
232 {
233 ascendingAction->setEnabled(!autoSortAction->isChecked());
234 descendingAction->setEnabled(!autoSortAction->isChecked());
235
236 treeWidget->setSortingEnabled(autoSortAction->isChecked());
237 }
238