1
/***************************************************************************
2
qgsdiagramoverlayplugin.cpp - description
3
---------------------------
5
copyright : (C) 2007 by Marco Hugentobler
6
email : marco dot hugentobler at karto dot baug dot ethz dot ch
7
***************************************************************************/
9
/***************************************************************************
11
* This program is free software; you can redistribute it and/or modify *
12
* it under the terms of the GNU General Public License as published by *
13
* the Free Software Foundation; either version 2 of the License, or *
14
* (at your option) any later version. *
16
***************************************************************************/
18
#include "qgsdiagramoverlayplugin.h"
19
#include "qgisinterface.h"
20
#include "qgsdiagramdialog.h"
21
#include "qgsdiagramoverlay.h"
22
#include "qgsmaplayerregistry.h"
23
#include "qgsproject.h"
24
#include "qgsvectorlayer.h"
25
#include <QDomDocument>
29
#define QGISEXTERN extern "C" __declspec( dllexport )
31
#define QGISEXTERN extern "C"
34
static const QString pluginName = QObject::tr( "Diagram Overlay" );
35
static const QString pluginDescription = QObject::tr( "A plugin for placing diagrams on vector layers" );
36
static const QString pluginVersion = QObject::tr( "Version 0.0.1" );
38
QgsDiagramOverlayPlugin::QgsDiagramOverlayPlugin( QgisInterface* iface ): QObject(), QgsVectorOverlayPlugin( pluginName, pluginDescription, pluginVersion ), mInterface( iface )
40
if ( iface && iface->mainWindow() )
42
connect( iface->mainWindow(), SIGNAL( projectRead() ), this, SLOT( projectRead() ) );
46
QgsDiagramOverlayPlugin::~QgsDiagramOverlayPlugin()
51
void QgsDiagramOverlayPlugin::projectRead()
53
//for a test, print out the content of the project file
54
QString projectFileName = QgsProject::instance()->fileName();
56
if ( projectFileName.isEmpty() )
61
QFile projectFile( projectFileName );
62
QDomDocument projectDocument;
63
if ( !projectDocument.setContent( &projectFile ) )
68
//iterate over all maplayers
69
QDomNodeList mapLayerList = projectDocument.documentElement().elementsByTagName( "maplayer" );
70
QDomElement mapLayerElem;
71
QDomNodeList overlayList;
72
QDomElement overlayElem;
73
QgsVectorLayer* currentVectorLayer = 0;
74
QgsDiagramOverlay* newDiagramOverlay = 0;
79
//iterate through all maplayer elements
80
for ( int i = 0; i < mapLayerList.size(); ++i )
82
mapLayerElem = mapLayerList.at( i ).toElement();
83
overlayList = mapLayerElem.elementsByTagName( "overlay" );
86
idList = mapLayerElem.elementsByTagName( "id" );
87
if ( idList.size() < 1 )
91
layerId = idList.at( 0 ).toElement().text();
93
//iterate through all overlay elements
94
for ( int j = 0; j < overlayList.size(); ++j )
96
overlayElem = overlayList.at( j ).toElement();
97
if ( overlayElem.attribute( "type" ) == "diagram" )
99
//get a pointer to the vector layer which owns the diagram overlay (use QgsMapLayerRegistry)
100
currentVectorLayer = qobject_cast<QgsVectorLayer *>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
101
if ( !currentVectorLayer )
106
//create an overlay object
107
newDiagramOverlay = new QgsDiagramOverlay( currentVectorLayer );
108
newDiagramOverlay->readXML( overlayElem );
110
//add the overlay to the vector layer
111
currentVectorLayer->addOverlay( newDiagramOverlay );
113
//notify the legend that the layer legend needs to be changed
116
mInterface->refreshLegend( currentVectorLayer );
123
QgsApplyDialog* QgsDiagramOverlayPlugin::dialog( QgsVectorLayer* vl ) const
125
return new QgsDiagramDialog( vl );
128
QGISEXTERN QgisPlugin* classFactory( QgisInterface* iface )
130
return new QgsDiagramOverlayPlugin( iface );
133
QGISEXTERN QString name()
138
QGISEXTERN QString description()
140
return pluginDescription;
143
QGISEXTERN QString version()
145
return pluginVersion;
148
QGISEXTERN int type()
150
return QgisPlugin::VECTOR_OVERLAY;
153
QGISEXTERN void unload( QgisPlugin* theQgsDiagramOverlayPluginPointer )
155
delete theQgsDiagramOverlayPluginPointer;