153
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 ** Contact: Qt Software Information (qt-info@nokia.com)
|
|
5 **
|
|
6 ** This file is part of the examples of the Qt Toolkit.
|
|
7 **
|
|
8 ** $QT_BEGIN_LICENSE:LGPL$
|
|
9 ** Commercial Usage
|
|
10 ** Licensees holding valid Qt Commercial licenses may use this file in
|
|
11 ** accordance with the Qt Commercial License Agreement provided with the
|
|
12 ** Software or, alternatively, in accordance with the terms contained in
|
|
13 ** a written agreement between you and Nokia.
|
|
14 **
|
|
15 ** GNU Lesser General Public License Usage
|
|
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
17 ** General Public License version 2.1 as published by the Free Software
|
|
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
19 ** packaging of this file. Please review the following information to
|
|
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
22 **
|
|
23 ** In addition, as a special exception, Nokia gives you certain
|
|
24 ** additional rights. These rights are described in the Nokia Qt LGPL
|
|
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|
26 ** 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 are unsure which license is appropriate for your use, please
|
|
37 ** contact the sales department at qt-sales@nokia.com.
|
|
38 ** $QT_END_LICENSE$
|
|
39 **
|
|
40 ****************************************************************************/
|
|
41 module mainwindow;
|
|
42
|
|
43
|
|
44 import qt.core.QModelIndex;
|
|
45 import qt.core.QFile;
|
|
46 import qt.gui.QMainWindow;
|
|
47 import qt.gui.QAction;
|
|
48 import qt.gui.QTreeView;
|
|
49 import qt.gui.QWidget;
|
|
50
|
|
51 import tango.text.convert.Format;
|
|
52
|
|
53 import ui_mainwindow;
|
|
54 import mainwindow;
|
|
55 import treemodel;
|
|
56
|
|
57
|
|
58 class MainWindow : public QMainWindow
|
|
59 {
|
|
60
|
|
61 mixin Ui_MainWindow;
|
|
62
|
|
63 public:
|
|
64
|
|
65 this(QWidget parent = null)
|
|
66 {
|
|
67 super(parent);
|
|
68 setupUi(this);
|
|
69
|
|
70 string[] headers;
|
|
71 headers ~= tr("Title");
|
|
72 headers ~= tr("Description");
|
|
73
|
|
74 auto file = new QFile(":/default.txt");
|
|
75 file.open(QIODevice.ReadOnly);
|
|
76 TreeModel model = new TreeModel(headers, file.readAll().data()[0..file.size()]);
|
|
77 file.close();
|
|
78
|
|
79 view.setModel(model);
|
|
80 for (int column = 0; column < model.columnCount(); ++column)
|
|
81 view.resizeColumnToContents(column);
|
|
82
|
|
83 exitAction.triggered.connect(&QApplication.quit);
|
|
84
|
|
85 view.selectionModel().selectionChanged.connect(&this.updateActions);
|
|
86
|
|
87 actionsMenu.aboutToShow.connect(&this.updateActions);
|
|
88 insertRowAction.triggered.connect(&this.insertRow);
|
|
89 insertColumnAction.triggered.connect(&this.insertColumn);
|
|
90 removeRowAction.triggered.connect(&this.removeRow);
|
|
91 removeColumnAction.triggered.connect(&this.removeColumn);
|
|
92 insertChildAction.triggered.connect(&this.insertChild);
|
|
93
|
|
94 updateActions();
|
|
95 }
|
|
96
|
|
97 public:
|
|
98
|
|
99 void updateActions()
|
|
100 {
|
|
101 bool hasSelection = !view.selectionModel().selection().isEmpty();
|
|
102 removeRowAction.setEnabled(hasSelection);
|
|
103 removeColumnAction.setEnabled(hasSelection);
|
|
104
|
|
105 bool hasCurrent = view.selectionModel().currentIndex().isValid();
|
|
106 insertRowAction.setEnabled(hasCurrent);
|
|
107 insertColumnAction.setEnabled(hasCurrent);
|
|
108
|
|
109 if (hasCurrent) {
|
|
110 view.closePersistentEditor(view.selectionModel().currentIndex());
|
|
111
|
|
112 int row = view.selectionModel().currentIndex().row();
|
|
113 int column = view.selectionModel().currentIndex().column();
|
|
114 if (view.selectionModel().currentIndex().parent().isValid())
|
|
115 statusBar().showMessage(Format(tr("Position: ({},{})"), row, column));
|
|
116 else
|
|
117 statusBar().showMessage(Format(tr("Position: ({},{}) in top level"), row, column));
|
|
118 }
|
|
119 }
|
|
120
|
|
121 private:
|
|
122
|
|
123 void insertChild()
|
|
124 {
|
|
125 QModelIndex index = view.selectionModel().currentIndex();
|
|
126 QAbstractItemModel model = view.model();
|
|
127
|
|
128 if (model.columnCount(index) == 0) {
|
|
129 if (!model.insertColumn(0, index))
|
|
130 return;
|
|
131 }
|
|
132
|
|
133 if (!model.insertRow(0, index))
|
|
134 return;
|
|
135
|
|
136 for (int column = 0; column < model.columnCount(index); ++column) {
|
|
137 QModelIndex child = model.index(0, column, index);
|
|
138 model.setData(child, QVariant("[No data]"), Qt.EditRole);
|
|
139 if (!model.headerData(column, Qt.Horizontal).isValid())
|
|
140 model.setHeaderData(column, Qt.Horizontal, QVariant("[No header]"), Qt.EditRole);
|
|
141 }
|
|
142
|
|
143 view.selectionModel().setCurrentIndex(model.index(0, 0, index), QItemSelectionModel.ClearAndSelect);
|
|
144 updateActions();
|
|
145 }
|
|
146
|
|
147 bool insertColumn()
|
|
148 {
|
|
149 QModelIndex parent = QModelIndex(); //moved from method head
|
|
150 QAbstractItemModel model = view.model();
|
|
151 int column = view.selectionModel().currentIndex().column();
|
|
152
|
|
153 // Insert a column in the parent item.
|
|
154 bool changed = model.insertColumn(column + 1, parent);
|
|
155 if (changed)
|
|
156 model.setHeaderData(column + 1, Qt.Horizontal, QVariant("[No header]"), Qt.EditRole);
|
|
157
|
|
158 updateActions();
|
|
159
|
|
160 return changed;
|
|
161 }
|
|
162
|
|
163 void insertRow()
|
|
164 {
|
|
165 QModelIndex index = view.selectionModel().currentIndex();
|
|
166 QAbstractItemModel model = view.model();
|
|
167
|
|
168 if (!model.insertRow(index.row()+1, index.parent()))
|
|
169 return;
|
|
170
|
|
171 updateActions();
|
|
172
|
|
173 for (int column = 0; column < model.columnCount(index.parent()); ++column) {
|
|
174 QModelIndex child = model.index(index.row()+1, column, index.parent());
|
|
175 model.setData(child, QVariant("[No data]"), Qt.EditRole);
|
|
176 }
|
|
177 }
|
|
178
|
|
179 bool removeColumn()
|
|
180 {
|
|
181 QModelIndex parent = QModelIndex(); //moved from method head
|
|
182 QAbstractItemModel model = view.model();
|
|
183 int column = view.selectionModel().currentIndex().column();
|
|
184
|
|
185 // Insert columns in each child of the parent item.
|
|
186 bool changed = model.removeColumn(column, parent);
|
|
187
|
|
188 if (!parent.isValid() && changed)
|
|
189 updateActions();
|
|
190
|
|
191 return changed;
|
|
192 }
|
|
193
|
|
194 void removeRow()
|
|
195 {
|
|
196 QModelIndex index = view.selectionModel().currentIndex();
|
|
197 QAbstractItemModel model = view.model();
|
|
198 if (model.removeRow(index.row(), index.parent()))
|
|
199 updateActions();
|
|
200 }
|
|
201
|
|
202 }
|