24 #include <QApplication> 25 #include <QGraphicsItem> 26 #include <QDomDocument> 27 #include <QDomElement> 30 #include <QMessageBox> 34 : QAbstractItemModel( parent )
35 , mComposition( composition )
47 if ( !index.isValid() )
57 const QModelIndex &
parent )
const 65 if ( !parent.isValid() && row >= 0 && row <
mItemsInScene.size() )
75 void QgsComposerModel::refreshItemsInScene()
81 QList<QgsComposerItem *>::const_iterator itemIt =
mItemZList.constBegin();
82 for ( ; itemIt !=
mItemZList.constEnd(); ++itemIt )
101 if ( !parent.isValid() )
106 QGraphicsItem * parentItem = itemFromIndex( parent );
127 if ( !index.isValid() )
138 case Qt::DisplayRole:
139 if ( index.column() == ItemId )
149 if ( index.column() == ItemId )
162 case Qt::TextAlignmentRole:
163 return Qt::AlignLeft & Qt::AlignVCenter;
165 case Qt::CheckStateRole:
166 switch ( index.column() )
170 return item->isVisible() ? Qt::Checked : Qt::Unchecked;
173 return item->
positionLock() ? Qt::Checked : Qt::Unchecked;
179 if ( index.column() == ItemId && item->isSelected() )
183 boldFont.setBold(
true );
201 if ( !index.isValid() )
210 switch ( index.column() )
215 emit dataChanged( index, index );
221 emit dataChanged( index, index );
226 item->
setId( value.toString() );
227 emit dataChanged( index, index );
236 static QIcon lockIcon;
237 if ( lockIcon.isNull() )
239 static QIcon showIcon;
240 if ( showIcon.isNull() )
245 case Qt::DisplayRole:
247 if ( section == ItemId )
258 case Qt::DecorationRole:
260 if ( section == Visibility )
262 return qVariantFromValue( showIcon );
264 else if ( section == LockStatus )
266 return qVariantFromValue( lockIcon );
275 case Qt::TextAlignmentRole:
276 return Qt::AlignLeft & Qt::AlignVCenter;
279 return QAbstractItemModel::headerData( section, orientation, role );
286 return Qt::MoveAction;
292 types <<
"application/x-vnd.qgis.qgis.composeritemid";
298 QMimeData *
mimeData =
new QMimeData();
299 QByteArray encodedData;
301 QDataStream stream( &encodedData, QIODevice::WriteOnly );
303 foreach (
const QModelIndex &
index, indexes )
305 if ( index.isValid() && index.column() == ItemId )
312 QString text = item->
uuid();
317 mimeData->setData(
"application/x-vnd.qgis.qgis.composeritemid", encodedData );
323 return item1->zValue() > item2->zValue();
327 Qt::DropAction action,
int row,
int column,
const QModelIndex &
parent )
329 if ( column != ItemId )
334 if ( action == Qt::IgnoreAction )
339 if ( !data->hasFormat(
"application/x-vnd.qgis.qgis.composeritemid" ) )
344 if ( parent.isValid() )
349 int beginRow = row != -1 ? row :
rowCount( QModelIndex() );
351 QByteArray encodedData = data->data(
"application/x-vnd.qgis.qgis.composeritemid" );
352 QDataStream stream( &encodedData, QIODevice::ReadOnly );
353 QList<QgsComposerItem*> droppedItems;
356 while ( !stream.atEnd() )
363 droppedItems << const_cast<QgsComposerItem*>( item );
368 if ( droppedItems.length() == 0 )
393 int insertPos = destPos;
394 QList<QgsComposerItem*>::iterator itemIt = droppedItems.begin();
395 for ( ; itemIt != droppedItems.end(); ++itemIt )
404 if ( listPos < destPos )
411 itemIt = droppedItems.begin();
412 for ( ; itemIt != droppedItems.end(); ++itemIt )
418 itemIt = droppedItems.begin();
419 for ( ; itemIt != droppedItems.end(); ++itemIt )
425 rebuildSceneItemList();
426 mComposition->updateZValues(
false );
434 if ( parent.isValid() )
453 refreshItemsInScene();
464 QList<QgsComposerItem*> sortedList;
468 QList<QGraphicsItem*> itemList = mComposition->items( Qt::DescendingOrder );
469 QList<QGraphicsItem*>::iterator itemIt = itemList.begin();
470 for ( ; itemIt != itemList.end(); ++itemIt )
477 sortedList.append( composerItem );
483 rebuildSceneItemList();
486 void QgsComposerModel::rebuildSceneItemList()
490 QList<QgsComposerItem*>::iterator zListIt =
mItemZList.begin();
492 for ( ; zListIt !=
mItemZList.end(); ++zListIt )
501 if ( sceneListPos == row )
506 else if ( sceneListPos != -1 )
509 beginMoveRows( QModelIndex(), sceneListPos, sceneListPos, QModelIndex(), row );
517 beginInsertRows( QModelIndex(), row, row );
527 beginInsertRows( QModelIndex(), 0, 0 );
529 refreshItemsInScene();
550 QModelIndex itemIndex = indexForItem( item );
551 if ( !itemIndex.isValid() )
557 refreshItemsInScene();
562 int row = itemIndex.row();
563 beginRemoveRows( QModelIndex(), row, row );
565 refreshItemsInScene();
585 QModelIndex itemIndex = indexForItem( item );
586 if ( !itemIndex.isValid() )
592 int row = itemIndex.row();
593 beginRemoveRows( QModelIndex(), row, row );
595 refreshItemsInScene();
615 rebuildSceneItemList();
627 QModelIndex itemIndex = indexForItem( item, ItemId );
628 if ( !itemIndex.isValid() )
634 emit dataChanged( itemIndex, itemIndex );
646 QModelIndex itemIndex = indexForItem( item, LockStatus );
647 if ( !itemIndex.isValid() )
653 emit dataChanged( itemIndex, itemIndex );
665 QModelIndex itemIndex = indexForItem( item, Visibility );
666 if ( !itemIndex.isValid() )
672 emit dataChanged( itemIndex, itemIndex );
684 QModelIndex itemIndex = indexForItem( item, ItemId );
685 if ( !itemIndex.isValid() )
691 emit dataChanged( itemIndex, itemIndex );
708 QMutableListIterator<QgsComposerItem*> it(
mItemZList );
709 if ( ! it.findNext( item ) )
716 while ( it.hasPrevious() )
722 if ( it.value() && !( it.value()->isRemoved() ) )
730 QModelIndex itemIndex = indexForItem( item );
731 if ( !itemIndex.isValid() )
737 int row = itemIndex.row();
738 beginMoveRows( QModelIndex(), row, row, QModelIndex(), row - 1 );
739 refreshItemsInScene();
758 QMutableListIterator<QgsComposerItem*> it(
mItemZList );
759 if ( ! it.findNext( item ) )
766 while ( it.hasNext() )
772 if ( it.value() && !( it.value()->isRemoved() ) )
780 QModelIndex itemIndex = indexForItem( item );
781 if ( !itemIndex.isValid() )
787 int row = itemIndex.row();
788 beginMoveRows( QModelIndex(), row, row, QModelIndex(), row + 2 );
789 refreshItemsInScene();
808 QMutableListIterator<QgsComposerItem*> it(
mItemZList );
809 if ( it.findNext( item ) )
816 QModelIndex itemIndex = indexForItem( item );
817 if ( !itemIndex.isValid() )
823 int row = itemIndex.row();
824 beginMoveRows( QModelIndex(), row, row, QModelIndex(), 0 );
825 refreshItemsInScene();
844 QMutableListIterator<QgsComposerItem*> it(
mItemZList );
845 if ( it.findNext( item ) )
852 QModelIndex itemIndex = indexForItem( item );
853 if ( !itemIndex.isValid() )
859 int row = itemIndex.row();
860 beginMoveRows( QModelIndex(), row, row, QModelIndex(),
rowCount() );
861 refreshItemsInScene();
869 QListIterator<QgsComposerItem*> it(
mItemZList );
871 if ( it.findPrevious( item ) )
874 while ( it.hasPrevious() )
877 if ( it.hasPrevious() && !it.peekPrevious()->isGroupMember() )
879 return it.previous();
890 QListIterator<QgsComposerItem*> it(
mItemZList );
891 if ( it.findNext( item ) )
894 while ( it.hasNext() )
896 if ( !it.peekNext()->isGroupMember() )
914 Qt::ItemFlags
flags = QAbstractItemModel::flags( index );
916 if ( ! index.isValid() )
918 return flags | Qt::ItemIsDropEnabled;;
921 switch ( index.column() )
925 return flags | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled;
927 return flags | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled;
929 return flags | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
933 QModelIndex QgsComposerModel::indexForItem(
QgsComposerItem *item,
const int column )
937 return QModelIndex();
944 return QModelIndex();
947 return index( row, column );
bool positionLock() const
Returns whether position lock for mouse drags is enabled returns true if item is locked for mouse mov...
Qt::DropActions supportedDropActions() const override
QgsComposerItem * getComposerItemAbove(QgsComposerItem *item) const
Finds the next composer item above an item.
QgsComposerModel(QgsComposition *composition, QObject *parent=0)
Constructor.
void rebuildZList()
Rebuilds the z-order list, based on the current stacking of items in the composition.
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
int zOrderListSize() const
Returns the size of the z-order list, which includes items which may have been removed from the compo...
A item that forms part of a map composition.
void setSelectedItem(QgsComposerItem *item)
Clears any selected items and sets an item as the current selection.
void updateItemVisibility(QgsComposerItem *item)
Must be called when an item's visibility changes.
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
virtual QStringList mimeTypes() const override
virtual QMimeData * mimeData(const QModelIndexList &indexes) const override
void setItemRestored(QgsComposerItem *item)
Restores an item to the composition.
bool reorderItemDown(QgsComposerItem *item)
Moves an item down the z-order list.
void updateItemDisplayName(QgsComposerItem *item)
Must be called when an item's display name is modified.
bool zOrderDescending(QgsComposerItem *item1, QgsComposerItem *item2)
QList< QgsComposerItem * > * zOrderList()
Returns the item z-order list.
QModelIndex parent(const QModelIndex &index) const override
void updateItemLockStatus(QgsComposerItem *item)
Must be called when an item's lock status changes.
QString uuid() const
Get item identification name.
bool setData(const QModelIndex &index, const QVariant &value, int role) override
QList< QgsComposerItem * > mItemZList
Maintains z-Order of items.
void setIsRemoved(const bool removed)
Sets whether this item has been removed from the composition.
void clear()
Clears all items from z-order list and resets the model.
const QgsComposerItem * getComposerItemByUuid(const QString theUuid) const
Returns a composer item given its unique identifier.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QList< QgsComposerItem * > mItemsInScene
Cached list of items from mItemZList which are currently in the scene.
Graphics scene for map printing.
QVariant data(const QModelIndex &index, int role) const override
void removeItem(QgsComposerItem *item)
Removes an item from the z-order list.
virtual QString displayName() const
Get item display name.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void setPositionLock(const bool lock)
Locks / unlocks the item position for mouse drags.
void setSelected(const QModelIndex &index)
Sets an item as the current selection from a QModelIndex.
bool reorderItemUp(QgsComposerItem *item)
Moves an item up the z-order list.
QgsComposerItem * getComposerItemBelow(QgsComposerItem *item) const
Finds the next composer item below an item.
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
virtual void setId(const QString &id)
Set item's id (which is not necessarly unique)
int columnCount(const QModelIndex &parent=QModelIndex()) const override
void updateItemSelectStatus(QgsComposerItem *item)
Must be called when an item's selection status changes.
void setItemRemoved(QgsComposerItem *item)
Marks an item as removed from the composition.
bool reorderItemToTop(QgsComposerItem *item)
Moves an item to the top of the z-order list.
void addItemAtTop(QgsComposerItem *item)
Adds an item to the top of the composition z stack.
virtual void setVisibility(const bool visible)
Sets visibility for item.
bool reorderItemToBottom(QgsComposerItem *item)
Moves an item to the bottom of the z-order list.
virtual int type() const override
return correct graphics item type.
Qt::ItemFlags flags(const QModelIndex &index) const override
QString id() const
Get item's id (which is not necessarly unique)