Skip to content

Commit 41c888c

Browse files
author
mhugent
committed
More options for composer table
git-svn-id: http://svn.osgeo.org/qgis/trunk@12688 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 615e82a commit 41c888c

5 files changed

+180
-32
lines changed

src/app/composer/qgscomposertablewidget.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "qgscomposermap.h"
2222
#include "qgsmaplayerregistry.h"
2323
#include "qgsvectorlayer.h"
24+
#include <QColorDialog>
2425
#include <QFontDialog>
2526

2627
QgsComposerTableWidget::QgsComposerTableWidget( QgsComposerTable* table ): QWidget( 0 ), mComposerTable( table )
@@ -167,6 +168,50 @@ void QgsComposerTableWidget::on_mContentFontPushButton_clicked()
167168
}
168169
}
169170

171+
void QgsComposerTableWidget::on_mGridStrokeWidthSpinBox_valueChanged( double d )
172+
{
173+
if ( !mComposerTable )
174+
{
175+
return;
176+
}
177+
mComposerTable->setGridStrokeWidth( d );
178+
mComposerTable->update();
179+
}
180+
181+
void QgsComposerTableWidget::on_mGridColorButton_clicked()
182+
{
183+
if ( !mComposerTable )
184+
{
185+
return;
186+
}
187+
188+
QColor newColor = QColorDialog::getColor( mComposerTable->gridColor(), 0, tr( "Select grid color" ) );
189+
if ( !newColor.isValid() )
190+
{
191+
return;
192+
}
193+
mGridColorButton->setColor( newColor );
194+
mComposerTable->setGridColor( newColor );
195+
mComposerTable->update();
196+
}
197+
198+
void QgsComposerTableWidget::on_mShowGridCheckBox_stateChanged( int state )
199+
{
200+
if ( !mComposerTable )
201+
{
202+
return;
203+
}
204+
205+
bool showGrid = false;
206+
if ( state == Qt::Checked )
207+
{
208+
showGrid = true;
209+
}
210+
mComposerTable->setShowGrid( showGrid );
211+
mComposerTable->update();
212+
}
213+
214+
170215
void QgsComposerTableWidget::updateGuiElements()
171216
{
172217
if ( !mComposerTable )
@@ -199,6 +244,16 @@ void QgsComposerTableWidget::updateGuiElements()
199244
}
200245
mMaximumColumnsSpinBox->setValue( mComposerTable->maximumNumberOfFeatures() );
201246
mMarginSpinBox->setValue( mComposerTable->lineTextDistance() );
247+
mGridStrokeWidthSpinBox->setValue( mComposerTable->gridStrokeWidth() );
248+
mGridColorButton->setColor( mComposerTable->gridColor() );
249+
if ( mComposerTable->showGrid() )
250+
{
251+
mShowGridCheckBox->setCheckState( Qt::Checked );
252+
}
253+
else
254+
{
255+
mShowGridCheckBox->setCheckState( Qt::Unchecked );
256+
}
202257
blockAllSignals( false );
203258
}
204259

@@ -208,6 +263,9 @@ void QgsComposerTableWidget::blockAllSignals( bool b )
208263
mComposerMapComboBox->blockSignals( b );
209264
mMaximumColumnsSpinBox->blockSignals( b );
210265
mMarginSpinBox->blockSignals( b );
266+
mGridColorButton->blockSignals( b );
267+
mGridStrokeWidthSpinBox->blockSignals( b );
268+
mShowGridCheckBox->blockSignals( b );
211269
}
212270

213271

src/app/composer/qgscomposertablewidget.h

+3
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ class QgsComposerTableWidget: public QWidget, private Ui::QgsComposerTableWidget
4242
void on_mComposerMapComboBox_currentIndexChanged( int index );
4343
void on_mMaximumColumnsSpinBox_valueChanged( int i );
4444
void on_mMarginSpinBox_valueChanged( double d );
45+
void on_mGridStrokeWidthSpinBox_valueChanged( double d );
46+
void on_mGridColorButton_clicked();
4547
void on_mHeaderFontPushButton_clicked();
4648
void on_mContentFontPushButton_clicked();
49+
void on_mShowGridCheckBox_stateChanged( int state );
4750
};
4851

4952
#endif // QGSCOMPOSERTABLEWIDGET_H

src/core/composer/qgscomposertable.cpp

+50-17
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
#include "qgsvectorlayer.h"
2323
#include <QPainter>
2424

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 ) )
2627
{
27-
mLineTextDistance = 1;
28+
2829
}
2930

3031
QgsComposerTable::~QgsComposerTable()
@@ -70,21 +71,24 @@ void QgsComposerTable::paint( QPainter* painter, const QStyleOptionGraphicsItem*
7071
//adapt item fram to max width / height
7172
adaptItemFrame( maxColumnWidthMap, attributeList );
7273

74+
drawBackground( painter );
75+
7376
//now draw the text
74-
double currentX = 0;
77+
double currentX = mGridStrokeWidth;
7578
double currentY;
7679

7780
QgsFieldMap vectorFields = mVectorLayer->pendingFields();
7881
QgsFieldMap::const_iterator fieldIt = vectorFields.constBegin();
7982
for ( ; fieldIt != vectorFields.constEnd(); ++fieldIt )
8083
{
81-
currentY = 0;
84+
currentY = mGridStrokeWidth;
8285
currentY += mLineTextDistance;
8386
currentY += fontAscentMillimeters( mHeaderFont );
8487
currentX += mLineTextDistance;
8588
drawText( painter, currentX, currentY, fieldIt.value().name(), mHeaderFont );
8689

8790
currentY += mLineTextDistance;
91+
currentY += mGridStrokeWidth;
8892

8993
//draw the attribute values
9094
QList<QgsAttributeMap>::const_iterator attIt = attributeList.begin();
@@ -99,18 +103,25 @@ void QgsComposerTable::paint( QPainter* painter, const QStyleOptionGraphicsItem*
99103
{
100104
drawText( painter, currentX, currentY, attMapIt.value().toString(), mContentFont );
101105
}
102-
103106
currentY += mLineTextDistance;
107+
currentY += mGridStrokeWidth;
104108
}
105109

106-
currentX += mLineTextDistance;
107110
currentX += maxColumnWidthMap[fieldIt.key()];
111+
currentX += mLineTextDistance;
112+
currentX += mGridStrokeWidth;
108113
}
109114

110115
//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+
}
114125

115126
//draw frame and selection boxes if necessary
116127
drawFrame( painter );
@@ -127,6 +138,12 @@ bool QgsComposerTable::writeXML( QDomElement& elem, QDomDocument & doc ) const
127138
composerTableElem.setAttribute( "lineTextDist", mLineTextDistance );
128139
composerTableElem.setAttribute( "headerFont", mHeaderFont.toString() );
129140
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+
130147
if ( mComposerMap )
131148
{
132149
composerTableElem.setAttribute( "composerMap", mComposerMap->id() );
@@ -154,6 +171,14 @@ bool QgsComposerTable::readXML( const QDomElement& itemElem, const QDomDocument&
154171
mHeaderFont.fromString( itemElem.attribute( "headerFont", "" ) );
155172
mContentFont.fromString( itemElem.attribute( "contentFont", "" ) );
156173
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 );
157182

158183
//composer map
159184
int composerMapId = itemElem.attribute( "composerMap", "-1" ).toInt();
@@ -263,7 +288,8 @@ bool QgsComposerTable::calculateMaxColumnWidths( QMap<int, double>& maxWidthMap,
263288
void QgsComposerTable::adaptItemFrame( const QMap<int, double>& maxWidthMap, const QList<QgsAttributeMap>& attributeList )
264289
{
265290
//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;
267293

268294
//adapt frame to total width
269295
double totalWidth = 0;
@@ -273,34 +299,41 @@ void QgsComposerTable::adaptItemFrame( const QMap<int, double>& maxWidthMap, con
273299
totalWidth += maxColWidthIt.value();
274300
}
275301
totalWidth += ( 2 * maxWidthMap.size() * mLineTextDistance );
302+
totalWidth += ( maxWidthMap.size() + 1 ) * mGridStrokeWidth;
276303
QTransform t = transform();
277304
setSceneRect( QRectF( t.dx(), t.dy(), totalWidth, totalHeight ) );
278305
}
279306

280307
void QgsComposerTable::drawHorizontalGridLines( QPainter* p, int nAttributes )
281308
{
282309
//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;
285314
currentY += ( fontAscentMillimeters( mHeaderFont ) + 2 * mLineTextDistance );
286315
for ( int i = 0; i < nAttributes; ++i )
287316
{
288-
p->drawLine( QPointF( 0, currentY ), QPointF( rect().width(), currentY ) );
317+
p->drawLine( QPointF( halfGridStrokeWidth, currentY ), QPointF( rect().width() - halfGridStrokeWidth, currentY ) );
318+
currentY += mGridStrokeWidth;
289319
currentY += ( fontAscentMillimeters( mContentFont ) + 2 * mLineTextDistance );
290320
}
291-
p->drawLine( QPointF( 0, currentY ), QPointF( rect().width(), currentY ) );
321+
p->drawLine( QPointF( halfGridStrokeWidth, currentY ), QPointF( rect().width() - halfGridStrokeWidth, currentY ) );
292322
}
293323

294324
void QgsComposerTable::drawVerticalGridLines( QPainter* p, const QMap<int, double>& maxWidthMap )
295325
{
296326
//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;
299331
QMap<int, double>::const_iterator maxColWidthIt = maxWidthMap.constBegin();
300332
for ( ; maxColWidthIt != maxWidthMap.constEnd(); ++maxColWidthIt )
301333
{
302334
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;
304337
}
305338
}
306339

src/core/composer/qgscomposertable.h

+14-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ class QgsComposerTable: public QgsComposerItem
5555
void setContentFont( const QFont& f ) { mContentFont = f; }
5656
QFont contentFont() const { return mContentFont; }
5757

58+
void setShowGrid( bool show ) { mShowGrid = show;}
59+
bool showGrid() const { return mShowGrid; }
60+
61+
void setGridStrokeWidth( double w ) { mGridStrokeWidth = w; }
62+
double gridStrokeWidth() const { return mGridStrokeWidth; }
63+
64+
void setGridColor( const QColor& c ) { mGridColor = c; }
65+
QColor gridColor() const { return mGridColor; }
66+
5867
private:
5968
/**Associated vector layer*/
6069
QgsVectorLayer* mVectorLayer;
@@ -64,10 +73,14 @@ class QgsComposerTable: public QgsComposerItem
6473
int mMaximumNumberOfFeatures;
6574
/**Distance between table lines and text*/
6675
double mLineTextDistance;
67-
QPen mGridPen;
76+
6877
QFont mHeaderFont;
6978
QFont mContentFont;
7079

80+
bool mShowGrid;
81+
double mGridStrokeWidth;
82+
QColor mGridColor;
83+
7184
/**Retrieves feature attributes*/
7285
bool getFeatureAttributes( QList<QgsAttributeMap>& attributes );
7386
/**Calculate the maximum width values of the vector attributes*/

0 commit comments

Comments
 (0)