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;
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() )
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 );