Skip to content

Commit 80f3eb7

Browse files
author
mhugent
committed
[FEATURE]: A table widget to display feature attributes in the composer (but it still needs a bit more settings...)
git-svn-id: http://svn.osgeo.org/qgis/trunk@12682 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 9268092 commit 80f3eb7

14 files changed

+863
-3
lines changed

src/app/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ SET(QGIS_APP_SRCS
8383
composer/qgscomposermapwidget.cpp
8484
composer/qgscomposerscalebarwidget.cpp
8585
composer/qgscomposershapewidget.cpp
86+
composer/qgscomposertablewidget.cpp
8687
composer/qgscomposerlegenditemdialog.cpp
8788
composer/qgscomposerlegendwidget.cpp
8889
composer/qgscompositionwidget.cpp
@@ -184,6 +185,7 @@ SET (QGIS_APP_MOC_HDRS
184185
composer/qgscomposermapwidget.h
185186
composer/qgscomposerpicturewidget.h
186187
composer/qgscomposerscalebarwidget.h
188+
composer/qgscomposertablewidget.h
187189
composer/qgscomposershapewidget.h
188190
composer/qgscompositionwidget.h
189191
composer/qgsitempositiondialog.h

src/app/composer/qgscomposer.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#include "qgscomposerscalebarwidget.h"
3636
#include "qgscomposershape.h"
3737
#include "qgscomposershapewidget.h"
38+
#include "qgscomposertable.h"
39+
#include "qgscomposertablewidget.h"
3840
#include "qgsexception.h"
3941
#include "qgsproject.h"
4042
#include "qgsmapcanvas.h"
@@ -112,6 +114,7 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title ): QMainWindow(),
112114
toggleActionGroup->addAction( mActionSelectMoveItem );
113115
toggleActionGroup->addAction( mActionAddBasicShape );
114116
toggleActionGroup->addAction( mActionAddArrow );
117+
toggleActionGroup->addAction( mActionAddTable );
115118
toggleActionGroup->setExclusive( true );
116119

117120
mActionAddNewMap->setCheckable( true );
@@ -160,6 +163,7 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title ): QMainWindow(),
160163
layoutMenu->addAction( mActionMoveItemContent );
161164
layoutMenu->addAction( mActionAddBasicShape );
162165
layoutMenu->addAction( mActionAddArrow );
166+
layoutMenu->addAction( mActionAddTable );
163167
layoutMenu->addSeparator();
164168
layoutMenu->addAction( mActionGroupItems );
165169
layoutMenu->addAction( mActionUngroupItems );
@@ -255,6 +259,7 @@ void QgsComposer::setupTheme()
255259
mActionAddNewScalebar->setIcon( QgisApp::getThemeIcon( "/mActionScaleBar.png" ) );
256260
mActionAddBasicShape->setIcon( QgisApp::getThemeIcon( "/mActionAddBasicShape.png" ) );
257261
mActionAddArrow->setIcon( QgisApp::getThemeIcon( "/mActionAddArrow.png" ) );
262+
mActionAddTable->setIcon( QgisApp::getThemeIcon( "/mActionOpenTable.png" ) );
258263
mActionSelectMoveItem->setIcon( QgisApp::getThemeIcon( "/mActionSelectPan.png" ) );
259264
mActionMoveItemContent->setIcon( QgisApp::getThemeIcon( "/mActionMoveItemContent.png" ) );
260265
mActionGroupItems->setIcon( QgisApp::getThemeIcon( "/mActionGroupItems.png" ) );
@@ -282,6 +287,7 @@ void QgsComposer::connectSlots()
282287
connect( mView, SIGNAL( composerPictureAdded( QgsComposerPicture* ) ), this, SLOT( addComposerPicture( QgsComposerPicture* ) ) );
283288
connect( mView, SIGNAL( composerShapeAdded( QgsComposerShape* ) ), this, SLOT( addComposerShape( QgsComposerShape* ) ) );
284289
connect( mView, SIGNAL( composerArrowAdded( QgsComposerArrow* ) ), this, SLOT( addComposerArrow( QgsComposerArrow* ) ) );
290+
connect( mView, SIGNAL( composerTableAdded( QgsComposerTable* ) ), this, SLOT( addComposerTable( QgsComposerTable* ) ) );
285291
connect( mView, SIGNAL( actionFinished() ), this, SLOT( setSelectionTool() ) );
286292
}
287293

@@ -826,6 +832,14 @@ void QgsComposer::on_mActionAddBasicShape_triggered()
826832
}
827833
}
828834

835+
void QgsComposer::on_mActionAddTable_triggered()
836+
{
837+
if ( mView )
838+
{
839+
mView->setCurrentTool( QgsComposerView::AddTable );
840+
}
841+
}
842+
829843
void QgsComposer::on_mActionAddArrow_triggered()
830844
{
831845
if ( mView )
@@ -1260,6 +1274,21 @@ void QgsComposer::readXML( const QDomElement& composerElem, const QDomDocument&
12601274
showItemOptions( newArrow );
12611275
}
12621276

1277+
//composer tables
1278+
QDomNodeList composerTableList = composerElem.elementsByTagName( "ComposerTable" );
1279+
for ( int i = 0; i < composerTableList.size(); ++i )
1280+
{
1281+
QDomElement currentTableElem = composerTableList.at( i ).toElement();
1282+
QgsComposerTable* newTable = new QgsComposerTable( mComposition );
1283+
newTable->readXML( currentTableElem, doc );
1284+
addComposerTable( newTable );
1285+
mComposition->addItem( newTable );
1286+
mComposition->update();
1287+
mComposition->clearSelection();
1288+
newTable->setSelected( true );
1289+
showItemOptions( newTable );
1290+
}
1291+
12631292
mComposition->sortZList();
12641293
mView->setComposition( mComposition );
12651294

@@ -1355,6 +1384,16 @@ void QgsComposer::addComposerShape( QgsComposerShape* shape )
13551384
mItemWidgetMap.insert( shape, sWidget );
13561385
}
13571386

1387+
void QgsComposer::addComposerTable( QgsComposerTable* table )
1388+
{
1389+
if ( !table )
1390+
{
1391+
return;
1392+
}
1393+
QgsComposerTableWidget* tWidget = new QgsComposerTableWidget( table );
1394+
mItemWidgetMap.insert( table, tWidget );
1395+
}
1396+
13581397
void QgsComposer::deleteItem( QgsComposerItem* item )
13591398
{
13601399
QMap<QgsComposerItem*, QWidget*>::iterator it = mItemWidgetMap.find( item );

src/app/composer/qgscomposer.h

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class QgsComposerMap;
2929
class QgsComposerPicture;
3030
class QgsComposerScaleBar;
3131
class QgsComposerShape;
32+
class QgsComposerTable;
3233
class QgsComposerView;
3334
class QgsComposition;
3435
class QgsMapCanvas;
@@ -150,6 +151,9 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
150151
//! Add ellipse shape item
151152
void on_mActionAddBasicShape_triggered();
152153

154+
//! Add attribute table
155+
void on_mActionAddTable_triggered();
156+
153157
//! Save composer as template
154158
void on_mActionSaveAsTemplate_triggered();
155159

@@ -218,6 +222,9 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
218222
/**Adds a composer shape to the item/widget map and creates a configuration widget*/
219223
void addComposerShape( QgsComposerShape* shape );
220224

225+
/**Adds a composer table to the item/widget map and creates a configuration widget*/
226+
void addComposerTable( QgsComposerTable* table );
227+
221228
/**Removes item from the item/widget map and deletes the configuration widget*/
222229
void deleteItem( QgsComposerItem* item );
223230

+214
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
/***************************************************************************
2+
qgscomposertablewidget.cpp
3+
--------------------------
4+
begin : January 2010
5+
copyright : (C) 2010 by Marco Hugentobler
6+
email : marco at hugis dot net
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include "qgscomposertablewidget.h"
19+
#include "qgscomposeritemwidget.h"
20+
#include "qgscomposertable.h"
21+
#include "qgscomposermap.h"
22+
#include "qgsmaplayerregistry.h"
23+
#include "qgsvectorlayer.h"
24+
#include <QFontDialog>
25+
26+
QgsComposerTableWidget::QgsComposerTableWidget( QgsComposerTable* table ): QWidget( 0 ), mComposerTable( table )
27+
{
28+
setupUi( this );
29+
//add widget for general composer item properties
30+
QgsComposerItemWidget* itemPropertiesWidget = new QgsComposerItemWidget( this, mComposerTable );
31+
mToolBox->addItem( itemPropertiesWidget, tr( "General options" ) );
32+
33+
//insert vector layers into combo
34+
QMap<QString, QgsMapLayer*> layerMap = QgsMapLayerRegistry::instance()->mapLayers();
35+
QMap<QString, QgsMapLayer*>::const_iterator mapIt = layerMap.constBegin();
36+
37+
for ( ; mapIt != layerMap.constEnd(); ++mapIt )
38+
{
39+
QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( mapIt.value() );
40+
if ( vl )
41+
{
42+
mLayerComboBox->addItem( vl->name(), mapIt.key() );
43+
}
44+
}
45+
46+
//insert composer maps into combo
47+
if ( mComposerTable )
48+
{
49+
const QgsComposition* tableComposition = mComposerTable->composition();
50+
if ( tableComposition )
51+
{
52+
QList<const QgsComposerMap*> mapList = tableComposition->composerMapItems();
53+
QList<const QgsComposerMap*>::const_iterator mapIt = mapList.constBegin();
54+
for ( ; mapIt != mapList.constEnd(); ++mapIt )
55+
{
56+
int mapId = ( *mapIt )->id();
57+
mComposerMapComboBox->addItem( tr( "Map %1" ).arg( mapId ), mapId );
58+
}
59+
}
60+
}
61+
62+
updateGuiElements();
63+
}
64+
65+
QgsComposerTableWidget::~QgsComposerTableWidget()
66+
{
67+
68+
}
69+
70+
void QgsComposerTableWidget::on_mLayerComboBox_currentIndexChanged( int index )
71+
{
72+
if ( !mComposerTable )
73+
{
74+
return;
75+
}
76+
77+
//set new layer to table item
78+
QVariant itemData = mLayerComboBox->itemData( index );
79+
if ( itemData.type() == QVariant::Invalid )
80+
{
81+
return;
82+
}
83+
84+
QString layerId = itemData.toString();
85+
QgsMapLayer* ml = QgsMapLayerRegistry::instance()->mapLayer( layerId );
86+
if ( ml )
87+
{
88+
QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( ml );
89+
if ( vl )
90+
{
91+
mComposerTable->setVectorLayer( vl );
92+
mComposerTable->update();
93+
}
94+
}
95+
}
96+
97+
void QgsComposerTableWidget::on_mComposerMapComboBox_currentIndexChanged( int index )
98+
{
99+
if ( !mComposerTable )
100+
{
101+
return;
102+
}
103+
104+
QVariant itemData = mComposerMapComboBox->itemData( index );
105+
if ( itemData.type() == QVariant::Invalid )
106+
{
107+
return;
108+
}
109+
110+
int mapId = itemData.toInt();
111+
const QgsComposition* tableComposition = mComposerTable->composition();
112+
if ( tableComposition )
113+
{
114+
mComposerTable->setComposerMap( tableComposition->getComposerMapById( mapId ) );
115+
mComposerTable->update();
116+
}
117+
}
118+
119+
void QgsComposerTableWidget::on_mMaximumColumnsSpinBox_valueChanged( int i )
120+
{
121+
if ( !mComposerTable )
122+
{
123+
return;
124+
}
125+
126+
mComposerTable->setMaximumNumberOfFeatures( i );
127+
mComposerTable->update();
128+
}
129+
130+
void QgsComposerTableWidget::on_mMarginSpinBox_valueChanged( double d )
131+
{
132+
if ( !mComposerTable )
133+
{
134+
return;
135+
}
136+
mComposerTable->setLineTextDistance( d );
137+
mComposerTable->update();
138+
}
139+
140+
void QgsComposerTableWidget::on_mHeaderFontPushButton_clicked()
141+
{
142+
if ( !mComposerTable )
143+
{
144+
return;
145+
}
146+
147+
bool ok;
148+
QFont newFont = QFontDialog::getFont( &ok, mComposerTable->headerFont(), 0, tr( "Select Font" ) );
149+
if ( ok )
150+
{
151+
mComposerTable->setHeaderFont( newFont );
152+
}
153+
}
154+
155+
void QgsComposerTableWidget::on_mContentFontPushButton_clicked()
156+
{
157+
if ( !mComposerTable )
158+
{
159+
return;
160+
}
161+
162+
bool ok;
163+
QFont newFont = QFontDialog::getFont( &ok, mComposerTable->contentFont(), 0, tr( "Select Font" ) );
164+
if ( ok )
165+
{
166+
mComposerTable->setContentFont( newFont );
167+
}
168+
}
169+
170+
void QgsComposerTableWidget::updateGuiElements()
171+
{
172+
if ( !mComposerTable )
173+
{
174+
return;
175+
}
176+
177+
blockAllSignals( true );
178+
179+
//layer combo box
180+
const QgsVectorLayer* vl = mComposerTable->vectorLayer();
181+
if ( vl )
182+
{
183+
int layerIndex = mLayerComboBox->findText( vl->name() );
184+
if ( layerIndex != -1 )
185+
{
186+
mLayerComboBox->setCurrentIndex( layerIndex );
187+
}
188+
}
189+
190+
//map combo box
191+
const QgsComposerMap* cm = mComposerTable->composerMap();
192+
if ( cm )
193+
{
194+
int mapIndex = mComposerMapComboBox->findText( tr( "Map %1" ).arg( cm->id() ) );
195+
if ( mapIndex != -1 )
196+
{
197+
mComposerMapComboBox->setCurrentIndex( mapIndex );
198+
}
199+
}
200+
mMaximumColumnsSpinBox->setValue( mComposerTable->maximumNumberOfFeatures() );
201+
mMarginSpinBox->setValue( mComposerTable->lineTextDistance() );
202+
blockAllSignals( false );
203+
}
204+
205+
void QgsComposerTableWidget::blockAllSignals( bool b )
206+
{
207+
mLayerComboBox->blockSignals( b );
208+
mComposerMapComboBox->blockSignals( b );
209+
mMaximumColumnsSpinBox->blockSignals( b );
210+
mMarginSpinBox->blockSignals( b );
211+
}
212+
213+
214+

0 commit comments

Comments
 (0)