Skip to content

Commit

Permalink
Implemented ComposerItem's Uuids
Browse files Browse the repository at this point in the history
- uuid are kept on cut/paste, but deleted on import template or copy/paste
- removed obsolete id reference in QgsComposerLabelWidget
  • Loading branch information
olivierdalang authored and mhugent committed Mar 15, 2013
1 parent b513674 commit 00c832b
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 31 deletions.
13 changes: 8 additions & 5 deletions python/core/composer/qgscomposeritem.sip
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
9 changes: 7 additions & 2 deletions src/app/composer/qgscomposeritemwidget.cpp
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
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
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
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
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
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
11 changes: 11 additions & 0 deletions src/core/composer/qgscomposition.cpp
Expand Up @@ -436,6 +436,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
16 changes: 16 additions & 0 deletions src/gui/qgscomposerview.cpp
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
20 changes: 17 additions & 3 deletions src/ui/qgscomposeritemwidgetbase.ui
Expand Up @@ -351,16 +351,30 @@
<property name="labelAlignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
<item row="0" column="0">
<widget class="QLabel" name="mIdLabel">
<item row="1" column="0">
<widget class="QLabel" name="mItemUuidLabel">
<property name="text">
<string>Item ID</string>
<string>Uuid (read-only)</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="mItemUuidLineEdit">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="mItemIdLineEdit"/>
</item>
<item row="0" column="0">
<widget class="QLabel" name="mItemIdLabel">
<property name="text">
<string>Id</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down

0 comments on commit 00c832b

Please sign in to comment.