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 "SchemaItem.hxx"
21 #include "QtGuiContext.hxx"
22 #include "Resource.hxx"
24 #include "ItemMimeData.hxx"
30 Q_DECLARE_METATYPE(YACS::HMI::Subject);
33 #include "YacsTrace.hxx"
36 using namespace YACS::HMI;
39 SchemaItem::SchemaItem(SchemaItem *parent, QString label, Subject* subject)
41 DEBTRACE("SchemaItem::SchemaItem " << label.toStdString() << " " << this);
46 _itemData << QVariant() << QVariant() << QVariant(); // --- 3 columns max
47 _itemDeco << QVariant() << QVariant() << QVariant(); // --- 3 columns max
48 _itemForeground << QVariant() << QVariant() << QVariant(); // --- 3 columns max
49 _itemBackground << QVariant() << QVariant() << QVariant(); // --- 3 columns max
50 _itemCheckState << QVariant() << QVariant() << QVariant(); // --- 3 columns max
51 _itemToolTip << QVariant() << QVariant() << QVariant(); // --- 3 columns max
52 _itemWhatsThis << QVariant() << QVariant() << QVariant(); // --- 3 columns max
53 _itemData.replace(YLabel, label);
54 _itemToolTip.replace(YLabel, label);
55 _itemWhatsThis.replace(YLabel, QString("This is the default WhatsThis of ") + label);
57 _itemForeground.replace(YLabel, QColor("blue"));
58 _itemBackground.replace(YLabel, QtGuiContext::getQtCurrent()->getSchemaModel()->stdBackBrush());
59 //_itemCheckState.replace(YLabel, Qt::Unchecked); // --- only for item with checkbox
63 _subject->attach(this);
64 QtGuiContext::getQtCurrent()->_mapOfSchemaItem[_subject]=this;
68 _parentItem->appendChild(this);
69 _execState = YACS::UNDEFINED;
73 SchemaItem::~SchemaItem()
75 DEBTRACE("SchemaItem::~SchemaItem " << _label.toStdString() << " " << this);
78 SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
80 model->beginRemoveRows(_parentItem->modelIndex(), position, position);
81 if (_subject) QtGuiContext::getQtCurrent()->_mapOfSchemaItem.erase(_subject);
82 _parentItem->removeChild(this);
83 model->endRemoveRows();
87 void SchemaItem::appendChild(SchemaItem *child)
89 DEBTRACE("SchemaItem::appendChild");
90 _childItems.append(child);
93 void SchemaItem::removeChild(SchemaItem *child)
95 DEBTRACE("SchemaItem::removeChild");
96 _childItems.removeAll(child);
99 void SchemaItem::insertChild(int row, SchemaItem *child)
101 DEBTRACE("SchemaItem::insertChild");
102 _childItems.insert(row, child);
105 SchemaItem *SchemaItem::child(int row)
107 //DEBTRACE("SchemaItem::child");
108 return _childItems.value(row);
111 int SchemaItem::childCount() const
113 //DEBTRACE("SchemaItem::childCount " << _label.toStdString() << " " << _childItems.count());
114 return _childItems.count();
117 int SchemaItem::columnCount() const
119 //DEBTRACE("SchemaItem::columnCount " << _itemData.count());
120 return _itemData.count();
123 QVariant SchemaItem::data(int column, int role) const
125 //DEBTRACE("SchemaItem::data "<< column);
126 if (role == Qt::DisplayRole)
127 return _itemData.value(column);
128 if (role == Qt::DecorationRole)
129 return _itemDeco.value(column);
130 if (role == Qt::ForegroundRole)
131 return _itemForeground.value(column);
132 if (role == Qt::BackgroundRole)
133 return _itemBackground.value(column);
134 if (role == Qt::CheckStateRole)
135 return _itemCheckState.value(column);
136 if (role == Qt::ToolTipRole)
137 if (QtGuiContext::getQtCurrent()->isEdition())
138 return editionToolTip(column);
140 return runToolTip(column);
141 if (role == Qt::WhatsThisRole)
142 if (QtGuiContext::getQtCurrent()->isEdition())
143 return editionWhatsThis(column);
145 return runWhatsThis(column);
149 Qt::ItemFlags SchemaItem::flags(const QModelIndex &index)
151 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;// | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
154 int SchemaItem::row() const
156 //DEBTRACE("SchemaItem::row");
159 row = _parentItem->_childItems.indexOf(const_cast<SchemaItem*>(this));
163 SchemaItem *SchemaItem::parent()
165 //DEBTRACE("SchemaItem::parent");
169 Subject* SchemaItem::getSubject()
174 void SchemaItem::update(GuiEvent event, int type, Subject* son)
176 //DEBTRACE("SchemaItem::update "<< eventName(event) <<" "<<type<<" "<<son);
177 QModelIndex index = QModelIndex();
178 SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
182 DEBTRACE("SchemaItem::update RENAME " << _subject->getName());
183 _label = _subject->getName().c_str();
184 _itemData.replace(YLabel, _label);
185 model->setData(modelIndex(YLabel), 0); // --- to emit dataChanged signal
189 _itemBackground.replace(YLabel, model->editedBackBrush());
191 _itemBackground.replace(YLabel, model->stdBackBrush());
192 model->setData(modelIndex(YLabel), 0); // --- to emit dataChanged signal
196 _itemBackground.replace(YLabel, model->emphasizeBackBrush());
198 _itemBackground.replace(YLabel, model->stdBackBrush());
199 model->setData(modelIndex(YLabel), 0); // --- to emit dataChanged signal
206 void SchemaItem::select(bool isSelected)
208 DEBTRACE("SchemaItem::select " << _label.toStdString() << " " << isSelected);
209 QItemSelectionModel* selectionModel = QtGuiContext::getQtCurrent()->getSelectionModel();
210 QModelIndex anIndex = modelIndex();
211 QItemSelection newSelection(anIndex, anIndex);
215 if (!QtGuiContext::getQtCurrent()->_mapOfEditionItem.count(_subject))
217 int elemType = _subject->getType();
218 YACS::HMI::GuiEvent event = YACS::HMI::ADD;
219 if (elemType == YACS::HMI::DATALINK) event = YACS::HMI::ADDLINK;
220 else if (elemType == YACS::HMI::CONTROLLINK) event = YACS::HMI::ADDCONTROLLINK;
221 QtGuiContext::getQtCurrent()->getEditionRoot()->update(event, elemType, _subject);
224 QtGuiContext::getQtCurrent()->getGMain()->raiseStacked();
225 QItemSelection currentSelected = selectionModel->selection();
226 if (currentSelected != newSelection)
228 DEBTRACE("currentSelected != newSelection");
229 // selectionModel->select(newSelection, QItemSelectionModel::ClearAndSelect);
230 selectionModel->select(newSelection, QItemSelectionModel::Clear);
231 selectionModel->select(newSelection, QItemSelectionModel::Select);
233 QtGuiContext::getQtCurrent()->setSelectedSubject(_subject);
236 selectionModel->select(newSelection, QItemSelectionModel::Deselect);
239 void SchemaItem::toggleState()
241 if (_itemCheckState.value(YLabel) == Qt::Unchecked)
243 DEBTRACE("SchemaItem::toggleState true");
244 _itemCheckState.replace(YLabel, Qt::Checked);
248 DEBTRACE("SchemaItem::toggleState false");
249 _itemCheckState.replace(YLabel, Qt::Unchecked);
253 QModelIndex SchemaItem::modelIndex(int column)
255 //DEBTRACE("SchemaItem::modelIndex " << _label.toStdString() << " " << column);
256 SchemaModel *schema = QtGuiContext::getQtCurrent()->getSchemaModel();
257 if (_parentItem && (_parentItem !=schema->getRootItem()))
258 return schema->index(row(),
260 _parentItem->modelIndex());
262 return schema->index(row(),
267 void SchemaItem::popupMenu(QWidget *caller, const QPoint &globalPos)
270 m.popupMenu(caller, globalPos);
274 * setData mime type must be coherent with SchemaModel::mimeTypes
276 ItemMimeData* SchemaItem::mimeData(ItemMimeData *mime)
278 DEBTRACE("SchemaItem::mimeData");
279 mime->setSubject(_subject);
280 mime->setData(getMimeFormat(), "_subject");
284 bool SchemaItem::dropMimeData(const QMimeData* data, Qt::DropAction action)
289 void SchemaItem::reparent(SchemaItem *parent)
291 _parentItem = parent;
293 _parentItem->appendChild(this);
296 //! used in node derived classes
297 void SchemaItem::setCaseValue()
301 QVariant SchemaItem::editionToolTip(int column) const
303 QString val = QString("Edition: ") + _itemData.value(0).toString();
304 QString val1 = _itemData.value(1).toString();
305 QString val2 = _itemData.value(2).toString();
306 if (!val1.isEmpty()) val += QString(" | ") + val1;
307 if (!val2.isEmpty()) val += QString(" | ") + val2;
311 QVariant SchemaItem::runToolTip(int column) const
313 QString val = QString("Execution: ") + _itemData.value(0).toString();
314 QString val1 = _itemData.value(1).toString();
315 QString val2 = _itemData.value(2).toString();
316 if (!val1.isEmpty()) val += QString(" | ") + val1;
317 if (!val2.isEmpty()) val += QString(" | ") + val2;
321 QVariant SchemaItem::editionWhatsThis(int column) const
323 QString val = QString("Edition help: ") + _itemWhatsThis.value(column).toString();
327 QVariant SchemaItem::runWhatsThis(int column) const
329 QString val = QString("Execution help: ") + _itemWhatsThis.value(column).toString();
333 QString SchemaItem::getMimeFormat()
335 return "yacs/subject";
338 void SchemaItem::setExecState(int execState)
340 DEBTRACE("SchemaItem::setExecState " << execState);
341 _execState = execState;
346 case YACS::UNDEFINED: sc = Resource::UNDEFINED ; stateDef = "UNDEFINED" ; break;
347 case YACS::INVALID: sc = Resource::INVALID ; stateDef = "INVALID" ; break;
348 case YACS::READY: sc = Resource::READY ; stateDef = "READY" ; break;
349 case YACS::TOLOAD: sc = Resource::TOLOAD ; stateDef = "TOLOAD" ; break;
350 case YACS::LOADED: sc = Resource::LOADED ; stateDef = "LOADED" ; break;
351 case YACS::TOACTIVATE: sc = Resource::TOACTIVATE ; stateDef = "TOACTIVATE" ; break;
352 case YACS::ACTIVATED: sc = Resource::ACTIVATED ; stateDef = "ACTIVATED" ; break;
353 case YACS::DESACTIVATED: sc = Resource::DESACTIVATED; stateDef = "DESACTIVATED"; break;
354 case YACS::DONE: sc = Resource::DONE ; stateDef = "DONE" ; break;
355 case YACS::SUSPENDED: sc = Resource::SUSPENDED ; stateDef = "SUSPENDED" ; break;
356 case YACS::LOADFAILED: sc = Resource::LOADFAILED ; stateDef = "LOADFAILED" ; break;
357 case YACS::EXECFAILED: sc = Resource::EXECFAILED ; stateDef = "EXECFAILED" ; break;
358 case YACS::PAUSE: sc = Resource::PAUSE ; stateDef = "PAUSE" ; break;
359 case YACS::INTERNALERR: sc = Resource::INTERNALERR ; stateDef = "INTERNALERR" ; break;
360 case YACS::DISABLED: sc = Resource::DISABLED ; stateDef = "DISABLED" ; break;
361 case YACS::FAILED: sc = Resource::FAILED ; stateDef = "FAILED" ; break;
362 case YACS::ERROR: sc = Resource::ERROR ; stateDef = "ERROR" ; break;
363 default: sc = Resource::DEFAULT ; stateDef = "---" ;
365 _itemData.replace(YState, stateDef);
366 _itemForeground.replace(YState, sc);