Skip to content

Commit caba437

Browse files
author
mhugent
committed
[FEATURE]: Export legend groups and layers with legendinterface and use this information to display groups in the composer legend. Todo: fix drag and frop in composer legend, readXML, cleanups in composer legend model
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13476 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 54a2306 commit caba437

16 files changed

+944
-321
lines changed

src/app/composer/qgscomposerlegendwidget.cpp

+69-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
#include "qgscomposeritemwidget.h"
2222
#include <QFontDialog>
2323

24+
#include "qgsapplegendinterface.h"
25+
#include "qgisapp.h"
26+
#include "qgsmapcanvas.h"
27+
#include "qgsmaprenderer.h"
28+
2429
QgsComposerLegendWidget::QgsComposerLegendWidget( QgsComposerLegend* legend ): mLegend( legend )
2530
{
2631
setupUi( this );
@@ -34,6 +39,14 @@ QgsComposerLegendWidget::QgsComposerLegendWidget( QgsComposerLegend* legend ): m
3439
mItemTreeView->setModel( legend->model() );
3540
}
3641

42+
updateLegend();
43+
44+
mItemTreeView->setDragEnabled( true );
45+
mItemTreeView->setAcceptDrops( true );
46+
mItemTreeView->setDropIndicatorShown( true );
47+
mItemTreeView->setDefaultDropAction( Qt::MoveAction );
48+
mItemTreeView->setDragDropMode( QAbstractItemView::InternalMove );
49+
3750
setGuiElements();
3851
}
3952

@@ -147,6 +160,26 @@ void QgsComposerLegendWidget::on_mTitleFontButton_clicked()
147160
}
148161
}
149162

163+
void QgsComposerLegendWidget::on_mGroupFontButton_clicked()
164+
{
165+
if ( mLegend )
166+
{
167+
bool ok;
168+
#if defined(Q_WS_MAC) && QT_VERSION >= 0x040500 && !defined(__LP64__)
169+
// Native Mac dialog works only for 64 bit Cocoa (observed in Qt 4.5.2, probably a Qt bug)
170+
QFont newFont = QFontDialog::getFont( &ok, mLegend->groupFont(), this, QString(), QFontDialog::DontUseNativeDialog );
171+
#else
172+
QFont newFont = QFontDialog::getFont( &ok, mLegend->groupFont() );
173+
#endif
174+
if ( ok )
175+
{
176+
mLegend->setGroupFont( newFont );
177+
mLegend->adjustBoxSize();
178+
mLegend->update();
179+
}
180+
}
181+
}
182+
150183
void QgsComposerLegendWidget::on_mLayerFontButton_clicked()
151184
{
152185
if ( mLegend )
@@ -387,9 +420,44 @@ void QgsComposerLegendWidget::on_mUpdatePushButton_clicked()
387420
}
388421

389422
void QgsComposerLegendWidget::on_mUpdateAllPushButton_clicked()
423+
{
424+
updateLegend();
425+
}
426+
427+
void QgsComposerLegendWidget::on_mAddGroupButton_clicked()
428+
{
429+
if ( mLegend && mLegend->model() )
430+
{
431+
mLegend->model()->addGroup();
432+
mLegend->update();
433+
}
434+
}
435+
436+
void QgsComposerLegendWidget::updateLegend()
390437
{
391438
if ( mLegend )
392439
{
393-
mLegend->updateLegend();
440+
QgisApp* app = QgisApp::instance();
441+
if ( !app )
442+
{
443+
return;
444+
}
445+
446+
//get layer id list
447+
QStringList layerIdList;
448+
QgsMapCanvas* canvas = app->mapCanvas();
449+
if ( canvas )
450+
{
451+
QgsMapRenderer* renderer = canvas->mapRenderer();
452+
if ( renderer )
453+
{
454+
layerIdList = renderer->layerSet();
455+
}
456+
}
457+
458+
//and also group info
459+
QgsAppLegendInterface legendIface( app->legend() );
460+
QList< GroupLayerInfo > groupInfo = legendIface.groupLayerRelationship();
461+
mLegend->model()->setLayerSetAndGroups( layerIdList, groupInfo );
394462
}
395463
}

src/app/composer/qgscomposerlegendwidget.h

+4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class QgsComposerLegendWidget: public QWidget, private Ui::QgsComposerLegendWidg
4343
void on_mSymbolSpaceSpinBox_valueChanged( double d );
4444
void on_mIconLabelSpaceSpinBox_valueChanged( double d );
4545
void on_mTitleFontButton_clicked();
46+
void on_mGroupFontButton_clicked();
4647
void on_mLayerFontButton_clicked();
4748
void on_mItemFontButton_clicked();
4849
void on_mBoxSpaceSpinBox_valueChanged( double d );
@@ -54,11 +55,14 @@ class QgsComposerLegendWidget: public QWidget, private Ui::QgsComposerLegendWidg
5455
void on_mEditPushButton_clicked();
5556
void on_mUpdatePushButton_clicked();
5657
void on_mUpdateAllPushButton_clicked();
58+
void on_mAddGroupButton_clicked();
5759

5860
private:
5961
QgsComposerLegendWidget();
6062
/**Sets GUI according to state of mLegend*/
6163
void setGuiElements();
64+
/**Updates the legend layers and groups*/
65+
void updateLegend();
6266

6367
QgsComposerLegend* mLegend;
6468
};

src/app/legend/qgsapplegendinterface.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ QStringList QgsAppLegendInterface::groups()
8080
return mLegend->groups();
8181
}
8282

83+
QList< GroupLayerInfo > QgsAppLegendInterface::groupLayerRelationship()
84+
{
85+
if ( mLegend )
86+
{
87+
return mLegend->groupLayerRelationship();
88+
}
89+
return QList< GroupLayerInfo >();
90+
}
91+
8392
bool QgsAppLegendInterface::groupExists( int groupIndex )
8493
{
8594
QModelIndex mi = mLegend->model()->index( groupIndex, 0 );

src/app/legend/qgsapplegendinterface.h

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class QgsAppLegendInterface : public QgsLegendInterface
4444
//! Return a string list of groups
4545
QStringList groups();
4646

47+
//! Return the relationship between groups and layers in the legend
48+
QList< GroupLayerInfo > groupLayerRelationship();
49+
4750
//! Return all layers in the project in legend order
4851
QList< QgsMapLayer * > layers() const;
4952

src/app/legend/qgslegend.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,51 @@ QStringList QgsLegend::groups()
12401240
return groupList;
12411241
}
12421242

1243+
QList< GroupLayerInfo > QgsLegend::groupLayerRelationship()
1244+
{
1245+
QList< GroupLayerInfo > groupLayerList;
1246+
1247+
int nTopLevelItems = topLevelItemCount();
1248+
QTreeWidgetItem* currentTopLevelItem = 0;
1249+
1250+
for ( int i = 0; i < nTopLevelItems; ++i )
1251+
{
1252+
currentTopLevelItem = topLevelItem( i );
1253+
//layer?
1254+
QgsLegendLayer* lLayer = dynamic_cast<QgsLegendLayer*>( currentTopLevelItem );
1255+
if ( lLayer )
1256+
{
1257+
if ( lLayer->layer() )
1258+
{
1259+
QList<QString> layerList;
1260+
layerList.push_back( lLayer->layer()->getLayerID() );
1261+
groupLayerList.push_back( qMakePair( QString(), layerList ) );
1262+
}
1263+
}
1264+
//group?
1265+
QgsLegendGroup* lGroup = dynamic_cast<QgsLegendGroup*>( currentTopLevelItem );
1266+
if ( lGroup )
1267+
{
1268+
int nLayers = lGroup->childCount();
1269+
QList<QString> layerList;
1270+
for ( int i = 0; i < nLayers; ++i )
1271+
{
1272+
QgsLegendLayer* lLayer = dynamic_cast<QgsLegendLayer*>( lGroup->child( i ) );
1273+
if ( lLayer )
1274+
{
1275+
if ( lLayer->layer() )
1276+
{
1277+
layerList.push_back( lLayer->layer()->getLayerID() );
1278+
}
1279+
}
1280+
}
1281+
groupLayerList.push_back( qMakePair( lGroup->text( 0 ), layerList ) );
1282+
}
1283+
}
1284+
1285+
return groupLayerList;
1286+
}
1287+
12431288
/**Returns the first item in the hierarchy*/
12441289
QTreeWidgetItem* QgsLegend::firstItem()
12451290
{

src/app/legend/qgslegend.h

+8
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class QDomNode;
3636
class QMouseEvent;
3737
class QTreeWidgetItem;
3838

39+
//Information about relationship between groups and layers
40+
//key: group name (or null strings for single layers without groups)
41+
//value: containter with layer ids contained in the group
42+
typedef QPair< QString, QList<QString> > GroupLayerInfo;
43+
3944
/**
4045
\class QgsLegend
4146
\brief A Legend treeview for QGIS
@@ -123,6 +128,9 @@ class QgsLegend : public QTreeWidget
123128
/**Returns a string list of groups*/
124129
QStringList groups();
125130

131+
//! Return the relationship between groups and layers in the legend
132+
QList< GroupLayerInfo > groupLayerRelationship();
133+
126134
/**Returns the first item in the hierarchy*/
127135
QTreeWidgetItem* firstItem();
128136

src/app/qgisapp.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ class QgisApp : public QMainWindow
155155
/** Get the mapcanvas object from the app */
156156
QgsMapCanvas * mapCanvas() { return mMapCanvas; };
157157

158+
//! returns pointer to map legend
159+
QgsLegend *legend() { return mMapLegend; }
160+
158161
//! Set theme (icons)
159162
void setTheme( QString themeName = "default" );
160163
//! Setup the toolbar popup menus for a given theme
@@ -586,8 +589,6 @@ class QgisApp : public QMainWindow
586589

587590
//! refresh map canvas
588591
void refreshMapCanvas();
589-
//! returns pointer to map legend
590-
QgsLegend *legend() { return mMapLegend; }
591592

592593
//! starts/stops editing mode of the current layer
593594
void toggleEditing();

src/core/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ SET(QGIS_CORE_SRCS
9494
composer/qgscomposeritem.cpp
9595
composer/qgscomposeritemgroup.cpp
9696
composer/qgscomposerlabel.cpp
97+
composer/qgscomposerlegenditem.cpp
9798
composer/qgscomposerpicture.cpp
9899
composer/qgscomposermap.cpp
99100
composer/qgscomposertable.cpp

0 commit comments

Comments
 (0)