Skip to content

Commit

Permalink
[FEATURE][composer] Allow setting attribute table header and content …
Browse files Browse the repository at this point in the history
…font colors. Fix saving alpha channel for grid colors.
  • Loading branch information
nyalldawson committed Jul 17, 2014
1 parent 025741f commit e696158
Show file tree
Hide file tree
Showing 6 changed files with 336 additions and 122 deletions.
36 changes: 36 additions & 0 deletions python/core/composer/qgscomposertable.sip
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ class QgsComposerTable: QgsComposerItem
*/
QFont headerFont() const;

/**Sets the color used to draw header text in the table.
* @param color header text color
* @see headerFontColor
* @see setHeaderFont
* @see setContentFontColor
* @note added in 2.5
*/
void setHeaderFontColor( const QColor& color );

/**Returns the color used to draw header text in the table.
* @returns color for header text
* @see setHeaderFontColor
* @see headerFont
* @see contentFontColor
* @note added in 2.5
*/
QColor headerFontColor() const;

/**Sets the horizontal alignment for table headers
* @param alignment Horizontal alignment for table header cells
* @note added in 2.3
Expand Down Expand Up @@ -83,6 +101,24 @@ class QgsComposerTable: QgsComposerItem
*/
QFont contentFont() const;

/**Sets the color used to draw text in table body cells.
* @param color table cell text color
* @see contentFontColor
* @see setContentFont
* @see setHeaderFontColor
* @note added in 2.5
*/
void setContentFontColor( const QColor& color );

/**Returns the color used to draw text in table body cells.
* @returns text color for table cells
* @see setContentFontColor
* @see contentFont
* @see headerFontColor
* @note added in 2.5
*/
QColor contentFontColor() const;

/**Sets whether grid lines should be drawn in the table
* @param show set to true to show grid lines
* @see showGrid
Expand Down
40 changes: 38 additions & 2 deletions src/app/composer/qgscomposertablewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ QgsComposerTableWidget::QgsComposerTableWidget( QgsComposerAttributeTable* table

refreshMapComboBox();

mHeaderFontColorButton->setColorDialogTitle( tr( "Select header font color" ) );
mHeaderFontColorButton->setColorDialogOptions( QColorDialog::ShowAlphaChannel );
mContentFontColorButton->setColorDialogTitle( tr( "Select content font color" ) );
mContentFontColorButton->setColorDialogOptions( QColorDialog::ShowAlphaChannel );
mGridColorButton->setColorDialogTitle( tr( "Select grid color" ) );
mGridColorButton->setColorDialogOptions( QColorDialog::ShowAlphaChannel );

updateGuiElements();
on_mComposerMapComboBox_activated( mComposerMapComboBox->currentIndex() );

Expand Down Expand Up @@ -220,6 +227,19 @@ void QgsComposerTableWidget::on_mHeaderFontPushButton_clicked()
}
}

void QgsComposerTableWidget::on_mHeaderFontColorButton_colorChanged( const QColor &newColor )
{
if ( !mComposerTable )
{
return;
}

mComposerTable->beginCommand( tr( "Table header font color" ) );
mComposerTable->setHeaderFontColor( newColor );
mComposerTable->update();
mComposerTable->endCommand();
}

void QgsComposerTableWidget::on_mContentFontPushButton_clicked()
{
if ( !mComposerTable )
Expand All @@ -242,6 +262,19 @@ void QgsComposerTableWidget::on_mContentFontPushButton_clicked()
}
}

void QgsComposerTableWidget::on_mContentFontColorButton_colorChanged( const QColor &newColor )
{
if ( !mComposerTable )
{
return;
}

mComposerTable->beginCommand( tr( "Table content font color" ) );
mComposerTable->setContentFontColor( newColor );
mComposerTable->update();
mComposerTable->endCommand();
}

void QgsComposerTableWidget::on_mGridStrokeWidthSpinBox_valueChanged( double d )
{
if ( !mComposerTable )
Expand Down Expand Up @@ -320,8 +353,6 @@ void QgsComposerTableWidget::updateGuiElements()
mMarginSpinBox->setValue( mComposerTable->lineTextDistance() );
mGridStrokeWidthSpinBox->setValue( mComposerTable->gridStrokeWidth() );
mGridColorButton->setColor( mComposerTable->gridColor() );
mGridColorButton->setColorDialogTitle( tr( "Select grid color" ) );
mGridColorButton->setColorDialogOptions( QColorDialog::ShowAlphaChannel );
if ( mComposerTable->showGrid() )
{
mShowGridGroupCheckBox->setChecked( true );
Expand All @@ -331,6 +362,9 @@ void QgsComposerTableWidget::updateGuiElements()
mShowGridGroupCheckBox->setChecked( false );
}

mHeaderFontColorButton->setColor( mComposerTable->headerFontColor() );
mContentFontColorButton->setColor( mComposerTable->contentFontColor() );

if ( mComposerTable->displayOnlyVisibleFeatures() && mShowOnlyVisibleFeaturesCheckBox->isEnabled() )
{
mShowOnlyVisibleFeaturesCheckBox->setCheckState( Qt::Checked );
Expand Down Expand Up @@ -367,6 +401,8 @@ void QgsComposerTableWidget::blockAllSignals( bool b )
mFeatureFilterEdit->blockSignals( b );
mFeatureFilterCheckBox->blockSignals( b );
mHeaderHAlignmentComboBox->blockSignals( b );
mHeaderFontColorButton->blockSignals( b );
mContentFontColorButton->blockSignals( b );
}

void QgsComposerTableWidget::setMaximumNumberOfFeatures( int n )
Expand Down
2 changes: 2 additions & 0 deletions src/app/composer/qgscomposertablewidget.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class QgsComposerTableWidget: public QgsComposerItemBaseWidget, private Ui::QgsC
void on_mGridStrokeWidthSpinBox_valueChanged( double d );
void on_mGridColorButton_colorChanged( const QColor& newColor );
void on_mHeaderFontPushButton_clicked();
void on_mHeaderFontColorButton_colorChanged( const QColor& newColor );
void on_mContentFontPushButton_clicked();
void on_mContentFontColorButton_colorChanged( const QColor& newColor );
void on_mShowGridGroupCheckBox_toggled( bool state );
void on_mShowOnlyVisibleFeaturesCheckBox_stateChanged( int state );
void on_mFeatureFilterCheckBox_stateChanged( int state );
Expand Down
41 changes: 34 additions & 7 deletions src/core/composer/qgscomposertable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@

#include "qgscomposertable.h"
#include "qgscomposertablecolumn.h"
#include "qgssymbollayerv2utils.h"
#include <QPainter>
#include <QSettings>


QgsComposerTable::QgsComposerTable( QgsComposition* composition )
: QgsComposerItem( composition )
, mLineTextDistance( 1.0 )
, mHeaderFontColor( Qt::black )
, mContentFontColor( Qt::black )
, mHeaderHAlignment( FollowColumn )
, mShowGrid( true )
, mGridStrokeWidth( 0.5 )
Expand Down Expand Up @@ -120,12 +123,14 @@ void QgsComposerTable::paint( QPainter* painter, const QStyleOptionGraphicsItem*
break;
}

painter->setPen( mHeaderFontColor );
drawText( painter, cell, ( *columnIt )->heading(), mHeaderFont, headerAlign, Qt::AlignVCenter, Qt::TextDontClip );

currentY += cellHeaderHeight;
currentY += mGridStrokeWidth;

//draw the attribute values
painter->setPen( mContentFontColor );
QList<QgsAttributeMap>::const_iterator attIt = mAttributeMaps.begin();
for ( ; attIt != mAttributeMaps.end(); ++attIt )
{
Expand Down Expand Up @@ -181,6 +186,12 @@ void QgsComposerTable::setHeaderFont( const QFont& f )
adjustFrameToSize();
}

void QgsComposerTable::setHeaderFontColor( const QColor &color )
{
mHeaderFontColor = color;
repaint();
}

void QgsComposerTable::setHeaderHAlignment( const QgsComposerTable::HeaderHAlignment alignment )
{
mHeaderHAlignment = alignment;
Expand All @@ -194,6 +205,12 @@ void QgsComposerTable::setContentFont( const QFont& f )
adjustFrameToSize();
}

void QgsComposerTable::setContentFontColor( const QColor &color )
{
mContentFontColor = color;
repaint();
}

void QgsComposerTable::setShowGrid( bool show )
{
mShowGrid = show;
Expand Down Expand Up @@ -248,12 +265,12 @@ bool QgsComposerTable::tableWriteXML( QDomElement& elem, QDomDocument & doc ) co
{
elem.setAttribute( "lineTextDist", QString::number( mLineTextDistance ) );
elem.setAttribute( "headerFont", mHeaderFont.toString() );
elem.setAttribute( "headerFontColor", QgsSymbolLayerV2Utils::encodeColor( mHeaderFontColor ) );
elem.setAttribute( "headerHAlignment", QString::number(( int )mHeaderHAlignment ) );
elem.setAttribute( "contentFont", mContentFont.toString() );
elem.setAttribute( "contentFontColor", QgsSymbolLayerV2Utils::encodeColor( mContentFontColor ) );
elem.setAttribute( "gridStrokeWidth", QString::number( mGridStrokeWidth ) );
elem.setAttribute( "gridColorRed", mGridColor.red() );
elem.setAttribute( "gridColorGreen", mGridColor.green() );
elem.setAttribute( "gridColorBlue", mGridColor.blue() );
elem.setAttribute( "gridColor", QgsSymbolLayerV2Utils::encodeColor( mGridColor ) );
elem.setAttribute( "showGrid", mShowGrid );

//columns
Expand All @@ -278,17 +295,27 @@ bool QgsComposerTable::tableReadXML( const QDomElement& itemElem, const QDomDocu
}

mHeaderFont.fromString( itemElem.attribute( "headerFont", "" ) );
mHeaderFontColor = QgsSymbolLayerV2Utils::decodeColor( itemElem.attribute( "headerFontColor", "0,0,0,255" ) );
mHeaderHAlignment = QgsComposerTable::HeaderHAlignment( itemElem.attribute( "headerHAlignment", "0" ).toInt() );
mContentFont.fromString( itemElem.attribute( "contentFont", "" ) );
mContentFontColor = QgsSymbolLayerV2Utils::decodeColor( itemElem.attribute( "contentFontColor", "0,0,0,255" ) );
mLineTextDistance = itemElem.attribute( "lineTextDist", "1.0" ).toDouble();
mGridStrokeWidth = itemElem.attribute( "gridStrokeWidth", "0.5" ).toDouble();
mShowGrid = itemElem.attribute( "showGrid", "1" ).toInt();

//grid color
int gridRed = itemElem.attribute( "gridColorRed", "0" ).toInt();
int gridGreen = itemElem.attribute( "gridColorGreen", "0" ).toInt();
int gridBlue = itemElem.attribute( "gridColorBlue", "0" ).toInt();
mGridColor = QColor( gridRed, gridGreen, gridBlue );
if ( itemElem.hasAttribute( "gridColor" ) )
{
mGridColor = QgsSymbolLayerV2Utils::decodeColor( itemElem.attribute( "gridColor", "0,0,0,255" ) );
}
else
{
//old style grid color
int gridRed = itemElem.attribute( "gridColorRed", "0" ).toInt();
int gridGreen = itemElem.attribute( "gridColorGreen", "0" ).toInt();
int gridBlue = itemElem.attribute( "gridColorBlue", "0" ).toInt();
mGridColor = QColor( gridRed, gridGreen, gridBlue );
}

//restore column specifications
qDeleteAll( mColumns );
Expand Down
38 changes: 38 additions & 0 deletions src/core/composer/qgscomposertable.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ class CORE_EXPORT QgsComposerTable: public QgsComposerItem
*/
QFont headerFont() const { return mHeaderFont; }

/**Sets the color used to draw header text in the table.
* @param color header text color
* @see headerFontColor
* @see setHeaderFont
* @see setContentFontColor
* @note added in 2.5
*/
void setHeaderFontColor( const QColor& color );

/**Returns the color used to draw header text in the table.
* @returns color for header text
* @see setHeaderFontColor
* @see headerFont
* @see contentFontColor
* @note added in 2.5
*/
QColor headerFontColor() const { return mHeaderFontColor; }

/**Sets the horizontal alignment for table headers
* @param alignment Horizontal alignment for table header cells
* @note added in 2.3
Expand Down Expand Up @@ -109,6 +127,24 @@ class CORE_EXPORT QgsComposerTable: public QgsComposerItem
*/
QFont contentFont() const { return mContentFont; }

/**Sets the color used to draw text in table body cells.
* @param color table cell text color
* @see contentFontColor
* @see setContentFont
* @see setHeaderFontColor
* @note added in 2.5
*/
void setContentFontColor( const QColor& color );

/**Returns the color used to draw text in table body cells.
* @returns text color for table cells
* @see setContentFontColor
* @see contentFont
* @see headerFontColor
* @note added in 2.5
*/
QColor contentFontColor() const { return mContentFontColor; }

/**Sets whether grid lines should be drawn in the table
* @param show set to true to show grid lines
* @see showGrid
Expand Down Expand Up @@ -213,7 +249,9 @@ class CORE_EXPORT QgsComposerTable: public QgsComposerItem
double mLineTextDistance;

QFont mHeaderFont;
QColor mHeaderFontColor;
QFont mContentFont;
QColor mContentFontColor;

HeaderHAlignment mHeaderHAlignment;

Expand Down
Loading

0 comments on commit e696158

Please sign in to comment.