author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Wed, 18 Aug 2010 10:37:55 +0300 | |
changeset 33 | 3e2da88830cd |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include <QtGui> |
|
42 |
||
43 |
#include "mainwindow.h" |
|
44 |
#include "treemodel.h" |
|
45 |
||
46 |
MainWindow::MainWindow(QWidget *parent) |
|
47 |
: QMainWindow(parent) |
|
48 |
{ |
|
49 |
setupUi(this); |
|
50 |
||
51 |
QStringList headers; |
|
52 |
headers << tr("Title") << tr("Description"); |
|
53 |
||
54 |
QFile file(":/default.txt"); |
|
55 |
file.open(QIODevice::ReadOnly); |
|
56 |
TreeModel *model = new TreeModel(headers, file.readAll()); |
|
57 |
file.close(); |
|
58 |
||
59 |
view->setModel(model); |
|
60 |
for (int column = 0; column < model->columnCount(); ++column) |
|
61 |
view->resizeColumnToContents(column); |
|
62 |
||
63 |
connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); |
|
64 |
||
65 |
connect(view->selectionModel(), |
|
66 |
SIGNAL(selectionChanged(const QItemSelection &, |
|
67 |
const QItemSelection &)), |
|
68 |
this, SLOT(updateActions())); |
|
69 |
||
70 |
connect(actionsMenu, SIGNAL(aboutToShow()), this, SLOT(updateActions())); |
|
71 |
connect(insertRowAction, SIGNAL(triggered()), this, SLOT(insertRow())); |
|
72 |
connect(insertColumnAction, SIGNAL(triggered()), this, SLOT(insertColumn())); |
|
73 |
connect(removeRowAction, SIGNAL(triggered()), this, SLOT(removeRow())); |
|
74 |
connect(removeColumnAction, SIGNAL(triggered()), this, SLOT(removeColumn())); |
|
75 |
connect(insertChildAction, SIGNAL(triggered()), this, SLOT(insertChild())); |
|
76 |
||
77 |
updateActions(); |
|
78 |
} |
|
79 |
||
80 |
void MainWindow::insertChild() |
|
81 |
{ |
|
82 |
QModelIndex index = view->selectionModel()->currentIndex(); |
|
83 |
QAbstractItemModel *model = view->model(); |
|
84 |
||
85 |
if (model->columnCount(index) == 0) { |
|
86 |
if (!model->insertColumn(0, index)) |
|
87 |
return; |
|
88 |
} |
|
89 |
||
90 |
if (!model->insertRow(0, index)) |
|
91 |
return; |
|
92 |
||
93 |
for (int column = 0; column < model->columnCount(index); ++column) { |
|
94 |
QModelIndex child = model->index(0, column, index); |
|
95 |
model->setData(child, QVariant("[No data]"), Qt::EditRole); |
|
96 |
if (!model->headerData(column, Qt::Horizontal).isValid()) |
|
97 |
model->setHeaderData(column, Qt::Horizontal, QVariant("[No header]"), |
|
98 |
Qt::EditRole); |
|
99 |
} |
|
100 |
||
101 |
view->selectionModel()->setCurrentIndex(model->index(0, 0, index), |
|
102 |
QItemSelectionModel::ClearAndSelect); |
|
103 |
updateActions(); |
|
104 |
} |
|
105 |
||
106 |
bool MainWindow::insertColumn(const QModelIndex &parent) |
|
107 |
{ |
|
108 |
QAbstractItemModel *model = view->model(); |
|
109 |
int column = view->selectionModel()->currentIndex().column(); |
|
110 |
||
111 |
// Insert a column in the parent item. |
|
112 |
bool changed = model->insertColumn(column + 1, parent); |
|
113 |
if (changed) |
|
114 |
model->setHeaderData(column + 1, Qt::Horizontal, QVariant("[No header]"), |
|
115 |
Qt::EditRole); |
|
116 |
||
117 |
updateActions(); |
|
118 |
||
119 |
return changed; |
|
120 |
} |
|
121 |
||
122 |
void MainWindow::insertRow() |
|
123 |
{ |
|
124 |
QModelIndex index = view->selectionModel()->currentIndex(); |
|
125 |
QAbstractItemModel *model = view->model(); |
|
126 |
||
127 |
if (!model->insertRow(index.row()+1, index.parent())) |
|
128 |
return; |
|
129 |
||
130 |
updateActions(); |
|
131 |
||
132 |
for (int column = 0; column < model->columnCount(index.parent()); ++column) { |
|
133 |
QModelIndex child = model->index(index.row()+1, column, index.parent()); |
|
134 |
model->setData(child, QVariant("[No data]"), Qt::EditRole); |
|
135 |
} |
|
136 |
} |
|
137 |
||
138 |
bool MainWindow::removeColumn(const QModelIndex &parent) |
|
139 |
{ |
|
140 |
QAbstractItemModel *model = view->model(); |
|
141 |
int column = view->selectionModel()->currentIndex().column(); |
|
142 |
||
143 |
// Insert columns in each child of the parent item. |
|
144 |
bool changed = model->removeColumn(column, parent); |
|
145 |
||
146 |
if (!parent.isValid() && changed) |
|
147 |
updateActions(); |
|
148 |
||
149 |
return changed; |
|
150 |
} |
|
151 |
||
152 |
void MainWindow::removeRow() |
|
153 |
{ |
|
154 |
QModelIndex index = view->selectionModel()->currentIndex(); |
|
155 |
QAbstractItemModel *model = view->model(); |
|
156 |
if (model->removeRow(index.row(), index.parent())) |
|
157 |
updateActions(); |
|
158 |
} |
|
159 |
||
160 |
void MainWindow::updateActions() |
|
161 |
{ |
|
162 |
bool hasSelection = !view->selectionModel()->selection().isEmpty(); |
|
163 |
removeRowAction->setEnabled(hasSelection); |
|
164 |
removeColumnAction->setEnabled(hasSelection); |
|
165 |
||
166 |
bool hasCurrent = view->selectionModel()->currentIndex().isValid(); |
|
167 |
insertRowAction->setEnabled(hasCurrent); |
|
168 |
insertColumnAction->setEnabled(hasCurrent); |
|
169 |
||
170 |
if (hasCurrent) { |
|
171 |
view->closePersistentEditor(view->selectionModel()->currentIndex()); |
|
172 |
||
173 |
int row = view->selectionModel()->currentIndex().row(); |
|
174 |
int column = view->selectionModel()->currentIndex().column(); |
|
175 |
if (view->selectionModel()->currentIndex().parent().isValid()) |
|
176 |
statusBar()->showMessage(tr("Position: (%1,%2)").arg(row).arg(column)); |
|
177 |
else |
|
178 |
statusBar()->showMessage(tr("Position: (%1,%2) in top level").arg(row).arg(column)); |
|
179 |
} |
|
180 |
} |