/
qgslayouttable.cpp
1302 lines (1114 loc) · 41.7 KB
/
qgslayouttable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
qgslayouttable.cpp
------------------
begin : November 2017
copyright : (C) 2017 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgslayouttable.h"
#include "qgslayout.h"
#include "qgslayoututils.h"
#include "qgslayouttablecolumn.h"
#include "qgssymbollayerutils.h"
#include "qgslayoutframe.h"
#include "qgsfontutils.h"
#include "qgssettings.h"
#include "qgslayoutpagecollection.h"
//
// QgsLayoutTableStyle
//
bool QgsLayoutTableStyle::writeXml( QDomElement &styleElem, QDomDocument &doc ) const
{
Q_UNUSED( doc )
styleElem.setAttribute( QStringLiteral( "cellBackgroundColor" ), QgsSymbolLayerUtils::encodeColor( cellBackgroundColor ) );
styleElem.setAttribute( QStringLiteral( "enabled" ), enabled );
return true;
}
bool QgsLayoutTableStyle::readXml( const QDomElement &styleElem )
{
cellBackgroundColor = QgsSymbolLayerUtils::decodeColor( styleElem.attribute( QStringLiteral( "cellBackgroundColor" ), QStringLiteral( "255,255,255,255" ) ) );
enabled = ( styleElem.attribute( QStringLiteral( "enabled" ), QStringLiteral( "0" ) ) != QLatin1String( "0" ) );
return true;
}
//
// QgsLayoutTable
//
QgsLayoutTable::QgsLayoutTable( QgsLayout *layout )
: QgsLayoutMultiFrame( layout )
{
//get default composer font from settings
QgsSettings settings;
QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
if ( !defaultFontString.isEmpty() )
{
mHeaderFont.setFamily( defaultFontString );
mContentFont.setFamily( defaultFontString );
}
initStyles();
}
QgsLayoutTable::~QgsLayoutTable()
{
qDeleteAll( mColumns );
mColumns.clear();
qDeleteAll( mCellStyles );
mCellStyles.clear();
}
bool QgsLayoutTable::writePropertiesToElement( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext & ) const
{
elem.setAttribute( QStringLiteral( "cellMargin" ), QString::number( mCellMargin ) );
elem.setAttribute( QStringLiteral( "emptyTableMode" ), QString::number( static_cast< int >( mEmptyTableMode ) ) );
elem.setAttribute( QStringLiteral( "emptyTableMessage" ), mEmptyTableMessage );
elem.setAttribute( QStringLiteral( "showEmptyRows" ), mShowEmptyRows );
elem.appendChild( QgsFontUtils::toXmlElement( mHeaderFont, doc, QStringLiteral( "headerFontProperties" ) ) );
elem.setAttribute( QStringLiteral( "headerFontColor" ), QgsSymbolLayerUtils::encodeColor( mHeaderFontColor ) );
elem.setAttribute( QStringLiteral( "headerHAlignment" ), QString::number( static_cast< int >( mHeaderHAlignment ) ) );
elem.setAttribute( QStringLiteral( "headerMode" ), QString::number( static_cast< int >( mHeaderMode ) ) );
elem.appendChild( QgsFontUtils::toXmlElement( mContentFont, doc, QStringLiteral( "contentFontProperties" ) ) );
elem.setAttribute( QStringLiteral( "contentFontColor" ), QgsSymbolLayerUtils::encodeColor( mContentFontColor ) );
elem.setAttribute( QStringLiteral( "gridStrokeWidth" ), QString::number( mGridStrokeWidth ) );
elem.setAttribute( QStringLiteral( "gridColor" ), QgsSymbolLayerUtils::encodeColor( mGridColor ) );
elem.setAttribute( QStringLiteral( "horizontalGrid" ), mHorizontalGrid );
elem.setAttribute( QStringLiteral( "verticalGrid" ), mVerticalGrid );
elem.setAttribute( QStringLiteral( "showGrid" ), mShowGrid );
elem.setAttribute( QStringLiteral( "backgroundColor" ), QgsSymbolLayerUtils::encodeColor( mBackgroundColor ) );
elem.setAttribute( QStringLiteral( "wrapBehavior" ), QString::number( static_cast< int >( mWrapBehavior ) ) );
//columns
QDomElement displayColumnsElem = doc.createElement( QStringLiteral( "displayColumns" ) );
for ( QgsLayoutTableColumn *column : qgis::as_const( mColumns ) )
{
QDomElement columnElem = doc.createElement( QStringLiteral( "column" ) );
column->writeXml( columnElem, doc );
displayColumnsElem.appendChild( columnElem );
}
elem.appendChild( displayColumnsElem );
//cell styles
QDomElement stylesElem = doc.createElement( QStringLiteral( "cellStyles" ) );
QMap< CellStyleGroup, QString >::const_iterator it = mCellStyleNames.constBegin();
for ( ; it != mCellStyleNames.constEnd(); ++it )
{
QString styleName = it.value();
QDomElement styleElem = doc.createElement( styleName );
QgsLayoutTableStyle *style = mCellStyles.value( it.key() );
if ( style )
{
style->writeXml( styleElem, doc );
stylesElem.appendChild( styleElem );
}
}
elem.appendChild( stylesElem );
return true;
}
bool QgsLayoutTable::readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &, const QgsReadWriteContext & )
{
mEmptyTableMode = QgsLayoutTable::EmptyTableMode( itemElem.attribute( QStringLiteral( "emptyTableMode" ), QStringLiteral( "0" ) ).toInt() );
mEmptyTableMessage = itemElem.attribute( QStringLiteral( "emptyTableMessage" ), tr( "No matching records" ) );
mShowEmptyRows = itemElem.attribute( QStringLiteral( "showEmptyRows" ), QStringLiteral( "0" ) ).toInt();
if ( !QgsFontUtils::setFromXmlChildNode( mHeaderFont, itemElem, QStringLiteral( "headerFontProperties" ) ) )
{
mHeaderFont.fromString( itemElem.attribute( QStringLiteral( "headerFont" ), QString() ) );
}
mHeaderFontColor = QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "headerFontColor" ), QStringLiteral( "0,0,0,255" ) ) );
mHeaderHAlignment = QgsLayoutTable::HeaderHAlignment( itemElem.attribute( QStringLiteral( "headerHAlignment" ), QStringLiteral( "0" ) ).toInt() );
mHeaderMode = QgsLayoutTable::HeaderMode( itemElem.attribute( QStringLiteral( "headerMode" ), QStringLiteral( "0" ) ).toInt() );
if ( !QgsFontUtils::setFromXmlChildNode( mContentFont, itemElem, QStringLiteral( "contentFontProperties" ) ) )
{
mContentFont.fromString( itemElem.attribute( QStringLiteral( "contentFont" ), QString() ) );
}
mContentFontColor = QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "contentFontColor" ), QStringLiteral( "0,0,0,255" ) ) );
mCellMargin = itemElem.attribute( QStringLiteral( "cellMargin" ), QStringLiteral( "1.0" ) ).toDouble();
mGridStrokeWidth = itemElem.attribute( QStringLiteral( "gridStrokeWidth" ), QStringLiteral( "0.5" ) ).toDouble();
mHorizontalGrid = itemElem.attribute( QStringLiteral( "horizontalGrid" ), QStringLiteral( "1" ) ).toInt();
mVerticalGrid = itemElem.attribute( QStringLiteral( "verticalGrid" ), QStringLiteral( "1" ) ).toInt();
mShowGrid = itemElem.attribute( QStringLiteral( "showGrid" ), QStringLiteral( "1" ) ).toInt();
mGridColor = QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "gridColor" ), QStringLiteral( "0,0,0,255" ) ) );
mBackgroundColor = QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "backgroundColor" ), QStringLiteral( "255,255,255,0" ) ) );
mWrapBehavior = QgsLayoutTable::WrapBehavior( itemElem.attribute( QStringLiteral( "wrapBehavior" ), QStringLiteral( "0" ) ).toInt() );
//restore column specifications
qDeleteAll( mColumns );
mColumns.clear();
QDomNodeList columnsList = itemElem.elementsByTagName( QStringLiteral( "displayColumns" ) );
if ( !columnsList.isEmpty() )
{
QDomElement columnsElem = columnsList.at( 0 ).toElement();
QDomNodeList columnEntryList = columnsElem.elementsByTagName( QStringLiteral( "column" ) );
for ( int i = 0; i < columnEntryList.size(); ++i )
{
QDomElement columnElem = columnEntryList.at( i ).toElement();
QgsLayoutTableColumn *column = new QgsLayoutTableColumn;
column->readXml( columnElem );
mColumns.append( column );
}
}
//restore cell styles
QDomNodeList stylesList = itemElem.elementsByTagName( QStringLiteral( "cellStyles" ) );
if ( !stylesList.isEmpty() )
{
QDomElement stylesElem = stylesList.at( 0 ).toElement();
QMap< CellStyleGroup, QString >::const_iterator it = mCellStyleNames.constBegin();
for ( ; it != mCellStyleNames.constEnd(); ++it )
{
QString styleName = it.value();
QDomNodeList styleList = stylesElem.elementsByTagName( styleName );
if ( !styleList.isEmpty() )
{
QDomElement styleElem = styleList.at( 0 ).toElement();
QgsLayoutTableStyle *style = mCellStyles.value( it.key() );
if ( style )
style->readXml( styleElem );
}
}
}
emit changed();
return true;
}
QSizeF QgsLayoutTable::totalSize() const
{
return mTableSize;
}
void QgsLayoutTable::refresh()
{
QgsLayoutMultiFrame::refresh();
refreshAttributes();
}
int QgsLayoutTable::rowsVisible( double frameHeight, int firstRow, bool includeHeader, bool includeEmptyRows ) const
{
//calculate header height
double headerHeight = 0;
if ( includeHeader )
{
//frame has a header
headerHeight = 2 * ( mShowGrid && mHorizontalGrid ? mGridStrokeWidth : 0 ) + 2 * mCellMargin + QgsLayoutUtils::fontAscentMM( mHeaderFont );
}
else
{
//frame has no header text, just the stroke
headerHeight = ( mShowGrid && mHorizontalGrid ? mGridStrokeWidth : 0 );
}
//remaining height available for content rows
double contentHeight = frameHeight - headerHeight;
double gridHeight = ( mShowGrid && mHorizontalGrid ? mGridStrokeWidth : 0 );
int currentRow = firstRow;
while ( contentHeight > 0 && currentRow <= mTableContents.count() )
{
double currentRowHeight = mMaxRowHeightMap.value( currentRow + 1 ) + gridHeight + 2 * mCellMargin;
contentHeight -= currentRowHeight;
currentRow++;
}
if ( includeEmptyRows && contentHeight > 0 )
{
double rowHeight = ( mShowGrid && mHorizontalGrid ? mGridStrokeWidth : 0 ) + 2 * mCellMargin + QgsLayoutUtils::fontAscentMM( mContentFont );
currentRow += std::max( std::floor( contentHeight / rowHeight ), 0.0 );
}
return currentRow - firstRow - 1;
}
int QgsLayoutTable::rowsVisible( int frameIndex, int firstRow, bool includeEmptyRows ) const
{
//get frame extent
if ( frameIndex >= frameCount() )
{
return 0;
}
QRectF frameExtent = frame( frameIndex )->extent();
bool includeHeader = false;
if ( ( mHeaderMode == QgsLayoutTable::FirstFrame && frameIndex < 1 )
|| ( mHeaderMode == QgsLayoutTable::AllFrames ) )
{
includeHeader = true;
}
return rowsVisible( frameExtent.height(), firstRow, includeHeader, includeEmptyRows );
}
QPair<int, int> QgsLayoutTable::rowRange( const int frameIndex ) const
{
//calculate row height
if ( frameIndex >= frameCount() )
{
//bad frame index
return qMakePair( 0, 0 );
}
//loop through all previous frames to calculate how many rows are visible in each
//as the entire height of a frame may not be utilized for content rows
int rowsAlreadyShown = 0;
for ( int idx = 0; idx < frameIndex; ++idx )
{
rowsAlreadyShown += rowsVisible( idx, rowsAlreadyShown, false );
}
//using zero based indexes
int firstVisible = std::min( rowsAlreadyShown, mTableContents.length() );
int possibleRowsVisible = rowsVisible( frameIndex, rowsAlreadyShown, false );
int lastVisible = std::min( firstVisible + possibleRowsVisible, mTableContents.length() );
return qMakePair( firstVisible, lastVisible );
}
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 ) )
{
const QRectF fullCell( currentX, currentY, mMaxColumnWidthMap[col] + 2 * mCellMargin, rowHeight );
//draw background
p->save();
p->setPen( Qt::NoPen );
p->setBrush( backgroundColor( row, col ) );
p->drawRect( fullCell );
p->restore();
// currentY = gridSize;
currentX += mCellMargin;
QVariant cellContents = mTableContents.at( row ).at( col );
QString str = cellContents.toString();
// disable text clipping to target text rectangle, because we manually clip to the full cell bounds below
// and it's ok if text overlaps into the margin (e.g. extenders or italicized text)
Qt::TextFlag textFlag = static_cast< Qt::TextFlag >( Qt::TextDontClip );
if ( ( mWrapBehavior != TruncateText || column->width() > 0 ) && textRequiresWrapping( str, column->width(), mContentFont ) )
{
str = wrappedText( str, column->width(), mContentFont );
}
p->save();
p->setClipRect( fullCell );
const QRectF textCell = QRectF( currentX, currentY + mCellMargin, mMaxColumnWidthMap[col], rowHeight - 2 * mCellMargin );
const QgsConditionalStyle style = conditionalCellStyle( row, col );
QColor foreColor = mContentFontColor;
if ( style.textColor().isValid() )
foreColor = style.textColor();
QgsLayoutUtils::drawText( p, textCell, str, mContentFont, foreColor, column->hAlignment(), column->vAlignment(), textFlag );
p->restore();
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 QgsLayoutTable::setCellMargin( const double margin )
{
if ( qgsDoubleNear( margin, mCellMargin ) )
{
return;
}
mCellMargin = margin;
//since spacing has changed, we need to recalculate the table size
recalculateTableSize();
emit changed();
}
void QgsLayoutTable::setEmptyTableBehavior( const QgsLayoutTable::EmptyTableMode mode )
{
if ( mode == mEmptyTableMode )
{
return;
}
mEmptyTableMode = mode;
//since appearance has changed, we need to recalculate the table size
recalculateTableSize();
emit changed();
}
void QgsLayoutTable::setEmptyTableMessage( const QString &message )
{
if ( message == mEmptyTableMessage )
{
return;
}
mEmptyTableMessage = message;
//since message has changed, we need to recalculate the table size
recalculateTableSize();
emit changed();
}
void QgsLayoutTable::setShowEmptyRows( const bool showEmpty )
{
if ( showEmpty == mShowEmptyRows )
{
return;
}
mShowEmptyRows = showEmpty;
update();
emit changed();
}
void QgsLayoutTable::setHeaderFont( const QFont &font )
{
if ( font == mHeaderFont )
{
return;
}
mHeaderFont = font;
//since font attributes have changed, we need to recalculate the table size
recalculateTableSize();
emit changed();
}
void QgsLayoutTable::setHeaderFontColor( const QColor &color )
{
if ( color == mHeaderFontColor )
{
return;
}
mHeaderFontColor = color;
update();
emit changed();
}
void QgsLayoutTable::setHeaderHAlignment( const QgsLayoutTable::HeaderHAlignment alignment )
{
if ( alignment == mHeaderHAlignment )
{
return;
}
mHeaderHAlignment = alignment;
update();
emit changed();
}
void QgsLayoutTable::setHeaderMode( const QgsLayoutTable::HeaderMode mode )
{
if ( mode == mHeaderMode )
{
return;
}
mHeaderMode = mode;
recalculateTableSize();
emit changed();
}
void QgsLayoutTable::setContentFont( const QFont &font )
{
if ( font == mContentFont )
{
return;
}
mContentFont = font;
//since font attributes have changed, we need to recalculate the table size
recalculateTableSize();
emit changed();
}
void QgsLayoutTable::setContentFontColor( const QColor &color )
{
if ( color == mContentFontColor )
{
return;
}
mContentFontColor = color;
update();
emit changed();
}
void QgsLayoutTable::setShowGrid( const bool showGrid )
{
if ( showGrid == mShowGrid )
{
return;
}
mShowGrid = showGrid;
//since grid spacing has changed, we need to recalculate the table size
recalculateTableSize();
emit changed();
}
void QgsLayoutTable::setGridStrokeWidth( const double width )
{
if ( qgsDoubleNear( width, mGridStrokeWidth ) )
{
return;
}
mGridStrokeWidth = width;
//since grid spacing has changed, we need to recalculate the table size
recalculateTableSize();
emit changed();
}
void QgsLayoutTable::setGridColor( const QColor &color )
{
if ( color == mGridColor )
{
return;
}
mGridColor = color;
update();
emit changed();
}
void QgsLayoutTable::setHorizontalGrid( const bool horizontalGrid )
{
if ( horizontalGrid == mHorizontalGrid )
{
return;
}
mHorizontalGrid = horizontalGrid;
//since grid spacing has changed, we need to recalculate the table size
recalculateTableSize();
emit changed();
}
void QgsLayoutTable::setVerticalGrid( const bool verticalGrid )
{
if ( verticalGrid == mVerticalGrid )
{
return;
}
mVerticalGrid = verticalGrid;
//since grid spacing has changed, we need to recalculate the table size
recalculateTableSize();
emit changed();
}
void QgsLayoutTable::setBackgroundColor( const QColor &color )
{
if ( color == mBackgroundColor )
{
return;
}
mBackgroundColor = color;
update();
emit changed();
}
void QgsLayoutTable::setWrapBehavior( QgsLayoutTable::WrapBehavior behavior )
{
if ( behavior == mWrapBehavior )
{
return;
}
mWrapBehavior = behavior;
recalculateTableSize();
emit changed();
}
void QgsLayoutTable::setColumns( const QgsLayoutTableColumns &columns )
{
//remove existing columns
qDeleteAll( mColumns );
mColumns.clear();
mColumns.append( columns );
}
void QgsLayoutTable::setCellStyle( QgsLayoutTable::CellStyleGroup group, const QgsLayoutTableStyle &style )
{
if ( mCellStyles.contains( group ) )
delete mCellStyles.take( group );
mCellStyles.insert( group, new QgsLayoutTableStyle( style ) );
}
const QgsLayoutTableStyle *QgsLayoutTable::cellStyle( QgsLayoutTable::CellStyleGroup group ) const
{
if ( !mCellStyles.contains( group ) )
return nullptr;
return mCellStyles.value( group );
}
QMap<int, QString> QgsLayoutTable::headerLabels() const
{
QMap<int, QString> headers;
QgsLayoutTableColumns::const_iterator columnIt = mColumns.constBegin();
int col = 0;
for ( ; columnIt != mColumns.constEnd(); ++columnIt )
{
headers.insert( col, ( *columnIt )->heading() );
col++;
}
return headers;
}
QgsConditionalStyle QgsLayoutTable::conditionalCellStyle( int, int ) const
{
return QgsConditionalStyle();
}
QSizeF QgsLayoutTable::fixedFrameSize( const int frameIndex ) const
{
Q_UNUSED( frameIndex )
return QSizeF( mTableSize.width(), 0 );
}
QSizeF QgsLayoutTable::minFrameSize( const int frameIndex ) const
{
double height = 0;
if ( ( mHeaderMode == QgsLayoutTable::FirstFrame && frameIndex < 1 )
|| ( mHeaderMode == QgsLayoutTable::AllFrames ) )
{
//header required, force frame to be high enough for header
height = 2 * ( mShowGrid ? mGridStrokeWidth : 0 ) + 2 * mCellMargin + QgsLayoutUtils::fontAscentMM( mHeaderFont );
}
return QSizeF( 0, height );
}
void QgsLayoutTable::refreshAttributes()
{
mMaxColumnWidthMap.clear();
mMaxRowHeightMap.clear();
mTableContents.clear();
//get new contents
if ( !getTableContents( mTableContents ) )
{
return;
}
}
void QgsLayoutTable::recalculateFrameSizes()
{
mTableSize = QSizeF( totalWidth(), totalHeight() );
QgsLayoutMultiFrame::recalculateFrameSizes();
}
void QgsLayoutTable::initStyles()
{
mCellStyles.insert( OddColumns, new QgsLayoutTableStyle() );
mCellStyles.insert( EvenColumns, new QgsLayoutTableStyle() );
mCellStyles.insert( OddRows, new QgsLayoutTableStyle() );
mCellStyles.insert( EvenRows, new QgsLayoutTableStyle() );
mCellStyles.insert( FirstColumn, new QgsLayoutTableStyle() );
mCellStyles.insert( LastColumn, new QgsLayoutTableStyle() );
mCellStyles.insert( HeaderRow, new QgsLayoutTableStyle() );
mCellStyles.insert( FirstRow, new QgsLayoutTableStyle() );
mCellStyles.insert( LastRow, new QgsLayoutTableStyle() );
mCellStyleNames.insert( OddColumns, QStringLiteral( "oddColumns" ) );
mCellStyleNames.insert( EvenColumns, QStringLiteral( "evenColumns" ) );
mCellStyleNames.insert( OddRows, QStringLiteral( "oddRows" ) );
mCellStyleNames.insert( EvenRows, QStringLiteral( "evenRows" ) );
mCellStyleNames.insert( FirstColumn, QStringLiteral( "firstColumn" ) );
mCellStyleNames.insert( LastColumn, QStringLiteral( "lastColumn" ) );
mCellStyleNames.insert( HeaderRow, QStringLiteral( "headerRow" ) );
mCellStyleNames.insert( FirstRow, QStringLiteral( "firstRow" ) );
mCellStyleNames.insert( LastRow, QStringLiteral( "lastRow" ) );
}
bool QgsLayoutTable::calculateMaxColumnWidths()
{
mMaxColumnWidthMap.clear();
//total number of cells (rows + 1 for header)
int cols = mColumns.count();
int cells = cols * ( mTableContents.count() + 1 );
QVector< double > widths( cells );
//first, go through all the column headers and calculate the sizes
QgsLayoutTableColumns::const_iterator columnIt = mColumns.constBegin();
int col = 0;
for ( ; columnIt != mColumns.constEnd(); ++columnIt )
{
if ( ( *columnIt )->width() > 0 )
{
//column has manually specified width
widths[col] = ( *columnIt )->width();
}
else if ( mHeaderMode != QgsLayoutTable::NoHeaders )
{
widths[col] = QgsLayoutUtils::textWidthMM( mHeaderFont, ( *columnIt )->heading() );
}
else
{
widths[col] = 0.0;
}
col++;
}
//next, go through all the table contents and calculate the sizes
QgsLayoutTableContents::const_iterator rowIt = mTableContents.constBegin();
double currentCellTextWidth;
int row = 1;
for ( ; rowIt != mTableContents.constEnd(); ++rowIt )
{
QgsLayoutTableRow::const_iterator colIt = rowIt->constBegin();
col = 0;
for ( ; colIt != rowIt->constEnd(); ++colIt )
{
if ( mColumns.at( col )->width() <= 0 )
{
//column width set to automatic, so check content size
QStringList multiLineSplit = ( *colIt ).toString().split( '\n' );
currentCellTextWidth = 0;
const auto constMultiLineSplit = multiLineSplit;
for ( const QString &line : constMultiLineSplit )
{
currentCellTextWidth = std::max( currentCellTextWidth, QgsLayoutUtils::textWidthMM( mContentFont, line ) );
}
widths[ row * cols + col ] = currentCellTextWidth;
}
else
{
widths[ row * cols + col ] = 0;
}
col++;
}
row++;
}
//calculate maximum
for ( int col = 0; col < cols; ++col )
{
double maxColWidth = 0;
for ( int row = 0; row < mTableContents.count() + 1; ++row )
{
maxColWidth = std::max( widths[ row * cols + col ], maxColWidth );
}
mMaxColumnWidthMap.insert( col, maxColWidth );
}
return true;
}
bool QgsLayoutTable::calculateMaxRowHeights()
{
mMaxRowHeightMap.clear();
//total number of cells (rows + 1 for header)
int cols = mColumns.count();
int cells = cols * ( mTableContents.count() + 1 );
QVector< double > heights( cells );
//first, go through all the column headers and calculate the sizes
QgsLayoutTableColumns::const_iterator columnIt = mColumns.constBegin();
int col = 0;
for ( ; columnIt != mColumns.constEnd(); ++columnIt )
{
//height
heights[col] = mHeaderMode != QgsLayoutTable::NoHeaders ? QgsLayoutUtils::textHeightMM( mHeaderFont, ( *columnIt )->heading() ) : 0;
col++;
}
//next, go through all the table contents and calculate the sizes
QgsLayoutTableContents::const_iterator rowIt = mTableContents.constBegin();
int row = 1;
for ( ; rowIt != mTableContents.constEnd(); ++rowIt )
{
QgsLayoutTableRow::const_iterator colIt = rowIt->constBegin();
col = 0;
for ( ; colIt != rowIt->constEnd(); ++colIt )
{
if ( textRequiresWrapping( ( *colIt ).toString(), mColumns.at( col )->width(), mContentFont ) )
{
//contents too wide for cell, need to wrap
heights[ row * cols + col ] = QgsLayoutUtils::textHeightMM( mContentFont, wrappedText( ( *colIt ).toString(), mColumns.at( col )->width(), mContentFont ) );
}
else
{
heights[ row * cols + col ] = QgsLayoutUtils::textHeightMM( mContentFont, ( *colIt ).toString() );
}
col++;
}
row++;
}
//calculate maximum
for ( int row = 0; row < mTableContents.count() + 1; ++row )
{
double maxRowHeight = 0;
for ( int col = 0; col < cols; ++col )
{
maxRowHeight = std::max( heights[ row * cols + col ], maxRowHeight );
}
mMaxRowHeightMap.insert( row, maxRowHeight );
}
return true;
}
double QgsLayoutTable::totalWidth()
{
//check how much space each column needs
if ( !calculateMaxColumnWidths() )
{