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