Version: 9.16.0
TreeView.cxx
Go to the documentation of this file.
1// Copyright (C) 2006-2026 CEA, EDF
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#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
38using namespace std;
39using namespace YACS::HMI;
40
41TreeView::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
60{
61}
62
64{
65 QTreeView::setModel(model);
67 DEBTRACE("_isEdition=" << _isEdition);
68}
69
70void 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
78{
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
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
123void 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
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
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 }
#define YASSERT(val)
YASSERT macro is always defined, used like assert, but throw a YACS::Exception instead of abort.
Definition: YacsTrace.hxx:59
#define DEBTRACE(msg)
Definition: YacsTrace.hxx:31
TypeCode * edGetType() const
Definition: DataPort.hxx:53
DynType kind() const
Definition: TypeCode.cxx:47
virtual int getColumnInSubject()
virtual Subject * getSubject()
virtual QString GetStrValue()
virtual void onHelpContextModule(const QString &, const QString &, const QString &=QString())
YACS::HMI::SubjectProc * getSubjectProc()
Definition: guiContext.hxx:52
void setInPortValue(YACS::ENGINE::DataPort *port, std::string value)
std::map< YACS::HMI::Subject *, YACS::HMI::SchemaItem * > _mapOfSchemaItem
static QtGuiContext * getQtCurrent()
YACS::HMI::GenericGui * getGMain()
YACS::HMI::GuiExecutor * getGuiExecutor()
QModelIndex modelIndex(int column=0)
Definition: SchemaItem.cxx:253
virtual void popupMenu(QWidget *caller, const QPoint &globalPos)
Definition: SchemaItem.cxx:267
virtual bool setName(std::string name)
virtual bool setValue(std::string value)
virtual YACS::ENGINE::DataPort * getPort()
virtual bool setCase(std::string caseId, SubjectNode *snode)
virtual Subject * getParent()
ValueDelegate * _valueDelegate
Definition: TreeView.hxx:51
virtual void onCommitData(QWidget *editor)
Definition: TreeView.cxx:143
virtual void setModel(QAbstractItemModel *model)
Definition: TreeView.cxx:63
void contextMenuEvent(QContextMenuEvent *event)
Definition: TreeView.cxx:123
virtual ~TreeView()
Definition: TreeView.cxx:59
void viewSelection(const QModelIndex &ind)
Definition: TreeView.cxx:70
virtual bool event(QEvent *event)
Definition: TreeView.cxx:99
virtual void setResultEditing(QWidget *editor, bool isOk)