Skip to content

Commit

Permalink
Apply patch #2868 (adds label alignment) with modifications. Thanks m…
Browse files Browse the repository at this point in the history
…edspx

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13938 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Jul 19, 2010
1 parent 014f568 commit 6bf395d
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 11 deletions.
59 changes: 59 additions & 0 deletions src/app/composer/qgscomposerlabelwidget.cpp
Expand Up @@ -34,6 +34,12 @@ QgsComposerLabelWidget::QgsComposerLabelWidget( QgsComposerLabel* label ): QWidg
{
mTextEdit->setText( mComposerLabel->text() );
mMarginDoubleSpinBox->setValue( mComposerLabel->margin() );
mTopRadioButton->setChecked( mComposerLabel->vAlign() == Qt::AlignTop );
mMiddleRadioButton->setChecked( mComposerLabel->vAlign() == Qt::AlignVCenter );
mBottomRadioButton->setChecked( mComposerLabel->vAlign() == Qt::AlignBottom );
mLeftRadioButton->setChecked( mComposerLabel->hAlign() == Qt::AlignLeft );
mCenterRadioButton->setChecked( mComposerLabel->hAlign() == Qt::AlignHCenter );
mRightRadioButton->setChecked( mComposerLabel->hAlign() == Qt::AlignRight );
}
}

Expand Down Expand Up @@ -88,3 +94,56 @@ void QgsComposerLabelWidget::on_mFontColorButton_clicked()
mComposerLabel->setFontColor( newColor );
}

void QgsComposerLabelWidget::on_mCenterRadioButton_clicked()
{
if ( mComposerLabel )
{
mComposerLabel->setHAlign( Qt::AlignHCenter );
mComposerLabel->update();
}
}

void QgsComposerLabelWidget::on_mRightRadioButton_clicked()
{
if ( mComposerLabel )
{
mComposerLabel->setHAlign( Qt::AlignRight );
mComposerLabel->update();
}
}

void QgsComposerLabelWidget::on_mLeftRadioButton_clicked()
{
if ( mComposerLabel )
{
mComposerLabel->setHAlign( Qt::AlignLeft );
mComposerLabel->update();
}
}

void QgsComposerLabelWidget::on_mTopRadioButton_clicked()
{
if ( mComposerLabel )
{
mComposerLabel->setVAlign( Qt::AlignTop );
mComposerLabel->update();
}
}

void QgsComposerLabelWidget::on_mBottomRadioButton_clicked()
{
if ( mComposerLabel )
{
mComposerLabel->setVAlign( Qt::AlignBottom );
mComposerLabel->update();
}
}

void QgsComposerLabelWidget::on_mMiddleRadioButton_clicked()
{
if ( mComposerLabel )
{
mComposerLabel->setVAlign( Qt::AlignVCenter );
mComposerLabel->update();
}
}
6 changes: 6 additions & 0 deletions src/app/composer/qgscomposerlabelwidget.h
Expand Up @@ -36,6 +36,12 @@ class QgsComposerLabelWidget: public QWidget, private Ui::QgsComposerLabelWidget
void on_mFontButton_clicked();
void on_mMarginDoubleSpinBox_valueChanged( double d );
void on_mFontColorButton_clicked();
void on_mCenterRadioButton_clicked();
void on_mLeftRadioButton_clicked();
void on_mRightRadioButton_clicked();
void on_mTopRadioButton_clicked();
void on_mBottomRadioButton_clicked();
void on_mMiddleRadioButton_clicked();

private:
QgsComposerLabel* mComposerLabel;
Expand Down
5 changes: 2 additions & 3 deletions src/core/composer/qgscomposeritem.cpp
Expand Up @@ -706,7 +706,7 @@ void QgsComposerItem::drawText( QPainter* p, double x, double y, const QString&
p->restore();
}

void QgsComposerItem::drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font ) const
void QgsComposerItem::drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignement, Qt::AlignmentFlag valignment ) const
{
QFont textFont = scaledFontPixelSize( font );

Expand All @@ -717,10 +717,9 @@ void QgsComposerItem::drawText( QPainter* p, const QRectF& rect, const QString&
p->setFont( textFont );
double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
p->scale( scaleFactor, scaleFactor );
p->drawText( scaledRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, text );
p->drawText( scaledRect, halignement | valignment | Qt::TextWordWrap, text );
p->restore();
}

void QgsComposerItem::drawArrowHead( QPainter* p, double x, double y, double angle, double arrowHeadWidth ) const
{
if ( !p )
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposeritem.h
Expand Up @@ -143,7 +143,7 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font ) const;

/**Like the above, but with a rectangle for multiline text*/
void drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font ) const;
void drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignement = Qt::AlignLeft, Qt::AlignmentFlag valignement = Qt::AlignTop ) const;

/**Returns the font width in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
double textWidthMillimeters( const QFont& font, const QString& text ) const;
Expand Down
18 changes: 16 additions & 2 deletions src/core/composer/qgscomposerlabel.cpp
Expand Up @@ -20,7 +20,8 @@
#include <QDomElement>
#include <QPainter>

QgsComposerLabel::QgsComposerLabel( QgsComposition *composition ): QgsComposerItem( composition ), mMargin( 1.0 ), mFontColor( QColor( 0, 0, 0 ) )
QgsComposerLabel::QgsComposerLabel( QgsComposition *composition ): QgsComposerItem( composition ), mMargin( 1.0 ), mFontColor( QColor( 0, 0, 0 ) ), \
mHAlignment( Qt::AlignLeft ), mVAlignment( Qt::AlignTop )
{
//default font size is 10 point
mFont.setPointSizeF( 10 );
Expand Down Expand Up @@ -49,7 +50,7 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
rect().height() - 2 * penWidth - 2 * mMargin );


drawText( painter, painterRect, displayText(), mFont );
drawText( painter, painterRect, displayText(), mFont, mHAlignment, mVAlignment );

drawFrame( painter );
if ( isSelected() )
Expand Down Expand Up @@ -112,6 +113,8 @@ QFont QgsComposerLabel::font() const

bool QgsComposerLabel::writeXML( QDomElement& elem, QDomDocument & doc ) const
{
QString alignment;

if ( elem.isNull() )
{
return false;
Expand All @@ -122,6 +125,9 @@ bool QgsComposerLabel::writeXML( QDomElement& elem, QDomDocument & doc ) const
composerLabelElem.setAttribute( "labelText", mText );
composerLabelElem.setAttribute( "margin", QString::number( mMargin ) );

composerLabelElem.setAttribute( "halign", mHAlignment );
composerLabelElem.setAttribute( "valign", mVAlignment );


//font
QDomElement labelFontElem = doc.createElement( "LabelFont" );
Expand All @@ -141,6 +147,8 @@ bool QgsComposerLabel::writeXML( QDomElement& elem, QDomDocument & doc ) const

bool QgsComposerLabel::readXML( const QDomElement& itemElem, const QDomDocument& doc )
{
QString alignment;

if ( itemElem.isNull() )
{
return false;
Expand All @@ -154,6 +162,12 @@ bool QgsComposerLabel::readXML( const QDomElement& itemElem, const QDomDocument&
//margin
mMargin = itemElem.attribute( "margin" ).toDouble();

//Horizontal alignment
mHAlignment = ( Qt::AlignmentFlag )( itemElem.attribute( "halign" ).toInt() );

//Vertical alignment
mVAlignment = ( Qt::AlignmentFlag )( itemElem.attribute( "valign" ).toInt() );

//font
QDomNodeList labelFontList = itemElem.elementsByTagName( "LabelFont" );
if ( labelFontList.size() > 0 )
Expand Down
10 changes: 10 additions & 0 deletions src/core/composer/qgscomposerlabel.h
Expand Up @@ -43,6 +43,10 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem

QFont font() const;
void setFont( const QFont& f );
Qt::AlignmentFlag vAlign() const { return mVAlignment; }
Qt::AlignmentFlag hAlign() const { return mHAlignment; }
void setHAlign( Qt::AlignmentFlag a ) {mHAlignment = a;}
void setVAlign( Qt::AlignmentFlag a ) {mVAlignment = a;}
double margin() {return mMargin;}
void setMargin( double m ) {mMargin = m;}

Expand Down Expand Up @@ -77,6 +81,12 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
// Font color
QColor mFontColor;

// Horizontal Alignment
Qt::AlignmentFlag mHAlignment;

// Vertical Alignment
Qt::AlignmentFlag mVAlignment;

/**Replaces replace '$CURRENT_DATE<(FORMAT)>' with the current date (e.g. $CURRENT_DATE(d 'June' yyyy)*/
void replaceDateText( QString& text ) const;
};
Expand Down
73 changes: 68 additions & 5 deletions src/ui/qgscomposerlabelwidgetbase.ui
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>203</width>
<width>519</width>
<height>362</height>
</rect>
</property>
Expand All @@ -30,8 +30,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>185</width>
<height>317</height>
<width>486</width>
<height>320</height>
</rect>
</property>
<attribute name="label">
Expand Down Expand Up @@ -65,7 +65,7 @@
</property>
</widget>
</item>
<item row="3" column="0">
<item row="5" column="0">
<widget class="QLabel" name="mMarginTextLabel">
<property name="text">
<string>Margin (mm)</string>
Expand All @@ -75,9 +75,72 @@
</property>
</widget>
</item>
<item row="4" column="0">
<item row="6" column="0">
<widget class="QDoubleSpinBox" name="mMarginDoubleSpinBox"/>
</item>
<item row="3" column="0">
<widget class="QGroupBox" name="buttonGroup1">
<property name="title">
<string>Horizontal Alignment:</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum>
</property>
<item>
<widget class="QRadioButton" name="mLeftRadioButton">
<property name="text">
<string>Left</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="mCenterRadioButton">
<property name="text">
<string>Center</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="mRightRadioButton">
<property name="text">
<string>Right</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="4" column="0">
<widget class="QGroupBox" name="buttonGroup2">
<property name="title">
<string>Vertical Alignment:</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QRadioButton" name="mTopRadioButton">
<property name="text">
<string>Top</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="mMiddleRadioButton">
<property name="text">
<string>Middle</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="mBottomRadioButton">
<property name="text">
<string>Bottom</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
Expand Down

0 comments on commit 6bf395d

Please sign in to comment.