22
22
#include " qgsvectorlayer.h"
23
23
#include < QPainter>
24
24
25
- QgsComposerTable::QgsComposerTable ( QgsComposition* composition ): QgsComposerItem( composition ), mVectorLayer( 0 ), mComposerMap( 0 ), mMaximumNumberOfFeatures( 5 )
25
+ QgsComposerTable::QgsComposerTable ( QgsComposition* composition ): QgsComposerItem( composition ), mVectorLayer( 0 ), mComposerMap( 0 ), \
26
+ mMaximumNumberOfFeatures( 5 ), mLineTextDistance( 1.0 ), mShowGrid( true ), mGridStrokeWidth( 0.5 ), mGridColor( QColor( 0 , 0 , 0 ) )
26
27
{
27
- mLineTextDistance = 1 ;
28
+
28
29
}
29
30
30
31
QgsComposerTable::~QgsComposerTable ()
@@ -70,21 +71,24 @@ void QgsComposerTable::paint( QPainter* painter, const QStyleOptionGraphicsItem*
70
71
// adapt item fram to max width / height
71
72
adaptItemFrame ( maxColumnWidthMap, attributeList );
72
73
74
+ drawBackground ( painter );
75
+
73
76
// now draw the text
74
- double currentX = 0 ;
77
+ double currentX = mGridStrokeWidth ;
75
78
double currentY;
76
79
77
80
QgsFieldMap vectorFields = mVectorLayer ->pendingFields ();
78
81
QgsFieldMap::const_iterator fieldIt = vectorFields.constBegin ();
79
82
for ( ; fieldIt != vectorFields.constEnd (); ++fieldIt )
80
83
{
81
- currentY = 0 ;
84
+ currentY = mGridStrokeWidth ;
82
85
currentY += mLineTextDistance ;
83
86
currentY += fontAscentMillimeters ( mHeaderFont );
84
87
currentX += mLineTextDistance ;
85
88
drawText ( painter, currentX, currentY, fieldIt.value ().name (), mHeaderFont );
86
89
87
90
currentY += mLineTextDistance ;
91
+ currentY += mGridStrokeWidth ;
88
92
89
93
// draw the attribute values
90
94
QList<QgsAttributeMap>::const_iterator attIt = attributeList.begin ();
@@ -99,18 +103,25 @@ void QgsComposerTable::paint( QPainter* painter, const QStyleOptionGraphicsItem*
99
103
{
100
104
drawText ( painter, currentX, currentY, attMapIt.value ().toString (), mContentFont );
101
105
}
102
-
103
106
currentY += mLineTextDistance ;
107
+ currentY += mGridStrokeWidth ;
104
108
}
105
109
106
- currentX += mLineTextDistance ;
107
110
currentX += maxColumnWidthMap[fieldIt.key ()];
111
+ currentX += mLineTextDistance ;
112
+ currentX += mGridStrokeWidth ;
108
113
}
109
114
110
115
// and the borders
111
- painter->setPen ( mGridPen );
112
- drawHorizontalGridLines ( painter, attributeList.size () );
113
- drawVerticalGridLines ( painter, maxColumnWidthMap );
116
+ if ( mShowGrid )
117
+ {
118
+ QPen gridPen;
119
+ gridPen.setWidthF ( mGridStrokeWidth );
120
+ gridPen.setColor ( mGridColor );
121
+ painter->setPen ( gridPen );
122
+ drawHorizontalGridLines ( painter, attributeList.size () );
123
+ drawVerticalGridLines ( painter, maxColumnWidthMap );
124
+ }
114
125
115
126
// draw frame and selection boxes if necessary
116
127
drawFrame ( painter );
@@ -127,6 +138,12 @@ bool QgsComposerTable::writeXML( QDomElement& elem, QDomDocument & doc ) const
127
138
composerTableElem.setAttribute ( " lineTextDist" , mLineTextDistance );
128
139
composerTableElem.setAttribute ( " headerFont" , mHeaderFont .toString () );
129
140
composerTableElem.setAttribute ( " contentFont" , mContentFont .toString () );
141
+ composerTableElem.setAttribute ( " gridStrokeWidth" , mGridStrokeWidth );
142
+ composerTableElem.setAttribute ( " gridColorRed" , mGridColor .red () );
143
+ composerTableElem.setAttribute ( " gridColorGreen" , mGridColor .green () );
144
+ composerTableElem.setAttribute ( " gridColorBlue" , mGridColor .blue () );
145
+ composerTableElem.setAttribute ( " showGrid" , mShowGrid );
146
+
130
147
if ( mComposerMap )
131
148
{
132
149
composerTableElem.setAttribute ( " composerMap" , mComposerMap ->id () );
@@ -154,6 +171,14 @@ bool QgsComposerTable::readXML( const QDomElement& itemElem, const QDomDocument&
154
171
mHeaderFont .fromString ( itemElem.attribute ( " headerFont" , " " ) );
155
172
mContentFont .fromString ( itemElem.attribute ( " contentFont" , " " ) );
156
173
mLineTextDistance = itemElem.attribute ( " lineTextDist" , " 1.0" ).toDouble ();
174
+ mGridStrokeWidth = itemElem.attribute ( " gridStrokeWidth" , " 0.5" ).toDouble ();
175
+ mShowGrid = itemElem.attribute ( " showGrid" , " 1" ).toInt ();
176
+
177
+ // grid color
178
+ int gridRed = itemElem.attribute ( " gridColorRed" , " 0" ).toInt ();
179
+ int gridGreen = itemElem.attribute ( " gridColorGreen" , " 0" ).toInt ();
180
+ int gridBlue = itemElem.attribute ( " gridColorBlue" , " 0" ).toInt ();
181
+ mGridColor = QColor ( gridRed, gridGreen, gridBlue );
157
182
158
183
// composer map
159
184
int composerMapId = itemElem.attribute ( " composerMap" , " -1" ).toInt ();
@@ -263,7 +288,8 @@ bool QgsComposerTable::calculateMaxColumnWidths( QMap<int, double>& maxWidthMap,
263
288
void QgsComposerTable::adaptItemFrame ( const QMap<int , double >& maxWidthMap, const QList<QgsAttributeMap>& attributeList )
264
289
{
265
290
// calculate height
266
- double totalHeight = fontAscentMillimeters ( mHeaderFont ) + attributeList.size () * fontAscentMillimeters ( mContentFont ) + ( attributeList.size () + 1 ) * mLineTextDistance * 2 ;
291
+ double totalHeight = fontAscentMillimeters ( mHeaderFont ) + attributeList.size () * fontAscentMillimeters ( mContentFont ) \
292
+ + ( attributeList.size () + 1 ) * mLineTextDistance * 2 + ( attributeList.size () + 2 ) * mGridStrokeWidth ;
267
293
268
294
// adapt frame to total width
269
295
double totalWidth = 0 ;
@@ -273,34 +299,41 @@ void QgsComposerTable::adaptItemFrame( const QMap<int, double>& maxWidthMap, con
273
299
totalWidth += maxColWidthIt.value ();
274
300
}
275
301
totalWidth += ( 2 * maxWidthMap.size () * mLineTextDistance );
302
+ totalWidth += ( maxWidthMap.size () + 1 ) * mGridStrokeWidth ;
276
303
QTransform t = transform ();
277
304
setSceneRect ( QRectF ( t.dx (), t.dy (), totalWidth, totalHeight ) );
278
305
}
279
306
280
307
void QgsComposerTable::drawHorizontalGridLines ( QPainter* p, int nAttributes )
281
308
{
282
309
// horizontal lines
283
- double currentY = 0 ;
284
- p->drawLine ( QPointF ( 0 , currentY ), QPointF ( rect ().width (), currentY ) );
310
+ double halfGridStrokeWidth = mGridStrokeWidth / 2.0 ;
311
+ double currentY = halfGridStrokeWidth;
312
+ p->drawLine ( QPointF ( halfGridStrokeWidth, currentY ), QPointF ( rect ().width () - halfGridStrokeWidth, currentY ) );
313
+ currentY += mGridStrokeWidth ;
285
314
currentY += ( fontAscentMillimeters ( mHeaderFont ) + 2 * mLineTextDistance );
286
315
for ( int i = 0 ; i < nAttributes; ++i )
287
316
{
288
- p->drawLine ( QPointF ( 0 , currentY ), QPointF ( rect ().width (), currentY ) );
317
+ p->drawLine ( QPointF ( halfGridStrokeWidth, currentY ), QPointF ( rect ().width () - halfGridStrokeWidth, currentY ) );
318
+ currentY += mGridStrokeWidth ;
289
319
currentY += ( fontAscentMillimeters ( mContentFont ) + 2 * mLineTextDistance );
290
320
}
291
- p->drawLine ( QPointF ( 0 , currentY ), QPointF ( rect ().width (), currentY ) );
321
+ p->drawLine ( QPointF ( halfGridStrokeWidth , currentY ), QPointF ( rect ().width () - halfGridStrokeWidth , currentY ) );
292
322
}
293
323
294
324
void QgsComposerTable::drawVerticalGridLines ( QPainter* p, const QMap<int , double >& maxWidthMap )
295
325
{
296
326
// vertical lines
297
- double currentX = 0 ;
298
- p->drawLine ( QPointF ( currentX, 0 ), QPointF ( currentX, rect ().height () ) );
327
+ double halfGridStrokeWidth = mGridStrokeWidth / 2.0 ;
328
+ double currentX = halfGridStrokeWidth;
329
+ p->drawLine ( QPointF ( currentX, halfGridStrokeWidth ), QPointF ( currentX, rect ().height () - halfGridStrokeWidth ) );
330
+ currentX += mGridStrokeWidth ;
299
331
QMap<int , double >::const_iterator maxColWidthIt = maxWidthMap.constBegin ();
300
332
for ( ; maxColWidthIt != maxWidthMap.constEnd (); ++maxColWidthIt )
301
333
{
302
334
currentX += ( maxColWidthIt.value () + 2 * mLineTextDistance );
303
- p->drawLine ( QPointF ( currentX, 0 ), QPointF ( currentX, rect ().height () ) );
335
+ p->drawLine ( QPointF ( currentX, halfGridStrokeWidth ), QPointF ( currentX, rect ().height () - halfGridStrokeWidth ) );
336
+ currentX += mGridStrokeWidth ;
304
337
}
305
338
}
306
339
0 commit comments