1 // Copyright (C) 2006-2012 CEA/DEN, EDF R&D
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.
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 #include "TreeView.hxx"
21 #include "SchemaItem.hxx"
22 #include "QtGuiContext.hxx"
23 #include "ValueDelegate.hxx"
26 #include "DataPort.hxx"
27 #include "TypeCode.hxx"
30 #include <QHeaderView>
34 #include "YacsTrace.hxx"
39 using namespace YACS::HMI;
41 TreeView::TreeView(QWidget *parent)
44 setDragDropMode(QAbstractItemView::DragDrop);
47 setDropIndicatorShown(true);
50 _valueDelegate = new ValueDelegate(parent);
52 connect(_valueDelegate, SIGNAL(commitData(QWidget*)),
53 this, SLOT(onCommitData(QWidget*)));
55 setItemDelegateForColumn(YLabel, _valueDelegate); // --- port label
56 setItemDelegateForColumn(YValue, _valueDelegate); // --- port value
63 void TreeView::setModel(QAbstractItemModel *model)
65 QTreeView::setModel(model);
66 _isEdition = QtGuiContext::getQtCurrent()->isEdition();
67 DEBTRACE("_isEdition=" << _isEdition);
70 void TreeView::viewSelection(const QModelIndex &ind)
72 QModelIndex ind0 = ind.sibling(ind.row(), 0);
73 //DEBTRACE("TreeView::viewSelection " << ind.row() << " " << ind.column() << " / " << ind0.row() << " " << ind0.column());
77 void TreeView::resizeColumns()
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);
86 setColumnHidden(YType, false);
87 setColumnHidden(YValue, false);
88 setColumnWidth(YType, 100);
89 setColumnWidth(YValue, 100);
93 setColumnHidden(YType, true);
94 setColumnHidden(YState, false);
95 setColumnWidth(YState, 100);
99 bool TreeView::event(QEvent *event)
101 if (event->type() == QEvent::WhatsThisClicked)
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
108 if (event->type() == QEvent::ToolTip)
110 QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event);
111 QModelIndex index = indexAt(helpEvent->pos());
114 QString valtip = model()->data(index, Qt::ToolTipRole).toString();
115 QToolTip::showText(helpEvent->globalPos(), valtip);
118 QToolTip::hideText();
120 return QTreeView::event(event);
123 void TreeView::contextMenuEvent(QContextMenuEvent *event)
125 QModelIndexList selList = selectedIndexes();
126 if (selList.isEmpty())
128 QModelIndex selected = selList.front();
129 if (selected.isValid())
131 SchemaItem* item = static_cast<SchemaItem*>(selected.internalPointer());
132 item->popupMenu(this, event->globalPos());
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.
143 void TreeView::onCommitData(QWidget *editor)
145 DEBTRACE("TreeView::onCommitData " << editor);
146 GenericEditor* gedit = dynamic_cast<GenericEditor*>(editor);
148 QString val = gedit->GetStrValue();
149 string strval = val.toStdString();
153 Subject *sub = gedit->getSubject();
155 SubjectDataPort *sdp = dynamic_cast<SubjectDataPort*>(sub);
158 if (gedit->getColumnInSubject() == YValue)
160 if (sdp->getPort()->edGetType()->kind() == YACS::ENGINE::String)
161 strval = "\"" + strval + "\"";
163 isOk = sdp->setValue(strval);
165 GuiExecutor* executor = QtGuiContext::getQtCurrent()->getGuiExecutor();
166 if (executor) executor->setInPortValue(sdp->getPort(), strval);
170 isOk = sdp->setName(strval);
175 SubjectNode *snode = dynamic_cast<SubjectNode*>(sub);
177 sub = snode->getParent();
178 SubjectSwitch *sswitch = dynamic_cast<SubjectSwitch*>(sub);
180 isOk = sswitch->setCase(strval, snode);
184 _valueDelegate->setResultEditing(editor, isOk);