|
| 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 | +} |
0 commit comments