Skip to content

Commit

Permalink
Added possibility to set label font color
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@12204 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Nov 20, 2009
1 parent 7f7de2f commit 49197a3
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 19 deletions.
9 changes: 8 additions & 1 deletion python/core/qgscomposerlabel.sip
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,19 @@ class QgsComposerLabel: QgsComposerItem
/**Returns the text as it appears on screen (with replaced data field)
@note this function was added in version 1.2*/
QString displayText() const;

/**Get font color
@note: this function was added in version 1.4*/
QFont font() const;
void setFont( const QFont& f );
double margin();
void setMargin( double m );

/**Sets text color
@note: this function was added in version 1.4*/
void setFontColor( const QColor& c);

QColor fontColor() const;

/** stores state in Dom node
* @param node is Dom node corresponding to 'Composer' tag
* @param temp write template file
Expand Down
15 changes: 15 additions & 0 deletions src/app/composer/qgscomposerlabelwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "qgscomposerlabelwidget.h"
#include "qgscomposerlabel.h"
#include "qgscomposeritemwidget.h"
#include <QColorDialog>
#include <QFontDialog>
#include <QWidget>

Expand Down Expand Up @@ -73,3 +74,17 @@ void QgsComposerLabelWidget::on_mMarginDoubleSpinBox_valueChanged( double d )
}
}

void QgsComposerLabelWidget::on_mFontColorButton_clicked()
{
if ( !mComposerLabel )
{
return;
}
QColor newColor = QColorDialog::getColor( mComposerLabel->fontColor() );
if ( !newColor.isValid() )
{
return;
}
mComposerLabel->setFontColor( newColor );
}

1 change: 1 addition & 0 deletions src/app/composer/qgscomposerlabelwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class QgsComposerLabelWidget: public QWidget, private Ui::QgsComposerLabelWidget
void on_mTextEdit_textChanged();
void on_mFontButton_clicked();
void on_mMarginDoubleSpinBox_valueChanged( double d );
void on_mFontColorButton_clicked();

private:
QgsComposerLabel* mComposerLabel;
Expand Down
1 change: 0 additions & 1 deletion src/core/composer/qgscomposeritem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,6 @@ void QgsComposerItem::drawText( QPainter* p, const QRectF& rect, const QString&

p->save();
p->setFont( textFont );
p->setPen( QColor( 0, 0, 0 ) ); //draw text always in black
double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
p->scale( scaleFactor, scaleFactor );
p->drawText( scaledRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, text );
Expand Down
26 changes: 24 additions & 2 deletions src/core/composer/qgscomposerlabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <QDomElement>
#include <QPainter>

QgsComposerLabel::QgsComposerLabel( QgsComposition *composition ): QgsComposerItem( composition ), mMargin( 1.0 )
QgsComposerLabel::QgsComposerLabel( QgsComposition *composition ): QgsComposerItem( composition ), mMargin( 1.0 ), mFontColor( QColor( 0, 0, 0 ) )
{
//default font size is 10 point
mFont.setPointSizeF( 10 );
Expand All @@ -38,7 +38,7 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
}

drawBackground( painter );
painter->setPen( QPen( QColor( 0, 0, 0 ) ) ); //draw all text black
painter->setPen( QPen( QColor( mFontColor ) ) ); //draw all text black
painter->setFont( mFont );

QFontMetricsF fontSize( mFont );
Expand Down Expand Up @@ -128,6 +128,13 @@ bool QgsComposerLabel::writeXML( QDomElement& elem, QDomDocument & doc ) const
labelFontElem.setAttribute( "description", mFont.toString() );
composerLabelElem.appendChild( labelFontElem );

//font color
QDomElement fontColorElem = doc.createElement( "FontColor" );
fontColorElem.setAttribute( "red", mFontColor.red() );
fontColorElem.setAttribute( "green", mFontColor.green() );
fontColorElem.setAttribute( "blue", mFontColor.blue() );
composerLabelElem.appendChild( fontColorElem );

elem.appendChild( composerLabelElem );
return _writeXML( composerLabelElem, doc );
}
Expand Down Expand Up @@ -155,6 +162,21 @@ bool QgsComposerLabel::readXML( const QDomElement& itemElem, const QDomDocument&
mFont.fromString( labelFontElem.attribute( "description" ) );
}

//font color
QDomNodeList fontColorList = itemElem.elementsByTagName( "FontColor" );
if ( fontColorList.size() > 0 )
{
QDomElement fontColorElem = fontColorList.at( 0 ).toElement();
int red = fontColorElem.attribute( "red", "0" ).toInt();
int green = fontColorElem.attribute( "green", "0" ).toInt();
int blue = fontColorElem.attribute( "blue", "0" ).toInt();
mFontColor = QColor( red, green, blue );
}
else
{
mFontColor = QColor( 0, 0, 0 );
}

//restore general composer item properties
QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
if ( composerItemList.size() > 0 )
Expand Down
10 changes: 10 additions & 0 deletions src/core/composer/qgscomposerlabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
double margin() {return mMargin;}
void setMargin( double m ) {mMargin = m;}

/**Sets text color
@note: this function was added in version 1.4*/
void setFontColor( const QColor& c ) {mFontColor = c;}
/**Get font color
@note: this function was added in version 1.4*/
QColor fontColor() const {return mFontColor;}

/** stores state in Dom node
* @param node is Dom node corresponding to 'Composer' tag
* @param temp write template file
Expand All @@ -64,6 +71,9 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
// Font
QFont mFont;

// Font color
QColor mFontColor;

// Border between text and fram (in mm)
double mMargin;

Expand Down
3 changes: 3 additions & 0 deletions src/core/composer/qgscomposerlegend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
currentYCoordinate += fontAscentMillimeters( mTitleFont );
if ( painter )
{
painter->setPen( QColor( 0, 0, 0 ) );
drawText( painter, mBoxSpace, currentYCoordinate, mTitle, mTitleFont );
}

Expand Down Expand Up @@ -126,6 +127,7 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
//draw layer Item
if ( painter )
{
painter->setPen( QColor( 0, 0, 0 ) );
drawText( painter, mBoxSpace, currentYCoordinate, currentLayerItem->text(), mLayerFont );
}
}
Expand Down Expand Up @@ -237,6 +239,7 @@ void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerIt
//finally draw text
if ( p )
{
p->setPen( QColor( 0, 0, 0 ) );
drawText( p, currentXCoord, currentYCoord + fontAscentMillimeters( mItemFont ) + ( realItemHeight - fontAscentMillimeters( mItemFont ) ) / 2, currentItem->text(), mItemFont );
}

Expand Down
1 change: 1 addition & 0 deletions src/core/composer/qgscomposermap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,7 @@ void QgsComposerMap::drawAnnotation( QPainter* p, const QPointF& pos, int rotati
p->save();
p->translate( pos );
p->rotate( rotation );
p->setPen( QColor( 0, 0, 0 ) );
drawText( p, 0, 0, annotationText, mGridAnnotationFont );
p->restore();
}
Expand Down
6 changes: 6 additions & 0 deletions src/core/composer/qgscomposershape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ QgsComposerShape::~QgsComposerShape()

void QgsComposerShape::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
{
if ( !painter )
{
return;
}
drawBackground( painter );

double width = rect().width();
double height = rect().height();
imageSizeConsideringRotation( width, height );
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgsnumericscalebarstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void QgsNumericScaleBarStyle::draw( QPainter* p, double xOffset ) const

p->save();
p->setFont( mScaleBar->font() );

p->setPen( QColor( 0, 0, 0 ) );
mScaleBar->drawText( p, mScaleBar->pen().widthF() + mScaleBar->boxContentSpace(), mScaleBar->boxContentSpace() + mScaleBar->fontAscentMillimeters( mScaleBar->font() ), scaleText(), mScaleBar->font() );

p->restore();
Expand Down
2 changes: 2 additions & 0 deletions src/core/composer/qgsscalebarstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ void QgsScaleBarStyle::drawLabels( QPainter* p ) const

if ( segmentCounter == 0 || segmentCounter >= nSegmentsLeft ) //don't draw label for intermediate left segments
{
p->setPen( QColor( 0, 0, 0 ) );
mScaleBar->drawText( p, segmentIt->first - mScaleBar->textWidthMillimeters( mScaleBar->font(), currentNumericLabel ) / 2 + xOffset, mScaleBar->fontAscentMillimeters( mScaleBar->font() ) + mScaleBar->boxContentSpace(), currentNumericLabel, mScaleBar->font() );
}

Expand All @@ -92,6 +93,7 @@ void QgsScaleBarStyle::drawLabels( QPainter* p ) const
if ( !segmentInfo.isEmpty() )
{
currentNumericLabel = QString::number( currentLabelNumber / mScaleBar->numMapUnitsPerScaleBarUnit() );
p->setPen( QColor( 0, 0, 0 ) );
mScaleBar->drawText( p, segmentInfo.last().first + mScaleBar->segmentMillimeters() - mScaleBar->textWidthMillimeters( mScaleBar->font(), currentNumericLabel ) / 2 + xOffset, mScaleBar->fontAscentMillimeters( mScaleBar->font() ) + mScaleBar->boxContentSpace(), currentNumericLabel + " " + mScaleBar->unitLabeling(), mScaleBar->font() );
}

Expand Down
32 changes: 18 additions & 14 deletions src/ui/qgscomposerlabelwidgetbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>203</width>
<height>303</height>
<height>362</height>
</rect>
</property>
<property name="sizePolicy">
Expand All @@ -20,9 +20,6 @@
<string>Label Options</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<property name="margin">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QToolBox" name="toolBox">
<property name="currentIndex">
Expand All @@ -33,14 +30,21 @@
<rect>
<x>0</x>
<y>0</y>
<width>203</width>
<height>271</height>
<width>185</width>
<height>318</height>
</rect>
</property>
<attribute name="label">
<string>Label</string>
</attribute>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QTextEdit" name="mTextEdit">
<property name="lineWrapMode">
<enum>QTextEdit::NoWrap</enum>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QPushButton" name="mFontButton">
<property name="sizePolicy">
Expand All @@ -55,6 +59,13 @@
</widget>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="mFontColorButton">
<property name="text">
<string>Font color...</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="mMarginTextLabel">
<property name="text">
<string>Margin (mm)</string>
Expand All @@ -64,16 +75,9 @@
</property>
</widget>
</item>
<item row="3" column="0">
<item row="4" column="0">
<widget class="QDoubleSpinBox" name="mMarginDoubleSpinBox"/>
</item>
<item row="0" column="0">
<widget class="QTextEdit" name="mTextEdit">
<property name="lineWrapMode">
<enum>QTextEdit::NoWrap</enum>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
Expand Down

0 comments on commit 49197a3

Please sign in to comment.