Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wonder committed Jun 5, 2009
1 parent 276b81d commit 9e111fb
Show file tree
Hide file tree
Showing 11 changed files with 1,156 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/plugins/CMakeLists.txt
Expand Up @@ -32,3 +32,5 @@ SUBDIRS (coordinate_capture dxf2shp_converter)
SUBDIRS (ogr_converter)

SUBDIRS (diagram_overlay)

SUBDIRS (labeling)
50 changes: 50 additions & 0 deletions src/plugins/labeling/CMakeLists.txt
@@ -0,0 +1,50 @@

########################################################
# Files

SET (labeling_SRCS
labeling.cpp
labelinggui.cpp
pallabeling.cpp
)

SET (labeling_UIS labelingguibase.ui)

SET (labeling_MOC_HDRS
labeling.h
labelinggui.h
)

SET (labeling_RCCS labeling.qrc)

########################################################
# Build

QT4_WRAP_UI (labeling_UIS_H ${labeling_UIS})

QT4_WRAP_CPP (labeling_MOC_SRCS ${labeling_MOC_HDRS})

QT4_ADD_RESOURCES(labeling_RCC_SRCS ${labeling_RCCS})

ADD_LIBRARY (labelingplugin MODULE ${labeling_SRCS} ${labeling_MOC_SRCS} ${labeling_RCC_SRCS} ${labeling_UIS_H})

INCLUDE_DIRECTORIES(
${CMAKE_CURRENT_BINARY_DIR}
../../core ../../core/raster ../../core/renderer ../../core/symbology
../../gui
..
)

TARGET_LINK_LIBRARIES(labelingplugin
qgis_core
qgis_gui
)


########################################################
# Install

INSTALL(TARGETS labelingplugin
RUNTIME DESTINATION ${QGIS_PLUGIN_DIR}
LIBRARY DESTINATION ${QGIS_PLUGIN_DIR})

195 changes: 195 additions & 0 deletions src/plugins/labeling/labeling.cpp
@@ -0,0 +1,195 @@
/***************************************************************************
labeling.cpp
Smart labeling for vector layers
-------------------
begin : June 2009
copyright : (C) Martin Dobias
email : wonder.sk at gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

//
// QGIS Specific includes
//

#include <qgisinterface.h>
#include <qgisgui.h>
#include <qgsmapcanvas.h>
#include <qgsvectorlayer.h>

#include "labeling.h"
#include "labelinggui.h"
#include "pallabeling.h"

//
// Qt4 Related Includes
//

#include <QAction>
#include <QMessageBox>
#include <QPainter>
#include <QToolBar>


static const char * const sIdent = "$Id: plugin.cpp 9327 2008-09-14 11:18:44Z jef $";
static const QString sName = QObject::tr( "Labeling" );
static const QString sDescription = QObject::tr( "Smart labeling for vector layers" );
static const QString sPluginVersion = QObject::tr( "Version 0.1" );
static const QgisPlugin::PLUGINTYPE sPluginType = QgisPlugin::UI;

//////////////////////////////////////////////////////////////////////
//
// THE FOLLOWING METHODS ARE MANDATORY FOR ALL PLUGINS
//
//////////////////////////////////////////////////////////////////////

/**
* Constructor for the plugin. The plugin is passed a pointer
* an interface object that provides access to exposed functions in QGIS.
* @param theQGisInterface - Pointer to the QGIS interface object
*/
Labeling::Labeling( QgisInterface * theQgisInterface ):
QgisPlugin( sName, sDescription, sPluginVersion, sPluginType ),
mQGisIface( theQgisInterface )
{
}

Labeling::~Labeling()
{
}

/*
* Initialize the GUI interface for the plugin - this is only called once when the plugin is
* added to the plugin registry in the QGIS application.
*/
void Labeling::initGui()
{
mLBL = new PalLabeling(mQGisIface->mapCanvas());

// Create the action for tool
mQActionPointer = new QAction( QIcon( ":/labeling/labeling.png" ), tr( "Labeling" ), this );
// Set the what's this text
mQActionPointer->setWhatsThis( tr( "Replace this with a short description of what the plugin does" ) );
// Connect the action to the run
connect( mQActionPointer, SIGNAL( triggered() ), this, SLOT( run() ) );
// Add the icon to the toolbar
mQGisIface->addToolBarIcon( mQActionPointer );
mQGisIface->addPluginToMenu( tr( "&Labeling" ), mQActionPointer );

connect( mQGisIface->mapCanvas(), SIGNAL( renderComplete( QPainter * ) ), this, SLOT( doLabeling( QPainter * ) ) );

}

void Labeling::doLabeling( QPainter * painter )
{
int w = painter->device()->width();
int h = painter->device()->height();


QgsMapLayer* layer = mQGisIface->activeLayer();
if (layer == NULL || layer->type() != QgsMapLayer::VectorLayer)
{
painter->drawLine(0,0,w,h);
return;
}

mLBL->doLabeling(painter);
}

// Slot called when the menu item is triggered
// If you created more menu items / toolbar buttons in initiGui, you should
// create a separate handler for each action - this single run() method will
// not be enough
void Labeling::run()
{
QgsMapLayer* layer = mQGisIface->activeLayer();
if (layer == NULL || layer->type() != QgsMapLayer::VectorLayer)
{
QMessageBox::warning(mQGisIface->mainWindow(), "Labeling", "Please select a vector layer first.");
return;
}
//QgsVectorLayer* vlayer = static_cast<QgsVectorLayer*>(layer);

LabelingGui myPluginGui( mLBL, layer->getLayerID(), mQGisIface->mainWindow() );

if (myPluginGui.exec())
{
// alter labeling
mLBL->removeLayer(layer->getLayerID());
mLBL->addLayer( myPluginGui.layerSettings() );

// trigger refresh
mQGisIface->mapCanvas()->refresh();
}
}

// Unload the plugin by cleaning up the GUI
void Labeling::unload()
{
// remove the GUI
mQGisIface->removePluginMenu( "&Labeling", mQActionPointer );
mQGisIface->removeToolBarIcon( mQActionPointer );
delete mQActionPointer;

delete mLBL;
}


//////////////////////////////////////////////////////////////////////////
//
//
// THE FOLLOWING CODE IS AUTOGENERATED BY THE PLUGIN BUILDER SCRIPT
// YOU WOULD NORMALLY NOT NEED TO MODIFY THIS, AND YOUR PLUGIN
// MAY NOT WORK PROPERLY IF YOU MODIFY THIS INCORRECTLY
//
//
//////////////////////////////////////////////////////////////////////////


/**
* Required extern functions needed for every plugin
* These functions can be called prior to creating an instance
* of the plugin class
*/
// Class factory to return a new instance of the plugin class
QGISEXTERN QgisPlugin * classFactory( QgisInterface * theQgisInterfacePointer )
{
return new Labeling( theQgisInterfacePointer );
}
// Return the name of the plugin - note that we do not user class members as
// the class may not yet be insantiated when this method is called.
QGISEXTERN QString name()
{
return sName;
}

// Return the description
QGISEXTERN QString description()
{
return sDescription;
}

// Return the type (either UI or MapLayer plugin)
QGISEXTERN int type()
{
return sPluginType;
}

// Return the version number for the plugin
QGISEXTERN QString version()
{
return sPluginVersion;
}

// Delete ourself
QGISEXTERN void unload( QgisPlugin * thePluginPointer )
{
delete thePluginPointer;
}
71 changes: 71 additions & 0 deletions src/plugins/labeling/labeling.h
@@ -0,0 +1,71 @@
/***************************************************************************
labeling.h
Smart labeling for vector layers
-------------------
begin : June 2009
copyright : (C) Martin Dobias
email : wonder.sk at gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef Labeling_H
#define Labeling_H

//QT4 includes
#include <QObject>

//QGIS includes
#include "../qgisplugin.h"

//forward declarations
class QAction;
class QPainter;
class QToolBar;

class QgisInterface;

class PalLabeling;

class Labeling: public QObject, public QgisPlugin
{
Q_OBJECT
public:

/**
* Constructor for a plugin. The QgisInterface pointer is passed by
* QGIS when it attempts to instantiate the plugin.
* @param theInterface Pointer to the QgisInterface object.
*/
Labeling( QgisInterface * theInterface );
//! Destructor
virtual ~Labeling();

public slots:
//! init the gui
virtual void initGui();
//! Show the dialog box
void run();
//! unload the plugin
void unload();

//! hook to renderComplete signal
void doLabeling(QPainter* painter);

private:

//! Pointer to the QGIS interface object
QgisInterface *mQGisIface;
//! Pointer to the qaction for this plugin
QAction * mQActionPointer;

PalLabeling* mLBL;
};

#endif //Labeling_H
Binary file added src/plugins/labeling/labeling.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions src/plugins/labeling/labeling.qrc
@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/labeling/" >
<file>labeling.png</file>
</qresource>
</RCC>

0 comments on commit 9e111fb

Please sign in to comment.