Skip to content
Permalink
Browse files

[FEATURE][composer] Allow negative margins for label items

If negative margins are set for a label, the label contents will
begin outside the bounds of the label. This is desirable for
aligning label items with other items while allowing for optical
margin alignment for the label type.
  • Loading branch information
nyalldawson committed Oct 14, 2014
1 parent a2a094b commit a57080cc7159391b449a462798d5065e48875f86
@@ -285,7 +285,7 @@ class QgsComposerItem : QgsComposerObject, QGraphicsRectItem
* @see setFrameJoinStyle
* @see setFrameOutlineColor
*/
void setFrameEnabled( const bool drawFrame );
virtual void setFrameEnabled( const bool drawFrame );

/**Sets frame outline color
* @param color new color for outline frame
@@ -123,6 +123,19 @@ class QgsComposerLabel : QgsComposerItem
//Overriden to contain part of label's text
virtual QString displayName() const;

/**In case of negative margins, the bounding rect may be larger than the
* label's frame
*/
QRectF boundingRect() const;

/**Reimplemented to call prepareGeometryChange after toggling frame
*/
virtual void setFrameEnabled( const bool drawFrame );

/**Reimplemented to call prepareGeometryChange after changing outline width
*/
virtual void setFrameOutlineWidth( const double outlineWidth );

public slots:
/* Sets rotation for the label
* @deprecated Use setItemRotation( double rotation ) instead
@@ -238,7 +238,7 @@ class CORE_EXPORT QgsComposerItem: public QgsComposerObject, public QGraphicsRec
* @see setFrameJoinStyle
* @see setFrameOutlineColor
*/
void setFrameEnabled( const bool drawFrame );
virtual void setFrameEnabled( const bool drawFrame );

/**Sets frame outline color
* @param color new color for outline frame
@@ -105,7 +105,9 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
painter->setRenderHint( QPainter::Antialiasing, true );

double penWidth = hasFrame() ? ( pen().widthF() / 2.0 ) : 0;
QRectF painterRect( penWidth + mMarginX, penWidth + mMarginY, rect().width() - 2 * penWidth - 2 * mMarginX, rect().height() - 2 * penWidth - 2 * mMarginY );
double xPenAdjust = mMarginX < 0 ? -penWidth : penWidth;
double yPenAdjust = mMarginY < 0 ? -penWidth : penWidth;
QRectF painterRect( xPenAdjust + mMarginX, yPenAdjust + mMarginY, rect().width() - 2 * xPenAdjust - 2 * mMarginX, rect().height() - 2 * yPenAdjust - 2 * mMarginY );

QString textToDraw = displayText();

@@ -314,16 +316,19 @@ void QgsComposerLabel::setMargin( const double m )
{
mMarginX = m;
mMarginY = m;
prepareGeometryChange();
}

void QgsComposerLabel::setMarginX( const double margin )
{
mMarginX = margin;
prepareGeometryChange();
}

void QgsComposerLabel::setMarginY( const double margin )
{
mMarginY = margin;
prepareGeometryChange();
}

void QgsComposerLabel::adjustSizeToText()
@@ -492,6 +497,36 @@ QString QgsComposerLabel::displayName() const
}
}

QRectF QgsComposerLabel::boundingRect() const
{
QRectF rectangle = rect();
double penWidth = hasFrame() ? ( pen().widthF() / 2.0 ) : 0;
rectangle.adjust( -penWidth, -penWidth, penWidth, penWidth );

if ( mMarginX < 0 )
{
rectangle.adjust( mMarginX, 0, -mMarginX, 0 );
}
if ( mMarginY < 0 )
{
rectangle.adjust( 0, mMarginY, 0, -mMarginY );
}

return rectangle;
}

void QgsComposerLabel::setFrameEnabled( const bool drawFrame )
{
QgsComposerItem::setFrameEnabled( drawFrame );
prepareGeometryChange();
}

void QgsComposerLabel::setFrameOutlineWidth( const double outlineWidth )
{
QgsComposerItem::setFrameOutlineWidth( outlineWidth );
prepareGeometryChange();
}

void QgsComposerLabel::itemShiftAdjustSize( double newWidth, double newHeight, double& xShift, double& yShift ) const
{
//keep alignment point constant
@@ -146,6 +146,19 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
//Overriden to contain part of label's text
virtual QString displayName() const;

/**In case of negative margins, the bounding rect may be larger than the
* label's frame
*/
QRectF boundingRect() const;

/**Reimplemented to call prepareGeometryChange after toggling frame
*/
virtual void setFrameEnabled( const bool drawFrame );

/**Reimplemented to call prepareGeometryChange after changing outline width
*/
virtual void setFrameOutlineWidth( const double outlineWidth );

public slots:
void refreshExpressionContext();

@@ -179,6 +179,9 @@
<property name="suffix">
<string> mm</string>
</property>
<property name="minimum">
<double>-99.989999999999995</double>
</property>
</widget>
</item>
<item row="5" column="0" colspan="2">
@@ -303,6 +306,9 @@
<property name="suffix">
<string> mm</string>
</property>
<property name="minimum">
<double>-99.989999999999995</double>
</property>
</widget>
</item>
<item row="3" column="0">

0 comments on commit a57080c

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