Skip to content

Commit

Permalink
[FEATURE]: Export legend groups and layers with legendinterface and u…
Browse files Browse the repository at this point in the history
…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 caba437
Show file tree
Hide file tree
Showing 16 changed files with 944 additions and 321 deletions.
70 changes: 69 additions & 1 deletion src/app/composer/qgscomposerlegendwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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();
}

Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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 );
}
}
4 changes: 4 additions & 0 deletions src/app/composer/qgscomposerlegendwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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;
};
Expand Down
9 changes: 9 additions & 0 deletions src/app/legend/qgsapplegendinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
3 changes: 3 additions & 0 deletions src/app/legend/qgsapplegendinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
45 changes: 45 additions & 0 deletions src/app/legend/qgslegend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
8 changes: 8 additions & 0 deletions src/app/legend/qgslegend.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down
5 changes: 3 additions & 2 deletions src/app/qgisapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit caba437

Please sign in to comment.