source: trunk/examples/itemviews/editabletreemodel/mainwindow.cpp@ 561

Last change on this file since 561 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 6.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 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 examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43
44#include "mainwindow.h"
45#include "treemodel.h"
46
47MainWindow::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
81void 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
107bool 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
123void 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
139bool 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
153void 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
161void 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}
Note: See TracBrowser for help on using the repository browser.