265 changes: 33 additions & 232 deletions src/plugins/scale_bar/plugin.cpp → src/app/qgsdecorationscalebar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ email : sbr00pwb@users.sourceforge.net
* *
***************************************************************************/

// includes
#include "qgsdecorationscalebar.h"

#include "qgisinterface.h"
#include "qgisgui.h"
#include "qgsapplication.h"
#include "qgsdecorationscalebardialog.h"

#include "qgisapp.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayer.h"
#include "qgsmaptopixel.h"
#include "qgspoint.h"
#include "qgsproject.h"

#include "plugin.h"

#include <QPainter>
#include <QAction>
Expand All @@ -47,32 +47,14 @@ email : sbr00pwb@users.sourceforge.net
//non qt includes
#include <cmath>

//the gui subclass
#include "plugingui.h"
#include "qgslogger.h"

//

#ifdef _MSC_VER
#define round(x) ((x) >= 0 ? floor((x)+0.5) : floor((x)-0.5))
#endif

static const QString name_ = QObject::tr( "ScaleBar" );
static const QString description_ = QObject::tr( "Draws a scale bar" );
static const QString version_ = QObject::tr( "Version 0.1" );
static const QgisPlugin::PLUGINTYPE type_ = QgisPlugin::UI;
static const QString icon_ = ":/scale_bar.png";


/**
* Constructor for the plugin. The plugin is passed a pointer to the main app
* and an interface object that provides access to exposed functions in QGIS.
* @param qgis Pointer to the QGIS main window
* @param _qI Pointer to the QGIS interface object
*/
QgsScaleBarPlugin::QgsScaleBarPlugin( QgisInterface * theQgisInterFace ):
QgisPlugin( name_, description_, version_, type_ ),
qGisInterface( theQgisInterFace )

QgsDecorationScaleBar::QgsDecorationScaleBar( QObject* parent )
: QObject( parent )
{
mPlacementLabels << tr( "Bottom Left" ) << tr( "Top Left" )
<< tr( "Top Right" ) << tr( "Bottom Right" );
Expand All @@ -87,38 +69,15 @@ QgsScaleBarPlugin::QgsScaleBarPlugin( QgisInterface * theQgisInterFace ):
mColor = Qt::black;
}

QgsScaleBarPlugin::~QgsScaleBarPlugin()
QgsDecorationScaleBar::~QgsDecorationScaleBar()
{

}

/*
* Initialize the GUI interface for the plugin
*/
void QgsScaleBarPlugin::initGui()
{
// Create the action for tool
myQActionPointer = new QAction( QIcon(), tr( "&Scale Bar" ), this );
setCurrentTheme( "" );
myQActionPointer->setWhatsThis( tr( "Creates a scale bar that is displayed on the map canvas" ) );
// Connect the action to the run
connect( myQActionPointer, SIGNAL( triggered() ), this, SLOT( run() ) );
//render the scale bar each time the map is rendered
connect( qGisInterface->mapCanvas(), SIGNAL( renderComplete( QPainter * ) ), this, SLOT( renderScaleBar( QPainter * ) ) );
//this resets this plugin up if a project is loaded
connect( qGisInterface->mainWindow(), SIGNAL( projectRead() ), this, SLOT( projectRead() ) );
// Add the icon to the toolbar
qGisInterface->addToolBarIcon( myQActionPointer );
qGisInterface->addPluginToMenu( tr( "&Decorations" ), myQActionPointer );
// this is called when the icon theme is changed
connect( qGisInterface, SIGNAL( currentThemeChanged( QString ) ), this, SLOT( setCurrentTheme( QString ) ) );
}

void QgsScaleBarPlugin::projectRead()
void QgsDecorationScaleBar::projectRead()
{
QgsDebugMsg( "+++++++++ scalebar plugin - project read slot called...." );


mPreferredSize = QgsProject::instance()->readNumEntry( "ScaleBar", "/PreferredSize", 30 );
mStyleIndex = QgsProject::instance()->readNumEntry( "ScaleBar", "/Style", 0 );
mPlacementIndex = QgsProject::instance()->readNumEntry( "ScaleBar", "/Placement", 2 );
Expand All @@ -129,63 +88,36 @@ void QgsScaleBarPlugin::projectRead()
int myBlueInt = QgsProject::instance()->readNumEntry( "ScaleBar", "/ColorBluePart", 0 );
mColor = QColor( myRedInt, myGreenInt, myBlueInt );
}
//method defined in interface
void QgsScaleBarPlugin::help()
{
//implement me!
}

// Slot called when the menu item is activated
void QgsScaleBarPlugin::run()
void QgsDecorationScaleBar::saveToProject()
{
QgsScaleBarPluginGui *myPluginGui = new QgsScaleBarPluginGui( qGisInterface->mainWindow(), QgisGui::ModalDialogFlags );
myPluginGui->setAttribute( Qt::WA_DeleteOnClose );
myPluginGui->setPreferredSize( mPreferredSize );
myPluginGui->setSnapping( mSnapping );
myPluginGui->setPlacementLabels( mPlacementLabels );
myPluginGui->setPlacement( mPlacementIndex );
myPluginGui->setEnabled( mEnabled );
myPluginGui->setStyleLabels( mStyleLabels );
myPluginGui->setStyle( mStyleIndex );
myPluginGui->setColor( mColor );

connect( myPluginGui, SIGNAL( changePreferredSize( int ) ), this, SLOT( setPreferredSize( int ) ) );
connect( myPluginGui, SIGNAL( changeSnapping( bool ) ), this, SLOT( setSnapping( bool ) ) );
connect( myPluginGui, SIGNAL( changePlacement( int ) ), this, SLOT( setPlacement( int ) ) );
connect( myPluginGui, SIGNAL( changeEnabled( bool ) ), this, SLOT( setEnabled( bool ) ) );
connect( myPluginGui, SIGNAL( changeStyle( int ) ), this, SLOT( setStyle( int ) ) );
connect( myPluginGui, SIGNAL( changeColor( QColor ) ), this, SLOT( setColor( QColor ) ) );
connect( myPluginGui, SIGNAL( refreshCanvas() ), this, SLOT( refreshCanvas() ) );
myPluginGui->show();
//set the map units in the spin box
int myUnits = qGisInterface->mapCanvas()->mapUnits();
switch ( myUnits )
{
case 0:
myPluginGui->getSpinSize()->setSuffix( tr( " metres/km" ) );
break;
case 1:
myPluginGui->getSpinSize()->setSuffix( tr( " feet/miles" ) );
break;
case 2:
myPluginGui->getSpinSize()->setSuffix( tr( " degrees" ) );
break;
default:
QgsDebugMsg( QString( "Error: not picked up map units - actual value = %1" ).arg( myUnits ) );
};
QgsProject::instance()->writeEntry( "ScaleBar", "/Placement", mPlacementIndex );
QgsProject::instance()->writeEntry( "ScaleBar", "/PreferredSize", mPreferredSize );
QgsProject::instance()->writeEntry( "ScaleBar", "/Snapping", mSnapping );
QgsProject::instance()->writeEntry( "ScaleBar", "/Enabled", mEnabled );
QgsProject::instance()->writeEntry( "ScaleBar", "/Style", mStyleIndex );
QgsProject::instance()->writeEntry( "ScaleBar", "/ColorRedPart", mColor.red() );
QgsProject::instance()->writeEntry( "ScaleBar", "/ColorGreenPart", mColor.green() );
QgsProject::instance()->writeEntry( "ScaleBar", "/ColorBluePart", mColor.blue() );
}


void QgsScaleBarPlugin::refreshCanvas()
void QgsDecorationScaleBar::run()
{
qGisInterface->mapCanvas()->refresh();
}
QgsDecorationScaleBarDialog dlg( *this, QgisApp::instance()->mapCanvas()->mapUnits(), QgisApp::instance() );

if ( dlg.exec() )
{
saveToProject();
QgisApp::instance()->mapCanvas()->refresh();
}
}


// Actual drawing of Scale Bar
void QgsScaleBarPlugin::renderScaleBar( QPainter * theQPainter )
void QgsDecorationScaleBar::renderScaleBar( QPainter * theQPainter )
{
QgsMapCanvas* canvas = QgisApp::instance()->mapCanvas();

int myBufferSize = 1; //softcode this later

//Get canvas dimensions
Expand All @@ -195,10 +127,10 @@ void QgsScaleBarPlugin::renderScaleBar( QPainter * theQPainter )
//Get map units per pixel. This can be negative at times (to do with
//projections) and that just confuses the rest of the code in this
//function, so force to a positive number.
double myMapUnitsPerPixelDouble = qAbs( qGisInterface->mapCanvas()->mapUnitsPerPixel() );
double myMapUnitsPerPixelDouble = qAbs( canvas->mapUnitsPerPixel() );

// Exit if the canvas width is 0 or layercount is 0 or QGIS will freeze
int myLayerCount = qGisInterface->mapCanvas()->layerCount();
int myLayerCount = canvas->layerCount();
if ( !myLayerCount || !myCanvasWidth || !myMapUnitsPerPixelDouble )
return;

Expand Down Expand Up @@ -241,7 +173,7 @@ void QgsScaleBarPlugin::renderScaleBar( QPainter * theQPainter )
}

//Get type of map units and set scale bar unit label text
QGis::UnitType myMapUnits = qGisInterface->mapCanvas()->mapUnits();
QGis::UnitType myMapUnits = canvas->mapUnits();
QString myScaleBarUnitLabel;
switch ( myMapUnits )
{
Expand Down Expand Up @@ -545,134 +477,3 @@ void QgsScaleBarPlugin::renderScaleBar( QPainter * theQPainter )
);
}
}



// Unload the plugin by cleaning up the GUI
void QgsScaleBarPlugin::unload()
{
// remove the GUI
qGisInterface->removePluginMenu( tr( "&Decorations" ), myQActionPointer );
qGisInterface->removeToolBarIcon( myQActionPointer );

// remove the northarrow from the canvas
disconnect( qGisInterface->mapCanvas(), SIGNAL( renderComplete( QPainter * ) ),
this, SLOT( renderScaleBar( QPainter * ) ) );
refreshCanvas();

delete myQActionPointer;
}

//! set placement of scale bar
void QgsScaleBarPlugin::setPlacement( int placementIndex )
{
mPlacementIndex = placementIndex;
QgsProject::instance()->writeEntry( "ScaleBar", "/Placement", mPlacementIndex );
}

//! set preferred size of scale bar
void QgsScaleBarPlugin::setPreferredSize( int thePreferredSize )
{
mPreferredSize = thePreferredSize;
QgsProject::instance()->writeEntry( "ScaleBar", "/PreferredSize", mPreferredSize );
}

//! set whether the scale bar length should snap to the closes A*10^B
void QgsScaleBarPlugin::setSnapping( bool theSnapping )
{
mSnapping = theSnapping;
QgsProject::instance()->writeEntry( "ScaleBar", "/Snapping", mSnapping );
}

//! set scale bar enable
void QgsScaleBarPlugin::setEnabled( bool theBool )
{
mEnabled = theBool;
QgsProject::instance()->writeEntry( "ScaleBar", "/Enabled", mEnabled );
}
//! set scale bar enable
void QgsScaleBarPlugin::setStyle( int styleIndex )
{
mStyleIndex = styleIndex;
QgsProject::instance()->writeEntry( "ScaleBar", "/Style", mStyleIndex );
}
//! set the scale bar color
void QgsScaleBarPlugin::setColor( QColor theQColor )
{
mColor = theQColor;
QgsProject::instance()->writeEntry( "ScaleBar", "/ColorRedPart", mColor.red() );
QgsProject::instance()->writeEntry( "ScaleBar", "/ColorGreenPart", mColor.green() );
QgsProject::instance()->writeEntry( "ScaleBar", "/ColorBluePart", mColor.blue() );
}

//! Set icons to the current theme
void QgsScaleBarPlugin::setCurrentTheme( QString theThemeName )
{
Q_UNUSED( theThemeName );
QString myCurThemePath = QgsApplication::activeThemePath() + "/plugins/scale_bar.png";
QString myDefThemePath = QgsApplication::defaultThemePath() + "/plugins/scale_bar.png";
QString myQrcPath = ":/scale_bar.png";
if ( QFile::exists( myCurThemePath ) )
{
myQActionPointer->setIcon( QIcon( myCurThemePath ) );
}
else if ( QFile::exists( myDefThemePath ) )
{
myQActionPointer->setIcon( QIcon( myDefThemePath ) );
}
else if ( QFile::exists( myQrcPath ) )
{
myQActionPointer->setIcon( QIcon( myQrcPath ) );
}
else
{
myQActionPointer->setIcon( QIcon() );
}
}

/**
* 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 QgsScaleBarPlugin( 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 name_;
}

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

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

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

QGISEXTERN QString icon()
{
return icon_;
}

// Delete ourself
QGISEXTERN void unload( QgisPlugin * thePluginPointer )
{
delete thePluginPointer;
}
64 changes: 13 additions & 51 deletions src/plugins/scale_bar/plugin.h → src/app/qgsdecorationscalebar.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,69 +20,34 @@ email : sbr00pwb@users.sourceforge.net
***************************************************************************/
#ifndef QGSCALEBARPLUGIN
#define QGSCALEBARPLUGIN
#include "../qgisplugin.h"

class QgisInterface;
class QAction;
class QToolBar;
class QPainter;

#include <QColor>
#include <QObject>

/**
* \class Plugin
* \brief OpenModeller plugin for QGIS
*
*/
class QgsScaleBarPlugin: public QObject, public QgisPlugin
class QgsDecorationScaleBar: public QObject
{
Q_OBJECT public:
/**
* Constructor for a plugin. The QgisInterface pointer is passed by
* QGIS when it attempts to instantiate the plugin.
* @param qI Pointer to the QgisInterface object.
*/
QgsScaleBarPlugin( QgisInterface * );


Q_OBJECT
public:
//! Constructor
QgsDecorationScaleBar( QObject* parent = NULL );
//! Destructor
virtual ~ QgsScaleBarPlugin();
virtual ~ QgsDecorationScaleBar();

public slots:
//! init the gui
virtual void initGui();
//!set values on the gui when a project is read or the gui first loaded
//! set values on the gui when a project is read or the gui first loaded
void projectRead();
//!this does the meaty bit of the work
//! save values to the project
void saveToProject();

//! this does the meaty bit of the work
void renderScaleBar( QPainter * );
//! Show the dialog box
void run();
//! Refresh the map display using the mapcanvas exported via the plugin interface
void refreshCanvas();
//! unload the plugin
void unload();
//! show the help document
void help();
//! set scale bar placement.
void setPlacement( int );
//! set preferred size of scale bar
void setPreferredSize( int );
//! set whether the scale bar length should snap to the closest A*10^B
void setSnapping( bool );
//! set whether scale bar is enabled
void setEnabled( bool );
//! set the scale bar style
void setStyle( int );
//! set the scale bar color
void setColor( QColor );
//! update the plugins theme when the app tells us its theme is changed
void setCurrentTheme( QString theThemeName );

private:



int pluginType;
//! Placement of the scale bar. An index and the translated text
int mPlacementIndex;
QStringList mPlacementLabels;
Expand All @@ -98,10 +63,7 @@ class QgsScaleBarPlugin: public QObject, public QgisPlugin
//! The scale bar color
QColor mColor;

//! Pointer to the QGIS interface object
QgisInterface *qGisInterface;
//! Pointer to the QAction object used in the menu and toolbar
QAction *myQActionPointer;
friend class QgsDecorationScaleBarDialog;
};

#endif
92 changes: 92 additions & 0 deletions src/app/qgsdecorationscalebardialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/***************************************************************************
* Copyright (C) 2003 by Tim Sutton *
* tim@linfiniti.com *
* *
* This is a plugin generated from the QGIS plugin template *
* *
* 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. *
***************************************************************************/

#include "qgsdecorationscalebardialog.h"

#include "qgsdecorationscalebar.h"

#include "qgslogger.h"
#include "qgscontexthelp.h"

#include <QColorDialog>

QgsDecorationScaleBarDialog::QgsDecorationScaleBarDialog( QgsDecorationScaleBar& deco, int units, QWidget* parent )
: QDialog( parent ), mDeco( deco )
{
setupUi( this );

// set the map units in the spin box
switch ( units )
{
case 0:
spnSize->setSuffix( tr( " metres/km" ) );
break;
case 1:
spnSize->setSuffix( tr( " feet/miles" ) );
break;
case 2:
spnSize->setSuffix( tr( " degrees" ) );
break;
default:
QgsDebugMsg( QString( "Error: not picked up map units - actual value = %1" ).arg( units ) );
}
spnSize->setValue( mDeco.mPreferredSize );

chkSnapping->setChecked( mDeco.mSnapping );
\
cboPlacement->clear();
cboPlacement->addItems( mDeco.mPlacementLabels );
cboPlacement->setCurrentIndex( mDeco.mPlacementIndex );

chkEnable->setChecked( mDeco.mEnabled );

cboStyle->clear();
cboStyle->addItems( mDeco.mStyleLabels );

cboStyle->setCurrentIndex( mDeco.mStyleIndex );

pbnChangeColor->setColor( mDeco.mColor );
}

QgsDecorationScaleBarDialog::~QgsDecorationScaleBarDialog()
{
}

void QgsDecorationScaleBarDialog::on_buttonBox_helpRequested()
{
QgsContextHelp::run( metaObject()->className() );
}

void QgsDecorationScaleBarDialog::on_buttonBox_accepted()
{
mDeco.mPlacementIndex = cboPlacement->currentIndex();
mDeco.mPreferredSize = spnSize->value();
mDeco.mSnapping = chkSnapping->isChecked();
mDeco.mEnabled = chkEnable->isChecked();
mDeco.mStyleIndex = cboStyle->currentIndex();
mDeco.mColor = pbnChangeColor->color();

accept();
}

void QgsDecorationScaleBarDialog::on_pbnChangeColor_clicked()
{
QColor color = QColorDialog::getColor( pbnChangeColor->color(), this );

if ( color.isValid() )
pbnChangeColor->setColor( color );
}

void QgsDecorationScaleBarDialog::on_buttonBox_rejected()
{
reject();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,30 @@
#ifndef QGSSCALEBARPLUGINGUI_H
#define QGSSCALEBARPLUGINGUI_H

#include <ui_pluginguibase.h>
#include "ui_qgsdecorationscalebardialog.h"
#include <QDialog>
#include "qgscontexthelp.h"

class QgsDecorationScaleBar;

/**
@author Peter Brewer
*/
class QgsScaleBarPluginGui : public QDialog, private Ui::QgsScaleBarPluginGuiBase
class QgsDecorationScaleBarDialog : public QDialog, private Ui::QgsDecorationScaleBarDialog
{
Q_OBJECT

public:
QgsScaleBarPluginGui( QWidget* parent = 0, Qt::WFlags fl = 0 );
~QgsScaleBarPluginGui();
void setPlacementLabels( QStringList& );
void setPlacement( int );
void setPreferredSize( int );
void setSnapping( bool );
void setEnabled( bool );
void setStyleLabels( QStringList& );
void setStyle( int );
void setColor( QColor );

//accessor for getting a pointer to the size spin widget
QSpinBox * getSpinSize();
QgsDecorationScaleBarDialog( QgsDecorationScaleBar& deco, int units, QWidget* parent = 0 );
~QgsDecorationScaleBarDialog();

private slots:
void on_buttonBox_accepted();
void on_buttonBox_rejected();
void on_buttonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }
void on_buttonBox_helpRequested();
void on_pbnChangeColor_clicked();

signals:
void drawRasterLayer( QString );
void drawVectorrLayer( QString, QString, QString );
void changePlacement( int );
void changePreferredSize( int );
void changeSnapping( bool );
void changeEnabled( bool );
void changeStyle( int );
void changeColor( QColor );
void refreshCanvas();
protected:
QgsDecorationScaleBar& mDeco;
};

#endif
1 change: 0 additions & 1 deletion src/plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ SET (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${QGIS_OUTPUT_DIRECTORY}/${QGIS_PLUGIN_SUBDI
ADD_SUBDIRECTORY(delimited_text)
ADD_SUBDIRECTORY(diagram_overlay)
ADD_SUBDIRECTORY(interpolation)
ADD_SUBDIRECTORY(scale_bar)
ADD_SUBDIRECTORY(oracle_raster)
ADD_SUBDIRECTORY(raster_terrain_analysis)
ADD_SUBDIRECTORY(coordinate_capture)
Expand Down
48 changes: 0 additions & 48 deletions src/plugins/scale_bar/CMakeLists.txt

This file was deleted.

97 changes: 0 additions & 97 deletions src/plugins/scale_bar/plugingui.cpp

This file was deleted.

124 changes: 0 additions & 124 deletions src/plugins/scale_bar/scale_bar.svg

This file was deleted.

5 changes: 0 additions & 5 deletions src/plugins/scale_bar/scalebar_plugin.qrc

This file was deleted.

13 changes: 13 additions & 0 deletions src/ui/qgisapp.ui
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
</property>
<addaction name="mActionDecorationCopyright"/>
<addaction name="mActionDecorationNorthArrow"/>
<addaction name="mActionDecorationScaleBar"/>
</widget>
<addaction name="mActionPan"/>
<addaction name="mActionZoomIn"/>
Expand Down Expand Up @@ -1515,6 +1516,18 @@
<string>&quot;Creates a north arrow that is displayed on the map canvas&quot;</string>
</property>
</action>
<action name="mActionDecorationScaleBar">
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/plugins/scale_bar.png</normaloff>:/images/themes/default/plugins/scale_bar.png</iconset>
</property>
<property name="text">
<string>&amp;Scale Bar</string>
</property>
<property name="whatsThis">
<string>Creates a scale bar that is displayed on the map canvas</string>
</property>
</action>
</widget>
<resources>
<include location="../../images/images.qrc"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>QgsScaleBarPluginGuiBase</class>
<widget class="QDialog" name="QgsScaleBarPluginGuiBase">
<class>QgsDecorationScaleBarDialog</class>
<widget class="QDialog" name="QgsDecorationScaleBarDialog">
<property name="geometry">
<rect>
<x>0</x>
Expand All @@ -11,7 +11,7 @@
</rect>
</property>
<property name="windowTitle">
<string>Scale Bar Plugin</string>
<string>Scale Bar Decoration</string>
</property>
<property name="windowIcon">
<iconset>
Expand Down Expand Up @@ -236,8 +236,6 @@
<tabstop>chkSnapping</tabstop>
<tabstop>buttonBox</tabstop>
</tabstops>
<resources>
<include location="scalebar_plugin.qrc"/>
</resources>
<resources/>
<connections/>
</ui>