1 // Copyright (C) 2006-2008 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
19 #include "TreeView.hxx"
20 #include "SchemaItem.hxx"
21 #include "QtGuiContext.hxx"
22 #include "ValueDelegate.hxx"
25 #include "DataPort.hxx"
26 #include "TypeCode.hxx"
29 #include <QHeaderView>
33 #include "YacsTrace.hxx"
38 using namespace YACS::HMI;
40 TreeView::TreeView(QWidget *parent)
43 setDragDropMode(QAbstractItemView::DragDrop);
46 setDropIndicatorShown(true);
49 _valueDelegate = new ValueDelegate(parent);
51 connect(_valueDelegate, SIGNAL(commitData(QWidget*)),
52 this, SLOT(onCommitData(QWidget*)));
54 setItemDelegateForColumn(YLabel, _valueDelegate); // --- port label
55 setItemDelegateForColumn(YValue, _valueDelegate); // --- port value
62 void TreeView::setModel(QAbstractItemModel *model)
64 QTreeView::setModel(model);
65 _isEdition = QtGuiContext::getQtCurrent()->isEdition();
66 DEBTRACE("_isEdition=" << _isEdition);
69 void TreeView::viewSelection(const QModelIndex &ind)
74 void TreeView::resizeColumns()
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);
83 setColumnHidden(YType, false);
84 setColumnHidden(YValue, false);
85 setColumnWidth(YType, 100);
86 setColumnWidth(YValue, 100);
90 setColumnHidden(YType, true);
91 setColumnHidden(YState, false);
92 setColumnWidth(YState, 100);
96 bool TreeView::event(QEvent *event)
98 if (event->type() == QEvent::ToolTip)
100 QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event);
101 QModelIndex index = indexAt(helpEvent->pos());
104 QString valtip = model()->data(index, Qt::ToolTipRole).toString();
105 QToolTip::showText(helpEvent->globalPos(), valtip);
108 QToolTip::hideText();
110 return QTreeView::event(event);
113 void TreeView::contextMenuEvent(QContextMenuEvent *event)
115 QModelIndexList selList = selectedIndexes();
116 if (selList.isEmpty())
118 QModelIndex selected = selList.front();
119 if (selected.isValid())
121 SchemaItem* item = static_cast<SchemaItem*>(selected.internalPointer());
122 item->popupMenu(this, event->globalPos());
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.
133 void TreeView::onCommitData(QWidget *editor)
135 DEBTRACE("EditionElementaryNode::onCommitData " << editor);
136 GenericEditor* gedit = dynamic_cast<GenericEditor*>(editor);
138 QString val = gedit->GetStrValue();
139 DEBTRACE(val.toStdString());
140 Subject *sub = gedit->getSubject();
142 SubjectDataPort *sdp = dynamic_cast<SubjectDataPort*>(sub);
144 string strval = val.toStdString();
147 if (gedit->getColumnInSubject() == YValue)
149 if (sdp->getPort()->edGetType()->kind() == YACS::ENGINE::String)
150 strval = "\"" + strval + "\"";
152 isOk = sdp->setValue(strval);
157 isOk = sdp->setName(strval);
161 _valueDelegate->setResultEditing(editor, isOk);