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