source: trunk/doc/src/snippets/qtreewidgetitemiterator-using/mainwindow.cpp@ 859

Last change on this file since 859 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 6.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include <QtGui>
42
43#include "mainwindow.h"
44
45MainWindow::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
96void 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
115void 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
133void 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
156void 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
175void MainWindow::sortAscending()
176{
177 treeWidget->sortItems(0, Qt::AscendingOrder);
178}
179
180void MainWindow::sortDescending()
181{
182 treeWidget->sortItems(0, Qt::DescendingOrder);
183}
184
185void MainWindow::updateMenus(QTreeWidgetItem *current)
186{
187 insertAction->setEnabled(current != 0);
188 removeAction->setEnabled(current != 0);
189}
190
191void MainWindow::updateSortItems()
192{
193 ascendingAction->setEnabled(!autoSortAction->isChecked());
194 descendingAction->setEnabled(!autoSortAction->isChecked());
195
196 treeWidget->setSortingEnabled(autoSortAction->isChecked());
197}
Note: See TracBrowser for help on using the repository browser.