Skip to content

Commit b6de272

Browse files
author
mhugent
committed
Added possibility to set label font color
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12204 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 017bca2 commit b6de272

12 files changed

+89
-19
lines changed

python/core/qgscomposerlabel.sip

+8-1
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,19 @@ class QgsComposerLabel: QgsComposerItem
2222
/**Returns the text as it appears on screen (with replaced data field)
2323
@note this function was added in version 1.2*/
2424
QString displayText() const;
25-
25+
/**Get font color
26+
@note: this function was added in version 1.4*/
2627
QFont font() const;
2728
void setFont( const QFont& f );
2829
double margin();
2930
void setMargin( double m );
3031

32+
/**Sets text color
33+
@note: this function was added in version 1.4*/
34+
void setFontColor( const QColor& c);
35+
36+
QColor fontColor() const;
37+
3138
/** stores state in Dom node
3239
* @param node is Dom node corresponding to 'Composer' tag
3340
* @param temp write template file

src/app/composer/qgscomposerlabelwidget.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "qgscomposerlabelwidget.h"
1919
#include "qgscomposerlabel.h"
2020
#include "qgscomposeritemwidget.h"
21+
#include <QColorDialog>
2122
#include <QFontDialog>
2223
#include <QWidget>
2324

@@ -73,3 +74,17 @@ void QgsComposerLabelWidget::on_mMarginDoubleSpinBox_valueChanged( double d )
7374
}
7475
}
7576

77+
void QgsComposerLabelWidget::on_mFontColorButton_clicked()
78+
{
79+
if ( !mComposerLabel )
80+
{
81+
return;
82+
}
83+
QColor newColor = QColorDialog::getColor( mComposerLabel->fontColor() );
84+
if ( !newColor.isValid() )
85+
{
86+
return;
87+
}
88+
mComposerLabel->setFontColor( newColor );
89+
}
90+

src/app/composer/qgscomposerlabelwidget.h

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class QgsComposerLabelWidget: public QWidget, private Ui::QgsComposerLabelWidget
3535
void on_mTextEdit_textChanged();
3636
void on_mFontButton_clicked();
3737
void on_mMarginDoubleSpinBox_valueChanged( double d );
38+
void on_mFontColorButton_clicked();
3839

3940
private:
4041
QgsComposerLabel* mComposerLabel;

src/core/composer/qgscomposeritem.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,6 @@ void QgsComposerItem::drawText( QPainter* p, const QRectF& rect, const QString&
715715

716716
p->save();
717717
p->setFont( textFont );
718-
p->setPen( QColor( 0, 0, 0 ) ); //draw text always in black
719718
double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
720719
p->scale( scaleFactor, scaleFactor );
721720
p->drawText( scaledRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, text );

src/core/composer/qgscomposerlabel.cpp

+24-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <QDomElement>
2121
#include <QPainter>
2222

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

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

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

131+
//font color
132+
QDomElement fontColorElem = doc.createElement( "FontColor" );
133+
fontColorElem.setAttribute( "red", mFontColor.red() );
134+
fontColorElem.setAttribute( "green", mFontColor.green() );
135+
fontColorElem.setAttribute( "blue", mFontColor.blue() );
136+
composerLabelElem.appendChild( fontColorElem );
137+
131138
elem.appendChild( composerLabelElem );
132139
return _writeXML( composerLabelElem, doc );
133140
}
@@ -155,6 +162,21 @@ bool QgsComposerLabel::readXML( const QDomElement& itemElem, const QDomDocument&
155162
mFont.fromString( labelFontElem.attribute( "description" ) );
156163
}
157164

165+
//font color
166+
QDomNodeList fontColorList = itemElem.elementsByTagName( "FontColor" );
167+
if ( fontColorList.size() > 0 )
168+
{
169+
QDomElement fontColorElem = fontColorList.at( 0 ).toElement();
170+
int red = fontColorElem.attribute( "red", "0" ).toInt();
171+
int green = fontColorElem.attribute( "green", "0" ).toInt();
172+
int blue = fontColorElem.attribute( "blue", "0" ).toInt();
173+
mFontColor = QColor( red, green, blue );
174+
}
175+
else
176+
{
177+
mFontColor = QColor( 0, 0, 0 );
178+
}
179+
158180
//restore general composer item properties
159181
QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
160182
if ( composerItemList.size() > 0 )

src/core/composer/qgscomposerlabel.h

+10
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
4646
double margin() {return mMargin;}
4747
void setMargin( double m ) {mMargin = m;}
4848

49+
/**Sets text color
50+
@note: this function was added in version 1.4*/
51+
void setFontColor( const QColor& c ) {mFontColor = c;}
52+
/**Get font color
53+
@note: this function was added in version 1.4*/
54+
QColor fontColor() const {return mFontColor;}
55+
4956
/** stores state in Dom node
5057
* @param node is Dom node corresponding to 'Composer' tag
5158
* @param temp write template file
@@ -64,6 +71,9 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
6471
// Font
6572
QFont mFont;
6673

74+
// Font color
75+
QColor mFontColor;
76+
6777
// Border between text and fram (in mm)
6878
double mMargin;
6979

src/core/composer/qgscomposerlegend.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
8888
currentYCoordinate += fontAscentMillimeters( mTitleFont );
8989
if ( painter )
9090
{
91+
painter->setPen( QColor( 0, 0, 0 ) );
9192
drawText( painter, mBoxSpace, currentYCoordinate, mTitle, mTitleFont );
9293
}
9394

@@ -126,6 +127,7 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
126127
//draw layer Item
127128
if ( painter )
128129
{
130+
painter->setPen( QColor( 0, 0, 0 ) );
129131
drawText( painter, mBoxSpace, currentYCoordinate, currentLayerItem->text(), mLayerFont );
130132
}
131133
}
@@ -237,6 +239,7 @@ void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerIt
237239
//finally draw text
238240
if ( p )
239241
{
242+
p->setPen( QColor( 0, 0, 0 ) );
240243
drawText( p, currentXCoord, currentYCoord + fontAscentMillimeters( mItemFont ) + ( realItemHeight - fontAscentMillimeters( mItemFont ) ) / 2, currentItem->text(), mItemFont );
241244
}
242245

src/core/composer/qgscomposermap.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,7 @@ void QgsComposerMap::drawAnnotation( QPainter* p, const QPointF& pos, int rotati
10311031
p->save();
10321032
p->translate( pos );
10331033
p->rotate( rotation );
1034+
p->setPen( QColor( 0, 0, 0 ) );
10341035
drawText( p, 0, 0, annotationText, mGridAnnotationFont );
10351036
p->restore();
10361037
}

src/core/composer/qgscomposershape.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ QgsComposerShape::~QgsComposerShape()
3636

3737
void QgsComposerShape::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
3838
{
39+
if ( !painter )
40+
{
41+
return;
42+
}
43+
drawBackground( painter );
44+
3945
double width = rect().width();
4046
double height = rect().height();
4147
imageSizeConsideringRotation( width, height );

src/core/composer/qgsnumericscalebarstyle.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void QgsNumericScaleBarStyle::draw( QPainter* p, double xOffset ) const
4949

5050
p->save();
5151
p->setFont( mScaleBar->font() );
52-
52+
p->setPen( QColor( 0, 0, 0 ) );
5353
mScaleBar->drawText( p, mScaleBar->pen().widthF() + mScaleBar->boxContentSpace(), mScaleBar->boxContentSpace() + mScaleBar->fontAscentMillimeters( mScaleBar->font() ), scaleText(), mScaleBar->font() );
5454

5555
p->restore();

src/core/composer/qgsscalebarstyle.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ void QgsScaleBarStyle::drawLabels( QPainter* p ) const
7878

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

@@ -92,6 +93,7 @@ void QgsScaleBarStyle::drawLabels( QPainter* p ) const
9293
if ( !segmentInfo.isEmpty() )
9394
{
9495
currentNumericLabel = QString::number( currentLabelNumber / mScaleBar->numMapUnitsPerScaleBarUnit() );
96+
p->setPen( QColor( 0, 0, 0 ) );
9597
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() );
9698
}
9799

src/ui/qgscomposerlabelwidgetbase.ui

+18-14
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<x>0</x>
88
<y>0</y>
99
<width>203</width>
10-
<height>303</height>
10+
<height>362</height>
1111
</rect>
1212
</property>
1313
<property name="sizePolicy">
@@ -20,9 +20,6 @@
2020
<string>Label Options</string>
2121
</property>
2222
<layout class="QGridLayout" name="gridLayout_2">
23-
<property name="margin">
24-
<number>0</number>
25-
</property>
2623
<item row="0" column="0">
2724
<widget class="QToolBox" name="toolBox">
2825
<property name="currentIndex">
@@ -33,14 +30,21 @@
3330
<rect>
3431
<x>0</x>
3532
<y>0</y>
36-
<width>203</width>
37-
<height>271</height>
33+
<width>185</width>
34+
<height>318</height>
3835
</rect>
3936
</property>
4037
<attribute name="label">
4138
<string>Label</string>
4239
</attribute>
4340
<layout class="QGridLayout" name="gridLayout">
41+
<item row="0" column="0">
42+
<widget class="QTextEdit" name="mTextEdit">
43+
<property name="lineWrapMode">
44+
<enum>QTextEdit::NoWrap</enum>
45+
</property>
46+
</widget>
47+
</item>
4448
<item row="1" column="0">
4549
<widget class="QPushButton" name="mFontButton">
4650
<property name="sizePolicy">
@@ -55,6 +59,13 @@
5559
</widget>
5660
</item>
5761
<item row="2" column="0">
62+
<widget class="QPushButton" name="mFontColorButton">
63+
<property name="text">
64+
<string>Font color...</string>
65+
</property>
66+
</widget>
67+
</item>
68+
<item row="3" column="0">
5869
<widget class="QLabel" name="mMarginTextLabel">
5970
<property name="text">
6071
<string>Margin (mm)</string>
@@ -64,16 +75,9 @@
6475
</property>
6576
</widget>
6677
</item>
67-
<item row="3" column="0">
78+
<item row="4" column="0">
6879
<widget class="QDoubleSpinBox" name="mMarginDoubleSpinBox"/>
6980
</item>
70-
<item row="0" column="0">
71-
<widget class="QTextEdit" name="mTextEdit">
72-
<property name="lineWrapMode">
73-
<enum>QTextEdit::NoWrap</enum>
74-
</property>
75-
</widget>
76-
</item>
7781
</layout>
7882
</widget>
7983
</widget>

0 commit comments

Comments
 (0)