@@ -122,20 +122,55 @@ bool QgsComposerTableV2::readXML( const QDomElement &itemElem, const QDomDocumen
122
122
QSizeF QgsComposerTableV2::totalSize () const
123
123
{
124
124
// TODO - handle multiple cell headers
125
+ // also check height calculation function
126
+
125
127
return mTableSize ;
126
128
}
127
129
128
- void QgsComposerTableV2::render ( QPainter *p, const QRectF &renderExtent )
130
+
131
+ QPair< int , int > QgsComposerTableV2::rowRange ( const QRectF extent, const int frameIndex ) const
129
132
{
130
- // do this via rows
131
- // eg, calculate how rows to->from
132
- // and render them
133
+ // calculate row height
134
+ // TODO - handle different header modes
135
+ // TODO - need to traverse all previous frames to calculate what is visible in each
136
+ // as the entire height of a frame may not be used for content
137
+ double headerHeight = 0 ;
138
+ double firstHeaderHeight = 2 * mGridStrokeWidth + 2 * mCellMargin + QgsComposerUtils::fontAscentMM ( mHeaderFont );
139
+
140
+ // int frameNumber = mFrameItems.indexOf( this );
141
+ if ( frameIndex < 1 )
142
+ {
143
+ // currently only header on first
144
+ headerHeight = firstHeaderHeight;
145
+ }
146
+ else
147
+ {
148
+ headerHeight = mGridStrokeWidth ;
149
+ }
150
+
151
+ // remaining height available for content rows
152
+ double contentHeight = extent.height () - headerHeight;
153
+ double rowHeight = mGridStrokeWidth + 2 * mCellMargin + QgsComposerUtils::fontAscentMM ( mContentFont );
154
+
155
+ // using zero based indexes
156
+ int firstVisible = qMax ( floor (( extent.top () - firstHeaderHeight ) / rowHeight ), 0.0 );
157
+ int rowsVisible = qMax ( floor ( contentHeight / rowHeight ), 0.0 );
158
+ int lastVisible = qMin ( firstVisible + rowsVisible, mTableContents .length () );
159
+
160
+ return qMakePair ( firstVisible, lastVisible );
161
+ }
133
162
163
+
164
+ void QgsComposerTableV2::render ( QPainter *p, const QRectF &renderExtent, const int frameIndex )
165
+ {
134
166
if ( !p )
135
167
{
136
168
return ;
137
169
}
138
170
171
+ // calculate which rows to show in this frame
172
+ QPair< int , int > rowsToShow = rowRange ( renderExtent, frameIndex );
173
+
139
174
if ( mComposition ->plotStyle () == QgsComposition::Print ||
140
175
mComposition ->plotStyle () == QgsComposition::Postscript )
141
176
{
@@ -160,43 +195,52 @@ void QgsComposerTableV2::render( QPainter *p, const QRectF &renderExtent )
160
195
double cellHeaderHeight = QgsComposerUtils::fontAscentMM ( mHeaderFont ) + 2 * mCellMargin ;
161
196
double cellBodyHeight = QgsComposerUtils::fontAscentMM ( mContentFont ) + 2 * mCellMargin ;
162
197
QRectF cell;
198
+
199
+ // TODO - should be controlled via a property, eg an enum with values
200
+ // always/never/first frame
201
+ bool drawHeader = frameIndex < 1 ;
202
+
163
203
for ( ; columnIt != mColumns .constEnd (); ++columnIt )
164
204
{
165
205
currentY = mGridStrokeWidth ;
166
206
currentX += mCellMargin ;
167
207
168
- cell = QRectF ( currentX, currentY, mMaxColumnWidthMap [col], cellHeaderHeight );
169
-
170
- // calculate alignment of header
171
- Qt::AlignmentFlag headerAlign = Qt::AlignLeft;
172
- switch ( mHeaderHAlignment )
208
+ if ( drawHeader )
173
209
{
174
- case FollowColumn:
175
- headerAlign = ( *columnIt )->hAlignment ();
176
- break ;
177
- case HeaderLeft:
178
- headerAlign = Qt::AlignLeft;
179
- break ;
180
- case HeaderCenter:
181
- headerAlign = Qt::AlignHCenter;
182
- break ;
183
- case HeaderRight:
184
- headerAlign = Qt::AlignRight;
185
- break ;
210
+ // draw the header
211
+ cell = QRectF ( currentX, currentY, mMaxColumnWidthMap [col], cellHeaderHeight );
212
+
213
+ // calculate alignment of header
214
+ Qt::AlignmentFlag headerAlign = Qt::AlignLeft;
215
+ switch ( mHeaderHAlignment )
216
+ {
217
+ case FollowColumn:
218
+ headerAlign = ( *columnIt )->hAlignment ();
219
+ break ;
220
+ case HeaderLeft:
221
+ headerAlign = Qt::AlignLeft;
222
+ break ;
223
+ case HeaderCenter:
224
+ headerAlign = Qt::AlignHCenter;
225
+ break ;
226
+ case HeaderRight:
227
+ headerAlign = Qt::AlignRight;
228
+ break ;
229
+ }
230
+
231
+
232
+ QgsComposerUtils::drawText ( p, cell, ( *columnIt )->heading (), mHeaderFont , mHeaderFontColor , headerAlign, Qt::AlignVCenter, Qt::TextDontClip );
233
+
234
+ currentY += cellHeaderHeight;
235
+ currentY += mGridStrokeWidth ;
186
236
}
187
237
188
- QgsComposerUtils::drawText ( p, cell, ( *columnIt )->heading (), mHeaderFont , mHeaderFontColor , headerAlign, Qt::AlignVCenter, Qt::TextDontClip );
189
-
190
- currentY += cellHeaderHeight;
191
- currentY += mGridStrokeWidth ;
192
-
193
238
// draw the attribute values
194
- QgsComposerTableContents::const_iterator attIt = mTableContents .begin ();
195
- for ( ; attIt != mTableContents .end (); ++attIt )
239
+ for ( int row = rowsToShow.first ; row < rowsToShow.second ; ++row )
196
240
{
197
241
cell = QRectF ( currentX, currentY, mMaxColumnWidthMap [col], cellBodyHeight );
198
242
199
- QVariant cellContents = ( *attIt ).at ( col );
243
+ QVariant cellContents = mTableContents . at ( row ).at ( col );
200
244
QString str = cellContents.toString ();
201
245
QgsComposerUtils::drawText ( p, cell, str, mContentFont , mContentFontColor , ( *columnIt )->hAlignment (), Qt::AlignVCenter, Qt::TextDontClip );
202
246
@@ -210,6 +254,7 @@ void QgsComposerTableV2::render( QPainter *p, const QRectF &renderExtent )
210
254
col++;
211
255
}
212
256
257
+
213
258
// and the borders
214
259
if ( mShowGrid )
215
260
{
@@ -218,13 +263,12 @@ void QgsComposerTableV2::render( QPainter *p, const QRectF &renderExtent )
218
263
gridPen.setColor ( mGridColor );
219
264
gridPen.setJoinStyle ( Qt::MiterJoin );
220
265
p->setPen ( gridPen );
221
- drawHorizontalGridLines ( p, mTableContents . size () );
222
- drawVerticalGridLines ( p, mMaxColumnWidthMap );
266
+ drawHorizontalGridLines ( p, rowsToShow. second - rowsToShow. first , drawHeader );
267
+ drawVerticalGridLines ( p, mMaxColumnWidthMap , rowsToShow. second - rowsToShow. first , drawHeader );
223
268
}
224
269
225
270
p->restore ();
226
271
227
-
228
272
}
229
273
230
274
void QgsComposerTableV2::setCellMargin ( const double margin )
@@ -438,14 +482,23 @@ double QgsComposerTableV2::totalHeight() const
438
482
return totalHeight;
439
483
}
440
484
441
- void QgsComposerTableV2::drawHorizontalGridLines ( QPainter *painter, const int rows ) const
485
+ void QgsComposerTableV2::drawHorizontalGridLines ( QPainter *painter, const int rows, const bool drawHeaderLines ) const
442
486
{
443
487
// horizontal lines
488
+ if ( rows < 1 && !drawHeaderLines )
489
+ {
490
+ return ;
491
+ }
492
+
444
493
double halfGridStrokeWidth = mGridStrokeWidth / 2.0 ;
445
- double currentY = halfGridStrokeWidth;
446
- painter->drawLine ( QPointF ( halfGridStrokeWidth, currentY ), QPointF ( mTableSize .width () - halfGridStrokeWidth, currentY ) );
447
- currentY += mGridStrokeWidth ;
448
- currentY += ( QgsComposerUtils::fontAscentMM ( mHeaderFont ) + 2 * mCellMargin );
494
+ double currentY = 0 ;
495
+ currentY = halfGridStrokeWidth;
496
+ if ( drawHeaderLines )
497
+ {
498
+ painter->drawLine ( QPointF ( halfGridStrokeWidth, currentY ), QPointF ( mTableSize .width () - halfGridStrokeWidth, currentY ) );
499
+ currentY += mGridStrokeWidth ;
500
+ currentY += ( QgsComposerUtils::fontAscentMM ( mHeaderFont ) + 2 * mCellMargin );
501
+ }
449
502
for ( int row = 0 ; row < rows; ++row )
450
503
{
451
504
painter->drawLine ( QPointF ( halfGridStrokeWidth, currentY ), QPointF ( mTableSize .width () - halfGridStrokeWidth, currentY ) );
@@ -455,18 +508,33 @@ void QgsComposerTableV2::drawHorizontalGridLines( QPainter *painter, const int r
455
508
painter->drawLine ( QPointF ( halfGridStrokeWidth, currentY ), QPointF ( mTableSize .width () - halfGridStrokeWidth, currentY ) );
456
509
}
457
510
458
- void QgsComposerTableV2::drawVerticalGridLines ( QPainter *painter, const QMap<int , double > &maxWidthMap ) const
511
+ void QgsComposerTableV2::drawVerticalGridLines ( QPainter *painter, const QMap<int , double > &maxWidthMap, const int numberRows, const bool hasHeader ) const
459
512
{
460
513
// vertical lines
514
+ if ( numberRows < 1 && !hasHeader )
515
+ {
516
+ return ;
517
+ }
518
+
519
+ // calculate height of table within frame
520
+ double tableHeight = 0 ;
521
+ if ( hasHeader )
522
+ {
523
+ tableHeight += mGridStrokeWidth + mCellMargin * 2 + QgsComposerUtils::fontAscentMM ( mHeaderFont );
524
+ }
525
+
526
+ tableHeight += numberRows * ( mGridStrokeWidth + mCellMargin * 2 + QgsComposerUtils::fontAscentMM ( mContentFont ) );
527
+ tableHeight += mGridStrokeWidth ;
528
+
461
529
double halfGridStrokeWidth = mGridStrokeWidth / 2.0 ;
462
530
double currentX = halfGridStrokeWidth;
463
- painter->drawLine ( QPointF ( currentX, halfGridStrokeWidth ), QPointF ( currentX, mTableSize . height () - halfGridStrokeWidth ) );
531
+ painter->drawLine ( QPointF ( currentX, halfGridStrokeWidth ), QPointF ( currentX, tableHeight - halfGridStrokeWidth ) );
464
532
currentX += mGridStrokeWidth ;
465
533
QMap<int , double >::const_iterator maxColWidthIt = maxWidthMap.constBegin ();
466
534
for ( ; maxColWidthIt != maxWidthMap.constEnd (); ++maxColWidthIt )
467
535
{
468
536
currentX += ( maxColWidthIt.value () + 2 * mCellMargin );
469
- painter->drawLine ( QPointF ( currentX, halfGridStrokeWidth ), QPointF ( currentX, mTableSize . height () - halfGridStrokeWidth ) );
537
+ painter->drawLine ( QPointF ( currentX, halfGridStrokeWidth ), QPointF ( currentX, tableHeight - halfGridStrokeWidth ) );
470
538
currentX += mGridStrokeWidth ;
471
539
}
472
540
}
0 commit comments