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);
223 if (!_index.isValid())
226 Qt::ItemFlags
flags = 0;
229 if ( ( _index.column() == 1 ) ||
230 ( _index.column() == 2 ) ||
231 ( _index.column() == 3 ) )
232 flags = Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
234 if ( _index.column() == 0 )
235 flags = Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
237 flags = Qt::ItemIsEnabled;
242 if ( item->isGroup() )
243 return flags | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
245 return flags | Qt::ItemIsDragEnabled;
261 if (_orientation == Qt::Horizontal && _role == Qt::DisplayRole) {
264 case 0 :
return QVariant(
"Name");
265 case 1 :
return QVariant(
"Show");
266 case 2 :
return QVariant(
"Source");
267 case 3 :
return QVariant(
"Target");
292 if (!_parent.isValid())
295 parentItem =
static_cast<TreeItem*
>(_parent.internalPointer());
299 return createIndex(_row, _column, childItem);
301 return QModelIndex();
314 if (!_index.isValid())
315 return QModelIndex();
321 return QModelIndex();
323 return createIndex(parentItem->
row(), 0, parentItem);
337 if (_parent.column() > 0)
340 if (!_parent.isValid())
343 parentItem =
static_cast<TreeItem*
>(_parent.internalPointer());
363 if (obj != 0 && item != 0){
365 bool updateRow =
false;
398 if ( index0.isValid() && index1.isValid() ){
400 emit dataChanged( index0, index1);
465 if ( item != 0 && !
isRoot(item) ){
468 QModelIndex parentIndex = itemIndex.parent();
470 beginRemoveRows( parentIndex, itemIndex.row(), itemIndex.row() );
491 QModelIndex oldParentIndex = itemIndex.parent();
494 beginMoveRows ( oldParentIndex, itemIndex.row(), itemIndex.row(), newParentIndex,0 );
510 if (_index.isValid()) {
512 if (item)
return item;
527 if (_index.isValid()) {
544 if (_index.isValid()) {
566 return QModelIndex();
568 QModelIndex
index = createIndex(_object->
row(), _column, _object);
578 emit dataChangedInside(
itemId(_index), _index.column(), _value );
606 return Qt::MoveAction;
619 types <<
"DataControl/dragDrop";
633 QMimeData *
mimeData =
new QMimeData();
634 QByteArray encodedData;
636 QDataStream stream(&encodedData, QIODevice::WriteOnly);
640 foreach (QModelIndex
index, _indexes) {
641 if (index.isValid()) {
643 if (!rows.contains( index.row() ) ){
645 stream << item->
id();
647 rows.push_back( index.row() );
652 mimeData->setData(
"DataControl/dragDrop", encodedData);
660 Qt::DropAction _action,
663 const QModelIndex &_parent)
665 if (_action == Qt::IgnoreAction)
668 if (!_data->hasFormat(
"DataControl/dragDrop"))
671 QByteArray encodedData = _data->data(
"DataControl/dragDrop");
672 QDataStream stream(&encodedData, QIODevice::ReadOnly);
676 while (!stream.atEnd()) {
683 if (ids.count() == 0)
689 if ( newParent == 0 || !newParent->isGroup() )
693 for (
int i = 0; i < ids.count(); i++){
695 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 * 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.
TreeItem * rootItem_
Root item of the 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.