1 // Copyright (C) 2007-2013 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.
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
22 #include "mainwindow.hxx"
23 #include "TreeModel.hxx"
25 #include <Basics_Utils.hxx>
26 #include "testhelper.hxx"
28 MainWindow::MainWindow(QWidget *parent)
34 headers << tr("Title") << tr("Description");
36 TreeModel *model = new TreeModel(headers);
37 TESTHELPER_loadDataFromFile(model, TESTHELPER_testfilename(DATAFILENAME));
39 view->setModel(model);
40 for (int column = 0; column < model->columnCount(); ++column)
41 view->resizeColumnToContents(column);
43 connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
45 connect(view->selectionModel(),
46 SIGNAL(selectionChanged(const QItemSelection &,
47 const QItemSelection &)),
48 this, SLOT(updateActions()));
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()));
61 void MainWindow::newData() {
62 LOG("MainWindow::newData(): START");
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())
71 QStringList path = text.trimmed().split("/");
72 TreeModel *model = (TreeModel *)view->model();
74 QString label = path.last();
76 DataObject * data = TESTHELPER_dummyObject(label);
77 model->addData(data,path);
79 LOG("MainWindow::newData(): END");
82 void MainWindow::insertChild()
84 QModelIndex index = view->selectionModel()->currentIndex();
85 QAbstractItemModel *model = view->model();
87 if (model->columnCount(index) == 0) {
88 if (!model->insertColumn(0, index))
92 if (!model->insertRow(0, index))
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]"),
103 view->selectionModel()->setCurrentIndex(model->index(0, 0, index),
104 QItemSelectionModel::ClearAndSelect);
108 bool MainWindow::insertColumn(const QModelIndex &parent)
110 QAbstractItemModel *model = view->model();
111 int column = view->selectionModel()->currentIndex().column();
113 // Insert a column in the parent item.
114 bool changed = model->insertColumn(column + 1, parent);
116 model->setHeaderData(column + 1, Qt::Horizontal, QVariant("[No header]"),
124 void MainWindow::insertRow()
126 QModelIndex index = view->selectionModel()->currentIndex();
127 QAbstractItemModel *model = view->model();
129 if (!model->insertRow(index.row()+1, index.parent()))
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);
140 bool MainWindow::removeColumn(const QModelIndex &parent)
142 QAbstractItemModel *model = view->model();
143 int column = view->selectionModel()->currentIndex().column();
145 // Insert columns in each child of the parent item.
146 bool changed = model->removeColumn(column, parent);
148 if (!parent.isValid() && changed)
154 void MainWindow::removeRow()
156 QModelIndex index = view->selectionModel()->currentIndex();
157 QAbstractItemModel *model = view->model();
158 if (model->removeRow(index.row(), index.parent()))
162 void MainWindow::updateActions()
164 bool hasSelection = !view->selectionModel()->selection().isEmpty();
165 removeRowAction->setEnabled(hasSelection);
166 removeColumnAction->setEnabled(hasSelection);
168 bool hasCurrent = view->selectionModel()->currentIndex().isValid();
169 insertRowAction->setEnabled(hasCurrent);
170 insertColumnAction->setEnabled(hasCurrent);
173 view->closePersistentEditor(view->selectionModel()->currentIndex());
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));
180 statusBar()->showMessage(tr("Position: (%1,%2) in top level").arg(row).arg(column));
184 void MainWindow::contextMenuEvent(QContextMenuEvent *event) {
186 menu.addAction(newDataAction);
187 menu.exec(event->globalPos());