Skip to content
Permalink
Browse files

Added possibility to set label font color

git-svn-id: http://svn.osgeo.org/qgis/trunk@12204 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent
mhugent committed Nov 20, 2009
1 parent 7f7de2f commit 49197a37e80529fd4ee9a3f0fec3e8ba523ecfac
@@ -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
@@ -18,6 +18,7 @@
#include "qgscomposerlabelwidget.h"
#include "qgscomposerlabel.h"
#include "qgscomposeritemwidget.h"
#include <QColorDialog>
#include <QFontDialog>
#include <QWidget>

@@ -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 );
}

@@ -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;
@@ -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 );
@@ -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 );
@@ -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 );
@@ -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 );
}
@@ -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 )
@@ -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
@@ -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;

@@ -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 );
}

@@ -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 );
}
}
@@ -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 );
}

@@ -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();
}
@@ -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 );
@@ -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();
@@ -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() );
}

@@ -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() );
}

@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>203</width>
<height>303</height>
<height>362</height>
</rect>
</property>
<property name="sizePolicy">
@@ -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">
@@ -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">
@@ -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>
@@ -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>

0 comments on commit 49197a3

Please sign in to comment.