44 #include "TreeModel.hh" 52 #include <ObjectTypes/Light/LightWidget.hh> 99 if (!_index.isValid())
106 std::cerr <<
"Root" << std::endl;
110 if ( _role == Qt::BackgroundRole ) {
112 return QVariant( QBrush(QColor(100,100,100) ) );
118 switch (_index.column() ) {
123 if ( _role == Qt::DecorationRole ) {
129 return QVariant (QIcon (OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"LightOff.png"));
135 if (_role != Qt::DisplayRole && _role != Qt::EditRole )
138 return QVariant(item->
name());
143 if (_role == Qt::CheckStateRole ) {
144 bool visible =
false;
146 if (item->isGroup() && item->
childCount() > 0)
148 QList< TreeItem* > children = item->
getLeafs();
150 visible = children[0]->visible();
151 for (
int i=0; i < children.size() ; ++i)
153 if (visible != children[i]->visible())
154 return QVariant(Qt::PartiallyChecked);
159 return (visible) ? QVariant(Qt::Checked) : QVariant(Qt::Unchecked);
164 if (_role == Qt::CheckStateRole ) {
167 if (item->isGroup() && item->
childCount() > 0)
169 QList< TreeItem* > children = item->
getLeafs();
171 source = children[0]->source();
172 for (
int i=0; i < children.size() ; ++i)
174 if (source != children[i]->source())
175 return QVariant(Qt::PartiallyChecked);
180 return (source) ? QVariant(Qt::Checked) : QVariant(Qt::Unchecked);
186 if (_role == Qt::CheckStateRole ) {
189 if (item->isGroup() && item->
childCount() > 0)
191 QList< TreeItem* > children = item->
getLeafs();
193 target = children[0]->target();
194 for (
int i=0; i < children.size() ; ++i)
196 if (target != children[i]->target())
197 return QVariant(Qt::PartiallyChecked);
202 return (target) ? QVariant(Qt::Checked) : QVariant(Qt::Unchecked);
224 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) 225 if (!_index.isValid())
228 Qt::ItemFlags
flags = 0;
230 if (!_index.isValid())
231 return Qt::ItemFlags();
233 Qt::ItemFlags flags = Qt::ItemFlags();
238 if ( ( _index.column() == 1 ) ||
239 ( _index.column() == 2 ) ||
240 ( _index.column() == 3 ) )
241 flags = Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
243 if ( _index.column() == 0 )
244 flags = Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
246 flags = Qt::ItemIsEnabled;
251 if ( item->isGroup() )
252 return flags | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
254 return flags | Qt::ItemIsDragEnabled;
270 if (_orientation == Qt::Horizontal && _role == Qt::DisplayRole) {
273 case 0 :
return QVariant(
"Name");
274 case 1 :
return QVariant(
"Show");
275 case 2 :
return QVariant(
"Source");
276 case 3 :
return QVariant(
"Target");
301 if (!_parent.isValid())
304 parentItem =
static_cast<TreeItem*
>(_parent.internalPointer());
308 return createIndex(_row, _column, childItem);
310 return QModelIndex();
323 if (!_index.isValid())
324 return QModelIndex();
330 return QModelIndex();
332 return createIndex(parentItem->
row(), 0, parentItem);
346 if (_parent.column() > 0)
349 if (!_parent.isValid())
352 parentItem =
static_cast<TreeItem*
>(_parent.internalPointer());
372 if (obj != 0 && item != 0){
374 bool updateRow =
false;
407 if ( index0.isValid() && index1.isValid() ){
409 emit dataChanged( index0, index1);
474 if ( item != 0 && !
isRoot(item) ){
477 QModelIndex parentIndex = itemIndex.parent();
479 beginRemoveRows( parentIndex, itemIndex.row(), itemIndex.row() );
500 QModelIndex oldParentIndex = itemIndex.parent();
503 beginMoveRows ( oldParentIndex, itemIndex.row(), itemIndex.row(), newParentIndex,0 );
519 if (_index.isValid()) {
521 if (item)
return item;
536 if (_index.isValid()) {
553 if (_index.isValid()) {
575 return QModelIndex();
577 QModelIndex
index = createIndex(_object->
row(), _column, _object);
587 emit dataChangedInside(
itemId(_index), _index.column(), _value );
615 return Qt::MoveAction;
628 types <<
"DataControl/dragDrop";
642 QMimeData *
mimeData =
new QMimeData();
643 QByteArray encodedData;
645 QDataStream stream(&encodedData, QIODevice::WriteOnly);
649 foreach (QModelIndex
index, _indexes) {
650 if (index.isValid()) {
652 if (!rows.contains( index.row() ) ){
654 stream << item->
id();
656 rows.push_back( index.row() );
661 mimeData->setData(
"DataControl/dragDrop", encodedData);
669 Qt::DropAction _action,
672 const QModelIndex &_parent)
674 if (_action == Qt::IgnoreAction)
677 if (!_data->hasFormat(
"DataControl/dragDrop"))
680 QByteArray encodedData = _data->data(
"DataControl/dragDrop");
681 QDataStream stream(&encodedData, QIODevice::ReadOnly);
685 while (!stream.atEnd()) {
692 if (ids.count() == 0)
698 if ( newParent == 0 || !newParent->isGroup() )
702 for (
int i = 0; i < ids.count(); i++){
704 emit moveBaseObject( ids[i], newParent->
id() );
DataType dataType()
dataType
Qt::DropActions supportedDropActions() const
supported drag & Drop actions
LightSource * lightSource()
int row() const
get the row of this item from the parent
void deleteSubtree()
delete the whole subtree below this item ( The item itself is not touched )
bool enabled() const
Get light source status.
QModelIndex getModelIndex(TreeItem *_object, int _column)
Return the ModelIndex corresponding to a given TreeItem and Column.
TreeItem * child(int row)
return a child
TreeItem * rootItem_
Root item of the tree.
TreeItem * childExists(int _objectId)
Check if the element exists in the subtree of this element.
int columnCount(const QModelIndex &_parent=QModelIndex()) const
Return the number of columns.
QModelIndex parent(const QModelIndex &_index) const
Get the parent ModelIndex.
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
BaseObject *& objectRoot()
Get the root of the object structure.
Qt::ItemFlags flags(const QModelIndex &_index) const
return the types of the corresponding entry
QVariant headerData(int _section, Qt::Orientation _orientation, int _role=Qt::DisplayRole) const
return the header data of the model
void setParent(TreeItem *_parent)
Set the parent pointer.
void moveItem(TreeItem *_item, TreeItem *_parent)
move the item to a new parent
TreeItem * parent()
Get the parent item ( 0 if root item )
QList< TreeItem *> getLeafs()
get all leafes of the tree below this object ( These will be all visible objects ) ...
void appendChild(TreeItem *child)
add a child to this node
TreeItem * getItem(const QModelIndex &_index) const
Get the TreeItem corresponding to a given ModelIndex.
bool dataType(DataType _type) const
QVariant data(const QModelIndex &_index, int _role) const
Get the data of the corresponding entry.
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
This is called when mimeData is dropped.
int itemId(const QModelIndex &_index) const
Get the id of a TreeItem corresponding to a given ModelIndex.
QModelIndex index(int _row, int _column, const QModelIndex &_parent=QModelIndex()) const
Get the ModelIndex at given row,column.
BaseObject * parent()
Get the parent item ( 0 if rootitem )
QStringList mimeTypes() const
stores the mimeType for Drag & Drop
QString name() const
return the name of the object. The name defaults to NONAME if unset.
QString itemName(const QModelIndex &_index) const
Get the name of a TreeItem corresponding to a given ModelIndex.
bool isRoot(TreeItem *_item)
Check if the given item is the root item.
QMimeData * mimeData(const QModelIndexList &indexes) const
get the mimeData for a given ModelIndex
void objectDeleted(int _id)
The object with the given id has been deleted. delete it from the internal tree.
void objectChanged(int _id)
The object with the given id has been changed. Check if model also has to be changed.
virtual bool visible()
return if object is visible
int rowCount(const QModelIndex &_parent=QModelIndex()) const
get the number of rows
TreeModel(QObject *_parent=0)
Constructor.
void objectAdded(BaseObject *_object)
The object with the given id has been added. add it to the internal tree.
void removeChild(TreeItem *_item)
Remove a child from this object.
int childCount() const
get the number of children
bool setData(const QModelIndex &_index, const QVariant &_value, int _role)
Set Data at 'index' to 'value'.
const DataType DATA_UNKNOWN(0)
None of the other Objects.
DLLEXPORT QIcon & typeIcon(DataType _id)
Get an QIcon associated with the given DataType.