Back to index

salome-gui  6.5.0
mainwindow.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include <QtGui>
00021 
00022 #include "mainwindow.hxx"
00023 #include "TreeModel.hxx"
00024 
00025 #include <Basics_Utils.hxx>
00026 #include "testhelper.hxx"
00027 
00028 MainWindow::MainWindow(QWidget *parent)
00029     : QMainWindow(parent)
00030 {
00031     setupUi(this);
00032 
00033     QStringList headers;
00034     headers << tr("Title") << tr("Description");
00035 
00036     TreeModel *model = new TreeModel(headers);
00037     TESTHELPER_loadDataFromFile(model, TESTHELPER_testfilename(DATAFILENAME));
00038 
00039     view->setModel(model);
00040     for (int column = 0; column < model->columnCount(); ++column)
00041         view->resizeColumnToContents(column);
00042 
00043     connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
00044 
00045     connect(view->selectionModel(),
00046             SIGNAL(selectionChanged(const QItemSelection &,
00047                                     const QItemSelection &)),
00048             this, SLOT(updateActions()));
00049 
00050     connect(actionsMenu, SIGNAL(aboutToShow()), this, SLOT(updateActions()));
00051     connect(insertRowAction, SIGNAL(triggered()), this, SLOT(insertRow()));
00052     connect(insertColumnAction, SIGNAL(triggered()), this, SLOT(insertColumn()));
00053     connect(removeRowAction, SIGNAL(triggered()), this, SLOT(removeRow()));
00054     connect(removeColumnAction, SIGNAL(triggered()), this, SLOT(removeColumn()));
00055     connect(insertChildAction, SIGNAL(triggered()), this, SLOT(insertChild()));
00056     connect(newDataAction, SIGNAL(triggered()), this, SLOT(newData()));
00057 
00058     updateActions();
00059 }
00060 
00061 void MainWindow::newData() {
00062   LOG("MainWindow::newData(): START");
00063 
00064   bool ok;
00065   QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
00066                                        tr("Data path:"), QLineEdit::Normal,
00067                                        "folder/subfolder/item", &ok);
00068   if (!ok || text.trimmed().isEmpty())
00069     return;
00070 
00071   QStringList path = text.trimmed().split("/");
00072   TreeModel *model = (TreeModel *)view->model();
00073 
00074   QString label = path.last();
00075   path.removeLast();
00076   DataObject * data = TESTHELPER_dummyObject(label);
00077   model->addData(data,path);
00078 
00079   LOG("MainWindow::newData(): END");
00080 }
00081 
00082 void MainWindow::insertChild()
00083 {
00084     QModelIndex index = view->selectionModel()->currentIndex();
00085     QAbstractItemModel *model = view->model();
00086 
00087     if (model->columnCount(index) == 0) {
00088         if (!model->insertColumn(0, index))
00089             return;
00090     }
00091 
00092     if (!model->insertRow(0, index))
00093         return;
00094 
00095     for (int column = 0; column < model->columnCount(index); ++column) {
00096         QModelIndex child = model->index(0, column, index);
00097         model->setData(child, QVariant("[No data]"), Qt::EditRole);
00098         if (!model->headerData(column, Qt::Horizontal).isValid())
00099             model->setHeaderData(column, Qt::Horizontal, QVariant("[No header]"),
00100                                  Qt::EditRole);
00101     }
00102 
00103     view->selectionModel()->setCurrentIndex(model->index(0, 0, index),
00104                                             QItemSelectionModel::ClearAndSelect);
00105     updateActions();
00106 }
00107 
00108 bool MainWindow::insertColumn(const QModelIndex &parent)
00109 {
00110     QAbstractItemModel *model = view->model();
00111     int column = view->selectionModel()->currentIndex().column();
00112 
00113     // Insert a column in the parent item.
00114     bool changed = model->insertColumn(column + 1, parent);
00115     if (changed)
00116         model->setHeaderData(column + 1, Qt::Horizontal, QVariant("[No header]"),
00117                              Qt::EditRole);
00118 
00119     updateActions();
00120 
00121     return changed;
00122 }
00123 
00124 void MainWindow::insertRow()
00125 {
00126     QModelIndex index = view->selectionModel()->currentIndex();
00127     QAbstractItemModel *model = view->model();
00128 
00129     if (!model->insertRow(index.row()+1, index.parent()))
00130         return;
00131 
00132     updateActions();
00133 
00134     for (int column = 0; column < model->columnCount(index.parent()); ++column) {
00135         QModelIndex child = model->index(index.row()+1, column, index.parent());
00136         model->setData(child, QVariant("[No data]"), Qt::EditRole);
00137     }
00138 }
00139 
00140 bool MainWindow::removeColumn(const QModelIndex &parent)
00141 {
00142     QAbstractItemModel *model = view->model();
00143     int column = view->selectionModel()->currentIndex().column();
00144 
00145     // Insert columns in each child of the parent item.
00146     bool changed = model->removeColumn(column, parent);
00147 
00148     if (!parent.isValid() && changed)
00149         updateActions();
00150 
00151     return changed;
00152 }
00153 
00154 void MainWindow::removeRow()
00155 {
00156     QModelIndex index = view->selectionModel()->currentIndex();
00157     QAbstractItemModel *model = view->model();
00158     if (model->removeRow(index.row(), index.parent()))
00159         updateActions();
00160 }
00161 
00162 void MainWindow::updateActions()
00163 {
00164     bool hasSelection = !view->selectionModel()->selection().isEmpty();
00165     removeRowAction->setEnabled(hasSelection);
00166     removeColumnAction->setEnabled(hasSelection);
00167 
00168     bool hasCurrent = view->selectionModel()->currentIndex().isValid();
00169     insertRowAction->setEnabled(hasCurrent);
00170     insertColumnAction->setEnabled(hasCurrent);
00171 
00172     if (hasCurrent) {
00173         view->closePersistentEditor(view->selectionModel()->currentIndex());
00174 
00175         int row = view->selectionModel()->currentIndex().row();
00176         int column = view->selectionModel()->currentIndex().column();
00177         if (view->selectionModel()->currentIndex().parent().isValid())
00178             statusBar()->showMessage(tr("Position: (%1,%2)").arg(row).arg(column));
00179         else
00180             statusBar()->showMessage(tr("Position: (%1,%2) in top level").arg(row).arg(column));
00181     }
00182 }
00183 
00184 void MainWindow::contextMenuEvent(QContextMenuEvent *event) {
00185   QMenu menu(this);
00186   menu.addAction(newDataAction);
00187   menu.exec(event->globalPos());
00188 }