Skip to content
Permalink
Browse files

Load and save custom snap lines in composer

  • Loading branch information
mhugent committed Mar 23, 2013
1 parent 66e98a1 commit 08e7267d38b163fb575ae3d618a18bbc9ecfed74
@@ -299,6 +299,19 @@ bool QgsComposition::writeXML( QDomElement& composerElem, QDomDocument& doc )
compositionElem.setAttribute( "snapGridOffsetX", QString::number( mSnapGridOffsetX ) );
compositionElem.setAttribute( "snapGridOffsetY", QString::number( mSnapGridOffsetY ) );

//custom snap lines
QList< QGraphicsLineItem* >::const_iterator snapLineIt = mSnapLines.constBegin();
for ( ; snapLineIt != mSnapLines.constEnd(); ++snapLineIt )
{
QDomElement snapLineElem = doc.createElement( "SnapLine" );
QLineF line = ( *snapLineIt )->line();
snapLineElem.setAttribute( "x1", QString::number( line.x1() ) );
snapLineElem.setAttribute( "y1", QString::number( line.y1() ) );
snapLineElem.setAttribute( "x2", QString::number( line.x2() ) );
snapLineElem.setAttribute( "y2", QString::number( line.y2() ) );
compositionElem.appendChild( snapLineElem );
}

compositionElem.setAttribute( "printResolution", mPrintResolution );
compositionElem.setAttribute( "printAsRaster", mPrintAsRaster );

@@ -368,6 +381,19 @@ bool QgsComposition::readXML( const QDomElement& compositionElem, const QDomDocu
mSnapGridOffsetX = compositionElem.attribute( "snapGridOffsetX" ).toDouble();
mSnapGridOffsetY = compositionElem.attribute( "snapGridOffsetY" ).toDouble();

//custom snap lines
QDomNodeList snapLineNodes = compositionElem.elementsByTagName( "SnapLine" );
for ( int i = 0; i < snapLineNodes.size(); ++i )
{
QDomElement snapLineElem = snapLineNodes.at( i ).toElement();
QGraphicsLineItem* snapItem = addSnapLine();
double x1 = snapLineElem.attribute( "x1" ).toDouble();
double y1 = snapLineElem.attribute( "y1" ).toDouble();
double x2 = snapLineElem.attribute( "x2" ).toDouble();
double y2 = snapLineElem.attribute( "y2" ).toDouble();
snapItem->setLine( x1, y1, x2, y2 );
}

mAlignmentSnap = compositionElem.attribute( "alignmentSnap", "1" ).toInt() == 0 ? false : true;
mAlignmentSnapTolerance = compositionElem.attribute( "alignmentSnapTolerance", "2.0" ).toDouble();

@@ -1063,10 +1089,17 @@ QPointF QgsComposition::alignPos( const QPointF& pos, const QgsComposerItem* exc
return result;
}

void QgsComposition::addSnapLine( QGraphicsLineItem* line )
QGraphicsLineItem* QgsComposition::addSnapLine()
{
addItem( line );
mSnapLines.push_back( line );
QGraphicsLineItem* item = new QGraphicsLineItem();
QPen linePen( Qt::SolidLine );
linePen.setColor( Qt::red );
linePen.setWidthF( 0.5 );
item->setPen( linePen );
item->setZValue( 100 );
addItem( item );
mSnapLines.push_back( item );
return item;
}

void QgsComposition::removeSnapLine( QGraphicsLineItem* line )
@@ -263,7 +263,7 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
QPointF alignPos( const QPointF& pos, const QgsComposerItem* excludeItem, double& alignX, double& alignY );

/**Add a custom snap line (can be horizontal or vertical)*/
void addSnapLine( QGraphicsLineItem* line );
QGraphicsLineItem* addSnapLine();
/**Remove custom snap line (and delete the object)*/
void removeSnapLine( QGraphicsLineItem* line );
/**Get nearest snap line*/
@@ -176,8 +176,7 @@ void QgsComposerRuler::mousePressEvent( QMouseEvent* event )
if ( !line )
{
//create new snap line
mLineSnapItem = createLineSnapItem();
mComposition->addSnapLine( mLineSnapItem );
mLineSnapItem = mComposition->addSnapLine();
}
else
{
@@ -244,14 +243,3 @@ void QgsComposerRuler::setSnapLinePosition( const QPointF& pos )
}
}
}

QGraphicsLineItem* QgsComposerRuler::createLineSnapItem()
{
QGraphicsLineItem* item = new QGraphicsLineItem();
QPen linePen( Qt::SolidLine );
linePen.setColor( Qt::red );
linePen.setWidthF( 0.5 );
item->setPen( linePen );
item->setZValue( 100 );
return item;
}
@@ -43,7 +43,6 @@ class QgsComposerRuler: public QWidget
QList< QPair< QgsComposerItem*, QgsComposerItem::ItemPositionMode > > mSnappedItems;

void setSnapLinePosition( const QPointF& pos );
static QGraphicsLineItem* createLineSnapItem();
};

#endif // QGSCOMPOSERRULER_H

0 comments on commit 08e7267

Please sign in to comment.
You can’t perform that action at this time.