13 changes: 8 additions & 5 deletions python/core/composer/qgscomposeritem.sip
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,19 @@ class QgsComposerItem: QObject, QGraphicsRectItem
/**Updates item, with the possibility to do custom update for subclasses*/
virtual void updateItem();

/**Get item identification name
/**Get item's id (which is not necessarly unique)
@note this method was added in version 1.7*/
QString id() const;

/**Set item identification name
@note this method was added in version 1.7
This method was moved from qgscomposerlabel so that every object can have a
id (NathanW)*/
/**Set item's id (which is not necessarly unique)
@note this method was added in version 1.7*/
void setId( const QString& id );

/**Get item identification name
@note this method was added in version 2.0
@note there is not setter since one can't manually set the id*/
QString uuid() const;

public slots:
virtual void setRotation( double r );
void repaint();
Expand Down
12 changes: 12 additions & 0 deletions python/core/composer/qgscomposition.sip
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,25 @@ class QgsComposition : QGraphicsScene
const QgsComposerHtml* getComposerHtmlByItem( QgsComposerItem *item ) const;

/**Returns a composer item given its text identifier.
Ids are not necessarely unique, but this function returns only one element.
@note added in 2.0
@param theId - A QString representing the identifier of the item to
retrieve.
@return QgsComposerItem pointer or 0 pointer if no such item exists.
**/
const QgsComposerItem* getComposerItemById( QString theId ) const;


/**Returns a composer item given its unique identifier.
Warning : ids are not necessarely unique, but this function returns only one element.
@note added in 2.0
@param theId - A QString representing the UUID of the item to retrieve.
@param inAllComposers - Whether the search should be done in all composers of the project
@return QgsComposerItem pointer or 0 pointer if no such item exists.
**/
//const QgsComposerItem* getComposerItemByUuid( QString theUuid, bool inAllComposers = false ) const;
const QgsComposerItem* getComposerItemByUuid( QString theUuid ) const;

int printResolution() const;
void setPrintResolution( int dpi );

Expand Down
9 changes: 7 additions & 2 deletions src/app/composer/qgscomposeritemwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,15 @@ void QgsComposerItemWidget::setValuesForGuiElements()
mFrameGroupBox->blockSignals( true );
mBackgroundGroupBox->blockSignals( true );
mItemIdLineEdit->blockSignals( true );
mItemUuidLineEdit->blockSignals( true );
mTransparencySpinBox->blockSignals( true );

int alphaPercent = ( 255 - mItem->brush().color().alpha() ) / 2.55;
mTransparencySpinBox->setValue( alphaPercent );
mTransparencySlider->setValue( alphaPercent );
mOutlineWidthSpinBox->setValue( mItem->pen().widthF() );
mItemIdLineEdit->setText( mItem->id() );
mItemUuidLineEdit->setText( mItem->uuid() );
mFrameGroupBox->setChecked( mItem->hasFrame() );
mBackgroundGroupBox->setChecked( mItem->hasBackground() );

Expand All @@ -364,15 +366,18 @@ void QgsComposerItemWidget::setValuesForGuiElements()
mFrameGroupBox->blockSignals( false );
mBackgroundGroupBox->blockSignals( false );
mItemIdLineEdit->blockSignals( false );
mItemUuidLineEdit->blockSignals( false );
mTransparencySpinBox->blockSignals( false );
}

void QgsComposerItemWidget::on_mItemIdLineEdit_textChanged( const QString &text )

void QgsComposerItemWidget::on_mItemIdLineEdit_editingFinished()
{
if ( mItem )
{
mItem->beginCommand( tr( "Item id changed" ), QgsComposerMergeCommand::ComposerLabelSetId );
mItem->setId( text );
mItem->setId( mItemIdLineEdit->text() );
mItemIdLineEdit->setText( mItem->id() );
mItem->endCommand();
}
}
2 changes: 1 addition & 1 deletion src/app/composer/qgscomposeritemwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class QgsComposerItemWidget: public QWidget, private Ui::QgsComposerItemWidgetBa
void on_mOutlineWidthSpinBox_valueChanged( double d );
void on_mFrameGroupBox_toggled( bool state );
void on_mBackgroundGroupBox_toggled( bool state );
void on_mItemIdLineEdit_textChanged( const QString& text );
void on_mItemIdLineEdit_editingFinished();

//adjust coordinates in line edits
void on_mXLineEdit_editingFinished() { changeItemPosition(); }
Expand Down
10 changes: 0 additions & 10 deletions src/app/composer/qgscomposerlabelwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,6 @@ void QgsComposerLabelWidget::on_mMiddleRadioButton_clicked()
}
}

void QgsComposerLabelWidget::on_mLabelIdLineEdit_textChanged( const QString& text )
{
if ( mComposerLabel )
{
mComposerLabel->beginCommand( tr( "Label id changed" ), QgsComposerMergeCommand::ComposerLabelSetId );
mComposerLabel->setId( text );
mComposerLabel->endCommand();
}
}

void QgsComposerLabelWidget::on_mRotationSpinBox_valueChanged( double v )
{
if ( mComposerLabel )
Expand Down
1 change: 0 additions & 1 deletion src/app/composer/qgscomposerlabelwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class QgsComposerLabelWidget: public QWidget, private Ui::QgsComposerLabelWidget
void on_mTopRadioButton_clicked();
void on_mBottomRadioButton_clicked();
void on_mMiddleRadioButton_clicked();
void on_mLabelIdLineEdit_textChanged( const QString& text );
void on_mRotationSpinBox_valueChanged( double v );

private slots:
Expand Down
21 changes: 19 additions & 2 deletions src/core/composer/qgscomposeritem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsView>
#include <QPainter>
#include <QUuid>

#include "qgsproject.h"

#include "qgscomposition.h"
#include "qgscomposeritem.h"
#include "qgscomposerframe.h"


#include <limits>
#include "qgsapplication.h"
#include "qgsrectangle.h" //just for debugging
Expand All @@ -51,6 +53,8 @@ QgsComposerItem::QgsComposerItem( QgsComposition* composition, bool manageZValue
, mLastValidViewScaleFactor( -1 )
, mRotation( 0 )
, mLastUsedPositionMode( UpperLeft )
, mId( "" )
, mUuid( QUuid::createUuid().toString() )
{
init( manageZValue );
}
Expand All @@ -68,6 +72,8 @@ QgsComposerItem::QgsComposerItem( qreal x, qreal y, qreal width, qreal height, Q
, mLastValidViewScaleFactor( -1 )
, mRotation( 0 )
, mLastUsedPositionMode( UpperLeft )
, mId( "" )
, mUuid( QUuid::createUuid().toString() )
{
init( manageZValue );
QTransform t;
Expand Down Expand Up @@ -153,6 +159,7 @@ bool QgsComposerItem::_writeXML( QDomElement& itemElem, QDomDocument& doc ) cons
composerItemElem.setAttribute( "zValue", QString::number( zValue() ) );
composerItemElem.setAttribute( "outlineWidth", QString::number( pen().widthF() ) );
composerItemElem.setAttribute( "rotation", QString::number( mRotation ) );
composerItemElem.setAttribute( "uuid", mUuid );
composerItemElem.setAttribute( "id", mId );
//position lock for mouse moves/resizes
if ( mItemPositionLocked )
Expand Down Expand Up @@ -201,8 +208,12 @@ bool QgsComposerItem::_readXML( const QDomElement& itemElem, const QDomDocument&
//rotation
mRotation = itemElem.attribute( "rotation", "0" ).toDouble();

//uuid
mUuid = itemElem.attribute( "uuid", QUuid::createUuid().toString() );

//id
mId = itemElem.attribute( "id", "" );
QString id = itemElem.attribute( "id", "" );
setId(id);

//frame
QString frame = itemElem.attribute( "frame" );
Expand Down Expand Up @@ -1255,3 +1266,9 @@ void QgsComposerItem::repaint()
{
update();
}

void QgsComposerItem::setId( const QString& id )
{
setToolTip( id );
mId = id;
}
19 changes: 12 additions & 7 deletions src/core/composer/qgscomposeritem.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,18 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
/**Updates item, with the possibility to do custom update for subclasses*/
virtual void updateItem() { QGraphicsRectItem::update(); }

/**Get item identification name
/**Get item's id (which is not necessarly unique)
@note this method was added in version 1.7*/
QString id() const { return mId; }

/**Set item identification name
@note this method was added in version 1.7
This method was moved from qgscomposerlabel so that every object can have a
id (NathanW)*/
void setId( const QString& id ) { mId = id; }
/**Set item's id (which is not necessarly unique)
@note this method was added in version 1.7*/
virtual void setId( const QString& id );

/**Get item identification name
@note this method was added in version 2.0
@note there is not setter since one can't manually set the id*/
QString uuid() const { return mUuid; }

public slots:
virtual void setRotation( double r );
Expand Down Expand Up @@ -392,8 +395,10 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
/**Emitted if the rectangle changes*/
void sizeChanged();
private:
// Label id (unique within the same composition)
// id (not unique)
QString mId;
// name (unique)
QString mUuid;

void init( bool manageZValue );
};
Expand Down
66 changes: 66 additions & 0 deletions src/core/composer/qgscomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,61 @@ const QgsComposerItem* QgsComposition::getComposerItemById( QString theId ) cons
}
return 0;
}
/*
const QgsComposerItem* QgsComposition::getComposerItemByUuid( QString theUuid, bool inAllComposers ) const
{
//This does not work since it seems impossible to get the QgisApp::instance() from here... Is there a workaround ?
QSet<QgsComposer*> composers = QSet<QgsComposer*>();
if( inAllComposers )
{
composers = QgisApp::instance()->printComposers();
}
else
{
composers.insert( this )
}
QSet<QgsComposer*>::const_iterator it = composers.constBegin();
for ( ; it != composers.constEnd(); ++it )
{
QList<QGraphicsItem *> itemList = ( *it )->items();
QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
for ( ; itemIt != itemList.end(); ++itemIt )
{
const QgsComposerItem* mypItem = dynamic_cast<const QgsComposerItem *>( *itemIt );
if ( mypItem )
{
if ( mypItem->uuid() == theUuid )
{
return mypItem;
}
}
}
}
return 0;
}
*/

const QgsComposerItem* QgsComposition::getComposerItemByUuid( QString theUuid ) const
{
QList<QGraphicsItem *> itemList = items();
QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
for ( ; itemIt != itemList.end(); ++itemIt )
{
const QgsComposerItem* mypItem = dynamic_cast<const QgsComposerItem *>( *itemIt );
if ( mypItem )
{
if ( mypItem->uuid() == theUuid )
{
return mypItem;
}
}
}

return 0;
}

int QgsComposition::pixelFontSize( double pointSize ) const
{
Expand Down Expand Up @@ -436,6 +491,17 @@ bool QgsComposition::loadFromTemplate( const QDomDocument& doc, QMap<QString, QS
return false;
}

// remove all uuid attributes since we don't want duplicates UUIDS
QDomNodeList composerItemsNodes = importDoc.elementsByTagName("ComposerItem");
for (int i=0; i<composerItemsNodes.count(); ++i)
{
QDomNode composerItemNode = composerItemsNodes.at(i);
if( composerItemNode.isElement() )
{
composerItemNode.toElement().removeAttribute("uuid");
}
}

//addItemsFromXML
addItemsFromXML( importDoc.documentElement(), importDoc, 0, addUndoCommands, 0 );

Expand Down
13 changes: 13 additions & 0 deletions src/core/composer/qgscomposition.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "qgscomposeritemcommand.h"
#include "qgsatlascomposition.h"

class QgisApp;
class QgsComposerFrame;
class QgsComposerItem;
class QgsComposerMap;
Expand All @@ -50,6 +51,7 @@ class QgsComposerAttributeTable;
class QgsComposerMultiFrame;
class QgsComposerMultiFrameCommand;
class QgsVectorLayer;
class QgsComposer;

/** \ingroup MapComposer
* Graphics scene for map printing. The class manages the paper item which always
Expand Down Expand Up @@ -158,13 +160,24 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
const QgsComposerHtml* getComposerHtmlByItem( QgsComposerItem *item ) const;

/**Returns a composer item given its text identifier.
Ids are not necessarely unique, but this function returns only one element.
@note added in 2.0
@param theId - A QString representing the identifier of the item to
retrieve.
@return QgsComposerItem pointer or 0 pointer if no such item exists.
**/
const QgsComposerItem* getComposerItemById( QString theId ) const;

/**Returns a composer item given its unique identifier.
@note added in 2.0
@param theId - A QString representing the UUID of the item to
@param inAllComposers - Whether the search should be done in all composers of the project
retrieve.
@return QgsComposerItem pointer or 0 pointer if no such item exists.
**/
//const QgsComposerItem* getComposerItemByUuid( QString theUuid, bool inAllComposers = false ) const;//does not work since it's impossible to get QGisApp::instance()
const QgsComposerItem* getComposerItemByUuid( QString theUuid ) const;

int printResolution() const {return mPrintResolution;}
void setPrintResolution( int dpi ) {mPrintResolution = dpi;}

Expand Down
16 changes: 16 additions & 0 deletions src/gui/qgscomposerview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,22 @@ void QgsComposerView::keyPressEvent( QKeyEvent * e )
}
}
doc.appendChild( documentElement );

//if it's a copy, we have to remove the UUIDs since we don't want any duplicate UUID
if ( e->matches( QKeySequence::Copy ) )
{
// remove all uuid attributes
QDomNodeList composerItemsNodes = doc.elementsByTagName("ComposerItem");
for (int i=0; i<composerItemsNodes.count(); ++i)
{
QDomNode composerItemNode = composerItemsNodes.at(i);
if( composerItemNode.isElement() )
{
composerItemNode.toElement().removeAttribute("uuid");
}
}
}

QMimeData *mimeData = new QMimeData;
mimeData->setData( "text/xml", doc.toByteArray() );
QClipboard *clipboard = QApplication::clipboard();
Expand Down
Loading