Skip to content

Commit

Permalink
[FEATURE][composer] Allow negative margins for label items
Browse files Browse the repository at this point in the history
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 31, 2014
1 parent a2a094b commit a57080c
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 3 deletions.
2 changes: 1 addition & 1 deletion python/core/composer/qgscomposeritem.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions python/core/composer/qgscomposerlabel.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposeritem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 36 additions & 1 deletion src/core/composer/qgscomposerlabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/core/composer/qgscomposerlabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
6 changes: 6 additions & 0 deletions src/ui/qgscomposerlabelwidgetbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down Expand Up @@ -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">
Expand Down

0 comments on commit a57080c

Please sign in to comment.