Back to index

salome-gui  6.5.0
TreeView.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 // Author: Guillaume Boulant (EDF/R&D)
00021 
00022 
00023 // include Qt
00024 #include <QString>
00025 #include <QMenu>
00026 #include <QModelIndex>
00027 #include <QAbstractItemView>
00028 
00029 // include Xcad
00030 #include "TreeView.hxx"
00031 #include "TreeModel.hxx"
00032 #include "TreeItem.hxx"
00033 #include "QtHelper.hxx"
00034 
00035 TreeView::TreeView(QWidget * parent)
00036   : QTreeView(parent)
00037 {
00038   // We authorize the multiple selection of items
00039   this->setSelectionMode(QAbstractItemView::ExtendedSelection);
00040 
00041   _lastActionId = 0;
00042 
00043   // Default actions for tests
00044   int displayActionId = addAction(QObject::tr("Afficher"));
00045   int editActionId    = addAction(QObject::tr("Editer"));
00046 }
00047 
00048 TreeView::~TreeView() {
00049 }
00050 
00060 int TreeView::addAction(QString label) {
00061   QAction * action = new QAction(this);
00062   int actionId = _lastActionId;
00063   action->setObjectName(_idToName(actionId));
00064   action->setText(label);
00065   _listActions << action;
00066   _lastActionId++;
00067 
00068   return actionId;
00069 }
00070 
00075 void TreeView::clearActions() {
00076   _listActions.clear();
00077 }
00078 
00083 QString TreeView::_idToName(int actionId) {
00084   return QString::number(actionId);
00085 }
00090 int TreeView::_nameToId(QString actionName) {
00091   return actionName.toInt();
00092 }
00093 
00094 void TreeView::contextMenuEvent(QContextMenuEvent *event) {
00095   if ( _listActions.size() == 0 ) {
00096     // Just return there is no actions defined for this popup menu
00097     return;
00098   }
00099 
00100   // _TODO_ display the QMenu only if the selected item is acceptable
00101   QMenu menu(this);
00102   for (int i = 0; i < _listActions.size(); ++i) {
00103     menu.addAction(_listActions.at(i));
00104   }
00105   connect(&menu, SIGNAL(triggered(QAction*)),
00106          this,  SLOT(processMenuAction(QAction*)));
00107 
00108   menu.exec(event->globalPos());
00109 }
00110 
00115 void TreeView::processMenuAction(QAction * actionSelected) {
00116   LOG("processMenuAction: START");
00117 
00118   // We first check than at least on item is selected
00119   QModelIndexList indexList = this->selectionModel()->selectedRows(0);
00120   if ( indexList.isEmpty() ) {
00121     LOG("No item selected");
00122     return;
00123   }
00124 
00125   // Then we can gather the list of model item ids associated the
00126   // selection.
00127   TreeModel *model = (TreeModel *)this->model();
00128   QListIterator<QModelIndex> it(indexList);
00129   QStringList nameIdList;
00130   while (it.hasNext()) {
00131     TreeItem * item = model->getItem(it.next());
00132     nameIdList << item->nameId();
00133   }
00134 
00135   // Finally, one can emit a signal to observers specifying the list of
00136   // id and the type of action (i.e. the action identifier)
00137   int actionId = _nameToId(actionSelected->objectName());
00138   LOG("TreeView::processMenuAction: signal emitted:\n"<<
00139       "item list: "<<nameIdList<<"\n"<<
00140       "action id: "<<actionId);
00141   emit itemListToProcess(nameIdList, actionId);
00142 
00143   LOG("processMenuAction: END");
00144 }