1 // Copyright (C) 2006-2014 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, or (at your option) any later version.
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 "SchemaModel.hxx"
21 #include "SchemaItem.hxx"
22 #include "SchemaNodeItem.hxx"
23 #include "SchemaProcItem.hxx"
24 #include "QtGuiContext.hxx"
25 #include "GuiEditor.hxx"
26 #include "ItemMimeData.hxx"
27 #include "guiObservers.hxx"
31 Q_DECLARE_METATYPE(YACS::HMI::Subject);
34 #include "YacsTrace.hxx"
37 using namespace YACS::HMI;
39 SchemaModel::SchemaModel(YACS::HMI::Subject *context,
41 QAbstractItemModel(parent)
43 DEBTRACE("SchemaModel::SchemaModel");
47 _context->attach(this);
48 _stdBackBrush = QColor("white");
49 _editedBackBrush = QColor("yellow");
50 _emphasizeBackBrush = QColor("magenta");
54 SchemaModel::~SchemaModel()
56 DEBTRACE("SchemaModel::~SchemaModel");
57 _subjectSet.clear(); // --- avoid destruction loop on delete context
60 QModelIndex SchemaModel::index(int row, int column, const QModelIndex &parent) const
62 // DEBTRACE("SchemaModel::index");
63 if (!hasIndex(row, column, parent))
66 SchemaItem *parentItem = 0;
68 if (!parent.isValid())
69 parentItem = _rootItem;
71 parentItem = static_cast<SchemaItem*>(parent.internalPointer());
73 SchemaItem *childItem = parentItem->child(row);
75 return createIndex(row, column, childItem);
80 QModelIndex SchemaModel::parent(const QModelIndex &index) const
82 // DEBTRACE("SchemaModel::parent");
85 SchemaItem *childItem = static_cast<SchemaItem*>(index.internalPointer());
86 SchemaItem *parentItem = childItem->parent();
87 if (parentItem == _rootItem)
89 return createIndex(parentItem->row(), 0, parentItem);
92 int SchemaModel::rowCount(const QModelIndex &parent) const
94 //DEBTRACE("SchemaModel::rowCount");
95 SchemaItem *parentItem;
96 if (parent.column() > 0)
98 if (!parent.isValid())
99 parentItem = _rootItem;
101 parentItem = static_cast<SchemaItem*>(parent.internalPointer());
103 return parentItem->childCount();
107 int SchemaModel::columnCount(const QModelIndex &parent) const
109 //DEBTRACE("SchemaModel::columnCount " << parent.isValid());
110 if (parent.isValid())
111 return static_cast<SchemaItem*>(parent.internalPointer())->columnCount();
114 if (_isEdition) return 3;
115 else return 3; //_rootItem->columnCount();
121 QVariant SchemaModel::data(const QModelIndex &index, int role) const
123 //DEBTRACE("SchemaModel::data");
124 if (!index.isValid())
126 SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
127 return item->data(index.column(), role);
130 QVariant SchemaModel::headerData(int section, Qt::Orientation orientation, int role) const
132 //DEBTRACE("SchemaModel::headerData");
133 if (role != Qt::DisplayRole)
136 if (orientation == Qt::Horizontal)
137 //return QString("Colonne %1").arg(section);
141 case YLabel: return QString("Name");
142 case YType: return QString("Type");
143 case YValue: return QString("Value");
144 default: return QString("- %1 -").arg(section);
149 case YLabel: return QString("Name");
150 case YType: return QString("Type");
151 case YState: return QString("State");
152 default: return QString("- %1 -").arg(section);
159 * For EditRole, setData only emit dataChanged signal,
160 * actual modification is done in SchemaItem.
162 bool SchemaModel::setData(const QModelIndex &index, const QVariant &value, int role)
164 DEBTRACE("SchemaModel::setData");
165 if (index.isValid() && role == Qt::EditRole)
167 DEBTRACE("Qt::EditRole, emit dataChanged");
168 emit dataChanged(index, index);
171 if (index.isValid() && role == Qt::CheckStateRole)
173 DEBTRACE("Qt::CheckStateRole, toggle state");
174 SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
176 emit dataChanged(index, index);
182 Qt::ItemFlags SchemaModel::flags(const QModelIndex &index) const
184 //DEBTRACE("SchemaModel::flags");
185 if (!index.isValid())
187 SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
188 return item->flags(index);
192 void SchemaModel::update(GuiEvent event, int type, Subject* son)
194 DEBTRACE("SchemaModel::update "<<eventName(event)<<" "<<type<<" "<<son);
197 case YACS::HMI::NEWROOT:
201 // DEBTRACE("SchemaModel::update(), event not handled: "<< eventName(event));
205 void SchemaModel::updateSelection(const QItemSelection &selected,
206 const QItemSelection &deselected)
208 DEBTRACE("SchemaModel::updateSelection");
210 QModelIndexList items = selected.indexes();
212 foreach (index, items)
214 SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
215 DEBTRACE("updateSelection select "<< item->getSubject()->getName());
216 item->getSubject()->select(true);
217 emit signalSelection(index);
220 items = deselected.indexes();
222 foreach (index, items)
224 SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
225 DEBTRACE("updateSelection deselect "<< item->getSubject()->getName());
226 item->getSubject()->select(false);
230 void SchemaModel::setEdition(bool isEdition)
232 _isEdition = isEdition;
235 const QBrush& SchemaModel::stdBackBrush()
237 return _stdBackBrush;
240 const QBrush& SchemaModel::editedBackBrush()
242 return _editedBackBrush;
245 const QBrush& SchemaModel::emphasizeBackBrush()
247 return _emphasizeBackBrush;
250 void SchemaModel::setNewRoot(YACS::HMI::Subject *root)
253 QString name = _root->getName().c_str();
254 _rootItem= new SchemaItem(0, "root", QtGuiContext::getQtCurrent());
255 SchemaProcItem *procItem = new SchemaProcItem(_rootItem, name, _root);
258 QMimeData* SchemaModel::mimeData(const QModelIndexList &indexes) const
260 DEBTRACE("SchemaModel::mimeData");
261 ItemMimeData *mime = new ItemMimeData;
262 mime->setSubject(_root);
266 QModelIndex index = indexes.first();
267 if (!index.isValid())
269 SchemaItem *item = static_cast<SchemaItem*>(index.internalPointer());
270 DEBTRACE("mimeData valid index");
271 return item->mimeData(mime);
274 bool SchemaModel::dropMimeData(const QMimeData* data, Qt::DropAction action,
275 int row, int column, const QModelIndex& parent)
277 DEBTRACE("SchemaModel::dropMimeData");
278 if (action == Qt::IgnoreAction)
281 string name = "empty";
282 SchemaItem *item = 0;
283 if (parent.isValid())
285 item = static_cast<SchemaItem*>(parent.internalPointer());
286 name = item->getSubject()->getName();
288 DEBTRACE(row << " " << column << " "<< name);
290 if ((row >= 0) && (column >=0))
292 QModelIndex ind = index(row, column, parent);
296 item = static_cast<SchemaItem*>(ind.internalPointer());
300 return item->dropMimeData(data, action);
303 Qt::DropActions SchemaModel::supportedDropActions() const
305 //DEBTRACE("SchemaModel::supportedDropActions");
306 return Qt::CopyAction | Qt::MoveAction | Qt::LinkAction;
309 QStringList SchemaModel::mimeTypes() const
311 //DEBTRACE("SchemaModel::mimeTypes");
313 types << "yacs/subject" << "yacs/subjectNode" << "yacs/subjectOutPort"
314 << "yacs/cataService" << "yacs/cataType" << "yacs/cataNode"
315 << "yacs/subjectOutGate";