Salome HOME
Merge from V6_main 01/04/2013
[modules/yacs.git] / src / genericgui / TreeView.cxx
1 // Copyright (C) 2006-2013  CEA/DEN, EDF R&D
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.
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 #include "TreeView.hxx"
21 #include "SchemaItem.hxx"
22 #include "QtGuiContext.hxx"
23 #include "ValueDelegate.hxx"
24
25 #include "Port.hxx"
26 #include "DataPort.hxx"
27 #include "TypeCode.hxx"
28
29 #include <QMenu>
30 #include <QHeaderView>
31 #include <QToolTip>
32
33 //#define _DEVDEBUG_
34 #include "YacsTrace.hxx"
35
36 #include <cassert>
37
38 using namespace std;
39 using namespace YACS::HMI;
40
41 TreeView::TreeView(QWidget *parent)
42   : QTreeView(parent)
43 {
44   setDragDropMode(QAbstractItemView::DragDrop);
45   setDragEnabled(true);
46   setAcceptDrops(true);
47   setDropIndicatorShown(true);
48   _isEdition = true;
49
50   _valueDelegate = new ValueDelegate(parent);
51
52   connect(_valueDelegate, SIGNAL(commitData(QWidget*)),
53           this, SLOT(onCommitData(QWidget*)));
54
55   setItemDelegateForColumn(YLabel, _valueDelegate); // --- port label
56   setItemDelegateForColumn(YValue, _valueDelegate); // --- port value
57 }
58
59 TreeView::~TreeView()
60 {
61 }
62
63 void TreeView::setModel(QAbstractItemModel *model)
64 {
65   QTreeView::setModel(model);
66   _isEdition = QtGuiContext::getQtCurrent()->isEdition();
67   DEBTRACE("_isEdition=" << _isEdition);
68 }
69
70 void TreeView::viewSelection(const QModelIndex &ind)
71 {
72   QModelIndex ind0 = ind.sibling(ind.row(), 0);
73   //DEBTRACE("TreeView::viewSelection " << ind.row() << " " << ind.column() << " / " << ind0.row() << " " << ind0.column());
74   scrollTo(ind0);
75 }
76
77 void TreeView::resizeColumns()
78 {
79   Subject *sproc = QtGuiContext::getQtCurrent()->getSubjectProc();
80   SchemaItem *item = QtGuiContext::getQtCurrent()->_mapOfSchemaItem[sproc];
81   QModelIndex index = item->modelIndex();
82   setExpanded(index, true);
83   resizeColumnToContents(0);
84   if (_isEdition)
85     {
86       setColumnHidden(YType,  false);
87       setColumnHidden(YValue, false);
88       setColumnWidth(YType,  100);
89       setColumnWidth(YValue, 100);
90     }
91   else
92     {
93       setColumnHidden(YType,  true);
94       setColumnHidden(YState, false);
95       setColumnWidth(YState, 100);
96     }
97 }
98
99 bool TreeView::event(QEvent *event)
100 {
101   if (event->type() == QEvent::WhatsThisClicked)
102     {
103       QWhatsThisClickedEvent* clicked = static_cast<QWhatsThisClickedEvent*>(event);
104       QtGuiContext::getQtCurrent()->getGMain()->onHelpContextModule("YACS",clicked->href());
105       return true; // what's this remains open if true is returned
106     }
107
108   if (event->type() == QEvent::ToolTip)
109     {
110       QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event);
111       QModelIndex index = indexAt(helpEvent->pos());
112       if (index.isValid())
113         {
114           QString valtip = model()->data(index, Qt::ToolTipRole).toString();
115           QToolTip::showText(helpEvent->globalPos(), valtip);
116         }
117       else
118         QToolTip::hideText();
119     }
120   return QTreeView::event(event);
121 }
122
123 void TreeView::contextMenuEvent(QContextMenuEvent *event)
124 {
125   QModelIndexList selList = selectedIndexes();
126   if (selList.isEmpty())
127     return;
128   QModelIndex selected = selList.front();
129   if (selected.isValid())
130     {
131       SchemaItem* item = static_cast<SchemaItem*>(selected.internalPointer());
132       item->popupMenu(this, event->globalPos());
133     }
134 }
135
136 /*!
137  *  After edition with a specific editor created by ValueDelegate
138  *  for a cell of Tree item, the resulting string is tested
139  *  for setValue on the corresponding subject. Result of the setValue
140  *  (succes or failure) is transmitted to ValueDelegate for further
141  *  action in case of failure.
142  */
143 void TreeView::onCommitData(QWidget *editor)
144 {
145   DEBTRACE("TreeView::onCommitData " << editor);
146   GenericEditor* gedit = dynamic_cast<GenericEditor*>(editor);
147   YASSERT(gedit);
148   QString val = gedit->GetStrValue();
149   string strval = val.toStdString();
150   DEBTRACE(strval);
151   bool isOk = false;
152
153   Subject *sub = gedit->getSubject();
154   YASSERT(sub);
155   SubjectDataPort *sdp = dynamic_cast<SubjectDataPort*>(sub);
156   if (sdp)
157     {
158       if (gedit->getColumnInSubject() == YValue)
159         {
160           if (sdp->getPort()->edGetType()->kind() == YACS::ENGINE::String)
161             strval = "\"" + strval + "\"";
162           DEBTRACE(strval);
163           isOk = sdp->setValue(strval);
164
165           GuiExecutor* executor = QtGuiContext::getQtCurrent()->getGuiExecutor();
166           if (executor) executor->setInPortValue(sdp->getPort(), strval);
167         }
168       else // --- YLabel
169         {
170           isOk = sdp->setName(strval);
171         }
172     }
173   else
174     {
175       SubjectNode *snode = dynamic_cast<SubjectNode*>(sub);
176       YASSERT(snode);
177       sub = snode->getParent();
178       SubjectSwitch *sswitch = dynamic_cast<SubjectSwitch*>(sub);
179       YASSERT(sswitch);
180       isOk = sswitch->setCase(strval, snode);
181     }
182
183   if (_valueDelegate)
184     _valueDelegate->setResultEditing(editor, isOk);
185  }