Skip to content

Commit

Permalink
Improve layer/group selection dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Hugentobler committed Oct 16, 2012
1 parent 78ae218 commit 166d5c8
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 83 deletions.
4 changes: 2 additions & 2 deletions src/app/CMakeLists.txt
Expand Up @@ -28,7 +28,6 @@ SET(QGIS_APP_SRCS
qgsdecorationscalebardialog.cpp
qgsdecorationgrid.cpp
qgsdecorationgriddialog.cpp
qgsembedlayerdialog.cpp
qgsformannotationdialog.cpp
qgshtmlannotationdialog.cpp
qgsdelattrdialog.cpp
Expand Down Expand Up @@ -95,6 +94,7 @@ SET(QGIS_APP_SRCS
qgspluginmanager.cpp
qgspluginmetadata.cpp
qgspluginregistry.cpp
qgsprojectlayergroupdialog.cpp
qgsprojectproperties.cpp
qgsrastercalcdialog.cpp
qgsrasterlayerproperties.cpp
Expand Down Expand Up @@ -189,7 +189,6 @@ SET (QGIS_APP_MOC_HDRS
qgsdelattrdialog.h
qgsdiagramproperties.h
qgsdisplayangle.h
qgsembedlayerdialog.h
qgsfeatureaction.h
qgsfieldcalculator.h
qgsformannotationdialog.h
Expand Down Expand Up @@ -241,6 +240,7 @@ SET (QGIS_APP_MOC_HDRS
qgsoptions.h
qgspastetransformations.h
qgspluginmanager.h
qgsprojectlayergroupdialog.h
qgsprojectproperties.h
qgsrastercalcdialog.h
qgsrasterlayerproperties.h
Expand Down
29 changes: 16 additions & 13 deletions src/app/qgisapp.cpp
Expand Up @@ -119,7 +119,6 @@
#include "qgsdecorationnortharrow.h"
#include "qgsdecorationscalebar.h"
#include "qgsdecorationgrid.h"
#include "qgsembedlayerdialog.h"
#include "qgsencodingfiledialog.h"
#include "qgsexception.h"
#include "qgsfeature.h"
Expand Down Expand Up @@ -156,6 +155,7 @@
#include "qgspoint.h"
#include "qgshandlebadlayers.h"
#include "qgsproject.h"
#include "qgsprojectlayergroupdialog.h"
#include "qgsprojectproperties.h"
#include "qgsproviderregistry.h"
#include "qgspythonrunner.h"
Expand Down Expand Up @@ -4422,7 +4422,7 @@ void QgisApp::deletePrintComposers()
for ( ; it != mPrintComposers.end(); ++it )
{
emit composerWillBeRemoved(( *it )->view() );
delete ( (*it)->composition() );
delete(( *it )->composition() );
delete( *it );
}
mPrintComposers.clear();
Expand Down Expand Up @@ -5867,30 +5867,33 @@ void QgisApp::addMapLayer( QgsMapLayer *theMapLayer )
void QgisApp::embedLayers()
{
//dialog to select groups/layers from other project files
QgsEmbedLayerDialog d( this );
QgsProjectLayerGroupDialog d( this );
if ( d.exec() == QDialog::Accepted )
{
mMapCanvas->freeze( true );

QString projectFile = d.selectedProjectFile();

//groups
QList< QPair < QString, QString > > groups = d.embeddedGroups();
QList< QPair < QString, QString > >::const_iterator groupIt = groups.constBegin();
QStringList groups = d.selectedGroups();
QStringList::const_iterator groupIt = groups.constBegin();
for ( ; groupIt != groups.constEnd(); ++groupIt )
{
mMapLegend->addEmbeddedGroup( groupIt->first, groupIt->second );
mMapLegend->addEmbeddedGroup( *groupIt, projectFile );
}

//layers
//layer ids
QList<QDomNode> brokenNodes;
QList< QPair< QgsVectorLayer*, QDomElement > > vectorLayerList;

QList< QPair < QString, QString > > layers = d.embeddedLayers();
QList< QPair < QString, QString > >::const_iterator layerIt = layers.constBegin();
for ( ; layerIt != layers.constEnd(); ++layerIt )
QStringList layerIds = d.selectedLayerIds();
QStringList::const_iterator layerIt = layerIds.constBegin();
for ( ; layerIt != layerIds.constEnd(); ++layerIt )
{
QgsProject::instance()->createEmbeddedLayer( layerIt->first, layerIt->second, brokenNodes, vectorLayerList );
QgsProject::instance()->createEmbeddedLayer( *layerIt, projectFile, brokenNodes, vectorLayerList );
}

mMapCanvas->freeze( false );
if ( groups.size() > 0 || layers.size() > 0 )
if ( groups.size() > 0 || layerIds.size() > 0 )
{
mMapCanvas->refresh();
}
Expand Down
Expand Up @@ -12,7 +12,7 @@
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsembedlayerdialog.h"
#include "qgsprojectlayergroupdialog.h"
#include "qgsproject.h"
#include "qgisapp.h"
#include "qgsapplication.h"
Expand All @@ -23,7 +23,7 @@
#include <QMessageBox>
#include <QSettings>

QgsEmbedLayerDialog::QgsEmbedLayerDialog( QWidget * parent, const QString& projectFile, Qt::WindowFlags f ): QDialog( parent, f )
QgsProjectLayerGroupDialog::QgsProjectLayerGroupDialog( QWidget * parent, const QString& projectFile, Qt::WindowFlags f ): QDialog( parent, f )
{
setupUi( this );

Expand All @@ -42,60 +42,63 @@ QgsEmbedLayerDialog::QgsEmbedLayerDialog( QWidget * parent, const QString& proje
QObject::connect( mButtonBox, SIGNAL( rejected() ), this, SLOT( reject() ) );
}

QgsEmbedLayerDialog::~QgsEmbedLayerDialog()
QgsProjectLayerGroupDialog::~QgsProjectLayerGroupDialog()
{
QSettings settings;
settings.setValue( "/Windows/EmbedLayer/geometry", saveGeometry() );
}

QList< QPair < QString, QString > > QgsEmbedLayerDialog::embeddedGroups() const
QStringList QgsProjectLayerGroupDialog::selectedGroups() const
{
QList< QPair < QString, QString > > result;

QList<QTreeWidgetItem*> items = mTreeWidget->selectedItems();
QList<QTreeWidgetItem*>::iterator itemIt = items.begin();
for ( ; itemIt != items.end(); ++itemIt )
{
if (( *itemIt )->data( 0, Qt::UserRole ).toString() == "group" )
QStringList groups;
QList<QTreeWidgetItem*> items = mTreeWidget->selectedItems();
QList<QTreeWidgetItem*>::iterator itemIt = items.begin();
for ( ; itemIt != items.end(); ++itemIt )
{
result.push_back( qMakePair(( *itemIt )->text( 0 ), mProjectPath ) );
if (( *itemIt )->data( 0, Qt::UserRole ).toString() == "group" )
{
groups.push_back( ( *itemIt )->text( 0 ) );
}
}
}

return result;
return groups;
}

QList< QPair < QString, QString > > QgsEmbedLayerDialog::embeddedLayers() const
QStringList QgsProjectLayerGroupDialog::selectedLayerIds() const
{
QList< QPair < QString, QString > > result;

QList<QTreeWidgetItem*> items = mTreeWidget->selectedItems();
QList<QTreeWidgetItem*>::iterator itemIt = items.begin();
for ( ; itemIt != items.end(); ++itemIt )
{
if (( *itemIt )->data( 0, Qt::UserRole ).toString() == "layer" )
QStringList layerIds;
QList<QTreeWidgetItem*> items = mTreeWidget->selectedItems();
QList<QTreeWidgetItem*>::iterator itemIt = items.begin();
for ( ; itemIt != items.end(); ++itemIt )
{
result.push_back( qMakePair(( *itemIt )->data( 0, Qt::UserRole + 1 ).toString(), mProjectPath ) );
if (( *itemIt )->data( 0, Qt::UserRole ).toString() == "layer" )
{
layerIds.push_back( ( *itemIt )->data( 0, Qt::UserRole + 1 ).toString() );
}
}
}
return result;
return layerIds;
}

QStringList QgsEmbedLayerDialog::layersAndGroupNames() const
QStringList QgsProjectLayerGroupDialog::selectedLayerNames() const
{
QStringList result;

QList<QTreeWidgetItem*> items = mTreeWidget->selectedItems();
QList<QTreeWidgetItem*>::iterator itemIt = items.begin();
for ( ; itemIt != items.end(); ++itemIt )
{
result.push_back(( *itemIt )->text( 0 ) );
}
QStringList layerNames;
QList<QTreeWidgetItem*> items = mTreeWidget->selectedItems();
QList<QTreeWidgetItem*>::iterator itemIt = items.begin();
for ( ; itemIt != items.end(); ++itemIt )
{
if (( *itemIt )->data( 0, Qt::UserRole ).toString() == "layer" )
{
layerNames.push_back( ( *itemIt )->text( 0 ) );
}
}
return layerNames;
}

return result;
QString QgsProjectLayerGroupDialog::selectedProjectFile() const
{
return mProjectFileLineEdit->text();
}

void QgsEmbedLayerDialog::on_mBrowseFileToolButton_clicked()
void QgsProjectLayerGroupDialog::on_mBrowseFileToolButton_clicked()
{
//line edit might emit editingFinished signal when loosing focus
mProjectFileLineEdit->blockSignals( true );
Expand All @@ -113,12 +116,12 @@ void QgsEmbedLayerDialog::on_mBrowseFileToolButton_clicked()
mProjectFileLineEdit->blockSignals( false );
}

void QgsEmbedLayerDialog::on_mProjectFileLineEdit_editingFinished()
void QgsProjectLayerGroupDialog::on_mProjectFileLineEdit_editingFinished()
{
changeProjectFile();
}

void QgsEmbedLayerDialog::changeProjectFile()
void QgsProjectLayerGroupDialog::changeProjectFile()
{
QFile projectFile( mProjectFileLineEdit->text() );
if ( !projectFile.exists() )
Expand Down Expand Up @@ -178,7 +181,7 @@ void QgsEmbedLayerDialog::changeProjectFile()
mProjectPath = mProjectFileLineEdit->text();
}

void QgsEmbedLayerDialog::addLegendGroupToTreeWidget( const QDomElement& groupElem, QTreeWidgetItem* parent )
void QgsProjectLayerGroupDialog::addLegendGroupToTreeWidget( const QDomElement& groupElem, QTreeWidgetItem* parent )
{
QDomNodeList groupChildren = groupElem.childNodes();
QDomElement currentChildElem;
Expand Down Expand Up @@ -215,7 +218,7 @@ void QgsEmbedLayerDialog::addLegendGroupToTreeWidget( const QDomElement& groupEl
}
}

void QgsEmbedLayerDialog::addLegendLayerToTreeWidget( const QDomElement& layerElem, QTreeWidgetItem* parent )
void QgsProjectLayerGroupDialog::addLegendLayerToTreeWidget( const QDomElement& layerElem, QTreeWidgetItem* parent )
{
if ( layerElem.attribute( "embedded" ) == "1" )
{
Expand All @@ -236,7 +239,7 @@ void QgsEmbedLayerDialog::addLegendLayerToTreeWidget( const QDomElement& layerEl
item->setData( 0, Qt::UserRole + 1, layerElem.firstChildElement( "filegroup" ).firstChildElement( "legendlayerfile" ).attribute( "layerid" ) );
}

void QgsEmbedLayerDialog::on_mTreeWidget_itemSelectionChanged()
void QgsProjectLayerGroupDialog::on_mTreeWidget_itemSelectionChanged()
{
mTreeWidget->blockSignals( true );
QList<QTreeWidgetItem*> items = mTreeWidget->selectedItems();
Expand All @@ -249,7 +252,7 @@ void QgsEmbedLayerDialog::on_mTreeWidget_itemSelectionChanged()
mTreeWidget->blockSignals( false );
}

void QgsEmbedLayerDialog::unselectChildren( QTreeWidgetItem* item )
void QgsProjectLayerGroupDialog::unselectChildren( QTreeWidgetItem* item )
{
if ( !item )
{
Expand All @@ -265,7 +268,7 @@ void QgsEmbedLayerDialog::unselectChildren( QTreeWidgetItem* item )
}
}

void QgsEmbedLayerDialog::on_mButtonBox_accepted()
void QgsProjectLayerGroupDialog::on_mButtonBox_accepted()
{
QSettings s;
QFileInfo fi( mProjectPath );
Expand Down
@@ -1,6 +1,6 @@
/***************************************************************************
qgsembedlayerdialog.h
---------------------
qgsprojectlayergroupdialog.h
----------------------------
begin : June 2011
copyright : (C) 2011 by Marco Hugentobler
email : marco dot hugentobler at sourcepole dot ch
Expand All @@ -12,28 +12,27 @@
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSEMBEDLAYERSDIALOG_H
#define QGSEMBEDLAYERSDIALOG_H
#ifndef QGSPROJECTLAYERGROUPDIALOG_H
#define QGSPROJECTLAYERGROUPDIALOG_H

#include "QDialog"
#include "ui_qgsembedlayerdialogbase.h"
#include "ui_qgsprojectlayergroupdialogbase.h"

class QDomElement;

class QgsEmbedLayerDialog: public QDialog, private Ui::QgsEmbedLayerDialogBase
/**A dialog to select layers and groups from a qgs project*/
class QgsProjectLayerGroupDialog: public QDialog, private Ui::QgsProjectLayerGroupDialogBase
{
Q_OBJECT
public:
/**Constructor. If a project file is given, the groups/layers are displayed directly and the file selection hidden*/
QgsEmbedLayerDialog( QWidget * parent = 0, const QString& projectFile = QString(), Qt::WindowFlags f = 0 );
~QgsEmbedLayerDialog();
QgsProjectLayerGroupDialog( QWidget * parent = 0, const QString& projectFile = QString(), Qt::WindowFlags f = 0 );
~QgsProjectLayerGroupDialog();

/**Returns name / projectfiles of groups to embed*/
QList< QPair < QString, QString > > embeddedGroups() const;
/**Returns layer id / projectfiles of single layers to embed*/
QList< QPair < QString, QString > > embeddedLayers() const;
/**Returns selected layer and group names*/
QStringList layersAndGroupNames() const;
QStringList selectedGroups() const;
QStringList selectedLayerIds() const;
QStringList selectedLayerNames() const;
QString selectedProjectFile() const;

private slots:
void on_mBrowseFileToolButton_clicked();
Expand All @@ -49,4 +48,4 @@ class QgsEmbedLayerDialog: public QDialog, private Ui::QgsEmbedLayerDialogBase
QString mProjectPath;
};

#endif // QGSEMBEDLAYERSDIALOG_H
#endif //QGSPROJECTLAYERGROUPDIALOG_H
24 changes: 17 additions & 7 deletions src/app/qgsprojectproperties.cpp
Expand Up @@ -23,13 +23,13 @@
#include "qgscomposer.h"
#include "qgscontexthelp.h"
#include "qgscoordinatetransform.h"
#include "qgsembedlayerdialog.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayer.h"
#include "qgsmaplayerregistry.h"
#include "qgsmaprenderer.h"
#include "qgsproject.h"
#include "qgsprojectlayergroupdialog.h"
#include "qgsrenderer.h"
#include "qgssnappingdialog.h"
#include "qgsrasterlayer.h"
Expand Down Expand Up @@ -828,17 +828,27 @@ void QgsProjectProperties::on_mRemoveWMSComposerButton_clicked()

void QgsProjectProperties::on_mAddLayerRestrictionButton_clicked()
{
QgsEmbedLayerDialog d( this, QgsProject::instance()->fileName() );
QgsProjectLayerGroupDialog d( this, QgsProject::instance()->fileName() );
d.setWindowTitle( tr( "Select restricted layers and groups" ) );
if ( d.exec() == QDialog::Accepted )
{
QStringList names = d.layersAndGroupNames();
QStringList::const_iterator nameIt = names.constBegin();
for ( ; nameIt != names.constEnd(); ++nameIt )
QStringList layerNames = d.selectedLayerNames();
QStringList::const_iterator layerIt = layerNames.constBegin();
for ( ; layerIt != layerNames.constEnd(); ++layerIt )
{
if ( mLayerRestrictionsListWidget->findItems( *nameIt, Qt::MatchExactly ).size() < 1 )
if ( mLayerRestrictionsListWidget->findItems( *layerIt, Qt::MatchExactly ).size() < 1 )
{
mLayerRestrictionsListWidget->addItem( *nameIt );
mLayerRestrictionsListWidget->addItem( *layerIt );
}
}

QStringList groups = d.selectedGroups();
QStringList::const_iterator groupIt = groups.constBegin();
for ( ; groupIt != groups.constEnd(); ++groupIt )
{
if ( mLayerRestrictionsListWidget->findItems( *groupIt, Qt::MatchExactly ).size() < 1 )
{
mLayerRestrictionsListWidget->addItem( *groupIt );
}
}
}
Expand Down
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>QgsEmbedLayerDialogBase</class>
<widget class="QDialog" name="QgsEmbedLayerDialogBase">
<class>QgsProjectLayerGroupDialogBase</class>
<widget class="QDialog" name="QgsProjectLayerGroupDialogBase">
<property name="geometry">
<rect>
<x>0</x>
Expand Down

0 comments on commit 166d5c8

Please sign in to comment.