1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <QInputDialog>
21 #include <QContextMenuEvent>
23 #include "mainwindow.hxx"
24 #include "TreeModel.hxx"
26 #include <Basics_Utils.hxx>
27 #include "testhelper.hxx"
29 MainWindow::MainWindow(QWidget *parent)
35 headers << tr("Title") << tr("Description");
37 TreeModel *model = new TreeModel(headers);
38 TESTHELPER_loadDataFromFile(model, TESTHELPER_testfilename(DATAFILENAME));
40 view->setModel(model);
41 for (int column = 0; column < model->columnCount(); ++column)
42 view->resizeColumnToContents(column);
44 connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
46 connect(view->selectionModel(),
47 SIGNAL(selectionChanged(const QItemSelection &,
48 const QItemSelection &)),
49 this, SLOT(updateActions()));
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()));
62 void MainWindow::newData() {
63 LOG("MainWindow::newData(): START");
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())
72 QStringList path = text.trimmed().split("/");
73 TreeModel *model = (TreeModel *)view->model();
75 QString label = path.last();
77 DataObject * data = TESTHELPER_dummyObject(label);
78 model->addData(data,path);
80 LOG("MainWindow::newData(): END");
83 void MainWindow::insertChild()
85 QModelIndex index = view->selectionModel()->currentIndex();
86 QAbstractItemModel *model = view->model();
88 if (model->columnCount(index) == 0) {
89 if (!model->insertColumn(0, index))
93 if (!model->insertRow(0, index))
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]"),
104 view->selectionModel()->setCurrentIndex(model->index(0, 0, index),
105 QItemSelectionModel::ClearAndSelect);
109 bool MainWindow::insertColumn(const QModelIndex &parent)
111 QAbstractItemModel *model = view->model();
112 int column = view->selectionModel()->currentIndex().column();
114 // Insert a column in the parent item.
115 bool changed = model->insertColumn(column + 1, parent);
117 model->setHeaderData(column + 1, Qt::Horizontal, QVariant("[No header]"),
125 void MainWindow::insertRow()
127 QModelIndex index = view->selectionModel()->currentIndex();
128 QAbstractItemModel *model = view->model();
130 if (!model->insertRow(index.row()+1, index.parent()))
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);
141 bool MainWindow::removeColumn(const QModelIndex &parent)
143 QAbstractItemModel *model = view->model();
144 int column = view->selectionModel()->currentIndex().column();
146 // Insert columns in each child of the parent item.
147 bool changed = model->removeColumn(column, parent);
149 if (!parent.isValid() && changed)
155 void MainWindow::removeRow()
157 QModelIndex index = view->selectionModel()->currentIndex();
158 QAbstractItemModel *model = view->model();
159 if (model->removeRow(index.row(), index.parent()))
163 void MainWindow::updateActions()
165 bool hasSelection = !view->selectionModel()->selection().isEmpty();
166 removeRowAction->setEnabled(hasSelection);
167 removeColumnAction->setEnabled(hasSelection);
169 bool hasCurrent = view->selectionModel()->currentIndex().isValid();
170 insertRowAction->setEnabled(hasCurrent);
171 insertColumnAction->setEnabled(hasCurrent);
174 view->closePersistentEditor(view->selectionModel()->currentIndex());
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));
181 statusBar()->showMessage(tr("Position: (%1,%2) in top level").arg(row).arg(column));
185 void MainWindow::contextMenuEvent(QContextMenuEvent *event) {
187 menu.addAction(newDataAction);
188 menu.exec(event->globalPos());