enh2392_plugin_layer_registry.diff

Plugin layer registry patch - Mathias Walker, 2010-01-25 07:29 AM

Download (10.6 KB)

View differences:

python/core/qgsmaplayer.sip (working copy)
35 35
    enum LayerType
36 36
    {
37 37
        VectorLayer,
38
        RasterLayer
38
        RasterLayer,
39
        PluginLayer
39 40
    };
40 41

  
41 42
    /** Constructor
python/core/core.sip (working copy)
43 43
%Include qgsmarkercatalogue.sip
44 44
%Include qgsmessageoutput.sip
45 45
%Include qgsoverlayobject.sip
46
%Include qgspluginlayerregistry.sip
46 47
%Include qgspoint.sip
47 48
%Include qgsproject.sip
48 49
%Include qgsprovidermetadata.sip
python/core/qgspluginlayerregistry.sip (revision 0)
1
/** callback class for creating plugin specific layers */
2
class QgsPluginLayerCreator
3
{
4
%TypeHeaderCode
5
#include "qgspluginlayerregistry.h"
6
%End
7

  
8
  public:
9

  
10
    QgsPluginLayerCreator();
11
    virtual ~QgsPluginLayerCreator();
12

  
13
    /** return new layer if node data corresponds to the parent plugin, else return NULL */
14
    virtual QgsMapLayer* createLayer(const QDomNode& layerNode);
15
};
16

  
17
/** a registry of callbacks to create plugin layers */
18
class QgsPluginLayerRegistry
19
{
20
%TypeHeaderCode
21
#include "qgspluginlayerregistry.h"
22
%End
23

  
24
  public:
25

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

  
29
    ~QgsPluginLayerRegistry();
30

  
31
    /** add plugin layer creator and return unique id */
32
    unsigned int addCreator(QgsPluginLayerCreator* creator);
33
    /** remove plugin layer creator with id */
34
    void removeCreator(unsigned int id);
35

  
36
    /** return new layer if corresponding plugin has been found, else return NULL */
37
    QgsMapLayer* createLayer(const QDomNode& layerNode);
38

  
39
  private:
40

  
41
    /** private since instance() creates it */
42
    QgsPluginLayerRegistry();
43
};
src/app/legend/qgslegendlayer.cpp (working copy)
165 165
    else
166 166
      vectorLayerSymbology( vlayer, widthScale ); // get and change symbology
167 167
  }
168
  else // RASTER
168
  else if ( theMapLayer->type() == QgsMapLayer::RasterLayer ) // RASTER
169 169
  {
170 170
    QgsRasterLayer* rlayer = qobject_cast<QgsRasterLayer *>( theMapLayer );
171 171
    rasterLayerSymbology( rlayer ); // get and change symbology
src/core/qgsproject.cpp (working copy)
30 30
#include "qgslogger.h"
31 31
#include "qgsprojectfiletransform.h"
32 32
#include "qgsprojectversion.h"
33
#include "qgspluginlayerregistry.h"
33 34

  
34 35
#include <QApplication>
35 36
#include <QFileInfo>
......
695 696
    {
696 697
      mapLayer = new QgsRasterLayer;
697 698
    }
699
    else
700
    {
701
      mapLayer = QgsPluginLayerRegistry::instance()->createLayer(node);
702
    }
698 703

  
699 704
    Q_CHECK_PTR( mapLayer );
700 705

  
......
885 890
  }
886 891
  else
887 892
  {
888
    QgsDebugMsg( "bad layer type" );
889
    return false;
893
    mapLayer = QgsPluginLayerRegistry::instance()->createLayer(layerNode);
890 894
  }
891 895

  
892 896
  if ( !mapLayer )
src/core/qgsmaplayer.cpp (working copy)
353 353

  
354 354
bool QgsMapLayer::isValid()
355 355
{
356
  if (type() == PluginLayer)
357
  {
358
    return true;
359
  }
360

  
356 361
  return mValid;
357 362
}
358 363

  
src/core/qgspluginlayerregistry.h (revision 0)
1
/***************************************************************************
2
                    qgspluginlayerregistry.cpp - class for
3
                    registering plugin layer creators
4
                             -------------------
5
    begin                : Mon Nov 30 2009
6
    copyright            : (C) 2009 by Mathias Walker, Sourcepole
7
    email                : mwa at sourcepole.ch
8
 ***************************************************************************/
9

  
10
/***************************************************************************
11
 *                                                                         *
12
 *   This program is free software; you can redistribute it and/or modify  *
13
 *   it under the terms of the GNU General Public License as published by  *
14
 *   the Free Software Foundation; either version 2 of the License, or     *
15
 *   (at your option) any later version.                                   *
16
 *                                                                         *
17
 ***************************************************************************/
18
/* $Id$ */
19

  
20
#ifndef QGSPLUGINLAYERREGSITRY_H
21
#define QGSPLUGINLAYERREGSITRY_H
22

  
23
#include <map>
24

  
25
#include <QDomNode>
26

  
27
class QgsMapLayer;
28

  
29
/** \ingroup core
30
    callback class for creating plugin specific layers
31
*/
32
class CORE_EXPORT QgsPluginLayerCreator
33
{
34
  public:
35

  
36
    QgsPluginLayerCreator();
37
    virtual ~QgsPluginLayerCreator();
38

  
39
    /** return new layer if node data corresponds to the parent plugin, else return NULL */
40
    virtual QgsMapLayer* createLayer(const QDomNode& layerNode);
41
};
42

  
43
//=============================================================================
44

  
45
/** \ingroup core
46
    a registry of callbacks to create plugin layers
47
*/
48
class CORE_EXPORT QgsPluginLayerRegistry
49
{
50
  public:
51

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

  
55
    ~QgsPluginLayerRegistry();
56

  
57
    /** add plugin layer creator and return unique id */
58
    unsigned int addCreator(QgsPluginLayerCreator* creator);
59
    /** remove plugin layer creator with id */
60
    void removeCreator(unsigned int id);
61

  
62
    /** return new layer if corresponding plugin has been found, else return NULL */
63
    QgsMapLayer* createLayer(const QDomNode& layerNode);
64

  
65
  private:
66

  
67
    typedef std::map<unsigned int, QgsPluginLayerCreator*> PluginLayerCreators;
68

  
69
    /** private since instance() creates it */
70
    QgsPluginLayerRegistry();
71

  
72
    /** pointer to canonical Singleton object */
73
    static QgsPluginLayerRegistry* _instance;
74

  
75
    PluginLayerCreators mPluginLayerCreators;
76
    unsigned int mLastId;
77
};
78

  
79
#endif // QGSPLUGINLAYERREGSITRY_H
src/core/qgspluginlayerregistry.cpp (revision 0)
1
/***************************************************************************
2
                    qgspluginlayerregistry.cpp - class for
3
                    registering plugin layer creators
4
                             -------------------
5
    begin                : Mon Nov 30 2009
6
    copyright            : (C) 2009 by Mathias Walker, Sourcepole
7
    email                : mwa at sourcepole.ch
8
 ***************************************************************************/
9

  
10
/***************************************************************************
11
 *                                                                         *
12
 *   This program is free software; you can redistribute it and/or modify  *
13
 *   it under the terms of the GNU General Public License as published by  *
14
 *   the Free Software Foundation; either version 2 of the License, or     *
15
 *   (at your option) any later version.                                   *
16
 *                                                                         *
17
 ***************************************************************************/
18
/* $Id$ */
19

  
20
#include "qgspluginlayerregistry.h"
21
#include "qgslogger.h"
22
#include "qgsmaplayer.h"
23

  
24
QgsPluginLayerCreator::QgsPluginLayerCreator()
25
{
26
}
27

  
28
QgsPluginLayerCreator::~QgsPluginLayerCreator()
29
{
30
}
31

  
32
QgsMapLayer* QgsPluginLayerCreator::createLayer(const QDomNode& layerNode)
33
{
34
  return NULL;
35
}
36

  
37
//=============================================================================
38

  
39
/** Static calls to enforce singleton behaviour */
40
QgsPluginLayerRegistry* QgsPluginLayerRegistry::_instance = NULL;
41
QgsPluginLayerRegistry* QgsPluginLayerRegistry::instance()
42
{
43
  if ( _instance == NULL )
44
  {
45
    _instance = new QgsPluginLayerRegistry();
46
  }
47
  return _instance;
48
}
49

  
50

  
51
QgsPluginLayerRegistry::QgsPluginLayerRegistry()
52
{
53
  mLastId = 0;
54
}
55

  
56
QgsPluginLayerRegistry::~QgsPluginLayerRegistry()
57
{
58
  if ( !mPluginLayerCreators.empty() )
59
  {
60
    QgsDebugMsg("QgsPluginLayerRegistry::~QgsPluginLayerRegistry(): creator list not empty");
61
  }
62
}
63

  
64
unsigned int QgsPluginLayerRegistry::addCreator(QgsPluginLayerCreator* creator)
65
{
66
  mLastId++;
67
  mPluginLayerCreators[mLastId] = creator;
68
  return mLastId;
69
}
70

  
71
void QgsPluginLayerRegistry::removeCreator(unsigned int id)
72
{
73
  PluginLayerCreators::iterator iter = mPluginLayerCreators.find(id);
74
  if ( iter != mPluginLayerCreators.end() )
75
  {
76
    mPluginLayerCreators.erase(id);
77
  }
78
}
79

  
80
QgsMapLayer* QgsPluginLayerRegistry::createLayer(const QDomNode& layerNode)
81
{
82
  QgsMapLayer* layer = NULL;
83
  for ( PluginLayerCreators::iterator iter = mPluginLayerCreators.begin(); iter != mPluginLayerCreators.end(); ++iter )
84
  {
85
    layer = iter->second->createLayer(layerNode);
86
    if ( layer != NULL )
87
    {
88
      break;
89
    }
90
  }
91
  return layer;
92
}
src/core/CMakeLists.txt (working copy)
60 60
  qgsoverlayobject.cpp
61 61
  qgspalgeometry.cpp
62 62
  qgspalobjectpositionmanager.cpp
63
  qgspluginlayerregistry.cpp
63 64
  qgspoint.cpp
64 65
  qgsproject.cpp
65 66
  qgsprojectfiletransform.cpp
......
388 389
  qgsmessageoutput.h
389 390
  qgsoverlayobjectpositionmanager.h
390 391
  qgspalobjectpositionmanager.h
392
  qgspluginlayerregistry.h
391 393
  qgspoint.h
392 394
  qgsproject.h
393 395
  qgsprojectfiletransform.h
src/core/qgsmaplayer.h (working copy)
48 48
    enum LayerType
49 49
    {
50 50
      VectorLayer,
51
      RasterLayer
51
      RasterLayer,
52
      PluginLayer
52 53
    };
53 54

  
54 55
    /** Constructor