~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to src/plugins/diagram_overlay/qgsdiagramoverlayplugin.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                         qgsdiagramoverlayplugin.cpp  -  description
 
3
                         ---------------------------
 
4
    begin                : January 2007
 
5
    copyright            : (C) 2007 by Marco Hugentobler
 
6
    email                : marco dot hugentobler at karto dot baug dot ethz dot ch
 
7
 ***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *                                                                         *
 
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.                                   *
 
15
 *                                                                         *
 
16
 ***************************************************************************/
 
17
 
 
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>
 
26
#include <QFile>
 
27
 
 
28
#ifdef WIN32
 
29
#define QGISEXTERN extern "C" __declspec( dllexport )
 
30
#else
 
31
#define QGISEXTERN extern "C"
 
32
#endif
 
33
 
 
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" );
 
37
 
 
38
QgsDiagramOverlayPlugin::QgsDiagramOverlayPlugin( QgisInterface* iface ): QObject(), QgsVectorOverlayPlugin( pluginName, pluginDescription, pluginVersion ), mInterface( iface )
 
39
{
 
40
  if ( iface && iface->mainWindow() )
 
41
  {
 
42
    connect( iface->mainWindow(), SIGNAL( projectRead() ), this, SLOT( projectRead() ) );
 
43
  }
 
44
}
 
45
 
 
46
QgsDiagramOverlayPlugin::~QgsDiagramOverlayPlugin()
 
47
{
 
48
 
 
49
}
 
50
 
 
51
void QgsDiagramOverlayPlugin::projectRead()
 
52
{
 
53
  //for a test, print out the content of the project file
 
54
  QString projectFileName = QgsProject::instance()->fileName();
 
55
 
 
56
  if ( projectFileName.isEmpty() )
 
57
  {
 
58
    return;
 
59
  }
 
60
 
 
61
  QFile projectFile( projectFileName );
 
62
  QDomDocument projectDocument;
 
63
  if ( !projectDocument.setContent( &projectFile ) )
 
64
  {
 
65
    return;
 
66
  }
 
67
 
 
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;
 
75
 
 
76
  QDomNodeList idList;
 
77
  QString layerId;
 
78
 
 
79
  //iterate through all maplayer elements
 
80
  for ( int i = 0; i < mapLayerList.size(); ++i )
 
81
  {
 
82
    mapLayerElem = mapLayerList.at( i ).toElement();
 
83
    overlayList = mapLayerElem.elementsByTagName( "overlay" );
 
84
 
 
85
    //find out layer id
 
86
    idList = mapLayerElem.elementsByTagName( "id" );
 
87
    if ( idList.size() < 1 )
 
88
    {
 
89
      continue;
 
90
    }
 
91
    layerId = idList.at( 0 ).toElement().text();
 
92
 
 
93
    //iterate through all overlay elements
 
94
    for ( int j = 0; j < overlayList.size(); ++j )
 
95
    {
 
96
      overlayElem = overlayList.at( j ).toElement();
 
97
      if ( overlayElem.attribute( "type" ) == "diagram" )
 
98
      {
 
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 )
 
102
        {
 
103
          continue;
 
104
        }
 
105
 
 
106
        //create an overlay object
 
107
        newDiagramOverlay = new QgsDiagramOverlay( currentVectorLayer );
 
108
        newDiagramOverlay->readXML( overlayElem );
 
109
 
 
110
        //add the overlay to the vector layer
 
111
        currentVectorLayer->addOverlay( newDiagramOverlay );
 
112
 
 
113
        //notify the legend that the layer legend needs to be changed
 
114
        if ( mInterface )
 
115
        {
 
116
          mInterface->refreshLegend( currentVectorLayer );
 
117
        }
 
118
      }
 
119
    }
 
120
  }
 
121
}
 
122
 
 
123
QgsApplyDialog* QgsDiagramOverlayPlugin::dialog( QgsVectorLayer* vl ) const
 
124
{
 
125
  return new QgsDiagramDialog( vl );
 
126
}
 
127
 
 
128
QGISEXTERN QgisPlugin* classFactory( QgisInterface* iface )
 
129
{
 
130
  return new QgsDiagramOverlayPlugin( iface );
 
131
}
 
132
 
 
133
QGISEXTERN QString name()
 
134
{
 
135
  return pluginName;
 
136
}
 
137
 
 
138
QGISEXTERN QString description()
 
139
{
 
140
  return pluginDescription;
 
141
}
 
142
 
 
143
QGISEXTERN QString version()
 
144
{
 
145
  return pluginVersion;
 
146
}
 
147
 
 
148
QGISEXTERN int type()
 
149
{
 
150
  return QgisPlugin::VECTOR_OVERLAY;
 
151
}
 
152
 
 
153
QGISEXTERN void unload( QgisPlugin* theQgsDiagramOverlayPluginPointer )
 
154
{
 
155
  delete theQgsDiagramOverlayPluginPointer;
 
156
}