Salome HOME
94691c9772824e688e0fff75313446fa75aa91ce
[modules/yacs.git] / src / genericgui / SchemaItem.cxx
1 //  Copyright (C) 2006-2008  CEA/DEN, EDF R&D
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.
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 #include "SchemaItem.hxx"
20 #include "QtGuiContext.hxx"
21 #include "Menus.hxx"
22 #include "ItemMimeData.hxx"
23
24 #include <QMetaType>
25 #include <QVariant>
26 #include <QMenu>
27
28 Q_DECLARE_METATYPE(YACS::HMI::Subject);
29
30 //#define _DEVDEBUG_
31 #include "YacsTrace.hxx"
32
33 using namespace std;
34 using namespace YACS::HMI;
35
36
37 SchemaItem::SchemaItem(SchemaItem *parent, QString label, Subject* subject)
38 {
39   DEBTRACE("SchemaItem::SchemaItem " << label.toStdString() << " " << this);
40   _parentItem = parent;
41   _label = label;
42   _subject = subject;
43
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);
54
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
58
59   if (_subject)
60     {
61       _subject->attach(this);
62       QtGuiContext::getQtCurrent()->_mapOfSchemaItem[_subject]=this;
63     }
64
65   if (_parentItem)
66     _parentItem->appendChild(this);
67   _execState = YACS::UNDEFINED;
68 }
69
70 SchemaItem::~SchemaItem()
71 {
72   DEBTRACE("SchemaItem::~SchemaItem " << _label.toStdString() << " " << this);
73   if (_parentItem)
74     {
75       SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
76       int position = row();
77       model->beginRemoveRows(_parentItem->modelIndex(), position, position);
78       if (_subject) QtGuiContext::getQtCurrent()->_mapOfSchemaItem.erase(_subject);
79       _parentItem->removeChild(this);
80       model->endRemoveRows();
81     }
82 }
83
84 void SchemaItem::appendChild(SchemaItem *child)
85 {
86   DEBTRACE("SchemaItem::appendChild");
87   _childItems.append(child);
88 }
89
90 void SchemaItem::removeChild(SchemaItem *child)
91 {
92   DEBTRACE("SchemaItem::removeChild");
93   _childItems.removeAll(child);
94 }
95
96 void SchemaItem::insertChild(int row, SchemaItem *child)
97 {
98   DEBTRACE("SchemaItem::insertChild");
99   _childItems.insert(row, child);
100 }
101
102 SchemaItem *SchemaItem::child(int row)
103 {
104   //DEBTRACE("SchemaItem::child");
105   return _childItems.value(row);
106 }
107
108 int SchemaItem::childCount() const
109 {
110   //DEBTRACE("SchemaItem::childCount " << _label.toStdString() << " " << _childItems.count());
111   return _childItems.count();
112 }
113
114 int SchemaItem::columnCount() const
115 {
116   //DEBTRACE("SchemaItem::columnCount " << _itemData.count());
117   return _itemData.count();
118 }
119
120 QVariant SchemaItem::data(int column, int role) const
121 {
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);
136     else
137       return runToolTip(column);
138   if (role == Qt::WhatsThisRole)
139     if (QtGuiContext::getQtCurrent()->isEdition())
140       return editionWhatsThis(column);
141     else
142       return runWhatsThis(column);
143   return QVariant();
144 }
145
146 Qt::ItemFlags SchemaItem::flags(const QModelIndex &index)
147 {
148   return Qt::ItemIsEnabled | Qt::ItemIsSelectable;// | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
149 }
150
151 int SchemaItem::row() const
152 {
153   //DEBTRACE("SchemaItem::row");
154   int row=0;
155   if (_parentItem)
156     row = _parentItem->_childItems.indexOf(const_cast<SchemaItem*>(this));
157   return row;
158 }
159
160 SchemaItem *SchemaItem::parent()
161 {
162   //DEBTRACE("SchemaItem::parent");
163   return _parentItem;
164 }
165
166 Subject* SchemaItem::getSubject()
167 {
168   return _subject;
169 }
170
171 void SchemaItem::update(GuiEvent event, int type, Subject* son)
172 {
173   //DEBTRACE("SchemaItem::update "<< eventName(event) <<" "<<type<<" "<<son);
174   QModelIndex index = QModelIndex();
175   SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
176   switch (event)
177     {
178     case RENAME:
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
183       break;
184     case EDIT:
185       if (type)
186         _itemBackground.replace(YLabel, model->editedBackBrush());
187       else
188         _itemBackground.replace(YLabel, model->stdBackBrush());
189       model->setData(modelIndex(YLabel), 0);  // --- to emit dataChanged signal
190       break;
191     default:
192       break;
193     }
194 }
195
196 void SchemaItem::select(bool isSelected)
197 {
198   DEBTRACE("SchemaItem::select " << _label.toStdString() << " " << isSelected);
199   QItemSelectionModel* selectionModel = QtGuiContext::getQtCurrent()->getSelectionModel();
200   QModelIndex anIndex = modelIndex();
201   QItemSelection newSelection(anIndex, anIndex);
202
203   if (isSelected)
204     {
205       if (!QtGuiContext::getQtCurrent()->_mapOfEditionItem.count(_subject))
206         {
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);
212         }
213
214       QtGuiContext::getQtCurrent()->getGMain()->raiseStacked();
215       QItemSelection currentSelected = selectionModel->selection();
216       if (currentSelected != newSelection)
217         {
218           DEBTRACE("currentSelected != newSelection");
219           // selectionModel->select(newSelection, QItemSelectionModel::ClearAndSelect);
220           selectionModel->select(newSelection, QItemSelectionModel::Clear);
221           selectionModel->select(newSelection, QItemSelectionModel::Select);
222         }
223       QtGuiContext::getQtCurrent()->setSelectedSubject(_subject);
224     }
225   else
226     selectionModel->select(newSelection, QItemSelectionModel::Deselect);
227 }
228
229 void SchemaItem::toggleState()
230 {
231   if (_itemCheckState.value(YLabel) == Qt::Unchecked)
232     {
233       DEBTRACE("SchemaItem::toggleState true");
234       _itemCheckState.replace(YLabel, Qt::Checked);
235     }
236   else
237     {
238       DEBTRACE("SchemaItem::toggleState false");
239       _itemCheckState.replace(YLabel, Qt::Unchecked);
240     }
241 }
242
243 QModelIndex SchemaItem::modelIndex(int column)
244 {
245   //DEBTRACE("SchemaItem::modelIndex " << _label.toStdString() << " " << column);
246   SchemaModel *schema = QtGuiContext::getQtCurrent()->getSchemaModel();
247   if (_parentItem && (_parentItem !=schema->getRootItem()))
248     return schema->index(row(),
249                          column,
250                          _parentItem->modelIndex());
251   else
252     return schema->index(row(),
253                          column,
254                          QModelIndex());
255 }
256
257 void SchemaItem::popupMenu(QWidget *caller, const QPoint &globalPos)
258 {
259   MenusBase m;
260   m.popupMenu(caller, globalPos);
261 }
262
263 /*!
264  * setData mime type must be coherent with SchemaModel::mimeTypes
265  */
266 ItemMimeData* SchemaItem::mimeData(ItemMimeData *mime)
267 {
268   DEBTRACE("SchemaItem::mimeData");
269   mime->setSubject(_subject);
270   mime->setData(getMimeFormat(), "_subject");
271   return mime;
272 }
273
274 bool SchemaItem::dropMimeData(const QMimeData* data, Qt::DropAction action)
275 {
276   return false;
277 }
278
279 void SchemaItem::reparent(SchemaItem *parent)
280 {
281   _parentItem = parent;
282   if (_parentItem)
283     _parentItem->appendChild(this);
284 }
285
286 //! used in node derived classes
287 void SchemaItem::setCaseValue()
288 {
289 }
290
291 QVariant SchemaItem::editionToolTip(int column) const
292 {
293   QString val = QString("Edition: ") + _itemData.value(column).toString();
294   return val;
295 }
296
297 QVariant SchemaItem::runToolTip(int column) const
298 {
299   QString val = QString("Execution: ") + _itemData.value(column).toString();
300   return val;
301 }
302
303 QVariant SchemaItem::editionWhatsThis(int column) const
304 {
305   QString val = QString("Edition help: ") + _itemWhatsThis.value(column).toString();
306   return val;
307 }
308
309 QVariant SchemaItem::runWhatsThis(int column) const
310 {
311   QString val = QString("Execution help: ") + _itemWhatsThis.value(column).toString();
312   return val;
313 }
314
315 QString SchemaItem::getMimeFormat()
316 {
317   return "yacs/subject";
318 }
319
320 void SchemaItem::setExecState(int execState)
321 {
322   DEBTRACE("SchemaItem::setExecState " << execState);
323   _execState = execState;
324   QString stateDef;
325   QColor sc;
326   switch (_execState)
327     {
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 = "---";
346    }
347   _itemData.replace(YState, stateDef);
348   _itemForeground.replace(YState, sc);
349 }