Skip to content
Permalink
Browse files
[FEATURE]: Export legend groups and layers with legendinterface and u…
…se 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
  • Loading branch information
mhugent committed May 13, 2010
1 parent 54a2306 commit caba437a41197362221dfb24ff37ba17fc4a8750
@@ -21,6 +21,11 @@
#include "qgscomposeritemwidget.h"
#include <QFontDialog>

#include "qgsapplegendinterface.h"
#include "qgisapp.h"
#include "qgsmapcanvas.h"
#include "qgsmaprenderer.h"

QgsComposerLegendWidget::QgsComposerLegendWidget( QgsComposerLegend* legend ): mLegend( legend )
{
setupUi( this );
@@ -34,6 +39,14 @@ QgsComposerLegendWidget::QgsComposerLegendWidget( QgsComposerLegend* legend ): m
mItemTreeView->setModel( legend->model() );
}

updateLegend();

mItemTreeView->setDragEnabled( true );
mItemTreeView->setAcceptDrops( true );
mItemTreeView->setDropIndicatorShown( true );
mItemTreeView->setDefaultDropAction( Qt::MoveAction );
mItemTreeView->setDragDropMode( QAbstractItemView::InternalMove );

setGuiElements();
}

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

void QgsComposerLegendWidget::on_mGroupFontButton_clicked()
{
if ( mLegend )
{
bool ok;
#if defined(Q_WS_MAC) && QT_VERSION >= 0x040500 && !defined(__LP64__)
// Native Mac dialog works only for 64 bit Cocoa (observed in Qt 4.5.2, probably a Qt bug)
QFont newFont = QFontDialog::getFont( &ok, mLegend->groupFont(), this, QString(), QFontDialog::DontUseNativeDialog );
#else
QFont newFont = QFontDialog::getFont( &ok, mLegend->groupFont() );
#endif
if ( ok )
{
mLegend->setGroupFont( newFont );
mLegend->adjustBoxSize();
mLegend->update();
}
}
}

void QgsComposerLegendWidget::on_mLayerFontButton_clicked()
{
if ( mLegend )
@@ -387,9 +420,44 @@ void QgsComposerLegendWidget::on_mUpdatePushButton_clicked()
}

void QgsComposerLegendWidget::on_mUpdateAllPushButton_clicked()
{
updateLegend();
}

void QgsComposerLegendWidget::on_mAddGroupButton_clicked()
{
if ( mLegend && mLegend->model() )
{
mLegend->model()->addGroup();
mLegend->update();
}
}

void QgsComposerLegendWidget::updateLegend()
{
if ( mLegend )
{
mLegend->updateLegend();
QgisApp* app = QgisApp::instance();
if ( !app )
{
return;
}

//get layer id list
QStringList layerIdList;
QgsMapCanvas* canvas = app->mapCanvas();
if ( canvas )
{
QgsMapRenderer* renderer = canvas->mapRenderer();
if ( renderer )
{
layerIdList = renderer->layerSet();
}
}

//and also group info
QgsAppLegendInterface legendIface( app->legend() );
QList< GroupLayerInfo > groupInfo = legendIface.groupLayerRelationship();
mLegend->model()->setLayerSetAndGroups( layerIdList, groupInfo );
}
}
@@ -43,6 +43,7 @@ class QgsComposerLegendWidget: public QWidget, private Ui::QgsComposerLegendWidg
void on_mSymbolSpaceSpinBox_valueChanged( double d );
void on_mIconLabelSpaceSpinBox_valueChanged( double d );
void on_mTitleFontButton_clicked();
void on_mGroupFontButton_clicked();
void on_mLayerFontButton_clicked();
void on_mItemFontButton_clicked();
void on_mBoxSpaceSpinBox_valueChanged( double d );
@@ -54,11 +55,14 @@ class QgsComposerLegendWidget: public QWidget, private Ui::QgsComposerLegendWidg
void on_mEditPushButton_clicked();
void on_mUpdatePushButton_clicked();
void on_mUpdateAllPushButton_clicked();
void on_mAddGroupButton_clicked();

private:
QgsComposerLegendWidget();
/**Sets GUI according to state of mLegend*/
void setGuiElements();
/**Updates the legend layers and groups*/
void updateLegend();

QgsComposerLegend* mLegend;
};
@@ -80,6 +80,15 @@ QStringList QgsAppLegendInterface::groups()
return mLegend->groups();
}

QList< GroupLayerInfo > QgsAppLegendInterface::groupLayerRelationship()
{
if ( mLegend )
{
return mLegend->groupLayerRelationship();
}
return QList< GroupLayerInfo >();
}

bool QgsAppLegendInterface::groupExists( int groupIndex )
{
QModelIndex mi = mLegend->model()->index( groupIndex, 0 );
@@ -44,6 +44,9 @@ class QgsAppLegendInterface : public QgsLegendInterface
//! Return a string list of groups
QStringList groups();

//! Return the relationship between groups and layers in the legend
QList< GroupLayerInfo > groupLayerRelationship();

//! Return all layers in the project in legend order
QList< QgsMapLayer * > layers() const;

@@ -1240,6 +1240,51 @@ QStringList QgsLegend::groups()
return groupList;
}

QList< GroupLayerInfo > QgsLegend::groupLayerRelationship()
{
QList< GroupLayerInfo > groupLayerList;

int nTopLevelItems = topLevelItemCount();
QTreeWidgetItem* currentTopLevelItem = 0;

for ( int i = 0; i < nTopLevelItems; ++i )
{
currentTopLevelItem = topLevelItem( i );
//layer?
QgsLegendLayer* lLayer = dynamic_cast<QgsLegendLayer*>( currentTopLevelItem );
if ( lLayer )
{
if ( lLayer->layer() )
{
QList<QString> layerList;
layerList.push_back( lLayer->layer()->getLayerID() );
groupLayerList.push_back( qMakePair( QString(), layerList ) );
}
}
//group?
QgsLegendGroup* lGroup = dynamic_cast<QgsLegendGroup*>( currentTopLevelItem );
if ( lGroup )
{
int nLayers = lGroup->childCount();
QList<QString> layerList;
for ( int i = 0; i < nLayers; ++i )
{
QgsLegendLayer* lLayer = dynamic_cast<QgsLegendLayer*>( lGroup->child( i ) );
if ( lLayer )
{
if ( lLayer->layer() )
{
layerList.push_back( lLayer->layer()->getLayerID() );
}
}
}
groupLayerList.push_back( qMakePair( lGroup->text( 0 ), layerList ) );
}
}

return groupLayerList;
}

/**Returns the first item in the hierarchy*/
QTreeWidgetItem* QgsLegend::firstItem()
{
@@ -36,6 +36,11 @@ class QDomNode;
class QMouseEvent;
class QTreeWidgetItem;

//Information about relationship between groups and layers
//key: group name (or null strings for single layers without groups)
//value: containter with layer ids contained in the group
typedef QPair< QString, QList<QString> > GroupLayerInfo;

/**
\class QgsLegend
\brief A Legend treeview for QGIS
@@ -123,6 +128,9 @@ class QgsLegend : public QTreeWidget
/**Returns a string list of groups*/
QStringList groups();

//! Return the relationship between groups and layers in the legend
QList< GroupLayerInfo > groupLayerRelationship();

/**Returns the first item in the hierarchy*/
QTreeWidgetItem* firstItem();

@@ -155,6 +155,9 @@ class QgisApp : public QMainWindow
/** Get the mapcanvas object from the app */
QgsMapCanvas * mapCanvas() { return mMapCanvas; };

//! returns pointer to map legend
QgsLegend *legend() { return mMapLegend; }

//! Set theme (icons)
void setTheme( QString themeName = "default" );
//! Setup the toolbar popup menus for a given theme
@@ -586,8 +589,6 @@ class QgisApp : public QMainWindow

//! refresh map canvas
void refreshMapCanvas();
//! returns pointer to map legend
QgsLegend *legend() { return mMapLegend; }

//! starts/stops editing mode of the current layer
void toggleEditing();
@@ -94,6 +94,7 @@ SET(QGIS_CORE_SRCS
composer/qgscomposeritem.cpp
composer/qgscomposeritemgroup.cpp
composer/qgscomposerlabel.cpp
composer/qgscomposerlegenditem.cpp
composer/qgscomposerpicture.cpp
composer/qgscomposermap.cpp
composer/qgscomposertable.cpp

0 comments on commit caba437

Please sign in to comment.