1 // Copyright (C) 2007-2014 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 // Author: Guillaume Boulant (EDF/R&D)
26 #include <QModelIndex>
27 #include <QAbstractItemView>
30 #include "TreeView.hxx"
31 #include "TreeModel.hxx"
32 #include "TreeItem.hxx"
33 #include "QtHelper.hxx"
35 TreeView::TreeView(QWidget * parent)
38 // We authorize the multiple selection of items
39 this->setSelectionMode(QAbstractItemView::ExtendedSelection);
43 // Default actions for tests
44 int displayActionId = addAction(QObject::tr("Afficher"));
45 int editActionId = addAction(QObject::tr("Editer"));
48 TreeView::~TreeView() {
52 * This function defines a menu item to add in the popup menu
53 * associated to this TreeView, and return an integer that corresponds
54 * to the unique identifier of this action (identifier used in the
55 * signal emitted to notify observers that an action has been
56 * selected). Then the caller of this function has to take care of
57 * this return id (i.e. has to store it in its internal tables) to be
58 * able to process the notifications from this TreeView.
60 int TreeView::addAction(QString label) {
61 QAction * action = new QAction(this);
62 int actionId = _lastActionId;
63 action->setObjectName(_idToName(actionId));
64 action->setText(label);
65 _listActions << action;
72 * This function removes all actions previously defined for the popup
73 * menu of this TreeView.
75 void TreeView::clearActions() {
80 * You must use this function to create the name of an action object
83 QString TreeView::_idToName(int actionId) {
84 return QString::number(actionId);
87 * You must use this function to create the id of an action object
88 * from its name (stored in objectName() attribute of the QAction).
90 int TreeView::_nameToId(QString actionName) {
91 return actionName.toInt();
94 void TreeView::contextMenuEvent(QContextMenuEvent *event) {
95 if ( _listActions.size() == 0 ) {
96 // Just return there is no actions defined for this popup menu
100 // _TODO_ display the QMenu only if the selected item is acceptable
102 for (int i = 0; i < _listActions.size(); ++i) {
103 menu.addAction(_listActions.at(i));
105 connect(&menu, SIGNAL(triggered(QAction*)),
106 this, SLOT(processMenuAction(QAction*)));
108 menu.exec(event->globalPos());
112 * This SLOT is connected on the signal emited by the menu when an
113 * action is selected.
115 void TreeView::processMenuAction(QAction * actionSelected) {
116 LOG("processMenuAction: START");
118 // We first check than at least on item is selected
119 QModelIndexList indexList = this->selectionModel()->selectedRows(0);
120 if ( indexList.isEmpty() ) {
121 LOG("No item selected");
125 // Then we can gather the list of model item ids associated the
127 TreeModel *model = (TreeModel *)this->model();
128 QListIterator<QModelIndex> it(indexList);
129 QStringList nameIdList;
130 while (it.hasNext()) {
131 TreeItem * item = model->getItem(it.next());
132 nameIdList << item->nameId();
135 // Finally, one can emit a signal to observers specifying the list of
136 // id and the type of action (i.e. the action identifier)
137 int actionId = _nameToId(actionSelected->objectName());
138 LOG("TreeView::processMenuAction: signal emitted:\n"<<
139 "item list: "<<nameIdList<<"\n"<<
140 "action id: "<<actionId);
141 emit itemListToProcess(nameIdList, actionId);
143 LOG("processMenuAction: END");