Salome HOME
updated copyright message
[modules/gui.git] / src / TreeData / Test / mainwindow.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, 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 <QInputDialog>
21 #include <QContextMenuEvent>
22
23 #include "mainwindow.hxx"
24 #include "TreeModel.hxx"
25
26 #include <Basics_Utils.hxx>
27 #include "testhelper.hxx"
28
29 MainWindow::MainWindow(QWidget *parent)
30     : QMainWindow(parent)
31 {
32     setupUi(this);
33
34     QStringList headers;
35     headers << tr("Title") << tr("Description");
36
37     TreeModel *model = new TreeModel(headers);
38     TESTHELPER_loadDataFromFile(model, TESTHELPER_testfilename(DATAFILENAME));
39
40     view->setModel(model);
41     for (int column = 0; column < model->columnCount(); ++column)
42         view->resizeColumnToContents(column);
43
44     connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
45
46     connect(view->selectionModel(),
47             SIGNAL(selectionChanged(const QItemSelection &,
48                                     const QItemSelection &)),
49             this, SLOT(updateActions()));
50
51     connect(actionsMenu, SIGNAL(aboutToShow()), this, SLOT(updateActions()));
52     connect(insertRowAction, SIGNAL(triggered()), this, SLOT(insertRow()));
53     connect(insertColumnAction, SIGNAL(triggered()), this, SLOT(insertColumn()));
54     connect(removeRowAction, SIGNAL(triggered()), this, SLOT(removeRow()));
55     connect(removeColumnAction, SIGNAL(triggered()), this, SLOT(removeColumn()));
56     connect(insertChildAction, SIGNAL(triggered()), this, SLOT(insertChild()));
57     connect(newDataAction, SIGNAL(triggered()), this, SLOT(newData()));
58
59     updateActions();
60 }
61
62 void MainWindow::newData() {
63   LOG("MainWindow::newData(): START");
64
65   bool ok;
66   QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
67                                        tr("Data path:"), QLineEdit::Normal,
68                                        "folder/subfolder/item", &ok);
69   if (!ok || text.trimmed().isEmpty())
70     return;
71
72   QStringList path = text.trimmed().split("/");
73   TreeModel *model = (TreeModel *)view->model();
74
75   QString label = path.last();
76   path.removeLast();
77   DataObject * data = TESTHELPER_dummyObject(label);
78   model->addData(data,path);
79
80   LOG("MainWindow::newData(): END");
81 }
82
83 void MainWindow::insertChild()
84 {
85     QModelIndex index = view->selectionModel()->currentIndex();
86     QAbstractItemModel *model = view->model();
87
88     if (model->columnCount(index) == 0) {
89         if (!model->insertColumn(0, index))
90             return;
91     }
92
93     if (!model->insertRow(0, index))
94         return;
95
96     for (int column = 0; column < model->columnCount(index); ++column) {
97         QModelIndex child = model->index(0, column, index);
98         model->setData(child, QVariant("[No data]"), Qt::EditRole);
99         if (!model->headerData(column, Qt::Horizontal).isValid())
100             model->setHeaderData(column, Qt::Horizontal, QVariant("[No header]"),
101                                  Qt::EditRole);
102     }
103
104     view->selectionModel()->setCurrentIndex(model->index(0, 0, index),
105                                             QItemSelectionModel::ClearAndSelect);
106     updateActions();
107 }
108
109 bool MainWindow::insertColumn(const QModelIndex &parent)
110 {
111     QAbstractItemModel *model = view->model();
112     int column = view->selectionModel()->currentIndex().column();
113
114     // Insert a column in the parent item.
115     bool changed = model->insertColumn(column + 1, parent);
116     if (changed)
117         model->setHeaderData(column + 1, Qt::Horizontal, QVariant("[No header]"),
118                              Qt::EditRole);
119
120     updateActions();
121
122     return changed;
123 }
124
125 void MainWindow::insertRow()
126 {
127     QModelIndex index = view->selectionModel()->currentIndex();
128     QAbstractItemModel *model = view->model();
129
130     if (!model->insertRow(index.row()+1, index.parent()))
131         return;
132
133     updateActions();
134
135     for (int column = 0; column < model->columnCount(index.parent()); ++column) {
136         QModelIndex child = model->index(index.row()+1, column, index.parent());
137         model->setData(child, QVariant("[No data]"), Qt::EditRole);
138     }
139 }
140
141 bool MainWindow::removeColumn(const QModelIndex &parent)
142 {
143     QAbstractItemModel *model = view->model();
144     int column = view->selectionModel()->currentIndex().column();
145
146     // Insert columns in each child of the parent item.
147     bool changed = model->removeColumn(column, parent);
148
149     if (!parent.isValid() && changed)
150         updateActions();
151
152     return changed;
153 }
154
155 void MainWindow::removeRow()
156 {
157     QModelIndex index = view->selectionModel()->currentIndex();
158     QAbstractItemModel *model = view->model();
159     if (model->removeRow(index.row(), index.parent()))
160         updateActions();
161 }
162
163 void MainWindow::updateActions()
164 {
165     bool hasSelection = !view->selectionModel()->selection().isEmpty();
166     removeRowAction->setEnabled(hasSelection);
167     removeColumnAction->setEnabled(hasSelection);
168
169     bool hasCurrent = view->selectionModel()->currentIndex().isValid();
170     insertRowAction->setEnabled(hasCurrent);
171     insertColumnAction->setEnabled(hasCurrent);
172
173     if (hasCurrent) {
174         view->closePersistentEditor(view->selectionModel()->currentIndex());
175
176         int row = view->selectionModel()->currentIndex().row();
177         int column = view->selectionModel()->currentIndex().column();
178         if (view->selectionModel()->currentIndex().parent().isValid())
179             statusBar()->showMessage(tr("Position: (%1,%2)").arg(row).arg(column));
180         else
181             statusBar()->showMessage(tr("Position: (%1,%2) in top level").arg(row).arg(column));
182     }
183 }
184
185 void MainWindow::contextMenuEvent(QContextMenuEvent *event) {
186   QMenu menu(this);
187   menu.addAction(newDataAction);
188   menu.exec(event->globalPos());
189 }