[composer] Fix table background extending beyond table rows (fix #11272).

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.
This commit is contained in:
Nyall Dawson 2014-10-03 09:08:14 +10:00
parent 7d3333efc1
commit 456e971372
12 changed files with 170 additions and 43 deletions

View File

@ -238,6 +238,20 @@ class QgsComposerTableV2: QgsComposerMultiFrame
*/ */
QColor gridColor() const; 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 a pointer to the list of QgsComposerTableColumns shown in the table
* @returns pointer to list of columns in table * @returns pointer to list of columns in table
* @see setColumns * @see setColumns

View File

@ -70,6 +70,12 @@ QgsComposerAttributeTableWidget::QgsComposerAttributeTableWidget( QgsComposerAtt
mGridColorButton->setColorDialogTitle( tr( "Select grid color" ) ); mGridColorButton->setColorDialogTitle( tr( "Select grid color" ) );
mGridColorButton->setAllowAlpha( true ); mGridColorButton->setAllowAlpha( true );
mGridColorButton->setContext( "composer" ); 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(); updateGuiElements();
on_mComposerMapComboBox_activated( mComposerMapComboBox->currentIndex() ); on_mComposerMapComboBox_activated( mComposerMapComboBox->currentIndex() );
@ -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() void QgsComposerAttributeTableWidget::updateGuiElements()
{ {
@ -479,6 +504,7 @@ void QgsComposerAttributeTableWidget::updateGuiElements()
{ {
mShowGridGroupCheckBox->setChecked( false ); mShowGridGroupCheckBox->setChecked( false );
} }
mBackgroundColorButton->setColor( mComposerTable->backgroundColor() );
mHeaderFontColorButton->setColor( mComposerTable->headerFontColor() ); mHeaderFontColorButton->setColor( mComposerTable->headerFontColor() );
mContentFontColorButton->setColor( mComposerTable->contentFontColor() ); mContentFontColorButton->setColor( mComposerTable->contentFontColor() );
@ -602,6 +628,7 @@ void QgsComposerAttributeTableWidget::blockAllSignals( bool b )
mMarginSpinBox->blockSignals( b ); mMarginSpinBox->blockSignals( b );
mGridColorButton->blockSignals( b ); mGridColorButton->blockSignals( b );
mGridStrokeWidthSpinBox->blockSignals( b ); mGridStrokeWidthSpinBox->blockSignals( b );
mBackgroundColorButton->blockSignals( b );
mShowGridGroupCheckBox->blockSignals( b ); mShowGridGroupCheckBox->blockSignals( b );
mShowOnlyVisibleFeaturesCheckBox->blockSignals( b ); mShowOnlyVisibleFeaturesCheckBox->blockSignals( b );
mUniqueOnlyCheckBox->blockSignals( b ); mUniqueOnlyCheckBox->blockSignals( b );

View File

@ -54,6 +54,7 @@ class QgsComposerAttributeTableWidget: public QgsComposerItemBaseWidget, private
void on_mMarginSpinBox_valueChanged( double d ); void on_mMarginSpinBox_valueChanged( double d );
void on_mGridStrokeWidthSpinBox_valueChanged( double d ); void on_mGridStrokeWidthSpinBox_valueChanged( double d );
void on_mGridColorButton_colorChanged( const QColor& newColor ); void on_mGridColorButton_colorChanged( const QColor& newColor );
void on_mBackgroundColorButton_colorChanged( const QColor &newColor );
void on_mHeaderFontPushButton_clicked(); void on_mHeaderFontPushButton_clicked();
void on_mHeaderFontColorButton_colorChanged( const QColor& newColor ); void on_mHeaderFontColorButton_colorChanged( const QColor& newColor );
void on_mContentFontPushButton_clicked(); void on_mContentFontPushButton_clicked();
@ -86,6 +87,7 @@ class QgsComposerAttributeTableWidget: public QgsComposerItemBaseWidget, private
void atlasToggled(); void atlasToggled();
void updateRelationsCombo(); void updateRelationsCombo();
}; };
#endif // QGSCOMPOSERATTRIBUTETABLEWIDGET_H #endif // QGSCOMPOSERATTRIBUTETABLEWIDGET_H

View File

@ -32,6 +32,7 @@ QgsComposerTableV2::QgsComposerTableV2( QgsComposition *composition, bool create
, mShowGrid( true ) , mShowGrid( true )
, mGridStrokeWidth( 0.5 ) , mGridStrokeWidth( 0.5 )
, mGridColor( Qt::black ) , mGridColor( Qt::black )
, mBackgroundColor( Qt::white )
{ {
if ( mComposition ) if ( mComposition )
@ -75,6 +76,7 @@ bool QgsComposerTableV2::writeXML( QDomElement& elem, QDomDocument & doc, bool i
elem.setAttribute( "gridStrokeWidth", QString::number( mGridStrokeWidth ) ); elem.setAttribute( "gridStrokeWidth", QString::number( mGridStrokeWidth ) );
elem.setAttribute( "gridColor", QgsSymbolLayerV2Utils::encodeColor( mGridColor ) ); elem.setAttribute( "gridColor", QgsSymbolLayerV2Utils::encodeColor( mGridColor ) );
elem.setAttribute( "showGrid", mShowGrid ); elem.setAttribute( "showGrid", mShowGrid );
elem.setAttribute( "backgroundColor", QgsSymbolLayerV2Utils::encodeColor( mBackgroundColor ) );
//columns //columns
QDomElement displayColumnsElem = doc.createElement( "displayColumns" ); QDomElement displayColumnsElem = doc.createElement( "displayColumns" );
@ -118,6 +120,7 @@ bool QgsComposerTableV2::readXML( const QDomElement &itemElem, const QDomDocumen
mGridStrokeWidth = itemElem.attribute( "gridStrokeWidth", "0.5" ).toDouble(); mGridStrokeWidth = itemElem.attribute( "gridStrokeWidth", "0.5" ).toDouble();
mShowGrid = itemElem.attribute( "showGrid", "1" ).toInt(); mShowGrid = itemElem.attribute( "showGrid", "1" ).toInt();
mGridColor = QgsSymbolLayerV2Utils::decodeColor( itemElem.attribute( "gridColor", "0,0,0,255" ) ); mGridColor = QgsSymbolLayerV2Utils::decodeColor( itemElem.attribute( "gridColor", "0,0,0,255" ) );
mBackgroundColor = QgsSymbolLayerV2Utils::decodeColor( itemElem.attribute( "backgroundColor", "255,255,255,0" ) );
//restore column specifications //restore column specifications
qDeleteAll( mColumns ); qDeleteAll( mColumns );
@ -252,16 +255,7 @@ void QgsComposerTableV2::render( QPainter *p, const QRectF &renderExtent, const
refreshAttributes(); refreshAttributes();
} }
p->save(); double gridSize = mShowGrid ? mGridStrokeWidth : 0;
//antialiasing on
p->setRenderHint( QPainter::Antialiasing, true );
p->setPen( Qt::SolidLine );
//now draw the text
double currentX = ( mShowGrid ? mGridStrokeWidth : 0 );
double currentY;
QList<QgsComposerTableColumn*>::const_iterator columnIt = mColumns.constBegin(); QList<QgsComposerTableColumn*>::const_iterator columnIt = mColumns.constBegin();
int col = 0; int col = 0;
@ -275,9 +269,48 @@ void QgsComposerTableV2::render( QPainter *p, const QRectF &renderExtent, const
//calculate whether drawing table contents is required //calculate whether drawing table contents is required
bool drawContents = !( emptyTable && mEmptyTableMode == QgsComposerTableV2::ShowMessage ); 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 ) for ( ; columnIt != mColumns.constEnd(); ++columnIt )
{ {
currentY = ( mShowGrid ? mGridStrokeWidth : 0 ); currentY = gridSize;
currentX += mCellMargin; currentX += mCellMargin;
Qt::TextFlag textFlag = ( Qt::TextFlag )0; Qt::TextFlag textFlag = ( Qt::TextFlag )0;
@ -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 ); QgsComposerUtils::drawText( p, cell, ( *columnIt )->heading(), mHeaderFont, mHeaderFontColor, headerAlign, Qt::AlignVCenter, textFlag );
currentY += cellHeaderHeight; currentY += cellHeaderHeight;
currentY += ( mShowGrid ? mGridStrokeWidth : 0 ); currentY += gridSize;
} }
if ( drawContents ) if ( drawContents )
@ -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 ); QgsComposerUtils::drawText( p, cell, str, mContentFont, mContentFontColor, ( *columnIt )->hAlignment(), Qt::AlignVCenter, textFlag );
currentY += cellBodyHeight; currentY += cellBodyHeight;
currentY += ( mShowGrid ? mGridStrokeWidth : 0 ); currentY += gridSize;
} }
} }
currentX += mMaxColumnWidthMap[ col ]; currentX += mMaxColumnWidthMap[ col ];
currentX += mCellMargin; currentX += mCellMargin;
currentX += ( mShowGrid ? mGridStrokeWidth : 0 ); currentX += gridSize;
col++; col++;
} }
//and the borders //and the borders
if ( mShowGrid ) 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; QPen gridPen;
gridPen.setWidthF( mGridStrokeWidth ); gridPen.setWidthF( mGridStrokeWidth );
gridPen.setColor( mGridColor ); gridPen.setColor( mGridColor );
@ -369,9 +389,8 @@ void QgsComposerTableV2::render( QPainter *p, const QRectF &renderExtent, const
//special case - no records and table is set to ShowMessage mode //special case - no records and table is set to ShowMessage mode
if ( emptyTable && mEmptyTableMode == QgsComposerTableV2::ShowMessage ) if ( emptyTable && mEmptyTableMode == QgsComposerTableV2::ShowMessage )
{ {
double messageX = ( mShowGrid ? mGridStrokeWidth : 0 ) + mCellMargin; double messageX = gridSize + mCellMargin;
double messageY = ( mShowGrid ? mGridStrokeWidth : 0 ) + double messageY = gridSize + ( drawHeader ? cellHeaderHeight + gridSize : 0 );
( drawHeader ? cellHeaderHeight + ( mShowGrid ? mGridStrokeWidth : 0 ) : 0 );
cell = QRectF( messageX, messageY, mTableSize.width() - messageX, cellBodyHeight ); cell = QRectF( messageX, messageY, mTableSize.width() - messageX, cellBodyHeight );
QgsComposerUtils::drawText( p, cell, mEmptyTableMessage, mContentFont, mContentFontColor, Qt::AlignHCenter, Qt::AlignVCenter, ( Qt::TextFlag )0 ); QgsComposerUtils::drawText( p, cell, mEmptyTableMessage, mContentFont, mContentFontColor, Qt::AlignHCenter, Qt::AlignVCenter, ( Qt::TextFlag )0 );
} }
@ -546,6 +565,19 @@ void QgsComposerTableV2::setGridColor( const QColor &color )
emit changed(); emit changed();
} }
void QgsComposerTableV2::setBackgroundColor( const QColor &color )
{
if ( color == mBackgroundColor )
{
return;
}
mBackgroundColor = color;
repaint();
emit changed();
}
void QgsComposerTableV2::setColumns( QgsComposerTableColumns columns ) void QgsComposerTableV2::setColumns( QgsComposerTableColumns columns )
{ {
//remove existing columns //remove existing columns

View File

@ -263,6 +263,20 @@ class CORE_EXPORT QgsComposerTableV2: public QgsComposerMultiFrame
*/ */
QColor gridColor() const { return mGridColor; } 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 a pointer to the list of QgsComposerTableColumns shown in the table
* @returns pointer to list of columns in table * @returns pointer to list of columns in table
* @see setColumns * @see setColumns
@ -353,6 +367,9 @@ class CORE_EXPORT QgsComposerTableV2: public QgsComposerMultiFrame
/**Color for grid lines*/ /**Color for grid lines*/
QColor mGridColor; QColor mGridColor;
/**Color for table background*/
QColor mBackgroundColor;
/**Columns to show in table*/ /**Columns to show in table*/
QgsComposerTableColumns mColumns; QgsComposerTableColumns mColumns;

View File

@ -17,16 +17,7 @@
<property name="spacing"> <property name="spacing">
<number>0</number> <number>0</number>
</property> </property>
<property name="leftMargin"> <property name="margin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number> <number>0</number>
</property> </property>
<item> <item>
@ -55,8 +46,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>391</width> <width>392</width>
<height>968</height> <height>1164</height>
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="mainLayout"> <layout class="QVBoxLayout" name="mainLayout">
@ -312,6 +303,49 @@
<item row="3" column="1"> <item row="3" column="1">
<widget class="QLineEdit" name="mEmptyMessageLineEdit"/> <widget class="QLineEdit" name="mEmptyMessageLineEdit"/>
</item> </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> </layout>
</widget> </widget>
</item> </item>

View File

@ -111,6 +111,7 @@ void TestQgsComposerTableV2::initTestCase()
mComposerAttributeTable->setMaximumNumberOfFeatures( 10 ); mComposerAttributeTable->setMaximumNumberOfFeatures( 10 );
mComposerAttributeTable->setContentFont( QgsFontUtils::getStandardTestFont() ); mComposerAttributeTable->setContentFont( QgsFontUtils::getStandardTestFont() );
mComposerAttributeTable->setHeaderFont( QgsFontUtils::getStandardTestFont() ); mComposerAttributeTable->setHeaderFont( QgsFontUtils::getStandardTestFont() );
mComposerAttributeTable->setBackgroundColor( Qt::yellow );
mReport = "<h1>Composer TableV2 Tests</h1>\n"; mReport = "<h1>Composer TableV2 Tests</h1>\n";
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 31 KiB

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 34 KiB

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 14 KiB