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 "SchemaItem.hxx"
20 #include "QtGuiContext.hxx"
22 #include "ItemMimeData.hxx"
28 Q_DECLARE_METATYPE(YACS::HMI::Subject);
31 #include "YacsTrace.hxx"
34 using namespace YACS::HMI;
37 SchemaItem::SchemaItem(SchemaItem *parent, QString label, Subject* subject)
39 DEBTRACE("SchemaItem::SchemaItem " << label.toStdString() << " " << this);
44 _itemData << QVariant() << QVariant() << QVariant(); // --- 3 columns max
45 _itemDeco << QVariant() << QVariant() << QVariant(); // --- 3 columns max
46 _itemForeground << QVariant() << QVariant() << QVariant(); // --- 3 columns max
47 _itemBackground << QVariant() << QVariant() << QVariant(); // --- 3 columns max
48 _itemCheckState << QVariant() << QVariant() << QVariant(); // --- 3 columns max
49 _itemToolTip << QVariant() << QVariant() << QVariant(); // --- 3 columns max
50 _itemWhatsThis << QVariant() << QVariant() << QVariant(); // --- 3 columns max
51 _itemData.replace(YLabel, label);
52 _itemToolTip.replace(YLabel, label);
53 _itemWhatsThis.replace(YLabel, QString("This is the default WhatsThis of ") + label);
55 _itemForeground.replace(YLabel, QColor("blue"));
56 _itemBackground.replace(YLabel, QtGuiContext::getQtCurrent()->getSchemaModel()->stdBackBrush());
57 //_itemCheckState.replace(YLabel, Qt::Unchecked); // --- only for item with checkbox
61 _subject->attach(this);
62 QtGuiContext::getQtCurrent()->_mapOfSchemaItem[_subject]=this;
66 _parentItem->appendChild(this);
67 _execState = YACS::UNDEFINED;
70 SchemaItem::~SchemaItem()
72 DEBTRACE("SchemaItem::~SchemaItem " << _label.toStdString() << " " << this);
75 SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
77 model->beginRemoveRows(_parentItem->modelIndex(), position, position);
78 if (_subject) QtGuiContext::getQtCurrent()->_mapOfSchemaItem.erase(_subject);
79 _parentItem->removeChild(this);
80 model->endRemoveRows();
84 void SchemaItem::appendChild(SchemaItem *child)
86 DEBTRACE("SchemaItem::appendChild");
87 _childItems.append(child);
90 void SchemaItem::removeChild(SchemaItem *child)
92 DEBTRACE("SchemaItem::removeChild");
93 _childItems.removeAll(child);
96 void SchemaItem::insertChild(int row, SchemaItem *child)
98 DEBTRACE("SchemaItem::insertChild");
99 _childItems.insert(row, child);
102 SchemaItem *SchemaItem::child(int row)
104 //DEBTRACE("SchemaItem::child");
105 return _childItems.value(row);
108 int SchemaItem::childCount() const
110 //DEBTRACE("SchemaItem::childCount " << _label.toStdString() << " " << _childItems.count());
111 return _childItems.count();
114 int SchemaItem::columnCount() const
116 //DEBTRACE("SchemaItem::columnCount " << _itemData.count());
117 return _itemData.count();
120 QVariant SchemaItem::data(int column, int role) const
122 //DEBTRACE("SchemaItem::data "<< column);
123 if (role == Qt::DisplayRole)
124 return _itemData.value(column);
125 if (role == Qt::DecorationRole)
126 return _itemDeco.value(column);
127 if (role == Qt::ForegroundRole)
128 return _itemForeground.value(column);
129 if (role == Qt::BackgroundRole)
130 return _itemBackground.value(column);
131 if (role == Qt::CheckStateRole)
132 return _itemCheckState.value(column);
133 if (role == Qt::ToolTipRole)
134 if (QtGuiContext::getQtCurrent()->isEdition())
135 return editionToolTip(column);
137 return runToolTip(column);
138 if (role == Qt::WhatsThisRole)
139 if (QtGuiContext::getQtCurrent()->isEdition())
140 return editionWhatsThis(column);
142 return runWhatsThis(column);
146 Qt::ItemFlags SchemaItem::flags(const QModelIndex &index)
148 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;// | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
151 int SchemaItem::row() const
153 //DEBTRACE("SchemaItem::row");
156 row = _parentItem->_childItems.indexOf(const_cast<SchemaItem*>(this));
160 SchemaItem *SchemaItem::parent()
162 //DEBTRACE("SchemaItem::parent");
166 Subject* SchemaItem::getSubject()
171 void SchemaItem::update(GuiEvent event, int type, Subject* son)
173 //DEBTRACE("SchemaItem::update "<< eventName(event) <<" "<<type<<" "<<son);
174 QModelIndex index = QModelIndex();
175 SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
179 DEBTRACE("SchemaItem::update RENAME " << _subject->getName());
180 _label = _subject->getName().c_str();
181 _itemData.replace(YLabel, _label);
182 model->setData(modelIndex(YLabel), 0); // --- to emit dataChanged signal
186 _itemBackground.replace(YLabel, model->editedBackBrush());
188 _itemBackground.replace(YLabel, model->stdBackBrush());
189 model->setData(modelIndex(YLabel), 0); // --- to emit dataChanged signal
196 void SchemaItem::select(bool isSelected)
198 DEBTRACE("SchemaItem::select " << _label.toStdString() << " " << isSelected);
199 QItemSelectionModel* selectionModel = QtGuiContext::getQtCurrent()->getSelectionModel();
200 QModelIndex anIndex = modelIndex();
201 QItemSelection newSelection(anIndex, anIndex);
205 if (!QtGuiContext::getQtCurrent()->_mapOfEditionItem.count(_subject))
207 int elemType = _subject->getType();
208 YACS::HMI::GuiEvent event = YACS::HMI::ADD;
209 if (elemType == YACS::HMI::DATALINK) event = YACS::HMI::ADDLINK;
210 else if (elemType == YACS::HMI::CONTROLLINK) event = YACS::HMI::ADDCONTROLLINK;
211 QtGuiContext::getQtCurrent()->getEditionRoot()->update(event, elemType, _subject);
214 QtGuiContext::getQtCurrent()->getGMain()->raiseStacked();
215 QItemSelection currentSelected = selectionModel->selection();
216 if (currentSelected != newSelection)
218 DEBTRACE("currentSelected != newSelection");
219 // selectionModel->select(newSelection, QItemSelectionModel::ClearAndSelect);
220 selectionModel->select(newSelection, QItemSelectionModel::Clear);
221 selectionModel->select(newSelection, QItemSelectionModel::Select);
223 QtGuiContext::getQtCurrent()->setSelectedSubject(_subject);
226 selectionModel->select(newSelection, QItemSelectionModel::Deselect);
229 void SchemaItem::toggleState()
231 if (_itemCheckState.value(YLabel) == Qt::Unchecked)
233 DEBTRACE("SchemaItem::toggleState true");
234 _itemCheckState.replace(YLabel, Qt::Checked);
238 DEBTRACE("SchemaItem::toggleState false");
239 _itemCheckState.replace(YLabel, Qt::Unchecked);
243 QModelIndex SchemaItem::modelIndex(int column)
245 //DEBTRACE("SchemaItem::modelIndex " << _label.toStdString() << " " << column);
246 SchemaModel *schema = QtGuiContext::getQtCurrent()->getSchemaModel();
247 if (_parentItem && (_parentItem !=schema->getRootItem()))
248 return schema->index(row(),
250 _parentItem->modelIndex());
252 return schema->index(row(),
257 void SchemaItem::popupMenu(QWidget *caller, const QPoint &globalPos)
260 m.popupMenu(caller, globalPos);
264 * setData mime type must be coherent with SchemaModel::mimeTypes
266 ItemMimeData* SchemaItem::mimeData(ItemMimeData *mime)
268 DEBTRACE("SchemaItem::mimeData");
269 mime->setSubject(_subject);
270 mime->setData(getMimeFormat(), "_subject");
274 bool SchemaItem::dropMimeData(const QMimeData* data, Qt::DropAction action)
279 void SchemaItem::reparent(SchemaItem *parent)
281 _parentItem = parent;
283 _parentItem->appendChild(this);
286 //! used in node derived classes
287 void SchemaItem::setCaseValue()
291 QVariant SchemaItem::editionToolTip(int column) const
293 QString val = QString("Edition: ") + _itemData.value(column).toString();
297 QVariant SchemaItem::runToolTip(int column) const
299 QString val = QString("Execution: ") + _itemData.value(column).toString();
303 QVariant SchemaItem::editionWhatsThis(int column) const
305 QString val = QString("Edition help: ") + _itemWhatsThis.value(column).toString();
309 QVariant SchemaItem::runWhatsThis(int column) const
311 QString val = QString("Execution help: ") + _itemWhatsThis.value(column).toString();
315 QString SchemaItem::getMimeFormat()
317 return "yacs/subject";
320 void SchemaItem::setExecState(int execState)
322 DEBTRACE("SchemaItem::setExecState " << execState);
323 _execState = execState;
328 case YACS::UNDEFINED: sc=Qt::lightGray; stateDef = "UNDEFINED"; break;
329 case YACS::INVALID: sc=Qt::red; stateDef = "INVALID"; break;
330 case YACS::READY: sc=Qt::darkGray; stateDef = "READY"; break;
331 case YACS::TOLOAD: sc=Qt::darkYellow; stateDef = "TOLOAD"; break;
332 case YACS::LOADED: sc=Qt::darkMagenta; stateDef = "LOADED"; break;
333 case YACS::TOACTIVATE: sc=Qt::darkCyan; stateDef = "TOACTIVATE"; break;
334 case YACS::ACTIVATED: sc=Qt::darkBlue; stateDef = "ACTIVATED"; break;
335 case YACS::DESACTIVATED: sc=Qt::gray; stateDef = "DESACTIVATED"; break;
336 case YACS::DONE: sc=Qt::darkGreen; stateDef = "DONE"; break;
337 case YACS::SUSPENDED: sc=Qt::gray; stateDef = "SUSPENDED"; break;
338 case YACS::LOADFAILED: sc.setHsv(320,255,255); stateDef = "LOADFAILED"; break;
339 case YACS::EXECFAILED: sc.setHsv( 20,255,255); stateDef = "EXECFAILED"; break;
340 case YACS::PAUSE: sc.setHsv(180,255,255); stateDef = "PAUSE"; break;
341 case YACS::INTERNALERR: sc.setHsv(340,255,255); stateDef = "INTERNALERR"; break;
342 case YACS::DISABLED: sc.setHsv( 40,255,255); stateDef = "DISABLED"; break;
343 case YACS::FAILED: sc.setHsv( 20,255,255); stateDef = "FAILED"; break;
344 case YACS::ERROR: sc.setHsv( 0,255,255); stateDef = "ERROR"; break;
345 default: sc=Qt::lightGray; stateDef = "---";
347 _itemData.replace(YState, stateDef);
348 _itemForeground.replace(YState, sc);