Salome HOME
Update copyrights 2014.
[modules/gui.git] / src / TreeData / Test / mainwindow.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <QtGui>
21
22 #include "mainwindow.hxx"
23 #include "TreeModel.hxx"
24
25 #include <Basics_Utils.hxx>
26 #include "testhelper.hxx"
27
28 MainWindow::MainWindow(QWidget *parent)
29     : QMainWindow(parent)
30 {
31     setupUi(this);
32
33     QStringList headers;
34     headers << tr("Title") << tr("Description");
35
36     TreeModel *model = new TreeModel(headers);
37     TESTHELPER_loadDataFromFile(model, TESTHELPER_testfilename(DATAFILENAME));
38
39     view->setModel(model);
40     for (int column = 0; column < model->columnCount(); ++column)
41         view->resizeColumnToContents(column);
42
43     connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
44
45     connect(view->selectionModel(),
46             SIGNAL(selectionChanged(const QItemSelection &,
47                                     const QItemSelection &)),
48             this, SLOT(updateActions()));
49
50     connect(actionsMenu, SIGNAL(aboutToShow()), this, SLOT(updateActions()));
51     connect(insertRowAction, SIGNAL(triggered()), this, SLOT(insertRow()));
52     connect(insertColumnAction, SIGNAL(triggered()), this, SLOT(insertColumn()));
53     connect(removeRowAction, SIGNAL(triggered()), this, SLOT(removeRow()));
54     connect(removeColumnAction, SIGNAL(triggered()), this, SLOT(removeColumn()));
55     connect(insertChildAction, SIGNAL(triggered()), this, SLOT(insertChild()));
56     connect(newDataAction, SIGNAL(triggered()), this, SLOT(newData()));
57
58     updateActions();
59 }
60
61 void MainWindow::newData() {
62   LOG("MainWindow::newData(): START");
63
64   bool ok;
65   QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
66                                        tr("Data path:"), QLineEdit::Normal,
67                                        "folder/subfolder/item", &ok);
68   if (!ok || text.trimmed().isEmpty())
69     return;
70
71   QStringList path = text.trimmed().split("/");
72   TreeModel *model = (TreeModel *)view->model();
73
74   QString label = path.last();
75   path.removeLast();
76   DataObject * data = TESTHELPER_dummyObject(label);
77   model->addData(data,path);
78
79   LOG("MainWindow::newData(): END");
80 }
81
82 void MainWindow::insertChild()
83 {
84     QModelIndex index = view->selectionModel()->currentIndex();
85     QAbstractItemModel *model = view->model();
86
87     if (model->columnCount(index) == 0) {
88         if (!model->insertColumn(0, index))
89             return;
90     }
91
92     if (!model->insertRow(0, index))
93         return;
94
95     for (int column = 0; column < model->columnCount(index); ++column) {
96         QModelIndex child = model->index(0, column, index);
97         model->setData(child, QVariant("[No data]"), Qt::EditRole);
98         if (!model->headerData(column, Qt::Horizontal).isValid())
99             model->setHeaderData(column, Qt::Horizontal, QVariant("[No header]"),
100                                  Qt::EditRole);
101     }
102
103     view->selectionModel()->setCurrentIndex(model->index(0, 0, index),
104                                             QItemSelectionModel::ClearAndSelect);
105     updateActions();
106 }
107
108 bool MainWindow::insertColumn(const QModelIndex &parent)
109 {
110     QAbstractItemModel *model = view->model();
111     int column = view->selectionModel()->currentIndex().column();
112
113     // Insert a column in the parent item.
114     bool changed = model->insertColumn(column + 1, parent);
115     if (changed)
116         model->setHeaderData(column + 1, Qt::Horizontal, QVariant("[No header]"),
117                              Qt::EditRole);
118
119     updateActions();
120
121     return changed;
122 }
123
124 void MainWindow::insertRow()
125 {
126     QModelIndex index = view->selectionModel()->currentIndex();
127     QAbstractItemModel *model = view->model();
128
129     if (!model->insertRow(index.row()+1, index.parent()))
130         return;
131
132     updateActions();
133
134     for (int column = 0; column < model->columnCount(index.parent()); ++column) {
135         QModelIndex child = model->index(index.row()+1, column, index.parent());
136         model->setData(child, QVariant("[No data]"), Qt::EditRole);
137     }
138 }
139
140 bool MainWindow::removeColumn(const QModelIndex &parent)
141 {
142     QAbstractItemModel *model = view->model();
143     int column = view->selectionModel()->currentIndex().column();
144
145     // Insert columns in each child of the parent item.
146     bool changed = model->removeColumn(column, parent);
147
148     if (!parent.isValid() && changed)
149         updateActions();
150
151     return changed;
152 }
153
154 void MainWindow::removeRow()
155 {
156     QModelIndex index = view->selectionModel()->currentIndex();
157     QAbstractItemModel *model = view->model();
158     if (model->removeRow(index.row(), index.parent()))
159         updateActions();
160 }
161
162 void MainWindow::updateActions()
163 {
164     bool hasSelection = !view->selectionModel()->selection().isEmpty();
165     removeRowAction->setEnabled(hasSelection);
166     removeColumnAction->setEnabled(hasSelection);
167
168     bool hasCurrent = view->selectionModel()->currentIndex().isValid();
169     insertRowAction->setEnabled(hasCurrent);
170     insertColumnAction->setEnabled(hasCurrent);
171
172     if (hasCurrent) {
173         view->closePersistentEditor(view->selectionModel()->currentIndex());
174
175         int row = view->selectionModel()->currentIndex().row();
176         int column = view->selectionModel()->currentIndex().column();
177         if (view->selectionModel()->currentIndex().parent().isValid())
178             statusBar()->showMessage(tr("Position: (%1,%2)").arg(row).arg(column));
179         else
180             statusBar()->showMessage(tr("Position: (%1,%2) in top level").arg(row).arg(column));
181     }
182 }
183
184 void MainWindow::contextMenuEvent(QContextMenuEvent *event) {
185   QMenu menu(this);
186   menu.addAction(newDataAction);
187   menu.exec(event->globalPos());
188 }