Salome HOME
updated copyright message
[modules/gui.git] / src / TreeData / TreeView.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // Author: Guillaume Boulant (EDF/R&D)
21
22
23 // include Qt
24 #include <QString>
25 #include <QMenu>
26 #include <QModelIndex>
27 #include <QAbstractItemView>
28
29 // include Xcad
30 #include "TreeView.hxx"
31 #include "TreeModel.hxx"
32 #include "TreeItem.hxx"
33 #include "QtHelper.hxx"
34
35 TreeView::TreeView(QWidget * parent)
36   : QTreeView(parent)
37 {
38   // We authorize the multiple selection of items
39   this->setSelectionMode(QAbstractItemView::ExtendedSelection);
40
41   _lastActionId = 0;
42
43   // Default actions for tests
44   /*int displayActionId = */addAction(QObject::tr("Afficher"));
45   /*int editActionId    = */addAction(QObject::tr("Editer"));
46 }
47
48 TreeView::~TreeView() {
49 }
50
51 /*!
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.
59  */
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;
66   _lastActionId++;
67
68   return actionId;
69 }
70
71 /*!
72  * This function removes all actions previously defined for the popup
73  * menu of this TreeView.
74  */
75 void TreeView::clearActions() {
76   _listActions.clear();
77 }
78
79 /*!
80  * You must use this function to create the name of an action object
81  * from its id.
82  */
83 QString TreeView::_idToName(int actionId) {
84   return QString::number(actionId);
85 }
86 /*!
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).
89  */
90 int TreeView::_nameToId(QString actionName) {
91   return actionName.toInt();
92 }
93
94 void TreeView::contextMenuEvent(QContextMenuEvent *event) {
95   if ( _listActions.size() == 0 ) {
96     // Just return there is no actions defined for this popup menu
97     return;
98   }
99
100   // _TODO_ display the QMenu only if the selected item is acceptable
101   QMenu menu(this);
102   for (int i = 0; i < _listActions.size(); ++i) {
103     menu.addAction(_listActions.at(i));
104   }
105   connect(&menu, SIGNAL(triggered(QAction*)),
106           this,  SLOT(processMenuAction(QAction*)));
107
108   menu.exec(event->globalPos());
109 }
110
111 /*!
112  * This SLOT is connected on the signal emited by the menu when an
113  * action is selected.
114  */
115 void TreeView::processMenuAction(QAction * actionSelected) {
116   LOG("processMenuAction: START");
117
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");
122     return;
123   }
124
125   // Then we can gather the list of model item ids associated the
126   // selection.
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();
133   }
134
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);
142
143   LOG("processMenuAction: END");
144 }