Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE] Support for custom plugin layers. Applied patch from #2392
…contributed by Mathias Walker. Thanks!

Some parts modified to make plugin layers easier to use and more robust.


git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12834 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Jan 26, 2010
1 parent 272f499 commit f951d16
Show file tree
Hide file tree
Showing 14 changed files with 387 additions and 7 deletions.
2 changes: 2 additions & 0 deletions python/core/core.sip
Expand Up @@ -43,6 +43,8 @@
%Include qgsmarkercatalogue.sip
%Include qgsmessageoutput.sip
%Include qgsoverlayobject.sip
%Include qgspluginlayer.sip
%Include qgspluginlayerregistry.sip
%Include qgspoint.sip
%Include qgsproject.sip
%Include qgsprovidermetadata.sip
Expand Down
12 changes: 10 additions & 2 deletions python/core/qgsmaplayer.sip
Expand Up @@ -22,6 +22,10 @@ class QgsMapLayer : QObject
{
sipClass = sipClass_QgsRasterLayer;
}
else if (layer->type() == QgsMapLayer::PluginLayer)
{
sipClass = sipClass_QgsPluginLayer;
}
}
else
{
Expand All @@ -35,7 +39,8 @@ public:
enum LayerType
{
VectorLayer,
RasterLayer
RasterLayer,
PluginLayer
};

/** Constructor
Expand Down Expand Up @@ -109,7 +114,7 @@ public:


/** True if the layer can be edited */
virtual bool isEditable() const = 0;
virtual bool isEditable() const;

/** sets state from Dom document
@param layer_node is Dom node corresponding to ``maplayer'' tag
Expand Down Expand Up @@ -323,6 +328,9 @@ signals:

protected:

/** set whether layer is valid or not - should be used in constructor */
void setValid( bool valid );

/** called by readXML(), used by children to read state specific to them from
project files.
*/
Expand Down
14 changes: 14 additions & 0 deletions python/core/qgspluginlayer.sip
@@ -0,0 +1,14 @@

class QgsPluginLayer : QgsMapLayer
{
%TypeHeaderCode
#include "qgspluginlayer.h"
%End

public:
QgsPluginLayer(QString layerType, QString layerName = QString());

/** return plugin layer type (the same as used in QgsPluginLayerRegistry) */
QString pluginLayerType();

};
52 changes: 52 additions & 0 deletions python/core/qgspluginlayerregistry.sip
@@ -0,0 +1,52 @@

class QgsPluginLayerType
{
%TypeHeaderCode
#include "qgspluginlayerregistry.h"
%End
public:

QgsPluginLayerType(QString name);
virtual ~QgsPluginLayerType();

QString name();

/** return new layer of this type. Return NULL on error */
virtual QgsPluginLayer* createLayer() /Factory/;

/** show plugin layer properties dialog. Return FALSE if the dialog cannot be shown. */
virtual bool showLayerProperties(QgsPluginLayer* layer);

};


class QgsPluginLayerRegistry
{
%TypeHeaderCode
#include "qgspluginlayerregistry.h"
%End
public:

/** means of accessing canonical single instance */
static QgsPluginLayerRegistry* instance();

~QgsPluginLayerRegistry();

/** add plugin layer type (take ownership) and return TRUE on success */
bool addPluginLayerType(QgsPluginLayerType* pluginLayerType /Transfer/);

/** remove plugin layer type and return TRUE on success */
bool removePluginLayerType(QString typeName);

/** return plugin layer type metadata or NULL if doesn't exist */
QgsPluginLayerType* pluginLayerType(QString typeName);

/** return new layer if corresponding plugin has been found, else return NULL */
// to be resolved
//QgsPluginLayer* createLayer(QString typeName);

private:

/** private since instance() creates it */
QgsPluginLayerRegistry();
};
2 changes: 1 addition & 1 deletion src/app/legend/qgslegendlayer.cpp
Expand Up @@ -165,7 +165,7 @@ void QgsLegendLayer::refreshSymbology( const QString& key, double widthScale )
else
vectorLayerSymbology( vlayer, widthScale ); // get and change symbology
}
else // RASTER
else if ( theMapLayer->type() == QgsMapLayer::RasterLayer ) // RASTER
{
QgsRasterLayer* rlayer = qobject_cast<QgsRasterLayer *>( theMapLayer );
rasterLayerSymbology( rlayer ); // get and change symbology
Expand Down
20 changes: 19 additions & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -118,6 +118,8 @@
#include "qgsoptions.h"
#include "qgspastetransformations.h"
#include "qgspluginitem.h"
#include "qgspluginlayer.h"
#include "qgspluginlayerregistry.h"
#include "qgspluginmanager.h"
#include "qgspluginmetadata.h"
#include "qgspluginregistry.h"
Expand Down Expand Up @@ -6108,7 +6110,7 @@ void QgisApp::showLayerProperties( QgsMapLayer *ml )
rlp->exec();
delete rlp; // delete since dialog cannot be reused without updating code
}
else // VECTOR
else if ( ml->type() == QgsMapLayer::VectorLayer ) // VECTOR
{
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer *>( ml );

Expand All @@ -6125,4 +6127,20 @@ void QgisApp::showLayerProperties( QgsMapLayer *ml )
vlp->exec();
delete vlp; // delete since dialog cannot be reused without updating code
}
else if ( ml->type() == QgsMapLayer::PluginLayer )
{
QgsPluginLayer* pl = qobject_cast<QgsPluginLayer *>( ml );
if ( !pl )
return;

QgsPluginLayerType* plt = QgsPluginLayerRegistry::instance()->pluginLayerType( pl->pluginLayerType() );
if ( !plt )
return;

if ( !plt->showLayerProperties( pl ) )
{
QMessageBox::information( this, tr( "Warning" ), tr( "This layer doesn't have a properties dialog." ) );
}

}
}
5 changes: 5 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -60,6 +60,8 @@ SET(QGIS_CORE_SRCS
qgsoverlayobject.cpp
qgspalgeometry.cpp
qgspalobjectpositionmanager.cpp
qgspluginlayer.cpp
qgspluginlayerregistry.cpp
qgspoint.cpp
qgsproject.cpp
qgsprojectfiletransform.cpp
Expand Down Expand Up @@ -221,6 +223,7 @@ SET(QGIS_CORE_MOC_HDRS
qgsmaplayerregistry.h
qgsmaprenderer.h
qgsmessageoutput.h
qgspluginlayer.h
qgsproject.h
qgsrunprocess.h
qgsvectorlayer.h
Expand Down Expand Up @@ -388,6 +391,8 @@ SET(QGIS_CORE_HDRS
qgsmessageoutput.h
qgsoverlayobjectpositionmanager.h
qgspalobjectpositionmanager.h
qgspluginlayer.h
qgspluginlayerregistry.h
qgspoint.h
qgsproject.h
qgsprojectfiletransform.h
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -850,3 +850,12 @@ void QgsMapLayer::setCacheImage( QImage * thepImage )
mpCacheImage = thepImage;
}

bool QgsMapLayer::isEditable() const
{
return false;
}

void QgsMapLayer::setValid( bool valid )
{
mValid = valid;
}
8 changes: 6 additions & 2 deletions src/core/qgsmaplayer.h
Expand Up @@ -48,7 +48,8 @@ class CORE_EXPORT QgsMapLayer : public QObject
enum LayerType
{
VectorLayer,
RasterLayer
RasterLayer,
PluginLayer
};

/** Constructor
Expand Down Expand Up @@ -126,7 +127,7 @@ class CORE_EXPORT QgsMapLayer : public QObject


/** True if the layer can be edited */
virtual bool isEditable() const = 0;
virtual bool isEditable() const;

/** sets state from Dom document
@param layer_node is Dom node corresponding to ``maplayer'' tag
Expand Down Expand Up @@ -343,6 +344,9 @@ class CORE_EXPORT QgsMapLayer : public QObject

protected:

/** set whether layer is valid or not - should be used in constructor */
void setValid( bool valid );

/** called by readXML(), used by children to read state specific to them from
project files.
*/
Expand Down
11 changes: 11 additions & 0 deletions src/core/qgspluginlayer.cpp
@@ -0,0 +1,11 @@
#include "qgspluginlayer.h"

QgsPluginLayer::QgsPluginLayer( QString layerType, QString layerName )
: QgsMapLayer( PluginLayer, layerName ), mPluginLayerType( layerType )
{
}

QString QgsPluginLayer::pluginLayerType()
{
return mPluginLayerType;
}
28 changes: 28 additions & 0 deletions src/core/qgspluginlayer.h
@@ -0,0 +1,28 @@
#ifndef QGSPLUGINLAYER_H
#define QGSPLUGINLAYER_H

#include "qgsmaplayer.h"

/** \ingroup core
Base class for plugin layers. These can be implemented by plugins
and registered in QgsPluginLayerRegistry.
In order to be readable from project files, they should set these attributes in layer DOM node:
"type" = "plugin"
"name" = "your_layer_type"
*/
class CORE_EXPORT QgsPluginLayer : public QgsMapLayer
{
Q_OBJECT

public:
QgsPluginLayer( QString layerType, QString layerName = QString() );

/** return plugin layer type (the same as used in QgsPluginLayerRegistry) */
QString pluginLayerType();

protected:
QString mPluginLayerType;
};

#endif // QGSPLUGINLAYER_H

0 comments on commit f951d16

Please sign in to comment.