Skip to content

Commit

Permalink
applied patch from NathanW for ticket #3339
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@14951 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
timlinux committed Dec 21, 2010
1 parent 80a4be9 commit f672e65
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ SET(QGIS_APP_SRCS
composer/qgscomposershapewidget.cpp
composer/qgscomposertablewidget.cpp
composer/qgscomposerlegenditemdialog.cpp
composer/qgscomposerlegendlayersdialog.cpp
composer/qgscomposerlegendwidget.cpp
composer/qgscompositionwidget.cpp
composer/qgsitempositiondialog.cpp
Expand Down
53 changes: 53 additions & 0 deletions src/app/composer/qgscomposerlegendlayersdialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/***************************************************************************
qgscomposerlegendlayersdialog.cpp
-------------------------------
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgscomposerlegendlayersdialog.h"

#include <QStandardItem>

QgsComposerLegendLayersDialog::QgsComposerLegendLayersDialog( QList<QgsMapLayer*> layers, QWidget* parent ): QDialog( parent )
{
setupUi( this );

QList<QgsMapLayer*>::iterator layerIt = layers.begin();
for (; layerIt != layers.end(); ++layerIt )
{
QListWidgetItem* item = new QListWidgetItem(( *layerIt )->name(), listMapLayers );
mItemLayerMap.insert( item, *layerIt );
}
}

QgsComposerLegendLayersDialog::QgsComposerLegendLayersDialog(): QDialog( 0 )
{

}

QgsComposerLegendLayersDialog::~QgsComposerLegendLayersDialog()
{

}

QgsMapLayer* QgsComposerLegendLayersDialog::selectedLayer()
{
QListWidgetItem* item = listMapLayers->currentItem();
if ( !item )
{
return 0;
}

QMap<QListWidgetItem*, QgsMapLayer*>::iterator it = mItemLayerMap.find( item );
QgsMapLayer* c = 0;
c = it.value();
return c;
}
38 changes: 38 additions & 0 deletions src/app/composer/qgscomposerlegendlayersdialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/***************************************************************************
qgscomposerlegenditemdialog.h
-----------------------------
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSCOMPOSERLEGENDLAYERSDIALOG_H
#define QGSCOMPOSERLEGENDLAYERSDIALOG_H

#include "ui_qgscomposerlegendlayersdialogbase.h"
#include "qgsmaplayer.h"

/** \ingroup MapComposer
* A dialog to add new layers to the legend.
* */
class QgsComposerLegendLayersDialog: private Ui::QgsComposerLegendLayersDialogBase, public QDialog
{
public:
QgsComposerLegendLayersDialog( QList<QgsMapLayer*> layers, QWidget* parent = 0 );
~QgsComposerLegendLayersDialog();
QgsMapLayer* selectedLayer();

private:
QgsComposerLegendLayersDialog(); //forbidden

/**Stores the relation between items and map layer pointers. */
QMap<QListWidgetItem*, QgsMapLayer*> mItemLayerMap;
};

#endif //QGSCOMPOSERLEGENDITEMDIALOG_H
62 changes: 61 additions & 1 deletion src/app/composer/qgscomposerlegendwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,29 @@
#include "qgscomposerlegendwidget.h"
#include "qgscomposerlegend.h"
#include "qgscomposerlegenditemdialog.h"
#include "qgscomposerlegendlayersdialog.h"
#include "qgscomposeritemwidget.h"
#include <QFontDialog>

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

#include <QMessageBox>

QgsComposerLegendWidget::QgsComposerLegendWidget( QgsComposerLegend* legend ): mLegend( legend )
{
setupUi( this );

// setup icons
mAddToolButton->setIcon( QIcon( QgsApplication::iconPath( "symbologyAdd.png" ) ) );
mEditPushButton->setIcon( QIcon( QgsApplication::iconPath( "symbologyEdit.png" ) ) );
mRemoveToolButton->setIcon( QIcon( QgsApplication::iconPath( "symbologyRemove.png" ) ) );
mMoveUpToolButton->setIcon( QIcon( QgsApplication::iconPath( "symbologyUp.png" ) ) );
mMoveDownToolButton->setIcon( QIcon( QgsApplication::iconPath( "symbologyDown.png" ) ) );

//add widget for item properties
QgsComposerItemWidget* itemPropertiesWidget = new QgsComposerItemWidget( this, legend );
toolBox->addItem( itemPropertiesWidget, tr( "Item Options" ) );
Expand Down Expand Up @@ -77,7 +88,6 @@ void QgsComposerLegendWidget::setGuiElements()
blockSignals( false );
}


void QgsComposerLegendWidget::on_mTitleLineEdit_textChanged( const QString& text )
{
if ( mLegend )
Expand Down Expand Up @@ -360,6 +370,56 @@ void QgsComposerLegendWidget::on_mMoveUpToolButton_clicked()
mLegend->endCommand();
}

void QgsComposerLegendWidget::on_mCheckBoxAutoUpdate_stateChanged ( int state )
{
if ( state == Qt::Checked )
{
mLegend->model()->setAutoUpdate( true );
}
else
{
mLegend->model()->setAutoUpdate( false );
}
}

void QgsComposerLegendWidget::on_mAddToolButton_clicked()
{
if ( !mLegend )
{
return;
}

QStandardItemModel* itemModel = qobject_cast<QStandardItemModel *>( mItemTreeView->model() );
if ( !itemModel )
{
return;
}

QgisApp* app = QgisApp::instance();
if ( !app )
{
return;
}

QgsMapCanvas* canvas = app->mapCanvas();
if ( canvas )
{
QList<QgsMapLayer*> layers = canvas->layers();

QgsComposerLegendLayersDialog addDialog( layers );
if ( addDialog.exec() == QDialog::Accepted )
{
QgsMapLayer* layer = addDialog.selectedLayer();
if ( layer )
{
mLegend->beginCommand( "Legend item added" );
mLegend->model()->addLayer( layer );
mLegend->endCommand();
}
}
}
}

void QgsComposerLegendWidget::on_mRemoveToolButton_clicked()
{
if ( !mLegend )
Expand Down
2 changes: 2 additions & 0 deletions src/app/composer/qgscomposerlegendwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ class QgsComposerLegendWidget: public QWidget, private Ui::QgsComposerLegendWidg
void on_mLayerFontButton_clicked();
void on_mItemFontButton_clicked();
void on_mBoxSpaceSpinBox_valueChanged( double d );
void on_mCheckBoxAutoUpdate_stateChanged (int state );

//item manipulation
void on_mMoveDownToolButton_clicked();
void on_mMoveUpToolButton_clicked();
void on_mRemoveToolButton_clicked();
void on_mAddToolButton_clicked();
void on_mEditPushButton_clicked();
void on_mUpdatePushButton_clicked();
void on_mUpdateAllPushButton_clicked();
Expand Down
23 changes: 23 additions & 0 deletions src/core/composer/qgslegendmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <QDomElement>
#include <QMimeData>
#include <QSettings>
#include <QMessageBox>

QgsLegendModel::QgsLegendModel(): QStandardItemModel()
{
Expand All @@ -40,6 +41,7 @@ QgsLegendModel::QgsLegendModel(): QStandardItemModel()
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( removeLayer( const QString& ) ) );
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( addLayer( QgsMapLayer* ) ) );
}

setItemPrototype( new QgsComposerSymbolItem() );

QWidgetList topLevelWidgets = QApplication::topLevelWidgets();
Expand Down Expand Up @@ -674,3 +676,24 @@ bool QgsLegendModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
emit layersChanged();
return true;
}

void QgsLegendModel::setAutoUpdate(bool autoUpdate)
{
mAutoUpdate = autoUpdate;
if ( autoUpdate )
{
if ( QgsMapLayerRegistry::instance() )
{
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( removeLayer( const QString& ) ) );
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( addLayer( QgsMapLayer* ) ) );
}
}
else
{
if ( QgsMapLayerRegistry::instance() )
{
disconnect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( removeLayer( const QString& ) ) );
disconnect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( addLayer( QgsMapLayer* ) ) );
}
}
}
5 changes: 5 additions & 0 deletions src/core/composer/qgslegendmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class CORE_EXPORT QgsLegendModel: public QStandardItemModel
/**Implements the drop operation*/
bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent );

void setAutoUpdate( bool autoUpdate );
bool AutoUpdaet() { return mAutoUpdate; }

public slots:
void removeLayer( const QString& layerId );
Expand Down Expand Up @@ -112,6 +114,9 @@ class CORE_EXPORT QgsLegendModel: public QStandardItemModel
/**True if this application has toplevel windows (normally true). If this is false, this means that the application
might not have a running x-server on unix systems and so QPixmap and QIcon cannot be used*/
bool mHasTopLevelWindow;

/**True if the legend is auto updated when layers are added or removed from the map canvas */
bool mAutoUpdate;
};

#endif
67 changes: 67 additions & 0 deletions src/ui/qgscomposerlegendlayersdialogbase.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>QgsComposerLegendLayersDialogBase</class>
<widget class="QDialog" name="QgsComposerLegendLayersDialogBase">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>252</width>
<height>194</height>
</rect>
</property>
<property name="windowTitle">
<string>Add layer to legend</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QListWidget" name="listMapLayers"/>
</item>
<item row="1" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>QgsComposerLegendLayersDialogBase</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>QgsComposerLegendLayersDialogBase</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>
Loading

0 comments on commit f672e65

Please sign in to comment.