|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the examples of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <QtGui> |
|
43 |
|
44 #include "mainwindow.h" |
|
45 #include "treemodel.h" |
|
46 |
|
47 MainWindow::MainWindow(QWidget *parent) |
|
48 : QMainWindow(parent) |
|
49 { |
|
50 setupUi(this); |
|
51 |
|
52 QStringList headers; |
|
53 headers << tr("Title") << tr("Description"); |
|
54 |
|
55 QFile file(":/default.txt"); |
|
56 file.open(QIODevice::ReadOnly); |
|
57 TreeModel *model = new TreeModel(headers, file.readAll()); |
|
58 file.close(); |
|
59 |
|
60 view->setModel(model); |
|
61 for (int column = 0; column < model->columnCount(); ++column) |
|
62 view->resizeColumnToContents(column); |
|
63 |
|
64 connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); |
|
65 |
|
66 connect(view->selectionModel(), |
|
67 SIGNAL(selectionChanged(const QItemSelection &, |
|
68 const QItemSelection &)), |
|
69 this, SLOT(updateActions())); |
|
70 |
|
71 connect(actionsMenu, SIGNAL(aboutToShow()), this, SLOT(updateActions())); |
|
72 connect(insertRowAction, SIGNAL(triggered()), this, SLOT(insertRow())); |
|
73 connect(insertColumnAction, SIGNAL(triggered()), this, SLOT(insertColumn())); |
|
74 connect(removeRowAction, SIGNAL(triggered()), this, SLOT(removeRow())); |
|
75 connect(removeColumnAction, SIGNAL(triggered()), this, SLOT(removeColumn())); |
|
76 connect(insertChildAction, SIGNAL(triggered()), this, SLOT(insertChild())); |
|
77 |
|
78 updateActions(); |
|
79 } |
|
80 |
|
81 void MainWindow::insertChild() |
|
82 { |
|
83 QModelIndex index = view->selectionModel()->currentIndex(); |
|
84 QAbstractItemModel *model = view->model(); |
|
85 |
|
86 if (model->columnCount(index) == 0) { |
|
87 if (!model->insertColumn(0, index)) |
|
88 return; |
|
89 } |
|
90 |
|
91 if (!model->insertRow(0, index)) |
|
92 return; |
|
93 |
|
94 for (int column = 0; column < model->columnCount(index); ++column) { |
|
95 QModelIndex child = model->index(0, column, index); |
|
96 model->setData(child, QVariant("[No data]"), Qt::EditRole); |
|
97 if (!model->headerData(column, Qt::Horizontal).isValid()) |
|
98 model->setHeaderData(column, Qt::Horizontal, QVariant("[No header]"), |
|
99 Qt::EditRole); |
|
100 } |
|
101 |
|
102 view->selectionModel()->setCurrentIndex(model->index(0, 0, index), |
|
103 QItemSelectionModel::ClearAndSelect); |
|
104 updateActions(); |
|
105 } |
|
106 |
|
107 bool MainWindow::insertColumn(const QModelIndex &parent) |
|
108 { |
|
109 QAbstractItemModel *model = view->model(); |
|
110 int column = view->selectionModel()->currentIndex().column(); |
|
111 |
|
112 // Insert a column in the parent item. |
|
113 bool changed = model->insertColumn(column + 1, parent); |
|
114 if (changed) |
|
115 model->setHeaderData(column + 1, Qt::Horizontal, QVariant("[No header]"), |
|
116 Qt::EditRole); |
|
117 |
|
118 updateActions(); |
|
119 |
|
120 return changed; |
|
121 } |
|
122 |
|
123 void MainWindow::insertRow() |
|
124 { |
|
125 QModelIndex index = view->selectionModel()->currentIndex(); |
|
126 QAbstractItemModel *model = view->model(); |
|
127 |
|
128 if (!model->insertRow(index.row()+1, index.parent())) |
|
129 return; |
|
130 |
|
131 updateActions(); |
|
132 |
|
133 for (int column = 0; column < model->columnCount(index.parent()); ++column) { |
|
134 QModelIndex child = model->index(index.row()+1, column, index.parent()); |
|
135 model->setData(child, QVariant("[No data]"), Qt::EditRole); |
|
136 } |
|
137 } |
|
138 |
|
139 bool MainWindow::removeColumn(const QModelIndex &parent) |
|
140 { |
|
141 QAbstractItemModel *model = view->model(); |
|
142 int column = view->selectionModel()->currentIndex().column(); |
|
143 |
|
144 // Insert columns in each child of the parent item. |
|
145 bool changed = model->removeColumn(column, parent); |
|
146 |
|
147 if (!parent.isValid() && changed) |
|
148 updateActions(); |
|
149 |
|
150 return changed; |
|
151 } |
|
152 |
|
153 void MainWindow::removeRow() |
|
154 { |
|
155 QModelIndex index = view->selectionModel()->currentIndex(); |
|
156 QAbstractItemModel *model = view->model(); |
|
157 if (model->removeRow(index.row(), index.parent())) |
|
158 updateActions(); |
|
159 } |
|
160 |
|
161 void MainWindow::updateActions() |
|
162 { |
|
163 bool hasSelection = !view->selectionModel()->selection().isEmpty(); |
|
164 removeRowAction->setEnabled(hasSelection); |
|
165 removeColumnAction->setEnabled(hasSelection); |
|
166 |
|
167 bool hasCurrent = view->selectionModel()->currentIndex().isValid(); |
|
168 insertRowAction->setEnabled(hasCurrent); |
|
169 insertColumnAction->setEnabled(hasCurrent); |
|
170 |
|
171 if (hasCurrent) { |
|
172 view->closePersistentEditor(view->selectionModel()->currentIndex()); |
|
173 |
|
174 int row = view->selectionModel()->currentIndex().row(); |
|
175 int column = view->selectionModel()->currentIndex().column(); |
|
176 if (view->selectionModel()->currentIndex().parent().isValid()) |
|
177 statusBar()->showMessage(tr("Position: (%1,%2)").arg(row).arg(column)); |
|
178 else |
|
179 statusBar()->showMessage(tr("Position: (%1,%2) in top level").arg(row).arg(column)); |
|
180 } |
|
181 } |