Skip to content

Commit 9e111fb

Browse files
author
wonder
committed
First prototype of labeling plugin based on PAL (usable already)
git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@10887 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 276b81d commit 9e111fb

File tree

11 files changed

+1156
-0
lines changed

11 files changed

+1156
-0
lines changed

src/plugins/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ SUBDIRS (coordinate_capture dxf2shp_converter)
3232
SUBDIRS (ogr_converter)
3333

3434
SUBDIRS (diagram_overlay)
35+
36+
SUBDIRS (labeling)

src/plugins/labeling/CMakeLists.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
########################################################
3+
# Files
4+
5+
SET (labeling_SRCS
6+
labeling.cpp
7+
labelinggui.cpp
8+
pallabeling.cpp
9+
)
10+
11+
SET (labeling_UIS labelingguibase.ui)
12+
13+
SET (labeling_MOC_HDRS
14+
labeling.h
15+
labelinggui.h
16+
)
17+
18+
SET (labeling_RCCS labeling.qrc)
19+
20+
########################################################
21+
# Build
22+
23+
QT4_WRAP_UI (labeling_UIS_H ${labeling_UIS})
24+
25+
QT4_WRAP_CPP (labeling_MOC_SRCS ${labeling_MOC_HDRS})
26+
27+
QT4_ADD_RESOURCES(labeling_RCC_SRCS ${labeling_RCCS})
28+
29+
ADD_LIBRARY (labelingplugin MODULE ${labeling_SRCS} ${labeling_MOC_SRCS} ${labeling_RCC_SRCS} ${labeling_UIS_H})
30+
31+
INCLUDE_DIRECTORIES(
32+
${CMAKE_CURRENT_BINARY_DIR}
33+
../../core ../../core/raster ../../core/renderer ../../core/symbology
34+
../../gui
35+
..
36+
)
37+
38+
TARGET_LINK_LIBRARIES(labelingplugin
39+
qgis_core
40+
qgis_gui
41+
)
42+
43+
44+
########################################################
45+
# Install
46+
47+
INSTALL(TARGETS labelingplugin
48+
RUNTIME DESTINATION ${QGIS_PLUGIN_DIR}
49+
LIBRARY DESTINATION ${QGIS_PLUGIN_DIR})
50+

src/plugins/labeling/labeling.cpp

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/***************************************************************************
2+
labeling.cpp
3+
Smart labeling for vector layers
4+
-------------------
5+
begin : June 2009
6+
copyright : (C) Martin Dobias
7+
email : wonder.sk at gmail.com
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+
//
19+
// QGIS Specific includes
20+
//
21+
22+
#include <qgisinterface.h>
23+
#include <qgisgui.h>
24+
#include <qgsmapcanvas.h>
25+
#include <qgsvectorlayer.h>
26+
27+
#include "labeling.h"
28+
#include "labelinggui.h"
29+
#include "pallabeling.h"
30+
31+
//
32+
// Qt4 Related Includes
33+
//
34+
35+
#include <QAction>
36+
#include <QMessageBox>
37+
#include <QPainter>
38+
#include <QToolBar>
39+
40+
41+
static const char * const sIdent = "$Id: plugin.cpp 9327 2008-09-14 11:18:44Z jef $";
42+
static const QString sName = QObject::tr( "Labeling" );
43+
static const QString sDescription = QObject::tr( "Smart labeling for vector layers" );
44+
static const QString sPluginVersion = QObject::tr( "Version 0.1" );
45+
static const QgisPlugin::PLUGINTYPE sPluginType = QgisPlugin::UI;
46+
47+
//////////////////////////////////////////////////////////////////////
48+
//
49+
// THE FOLLOWING METHODS ARE MANDATORY FOR ALL PLUGINS
50+
//
51+
//////////////////////////////////////////////////////////////////////
52+
53+
/**
54+
* Constructor for the plugin. The plugin is passed a pointer
55+
* an interface object that provides access to exposed functions in QGIS.
56+
* @param theQGisInterface - Pointer to the QGIS interface object
57+
*/
58+
Labeling::Labeling( QgisInterface * theQgisInterface ):
59+
QgisPlugin( sName, sDescription, sPluginVersion, sPluginType ),
60+
mQGisIface( theQgisInterface )
61+
{
62+
}
63+
64+
Labeling::~Labeling()
65+
{
66+
}
67+
68+
/*
69+
* Initialize the GUI interface for the plugin - this is only called once when the plugin is
70+
* added to the plugin registry in the QGIS application.
71+
*/
72+
void Labeling::initGui()
73+
{
74+
mLBL = new PalLabeling(mQGisIface->mapCanvas());
75+
76+
// Create the action for tool
77+
mQActionPointer = new QAction( QIcon( ":/labeling/labeling.png" ), tr( "Labeling" ), this );
78+
// Set the what's this text
79+
mQActionPointer->setWhatsThis( tr( "Replace this with a short description of what the plugin does" ) );
80+
// Connect the action to the run
81+
connect( mQActionPointer, SIGNAL( triggered() ), this, SLOT( run() ) );
82+
// Add the icon to the toolbar
83+
mQGisIface->addToolBarIcon( mQActionPointer );
84+
mQGisIface->addPluginToMenu( tr( "&Labeling" ), mQActionPointer );
85+
86+
connect( mQGisIface->mapCanvas(), SIGNAL( renderComplete( QPainter * ) ), this, SLOT( doLabeling( QPainter * ) ) );
87+
88+
}
89+
90+
void Labeling::doLabeling( QPainter * painter )
91+
{
92+
int w = painter->device()->width();
93+
int h = painter->device()->height();
94+
95+
96+
QgsMapLayer* layer = mQGisIface->activeLayer();
97+
if (layer == NULL || layer->type() != QgsMapLayer::VectorLayer)
98+
{
99+
painter->drawLine(0,0,w,h);
100+
return;
101+
}
102+
103+
mLBL->doLabeling(painter);
104+
}
105+
106+
// Slot called when the menu item is triggered
107+
// If you created more menu items / toolbar buttons in initiGui, you should
108+
// create a separate handler for each action - this single run() method will
109+
// not be enough
110+
void Labeling::run()
111+
{
112+
QgsMapLayer* layer = mQGisIface->activeLayer();
113+
if (layer == NULL || layer->type() != QgsMapLayer::VectorLayer)
114+
{
115+
QMessageBox::warning(mQGisIface->mainWindow(), "Labeling", "Please select a vector layer first.");
116+
return;
117+
}
118+
//QgsVectorLayer* vlayer = static_cast<QgsVectorLayer*>(layer);
119+
120+
LabelingGui myPluginGui( mLBL, layer->getLayerID(), mQGisIface->mainWindow() );
121+
122+
if (myPluginGui.exec())
123+
{
124+
// alter labeling
125+
mLBL->removeLayer(layer->getLayerID());
126+
mLBL->addLayer( myPluginGui.layerSettings() );
127+
128+
// trigger refresh
129+
mQGisIface->mapCanvas()->refresh();
130+
}
131+
}
132+
133+
// Unload the plugin by cleaning up the GUI
134+
void Labeling::unload()
135+
{
136+
// remove the GUI
137+
mQGisIface->removePluginMenu( "&Labeling", mQActionPointer );
138+
mQGisIface->removeToolBarIcon( mQActionPointer );
139+
delete mQActionPointer;
140+
141+
delete mLBL;
142+
}
143+
144+
145+
//////////////////////////////////////////////////////////////////////////
146+
//
147+
//
148+
// THE FOLLOWING CODE IS AUTOGENERATED BY THE PLUGIN BUILDER SCRIPT
149+
// YOU WOULD NORMALLY NOT NEED TO MODIFY THIS, AND YOUR PLUGIN
150+
// MAY NOT WORK PROPERLY IF YOU MODIFY THIS INCORRECTLY
151+
//
152+
//
153+
//////////////////////////////////////////////////////////////////////////
154+
155+
156+
/**
157+
* Required extern functions needed for every plugin
158+
* These functions can be called prior to creating an instance
159+
* of the plugin class
160+
*/
161+
// Class factory to return a new instance of the plugin class
162+
QGISEXTERN QgisPlugin * classFactory( QgisInterface * theQgisInterfacePointer )
163+
{
164+
return new Labeling( theQgisInterfacePointer );
165+
}
166+
// Return the name of the plugin - note that we do not user class members as
167+
// the class may not yet be insantiated when this method is called.
168+
QGISEXTERN QString name()
169+
{
170+
return sName;
171+
}
172+
173+
// Return the description
174+
QGISEXTERN QString description()
175+
{
176+
return sDescription;
177+
}
178+
179+
// Return the type (either UI or MapLayer plugin)
180+
QGISEXTERN int type()
181+
{
182+
return sPluginType;
183+
}
184+
185+
// Return the version number for the plugin
186+
QGISEXTERN QString version()
187+
{
188+
return sPluginVersion;
189+
}
190+
191+
// Delete ourself
192+
QGISEXTERN void unload( QgisPlugin * thePluginPointer )
193+
{
194+
delete thePluginPointer;
195+
}

src/plugins/labeling/labeling.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/***************************************************************************
2+
labeling.h
3+
Smart labeling for vector layers
4+
-------------------
5+
begin : June 2009
6+
copyright : (C) Martin Dobias
7+
email : wonder.sk at gmail.com
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+
#ifndef Labeling_H
19+
#define Labeling_H
20+
21+
//QT4 includes
22+
#include <QObject>
23+
24+
//QGIS includes
25+
#include "../qgisplugin.h"
26+
27+
//forward declarations
28+
class QAction;
29+
class QPainter;
30+
class QToolBar;
31+
32+
class QgisInterface;
33+
34+
class PalLabeling;
35+
36+
class Labeling: public QObject, public QgisPlugin
37+
{
38+
Q_OBJECT
39+
public:
40+
41+
/**
42+
* Constructor for a plugin. The QgisInterface pointer is passed by
43+
* QGIS when it attempts to instantiate the plugin.
44+
* @param theInterface Pointer to the QgisInterface object.
45+
*/
46+
Labeling( QgisInterface * theInterface );
47+
//! Destructor
48+
virtual ~Labeling();
49+
50+
public slots:
51+
//! init the gui
52+
virtual void initGui();
53+
//! Show the dialog box
54+
void run();
55+
//! unload the plugin
56+
void unload();
57+
58+
//! hook to renderComplete signal
59+
void doLabeling(QPainter* painter);
60+
61+
private:
62+
63+
//! Pointer to the QGIS interface object
64+
QgisInterface *mQGisIface;
65+
//! Pointer to the qaction for this plugin
66+
QAction * mQActionPointer;
67+
68+
PalLabeling* mLBL;
69+
};
70+
71+
#endif //Labeling_H

src/plugins/labeling/labeling.png

624 Bytes
Loading

src/plugins/labeling/labeling.qrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<RCC>
2+
<qresource prefix="/labeling/" >
3+
<file>labeling.png</file>
4+
</qresource>
5+
</RCC>

0 commit comments

Comments
 (0)