Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE][composer] Add option to table item to show headers on first
frame, all frames, or no frames (fix #11136) (Sponsored by City of
Uster, Switzerland)
  • Loading branch information
nyalldawson committed Sep 17, 2014
1 parent c833a93 commit 2d24b32
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 19 deletions.
21 changes: 21 additions & 0 deletions src/app/composer/qgscomposerattributetablewidget.cpp
Expand Up @@ -467,6 +467,7 @@ void QgsComposerAttributeTableWidget::updateGuiElements()
mFeatureFilterButton->setEnabled( mComposerTable->filterFeatures() );

mHeaderHAlignmentComboBox->setCurrentIndex(( int )mComposerTable->headerHAlignment() );
mHeaderModeComboBox->setCurrentIndex(( int )mComposerTable->headerMode() );

blockAllSignals( false );
}
Expand All @@ -484,6 +485,7 @@ void QgsComposerAttributeTableWidget::blockAllSignals( bool b )
mFeatureFilterEdit->blockSignals( b );
mFeatureFilterCheckBox->blockSignals( b );
mHeaderHAlignmentComboBox->blockSignals( b );
mHeaderModeComboBox->blockSignals( b );
mHeaderFontColorButton->blockSignals( b );
mContentFontColorButton->blockSignals( b );
}
Expand Down Expand Up @@ -620,6 +622,25 @@ void QgsComposerAttributeTableWidget::on_mHeaderHAlignmentComboBox_currentIndexC
}
}

void QgsComposerAttributeTableWidget::on_mHeaderModeComboBox_currentIndexChanged( int index )
{
if ( !mComposerTable )
{
return;
}

QgsComposition* composition = mComposerTable->composition();
if ( composition )
{
composition->beginMultiFrameCommand( mComposerTable, tr( "Table header mode changed" ) );
}
mComposerTable->setHeaderMode(( QgsComposerTableV2::HeaderMode )index );
if ( composition )
{
composition->endMultiFrameCommand();
}
}

void QgsComposerAttributeTableWidget::changeLayer( QgsMapLayer *layer )
{
if ( !mComposerTable )
Expand Down
1 change: 1 addition & 0 deletions src/app/composer/qgscomposerattributetablewidget.h
Expand Up @@ -60,6 +60,7 @@ class QgsComposerAttributeTableWidget: public QgsComposerItemBaseWidget, private
void on_mFeatureFilterEdit_editingFinished();
void on_mFeatureFilterButton_clicked();
void on_mHeaderHAlignmentComboBox_currentIndexChanged( int index );
void on_mHeaderModeComboBox_currentIndexChanged( int index );
void changeLayer( QgsMapLayer* layer );
void on_mAddFramePushButton_clicked();

Expand Down
40 changes: 29 additions & 11 deletions src/core/composer/qgscomposertablev2.cpp
Expand Up @@ -26,6 +26,7 @@ QgsComposerTableV2::QgsComposerTableV2( QgsComposition *composition, bool create
, mCellMargin( 1.0 )
, mHeaderFontColor( Qt::black )
, mHeaderHAlignment( FollowColumn )
, mHeaderMode( FirstFrame )
, mContentFontColor( Qt::black )
, mShowGrid( true )
, mGridStrokeWidth( 0.5 )
Expand Down Expand Up @@ -100,6 +101,7 @@ bool QgsComposerTableV2::readXML( const QDomElement &itemElem, const QDomDocumen
mHeaderFont.fromString( itemElem.attribute( "headerFont", "" ) );
mHeaderFontColor = QgsSymbolLayerV2Utils::decodeColor( itemElem.attribute( "headerFontColor", "0,0,0,255" ) );
mHeaderHAlignment = QgsComposerTableV2::HeaderHAlignment( itemElem.attribute( "headerHAlignment", "0" ).toInt() );
mHeaderMode = QgsComposerTableV2::HeaderMode( itemElem.attribute( "headerMode", "0" ).toInt() );
mContentFont.fromString( itemElem.attribute( "contentFont", "" ) );
mContentFontColor = QgsSymbolLayerV2Utils::decodeColor( itemElem.attribute( "contentFontColor", "0,0,0,255" ) );
mCellMargin = itemElem.attribute( "cellMargin", "1.0" ).toDouble();
Expand Down Expand Up @@ -139,16 +141,15 @@ QSizeF QgsComposerTableV2::totalSize() const
QPair< int, int > QgsComposerTableV2::rowRange( const QRectF extent, const int frameIndex ) const
{
//calculate row height
//TODO - handle different header modes
//TODO - need to traverse all previous frames to calculate what is visible in each
//as the entire height of a frame may not be used for content
double headerHeight = 0;
double firstHeaderHeight = 2 * mGridStrokeWidth + 2 * mCellMargin + QgsComposerUtils::fontAscentMM( mHeaderFont );

//int frameNumber = mFrameItems.indexOf( this );
if ( frameIndex < 1 )
if (( mHeaderMode == QgsComposerTableV2::FirstFrame && frameIndex < 1 )
|| ( mHeaderMode == QgsComposerTableV2::AllFrames ) )
{
//currently only header on first
//frame has a header
headerHeight = firstHeaderHeight;
}
else
Expand Down Expand Up @@ -204,9 +205,9 @@ void QgsComposerTableV2::render( QPainter *p, const QRectF &renderExtent, const
double cellBodyHeight = QgsComposerUtils::fontAscentMM( mContentFont ) + 2 * mCellMargin;
QRectF cell;

//TODO - should be controlled via a property, eg an enum with values
//always/never/first frame
bool drawHeader = frameIndex < 1;
//calculate whether a header is required
bool drawHeader = (( mHeaderMode == QgsComposerTableV2::FirstFrame && frameIndex < 1 )
|| ( mHeaderMode == QgsComposerTableV2::AllFrames ) );

for ( ; columnIt != mColumns.constEnd(); ++columnIt )
{
Expand Down Expand Up @@ -325,6 +326,18 @@ void QgsComposerTableV2::setHeaderHAlignment( const QgsComposerTableV2::HeaderHA
repaint();
}

void QgsComposerTableV2::setHeaderMode( const QgsComposerTableV2::HeaderMode mode )
{
if ( mode == mHeaderMode )
{
return;
}

mHeaderMode = mode;
adjustFrameToSize();

}

void QgsComposerTableV2::setContentFont( const QFont &font )
{
if ( font == mContentFont )
Expand Down Expand Up @@ -415,10 +428,10 @@ QSizeF QgsComposerTableV2::fixedFrameSize( const int frameIndex ) const
QSizeF QgsComposerTableV2::minFrameSize( const int frameIndex ) const
{
double height = 0;
if ( frameIndex == 0 )
if (( mHeaderMode == QgsComposerTableV2::FirstFrame && frameIndex < 1 )
|| ( mHeaderMode == QgsComposerTableV2::AllFrames ) )
{
//force first frame to be high enough for header
//TODO - handle different header modes
//header required, force frame to be high enough for header
height = 2 * mGridStrokeWidth + 2 * mCellMargin + QgsComposerUtils::fontAscentMM( mHeaderFont );
}
return QSizeF( 0, height );
Expand Down Expand Up @@ -449,7 +462,12 @@ bool QgsComposerTableV2::calculateMaxColumnWidths()
int col = 0;
for ( ; columnIt != mColumns.constEnd(); ++columnIt )
{
mMaxColumnWidthMap.insert( col, QgsComposerUtils::textWidthMM( mHeaderFont, ( *columnIt )->heading() ) );
double width = 0;
if ( mHeaderMode != QgsComposerTableV2::NoHeaders )
{
width = QgsComposerUtils::textWidthMM( mHeaderFont, ( *columnIt )->heading() );
}
mMaxColumnWidthMap.insert( col, width );
col++;
}

Expand Down
26 changes: 26 additions & 0 deletions src/core/composer/qgscomposertablev2.h
Expand Up @@ -63,6 +63,15 @@ class CORE_EXPORT QgsComposerTableV2: public QgsComposerMultiFrame
HeaderRight /*!< align headers right */
};

/*! Controls where headers are shown in the table
*/
enum HeaderMode
{
FirstFrame = 0, /*!< header shown on first frame only */
AllFrames, /*!< headers shown on all frames */
NoHeaders /*!< no headers shown for table */
};

QgsComposerTableV2( QgsComposition* composition, bool createUndoCommands );
QgsComposerTableV2();

Expand Down Expand Up @@ -131,6 +140,20 @@ class CORE_EXPORT QgsComposerTableV2: public QgsComposerMultiFrame
*/
HeaderHAlignment headerHAlignment() const { return mHeaderHAlignment; }

/**Sets the display mode for headers in the table. This property controls
* if and where headers are shown in the table.
* @param mode display mode for headers
* @see headerMode
*/
void setHeaderMode( const HeaderMode mode );

/**Returns the display mode for headers in the table. This property controls
* if and where headers are shown in the table.
* @returns display mode for headers
* @see setHeaderMode
*/
HeaderMode headerMode() const { return mHeaderMode; }

/**Sets the font used to draw text in table body cells.
* @param font font for table cells
* @see contentFont
Expand Down Expand Up @@ -263,6 +286,9 @@ class CORE_EXPORT QgsComposerTableV2: public QgsComposerMultiFrame
/**Alignment for table headers*/
HeaderHAlignment mHeaderHAlignment;

/**Header display mode*/
HeaderMode mHeaderMode;

/**Table contents font*/
QFont mContentFont;

Expand Down
42 changes: 34 additions & 8 deletions src/ui/qgscomposerattributetablewidgetbase.ui
Expand Up @@ -45,9 +45,9 @@
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<y>-21</y>
<width>392</width>
<height>813</height>
<height>846</height>
</rect>
</property>
<layout class="QVBoxLayout" name="mainLayout">
Expand Down Expand Up @@ -297,7 +297,14 @@
<string>Table heading</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="3" column="1" colspan="2">
<item row="1" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>Display</string>
</property>
</widget>
</item>
<item row="5" column="1" colspan="2">
<widget class="QComboBox" name="mHeaderHAlignmentComboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
Expand Down Expand Up @@ -327,35 +334,35 @@
</item>
</widget>
</item>
<item row="3" column="0">
<item row="5" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Alignment</string>
</property>
</widget>
</item>
<item row="2" column="0">
<item row="4" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Color</string>
</property>
</widget>
</item>
<item row="0" column="0">
<item row="2" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Font</string>
</property>
</widget>
</item>
<item row="0" column="1" colspan="2">
<item row="2" column="1" colspan="2">
<widget class="QPushButton" name="mHeaderFontPushButton">
<property name="text">
<string>Choose font...</string>
</property>
</widget>
</item>
<item row="2" column="1" colspan="2">
<item row="4" column="1" colspan="2">
<widget class="QgsColorButtonV2" name="mHeaderFontColorButton">
<property name="minimumSize">
<size>
Expand All @@ -374,6 +381,25 @@
</property>
</widget>
</item>
<item row="1" column="1" colspan="2">
<widget class="QComboBox" name="mHeaderModeComboBox">
<item>
<property name="text">
<string>On first frame</string>
</property>
</item>
<item>
<property name="text">
<string>On all frames</string>
</property>
</item>
<item>
<property name="text">
<string>No header</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down

0 comments on commit 2d24b32

Please sign in to comment.