Skip to content

Commit

Permalink
[composer] Fix table background extending beyond table rows (fix #11272
Browse files Browse the repository at this point in the history
…).

It was necessary to add a new 'table background' color option to prevent
regressions when loading old compositions, since users may desire
the old behaviour where the entire frame is filled.
  • Loading branch information
nyalldawson committed Oct 2, 2014
1 parent 7d3333e commit 456e971
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 43 deletions.
14 changes: 14 additions & 0 deletions python/core/composer/qgscomposertablev2.sip
Expand Up @@ -237,6 +237,20 @@ class QgsComposerTableV2: QgsComposerMultiFrame
* @see gridStrokeWidth
*/
QColor gridColor() const;

/**Sets color used for background of table.
* @param color table background color
* @see backgroundColor
* @see setGridColor
*/
void setBackgroundColor( const QColor& color );

/**Returns the color used for the background of the table.
* @returns table background color
* @see setBackgroundColor
* @see gridColor
*/
QColor backgroundColor() const;

/**Returns a pointer to the list of QgsComposerTableColumns shown in the table
* @returns pointer to list of columns in table
Expand Down
29 changes: 28 additions & 1 deletion src/app/composer/qgscomposerattributetablewidget.cpp
Expand Up @@ -70,6 +70,12 @@ QgsComposerAttributeTableWidget::QgsComposerAttributeTableWidget( QgsComposerAtt
mGridColorButton->setColorDialogTitle( tr( "Select grid color" ) );
mGridColorButton->setAllowAlpha( true );
mGridColorButton->setContext( "composer" );
mGridColorButton->setDefaultColor( Qt::black );
mBackgroundColorButton->setColorDialogTitle( tr( "Select background color" ) );
mBackgroundColorButton->setAllowAlpha( true );
mBackgroundColorButton->setContext( "composer" );
mBackgroundColorButton->setShowNoColor( true );
mBackgroundColorButton->setNoColorString( tr( "No background" ) );

updateGuiElements();
on_mComposerMapComboBox_activated( mComposerMapComboBox->currentIndex() );
Expand Down Expand Up @@ -428,6 +434,25 @@ void QgsComposerAttributeTableWidget::on_mShowGridGroupCheckBox_toggled( bool st
}
}

void QgsComposerAttributeTableWidget::on_mBackgroundColorButton_colorChanged( const QColor& newColor )
{
if ( !mComposerTable )
{
return;
}

QgsComposition* composition = mComposerTable->composition();
if ( composition )
{
composition->beginMultiFrameCommand( mComposerTable, tr( "Table background color" ) );
}
mComposerTable->setBackgroundColor( newColor );
mComposerTable->update();
if ( composition )
{
composition->endMultiFrameCommand();
}
}

void QgsComposerAttributeTableWidget::updateGuiElements()
{
Expand Down Expand Up @@ -479,6 +504,7 @@ void QgsComposerAttributeTableWidget::updateGuiElements()
{
mShowGridGroupCheckBox->setChecked( false );
}
mBackgroundColorButton->setColor( mComposerTable->backgroundColor() );

mHeaderFontColorButton->setColor( mComposerTable->headerFontColor() );
mContentFontColorButton->setColor( mComposerTable->contentFontColor() );
Expand Down Expand Up @@ -547,7 +573,7 @@ void QgsComposerAttributeTableWidget::updateRelationsCombo()
if ( atlasLayer )
{
QList<QgsRelation> relations = QgsProject::instance()->relationManager()->referencedRelations( mComposerTable->composition()->atlasComposition().coverageLayer() );
Q_FOREACH ( const QgsRelation& relation, relations )
Q_FOREACH( const QgsRelation& relation, relations )
{
mRelationsComboBox->addItem( relation.name(), relation.id() );
}
Expand Down Expand Up @@ -602,6 +628,7 @@ void QgsComposerAttributeTableWidget::blockAllSignals( bool b )
mMarginSpinBox->blockSignals( b );
mGridColorButton->blockSignals( b );
mGridStrokeWidthSpinBox->blockSignals( b );
mBackgroundColorButton->blockSignals( b );
mShowGridGroupCheckBox->blockSignals( b );
mShowOnlyVisibleFeaturesCheckBox->blockSignals( b );
mUniqueOnlyCheckBox->blockSignals( b );
Expand Down
2 changes: 2 additions & 0 deletions src/app/composer/qgscomposerattributetablewidget.h
Expand Up @@ -54,6 +54,7 @@ class QgsComposerAttributeTableWidget: public QgsComposerItemBaseWidget, private
void on_mMarginSpinBox_valueChanged( double d );
void on_mGridStrokeWidthSpinBox_valueChanged( double d );
void on_mGridColorButton_colorChanged( const QColor& newColor );
void on_mBackgroundColorButton_colorChanged( const QColor &newColor );
void on_mHeaderFontPushButton_clicked();
void on_mHeaderFontColorButton_colorChanged( const QColor& newColor );
void on_mContentFontPushButton_clicked();
Expand Down Expand Up @@ -86,6 +87,7 @@ class QgsComposerAttributeTableWidget: public QgsComposerItemBaseWidget, private
void atlasToggled();

void updateRelationsCombo();

};

#endif // QGSCOMPOSERATTRIBUTETABLEWIDGET_H
92 changes: 62 additions & 30 deletions src/core/composer/qgscomposertablev2.cpp
Expand Up @@ -32,6 +32,7 @@ QgsComposerTableV2::QgsComposerTableV2( QgsComposition *composition, bool create
, mShowGrid( true )
, mGridStrokeWidth( 0.5 )
, mGridColor( Qt::black )
, mBackgroundColor( Qt::white )
{

if ( mComposition )
Expand Down Expand Up @@ -75,6 +76,7 @@ bool QgsComposerTableV2::writeXML( QDomElement& elem, QDomDocument & doc, bool i
elem.setAttribute( "gridStrokeWidth", QString::number( mGridStrokeWidth ) );
elem.setAttribute( "gridColor", QgsSymbolLayerV2Utils::encodeColor( mGridColor ) );
elem.setAttribute( "showGrid", mShowGrid );
elem.setAttribute( "backgroundColor", QgsSymbolLayerV2Utils::encodeColor( mBackgroundColor ) );

//columns
QDomElement displayColumnsElem = doc.createElement( "displayColumns" );
Expand Down Expand Up @@ -118,6 +120,7 @@ bool QgsComposerTableV2::readXML( const QDomElement &itemElem, const QDomDocumen
mGridStrokeWidth = itemElem.attribute( "gridStrokeWidth", "0.5" ).toDouble();
mShowGrid = itemElem.attribute( "showGrid", "1" ).toInt();
mGridColor = QgsSymbolLayerV2Utils::decodeColor( itemElem.attribute( "gridColor", "0,0,0,255" ) );
mBackgroundColor = QgsSymbolLayerV2Utils::decodeColor( itemElem.attribute( "backgroundColor", "255,255,255,0" ) );

//restore column specifications
qDeleteAll( mColumns );
Expand Down Expand Up @@ -252,16 +255,7 @@ void QgsComposerTableV2::render( QPainter *p, const QRectF &renderExtent, const
refreshAttributes();
}

p->save();
//antialiasing on
p->setRenderHint( QPainter::Antialiasing, true );

p->setPen( Qt::SolidLine );

//now draw the text
double currentX = ( mShowGrid ? mGridStrokeWidth : 0 );
double currentY;

double gridSize = mShowGrid ? mGridStrokeWidth : 0;
QList<QgsComposerTableColumn*>::const_iterator columnIt = mColumns.constBegin();

int col = 0;
Expand All @@ -275,9 +269,48 @@ void QgsComposerTableV2::render( QPainter *p, const QRectF &renderExtent, const
//calculate whether drawing table contents is required
bool drawContents = !( emptyTable && mEmptyTableMode == QgsComposerTableV2::ShowMessage );

int numberRowsToDraw = rowsToShow.second - rowsToShow.first;
if ( mEmptyTableMode == QgsComposerTableV2::DrawEmptyCells )
{
numberRowsToDraw = rowsVisible( frameIndex );
}
bool mergeCells = false;
if ( emptyTable && mEmptyTableMode == QgsComposerTableV2::ShowMessage )
{
//draw a merged row for the empty table message
numberRowsToDraw++;
mergeCells = true;
}

p->save();
//antialiasing on
p->setRenderHint( QPainter::Antialiasing, true );

//draw table background
if ( mBackgroundColor.alpha() > 0 )
{
p->save();
p->setPen( Qt::NoPen );
p->setBrush( QBrush( mBackgroundColor ) );
double totalHeight = ( drawHeader || ( numberRowsToDraw > 0 ) ? gridSize : 0 ) +
( drawHeader ? cellHeaderHeight + gridSize : 0.0 ) +
( drawContents ? numberRowsToDraw : 1 ) * ( cellBodyHeight + gridSize );

if ( totalHeight > 0 )
{
QRectF backgroundRect( 0, 0, mTableSize.width(), totalHeight );
p->drawRect( backgroundRect );
}
p->restore();
}

//now draw the text
double currentX = gridSize;
double currentY;
p->setPen( Qt::SolidLine );
for ( ; columnIt != mColumns.constEnd(); ++columnIt )
{
currentY = ( mShowGrid ? mGridStrokeWidth : 0 );
currentY = gridSize;
currentX += mCellMargin;

Qt::TextFlag textFlag = ( Qt::TextFlag )0;
Expand Down Expand Up @@ -315,7 +348,7 @@ void QgsComposerTableV2::render( QPainter *p, const QRectF &renderExtent, const
QgsComposerUtils::drawText( p, cell, ( *columnIt )->heading(), mHeaderFont, mHeaderFontColor, headerAlign, Qt::AlignVCenter, textFlag );

currentY += cellHeaderHeight;
currentY += ( mShowGrid ? mGridStrokeWidth : 0 );
currentY += gridSize;
}

if ( drawContents )
Expand All @@ -331,32 +364,19 @@ void QgsComposerTableV2::render( QPainter *p, const QRectF &renderExtent, const
QgsComposerUtils::drawText( p, cell, str, mContentFont, mContentFontColor, ( *columnIt )->hAlignment(), Qt::AlignVCenter, textFlag );

currentY += cellBodyHeight;
currentY += ( mShowGrid ? mGridStrokeWidth : 0 );
currentY += gridSize;
}
}

currentX += mMaxColumnWidthMap[ col ];
currentX += mCellMargin;
currentX += ( mShowGrid ? mGridStrokeWidth : 0 );
currentX += gridSize;
col++;
}

//and the borders
if ( mShowGrid )
{
int numberRowsToDraw = rowsToShow.second - rowsToShow.first;
if ( mEmptyTableMode == QgsComposerTableV2::DrawEmptyCells )
{
numberRowsToDraw = rowsVisible( frameIndex );
}
bool mergeCells = false;
if ( emptyTable && mEmptyTableMode == QgsComposerTableV2::ShowMessage )
{
//draw a merged row for the empty table message
numberRowsToDraw++;
mergeCells = true;
}

QPen gridPen;
gridPen.setWidthF( mGridStrokeWidth );
gridPen.setColor( mGridColor );
Expand All @@ -369,9 +389,8 @@ void QgsComposerTableV2::render( QPainter *p, const QRectF &renderExtent, const
//special case - no records and table is set to ShowMessage mode
if ( emptyTable && mEmptyTableMode == QgsComposerTableV2::ShowMessage )
{
double messageX = ( mShowGrid ? mGridStrokeWidth : 0 ) + mCellMargin;
double messageY = ( mShowGrid ? mGridStrokeWidth : 0 ) +
( drawHeader ? cellHeaderHeight + ( mShowGrid ? mGridStrokeWidth : 0 ) : 0 );
double messageX = gridSize + mCellMargin;
double messageY = gridSize + ( drawHeader ? cellHeaderHeight + gridSize : 0 );
cell = QRectF( messageX, messageY, mTableSize.width() - messageX, cellBodyHeight );
QgsComposerUtils::drawText( p, cell, mEmptyTableMessage, mContentFont, mContentFontColor, Qt::AlignHCenter, Qt::AlignVCenter, ( Qt::TextFlag )0 );
}
Expand Down Expand Up @@ -546,6 +565,19 @@ void QgsComposerTableV2::setGridColor( const QColor &color )
emit changed();
}

void QgsComposerTableV2::setBackgroundColor( const QColor &color )
{
if ( color == mBackgroundColor )
{
return;
}

mBackgroundColor = color;
repaint();

emit changed();
}

void QgsComposerTableV2::setColumns( QgsComposerTableColumns columns )
{
//remove existing columns
Expand Down
17 changes: 17 additions & 0 deletions src/core/composer/qgscomposertablev2.h
Expand Up @@ -263,6 +263,20 @@ class CORE_EXPORT QgsComposerTableV2: public QgsComposerMultiFrame
*/
QColor gridColor() const { return mGridColor; }

/**Sets color used for background of table.
* @param color table background color
* @see backgroundColor
* @see setGridColor
*/
void setBackgroundColor( const QColor& color );

/**Returns the color used for the background of the table.
* @returns table background color
* @see setBackgroundColor
* @see gridColor
*/
QColor backgroundColor() const { return mBackgroundColor; }

/**Returns a pointer to the list of QgsComposerTableColumns shown in the table
* @returns pointer to list of columns in table
* @see setColumns
Expand Down Expand Up @@ -353,6 +367,9 @@ class CORE_EXPORT QgsComposerTableV2: public QgsComposerMultiFrame
/**Color for grid lines*/
QColor mGridColor;

/**Color for table background*/
QColor mBackgroundColor;

/**Columns to show in table*/
QgsComposerTableColumns mColumns;

Expand Down
58 changes: 46 additions & 12 deletions src/ui/qgscomposerattributetablewidgetbase.ui
Expand Up @@ -17,16 +17,7 @@
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<property name="margin">
<number>0</number>
</property>
<item>
Expand Down Expand Up @@ -55,8 +46,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>391</width>
<height>968</height>
<width>392</width>
<height>1164</height>
</rect>
</property>
<layout class="QVBoxLayout" name="mainLayout">
Expand Down Expand Up @@ -312,6 +303,49 @@
<item row="3" column="1">
<widget class="QLineEdit" name="mEmptyMessageLineEdit"/>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_10">
<property name="text">
<string>Background color</string>
</property>
</widget>
</item>
<item row="4" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QgsColorButtonV2" name="mBackgroundColorButton">
<property name="minimumSize">
<size>
<width>120</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>120</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</item>
Expand Down
1 change: 1 addition & 0 deletions tests/src/core/testqgscomposertablev2.cpp
Expand Up @@ -111,6 +111,7 @@ void TestQgsComposerTableV2::initTestCase()
mComposerAttributeTable->setMaximumNumberOfFeatures( 10 );
mComposerAttributeTable->setContentFont( QgsFontUtils::getStandardTestFont() );
mComposerAttributeTable->setHeaderFont( QgsFontUtils::getStandardTestFont() );
mComposerAttributeTable->setBackgroundColor( Qt::yellow );

mReport = "<h1>Composer TableV2 Tests</h1>\n";
}
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 456e971

Please sign in to comment.