void QgsLayoutTable::render( QgsLayoutItemRenderContext &context, const QRectF &, const int frameIndex ) { bool emptyTable = mTableContents.length() == 0; if ( emptyTable && mEmptyTableMode == QgsLayoutTable::HideTable ) { //empty table set to hide table mode, so don't draw anything return; } if ( !mLayout->renderContext().isPreviewRender() ) { //exporting composition, so force an attribute refresh //we do this in case vector layer has changed via an external source (e.g., another database user) refreshAttributes(); } //calculate which rows to show in this frame QPair< int, int > rowsToShow = rowRange( frameIndex ); double gridSizeX = mShowGrid && mVerticalGrid ? mGridStrokeWidth : 0; double gridSizeY = mShowGrid && mHorizontalGrid ? mGridStrokeWidth : 0; double cellHeaderHeight = QgsLayoutUtils::fontAscentMM( mHeaderFont ) + 2 * mCellMargin; double cellBodyHeight = QgsLayoutUtils::fontAscentMM( mContentFont ) + 2 * mCellMargin; QRectF cell; //calculate whether a header is required bool drawHeader = ( ( mHeaderMode == QgsLayoutTable::FirstFrame && frameIndex < 1 ) || ( mHeaderMode == QgsLayoutTable::AllFrames ) ); //calculate whether drawing table contents is required bool drawContents = !( emptyTable && mEmptyTableMode == QgsLayoutTable::ShowMessage ); int numberRowsToDraw = rowsToShow.second - rowsToShow.first; int numberEmptyRows = 0; if ( drawContents && mShowEmptyRows ) { numberRowsToDraw = rowsVisible( frameIndex, rowsToShow.first, true ); numberEmptyRows = numberRowsToDraw - rowsToShow.second + rowsToShow.first; } bool mergeCells = false; if ( emptyTable && mEmptyTableMode == QgsLayoutTable::ShowMessage ) { //draw a merged row for the empty table message numberRowsToDraw++; rowsToShow.second++; mergeCells = true; } QPainter *p = context.renderContext().painter(); p->save(); // painter is scaled to dots, so scale back to layout units p->scale( context.renderContext().scaleFactor(), context.renderContext().scaleFactor() ); //draw the text p->setPen( Qt::SolidLine ); double currentX = gridSizeX; double currentY = gridSizeY; if ( drawHeader ) { //draw the headers int col = 0; for ( const QgsLayoutTableColumn *column : qgis::as_const( mColumns ) ) { //draw background p->save(); p->setPen( Qt::NoPen ); p->setBrush( backgroundColor( -1, col ) ); p->drawRect( QRectF( currentX, currentY, mMaxColumnWidthMap[col] + 2 * mCellMargin, cellHeaderHeight ) ); p->restore(); currentX += mCellMargin; Qt::TextFlag textFlag = static_cast< Qt::TextFlag >( 0 ); if ( column->width() <= 0 ) { //automatic column width, so we use the Qt::TextDontClip flag when drawing contents, as this works nicer for italicised text //which may slightly exceed the calculated width //if column size was manually set then we do apply text clipping, to avoid painting text outside of columns width textFlag = Qt::TextDontClip; } cell = QRectF( currentX, currentY, mMaxColumnWidthMap[col], cellHeaderHeight ); //calculate alignment of header Qt::AlignmentFlag headerAlign = Qt::AlignLeft; switch ( mHeaderHAlignment ) { case FollowColumn: headerAlign = column->hAlignment(); break; case HeaderLeft: headerAlign = Qt::AlignLeft; break; case HeaderCenter: headerAlign = Qt::AlignHCenter; break; case HeaderRight: headerAlign = Qt::AlignRight; break; } QgsLayoutUtils::drawText( p, cell, column->heading(), mHeaderFont, mHeaderFontColor, headerAlign, Qt::AlignVCenter, textFlag ); currentX += mMaxColumnWidthMap[ col ]; currentX += mCellMargin; currentX += gridSizeX; col++; } currentY += cellHeaderHeight; currentY += gridSizeY; } //now draw the body cells int rowsDrawn = 0; if ( drawContents ) { //draw the attribute values for ( int row = rowsToShow.first; row < rowsToShow.second; ++row ) { rowsDrawn++; currentX = gridSizeX; int col = 0; //calculate row height double rowHeight = mMaxRowHeightMap[row + 1] + 2 * mCellMargin; for ( const QgsLayoutTableColumn *column : qgis::as_const( mColumns ) ) { //draw background p->save(); p->setPen( Qt::NoPen ); p->setBrush( backgroundColor( row, col ) ); p->drawRect( QRectF( currentX, currentY, mMaxColumnWidthMap[col] + 2 * mCellMargin, rowHeight ) ); p->restore(); // currentY = gridSize; currentX += mCellMargin; QVariant cellContents = mTableContents.at( row ).at( col ); QString str = cellContents.toString(); Qt::TextFlag textFlag = static_cast< Qt::TextFlag >( 0 ); if ( column->width() <= 0 && mWrapBehavior == TruncateText ) { //automatic column width, so we use the Qt::TextDontClip flag when drawing contents, as this works nicer for italicised text //which may slightly exceed the calculated width //if column size was manually set then we do apply text clipping, to avoid painting text outside of columns width textFlag = Qt::TextDontClip; } else if ( textRequiresWrapping( str, column->width(), mContentFont ) ) { str = wrappedText( str, column->width(), mContentFont ); } cell = QRectF( currentX, currentY, mMaxColumnWidthMap[col], rowHeight ); QgsLayoutUtils::drawText( p, cell, str, mContentFont, mContentFontColor, column->hAlignment(), column->vAlignment(), textFlag ); currentX += mMaxColumnWidthMap[ col ]; currentX += mCellMargin; currentX += gridSizeX; col++; } currentY += rowHeight; currentY += gridSizeY; } } if ( numberRowsToDraw > rowsDrawn ) { p->save(); p->setPen( Qt::NoPen ); //draw background of empty rows for ( int row = rowsDrawn; row < numberRowsToDraw; ++row ) { currentX = gridSizeX; int col = 0; if ( mergeCells ) { p->setBrush( backgroundColor( row + 10000, 0 ) ); p->drawRect( QRectF( gridSizeX, currentY, mTableSize.width() - 2 * gridSizeX, cellBodyHeight ) ); } else { for ( QgsLayoutTableColumn *column : qgis::as_const( mColumns ) ) { Q_UNUSED( column ); //draw background //we use a bit of a hack here - since we don't want these extra blank rows to match the firstrow/lastrow rule, add 10000 to row number p->setBrush( backgroundColor( row + 10000, col ) ); p->drawRect( QRectF( currentX, currentY, mMaxColumnWidthMap[col] + 2 * mCellMargin, cellBodyHeight ) ); // currentY = gridSize; currentX += mMaxColumnWidthMap[ col ] + 2 * mCellMargin; currentX += gridSizeX; col++; } } currentY += cellBodyHeight + gridSizeY; } p->restore(); } //and the borders if ( mShowGrid ) { QPen gridPen; gridPen.setWidthF( mGridStrokeWidth ); gridPen.setColor( mGridColor ); gridPen.setJoinStyle( Qt::MiterJoin ); p->setPen( gridPen ); if ( mHorizontalGrid ) { drawHorizontalGridLines( p, rowsToShow.first, rowsToShow.second + numberEmptyRows, drawHeader ); } if ( mVerticalGrid ) { drawVerticalGridLines( p, mMaxColumnWidthMap, rowsToShow.first, rowsToShow.second + numberEmptyRows, drawHeader, mergeCells ); } } //special case - no records and table is set to ShowMessage mode if ( emptyTable && mEmptyTableMode == QgsLayoutTable::ShowMessage ) { double messageX = gridSizeX + mCellMargin; double messageY = gridSizeY + ( drawHeader ? cellHeaderHeight + gridSizeY : 0 ); cell = QRectF( messageX, messageY, mTableSize.width() - messageX, cellBodyHeight ); QgsLayoutUtils::drawText( p, cell, mEmptyTableMessage, mContentFont, mContentFontColor, Qt::AlignHCenter, Qt::AlignVCenter, static_cast< Qt::TextFlag >( 0 ) ); } p->restore(); }
void QgsComposerTableV2::render( QPainter *p, const QRectF &, const int frameIndex ) { if ( !p ) { return; } bool emptyTable = mTableContents.length() == 0; if ( emptyTable && mEmptyTableMode == QgsComposerTableV2::HideTable ) { //empty table set to hide table mode, so don't draw anything return; } if ( mComposition->plotStyle() == QgsComposition::Print || mComposition->plotStyle() == QgsComposition::Postscript ) { //exporting composition, so force an attribute refresh //we do this in case vector layer has changed via an external source (eg, another database user) refreshAttributes(); } //calculate which rows to show in this frame QPair< int, int > rowsToShow = rowRange( frameIndex ); double gridSize = mShowGrid ? mGridStrokeWidth : 0; double cellHeaderHeight = QgsComposerUtils::fontAscentMM( mHeaderFont ) + 2 * mCellMargin; double cellBodyHeight = QgsComposerUtils::fontAscentMM( mContentFont ) + 2 * mCellMargin; QRectF cell; //calculate whether a header is required bool drawHeader = (( mHeaderMode == QgsComposerTableV2::FirstFrame && frameIndex < 1 ) || ( mHeaderMode == QgsComposerTableV2::AllFrames ) ); //calculate whether drawing table contents is required bool drawContents = !( emptyTable && mEmptyTableMode == QgsComposerTableV2::ShowMessage ); int numberRowsToDraw = rowsToShow.second - rowsToShow.first; int numberEmptyRows = 0; if ( drawContents && mShowEmptyRows ) { numberRowsToDraw = rowsVisible( frameIndex, rowsToShow.first, true ); numberEmptyRows = numberRowsToDraw - rowsToShow.second + rowsToShow.first; } bool mergeCells = false; if ( emptyTable && mEmptyTableMode == QgsComposerTableV2::ShowMessage ) { //draw a merged row for the empty table message numberRowsToDraw++; rowsToShow.second++; 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 ); if ( drawContents ) { for ( int row = rowsToShow.first; row < rowsToShow.second; ++row ) { totalHeight += mMaxRowHeightMap[ row + 1 ] + 2 * mCellMargin + gridSize; } if ( numberEmptyRows > 0 ) { //draw empty rows totalHeight += ( cellBodyHeight + gridSize ) * numberEmptyRows; } } else { totalHeight += cellBodyHeight + gridSize; } if ( totalHeight > 0 ) { QRectF backgroundRect( 0, 0, mTableSize.width(), totalHeight ); p->drawRect( backgroundRect ); } p->restore(); } //now draw the text p->setPen( Qt::SolidLine ); double currentX = gridSize; double currentY = gridSize; if ( drawHeader ) { //draw the headers int col = 0; for ( QList<QgsComposerTableColumn*>::const_iterator columnIt = mColumns.constBegin(); columnIt != mColumns.constEnd(); ++columnIt ) { currentX += mCellMargin; Qt::TextFlag textFlag = ( Qt::TextFlag )0; if (( *columnIt )->width() <= 0 ) { //automatic column width, so we use the Qt::TextDontClip flag when drawing contents, as this works nicer for italicised text //which may slightly exceed the calculated width //if column size was manually set then we do apply text clipping, to avoid painting text outside of columns width textFlag = Qt::TextDontClip; } cell = QRectF( currentX, currentY, mMaxColumnWidthMap[col], cellHeaderHeight ); //calculate alignment of header Qt::AlignmentFlag headerAlign = Qt::AlignLeft; switch ( mHeaderHAlignment ) { case FollowColumn: headerAlign = ( *columnIt )->hAlignment(); break; case HeaderLeft: headerAlign = Qt::AlignLeft; break; case HeaderCenter: headerAlign = Qt::AlignHCenter; break; case HeaderRight: headerAlign = Qt::AlignRight; break; } QgsComposerUtils::drawText( p, cell, ( *columnIt )->heading(), mHeaderFont, mHeaderFontColor, headerAlign, Qt::AlignVCenter, textFlag ); currentX += mMaxColumnWidthMap[ col ]; currentX += mCellMargin; currentX += gridSize; col++; } currentY += cellHeaderHeight; currentY += gridSize; } //now draw the body cells if ( drawContents ) { //draw the attribute values for ( int row = rowsToShow.first; row < rowsToShow.second; ++row ) { currentX = gridSize; int col = 0; //calculate row height double rowHeight = mMaxRowHeightMap[row + 1] + 2 * mCellMargin; for ( QList<QgsComposerTableColumn*>::const_iterator columnIt = mColumns.constBegin(); columnIt != mColumns.constEnd(); ++columnIt ) { // currentY = gridSize; currentX += mCellMargin; QVariant cellContents = mTableContents.at( row ).at( col ); QString str = cellContents.toString(); Qt::TextFlag textFlag = ( Qt::TextFlag )0; if (( *columnIt )->width() <= 0 && mWrapBehaviour == TruncateText ) { //automatic column width, so we use the Qt::TextDontClip flag when drawing contents, as this works nicer for italicised text //which may slightly exceed the calculated width //if column size was manually set then we do apply text clipping, to avoid painting text outside of columns width textFlag = Qt::TextDontClip; } else if ( textRequiresWrapping( str, ( *columnIt )->width(), mContentFont ) ) { str = wrappedText( str, ( *columnIt )->width(), mContentFont ); } cell = QRectF( currentX, currentY, mMaxColumnWidthMap[col], rowHeight ); QgsComposerUtils::drawText( p, cell, str, mContentFont, mContentFontColor, ( *columnIt )->hAlignment(), ( *columnIt )->vAlignment(), textFlag ); currentX += mMaxColumnWidthMap[ col ]; currentX += mCellMargin; currentX += gridSize; col++; } currentY += rowHeight; currentY += gridSize; } } //and the borders if ( mShowGrid ) { QPen gridPen; gridPen.setWidthF( mGridStrokeWidth ); gridPen.setColor( mGridColor ); gridPen.setJoinStyle( Qt::MiterJoin ); p->setPen( gridPen ); drawHorizontalGridLines( p, rowsToShow.first, rowsToShow.second + numberEmptyRows, drawHeader ); drawVerticalGridLines( p, mMaxColumnWidthMap, rowsToShow.first, rowsToShow.second + numberEmptyRows, drawHeader, mergeCells ); } //special case - no records and table is set to ShowMessage mode if ( emptyTable && mEmptyTableMode == QgsComposerTableV2::ShowMessage ) { 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 ); } p->restore(); }