Skip to content

Commit

Permalink
Moved north arrow plugin to app
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Aug 21, 2011
1 parent 2095872 commit 5ca2c32
Show file tree
Hide file tree
Showing 18 changed files with 130 additions and 586 deletions.
1 change: 1 addition & 0 deletions images/images.qrc
Expand Up @@ -351,6 +351,7 @@
<file>themes/default/locked.png</file>
<file>themes/default/unlocked.png</file>
<file>themes/default/plugins/copyright_label.png</file>
<file>themes/default/plugins/north_arrow.png</file>
</qresource>
<qresource prefix="/images/tips">
<file alias="symbol_levels.png">qgis_tips/symbol_levels.png</file>
Expand Down
File renamed without changes
4 changes: 4 additions & 0 deletions src/app/CMakeLists.txt
Expand Up @@ -23,6 +23,8 @@ SET(QGIS_APP_SRCS
qgsdbtablemodel.cpp
qgsdecorationcopyright.cpp
qgsdecorationcopyrightdialog.cpp
qgsdecorationnortharrow.cpp
qgsdecorationnortharrowdialog.cpp
qgsembedlayerdialog.cpp
qgsformannotationdialog.cpp
qgsdelattrdialog.cpp
Expand Down Expand Up @@ -157,6 +159,8 @@ SET (QGIS_APP_MOC_HDRS
qgsdbtablemodel.h
qgsdecorationcopyright.h
qgsdecorationcopyrightdialog.h
qgsdecorationnortharrow.h
qgsdecorationnortharrowdialog.h
qgsdelattrdialog.h
qgsdisplayangle.h
qgsembedlayerdialog.h
Expand Down
9 changes: 8 additions & 1 deletion src/app/qgisapp.cpp 100755 → 100644
Expand Up @@ -115,6 +115,7 @@
#include "qgscustomprojectiondialog.h"
#include "qgsdatasourceuri.h"
#include "qgsdecorationcopyright.h"
#include "qgsdecorationnortharrow.h"
#include "qgsembedlayerdialog.h"
#include "qgsencodingfiledialog.h"
#include "qgsexception.h"
Expand Down Expand Up @@ -1473,6 +1474,7 @@ void QgisApp::setTheme( QString theThemeName )
mActionRotateLabel->setIcon( getThemeIcon( "/mActionRotateLabel.png" ) );
mActionChangeLabelProperties->setIcon( getThemeIcon( "/mActionChangeLabelProperties.png" ) );
mActionDecorationCopyright->setIcon( getThemeIcon( "/plugins/copyright_label.png" ) );
mActionDecorationNorthArrow->setIcon( getThemeIcon( "/plugins/north_arrow.png" ) );

//change themes of all composers
QSet<QgsComposer*>::iterator composerIt = mPrintComposers.begin();
Expand Down Expand Up @@ -1808,10 +1810,15 @@ void QgisApp::createMapTips()

void QgisApp::createDecorations()
{
mDecorationCopyright = new QgsDecorationCopyright(this);
mDecorationCopyright = new QgsDecorationCopyright( this );
connect( mActionDecorationCopyright, SIGNAL( triggered() ), mDecorationCopyright, SLOT( run() ) );
connect( mMapCanvas, SIGNAL( renderComplete( QPainter * ) ), mDecorationCopyright, SLOT( renderLabel( QPainter * ) ) );
connect( this, SIGNAL( projectRead() ), mDecorationCopyright, SLOT( projectRead() ) );

mDecorationNorthArrow = new QgsDecorationNorthArrow( this );
connect( mActionDecorationNorthArrow, SIGNAL( triggered() ), mDecorationNorthArrow, SLOT( run() ) );
connect( mMapCanvas, SIGNAL( renderComplete( QPainter * ) ), mDecorationNorthArrow, SLOT( renderNorthArrow( QPainter * ) ) );
connect( this, SIGNAL( projectRead() ), mDecorationNorthArrow, SLOT( projectRead() ) );
}

// Update file menu with the current list of recently accessed projects
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -70,6 +70,7 @@ class QgsSnappingDialog;
class QgsGPSInformationWidget;

class QgsDecorationCopyright;
class QgsDecorationNorthArrow;

#include <QMainWindow>
#include <QToolBar>
Expand Down Expand Up @@ -1055,6 +1056,7 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
QgsTileScaleWidget * mpTileScaleWidget;

QgsDecorationCopyright* mDecorationCopyright;
QgsDecorationNorthArrow* mDecorationNorthArrow;

int mLastComposerId;

Expand Down
Expand Up @@ -20,17 +20,17 @@ email : tim@linfiniti.com
***************************************************************************/

// includes
#include "qgsdecorationnortharrow.h"

#include "qgisinterface.h"
#include "qgisgui.h"
#include "qgsdecorationnortharrowdialog.h"

#include "qgisapp.h"
#include "qgscoordinatetransform.h"
#include "qgsmaplayer.h"
#include "plugin.h"
#include "qgsproject.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsmaprenderer.h"
#include "qgsapplication.h"

// qt includes
#include <QPainter>
Expand All @@ -43,22 +43,14 @@ email : tim@linfiniti.com
#include <cmath>
#include <cassert>

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

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

static const QString name_ = QObject::tr( "NorthArrow" );
static const QString description_ = QObject::tr( "Displays a north arrow overlayed onto the map" );
static const QString version_ = QObject::tr( "Version 0.1" );
static const QgisPlugin::PLUGINTYPE type_ = QgisPlugin::UI;
static const QString icon_ = ":/north_arrow.png";

const double QgsNorthArrowPlugin::PI = 3.14159265358979323846;
const double QgsDecorationNorthArrow::PI = 3.14159265358979323846;
// const double QgsNorthArrowPlugin::DEG2RAD = 0.0174532925199433;
const double QgsNorthArrowPlugin::TOL = 1e-8;
const double QgsDecorationNorthArrow::TOL = 1e-8;


/**
Expand All @@ -67,47 +59,22 @@ const double QgsNorthArrowPlugin::TOL = 1e-8;
* @param qgis Pointer to the QGIS main window
* @param _qI Pointer to the QGIS interface object
*/
QgsNorthArrowPlugin::QgsNorthArrowPlugin( QgisInterface * theQgisInterFace ):
QgisPlugin( name_, description_, version_, type_ ),
qGisInterface( theQgisInterFace )
QgsDecorationNorthArrow::QgsDecorationNorthArrow( QObject* parent )
: QObject( parent )
{
mRotationInt = 0;
mAutomatic = true;
mPlacementLabels << tr( "Bottom Left" ) << tr( "Top Left" )
<< tr( "Top Right" ) << tr( "Bottom Right" );
}

QgsNorthArrowPlugin::~QgsNorthArrowPlugin()
{
projectRead();
}

/*
* Initialize the GUI interface for the plugin
*/
void QgsNorthArrowPlugin::initGui()
QgsDecorationNorthArrow::~QgsDecorationNorthArrow()
{
// Create the action for tool
myQActionPointer = new QAction( QIcon(), tr( "&North Arrow" ), this );
setCurrentTheme( "" );
myQActionPointer->setWhatsThis( tr( "Creates a north arrow that is displayed on the map canvas" ) );
// Connect the action to the run
connect( myQActionPointer, SIGNAL( triggered() ), this, SLOT( run() ) );
//render the arrow each time the map is rendered
connect( qGisInterface->mapCanvas(), SIGNAL( renderComplete( QPainter * ) ), this, SLOT( renderNorthArrow( 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 & appropriate menu
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 ) ) );

projectRead();
refreshCanvas();

}

void QgsNorthArrowPlugin::projectRead()
void QgsDecorationNorthArrow::projectRead()
{
//default text to start with - try to fetch it from qgsproject

Expand All @@ -117,40 +84,27 @@ void QgsNorthArrowPlugin::projectRead()
mAutomatic = QgsProject::instance()->readBoolEntry( "NorthArrow", "/Automatic", true );
}

//method defined in interface
void QgsNorthArrowPlugin::help()
void QgsDecorationNorthArrow::saveToProject()
{
//implement me!
QgsProject::instance()->writeEntry( "NorthArrow", "/Rotation", mRotationInt );
QgsProject::instance()->writeEntry( "NorthArrow", "/Placement", mPlacementIndex );
QgsProject::instance()->writeEntry( "NorthArrow", "/Enabled", mEnable );
QgsProject::instance()->writeEntry( "NorthArrow", "/Automatic", mAutomatic );
}

// Slot called when the buffer menu item is activated
void QgsNorthArrowPlugin::run()
void QgsDecorationNorthArrow::run()
{
QgsNorthArrowPluginGui *myPluginGui = new QgsNorthArrowPluginGui( qGisInterface->mainWindow(), QgisGui::ModalDialogFlags );
myPluginGui->setAttribute( Qt::WA_DeleteOnClose );
//overides function by the same name created in .ui
myPluginGui->setRotation( mRotationInt );
myPluginGui->setPlacementLabels( mPlacementLabels );
myPluginGui->setPlacement( mPlacementIndex );
myPluginGui->setEnabled( mEnable );
myPluginGui->setAutomatic( mAutomatic );

//listen for when the layer has been made so we can draw it
connect( myPluginGui, SIGNAL( rotationChanged( int ) ), this, SLOT( rotationChanged( int ) ) );
connect( myPluginGui, SIGNAL( changePlacement( int ) ), this, SLOT( setPlacement( int ) ) );
connect( myPluginGui, SIGNAL( enableAutomatic( bool ) ), this, SLOT( setAutomatic( bool ) ) );
connect( myPluginGui, SIGNAL( enableNorthArrow( bool ) ), this, SLOT( setEnabled( bool ) ) );
connect( myPluginGui, SIGNAL( needToRefresh() ), this, SLOT( refreshCanvas() ) );
myPluginGui->show();
}
QgsDecorationNorthArrowDialog dlg( *this, QgisApp::instance() );

//! Refresh the map display using the mapcanvas exported via the plugin interface
void QgsNorthArrowPlugin::refreshCanvas()
{
qGisInterface->mapCanvas()->refresh();
if ( dlg.exec() )
{
saveToProject();
QgisApp::instance()->mapCanvas()->refresh();
}
}

void QgsNorthArrowPlugin::renderNorthArrow( QPainter * theQPainter )
void QgsDecorationNorthArrow::renderNorthArrow( QPainter * theQPainter )
{

//Large IF statement controlled by enable check box
Expand Down Expand Up @@ -245,57 +199,16 @@ void QgsNorthArrowPlugin::renderNorthArrow( QPainter * theQPainter )
}

}
// Unload the plugin by cleaning up the GUI
void QgsNorthArrowPlugin::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( renderNorthArrow( QPainter * ) ) );
refreshCanvas();

delete myQActionPointer;
}


void QgsNorthArrowPlugin::rotationChanged( int theInt )
{
mRotationInt = theInt;
QgsProject::instance()->writeEntry( "NorthArrow", "/Rotation", mRotationInt );
}

//! set placement of north arrow
void QgsNorthArrowPlugin::setPlacement( int placementIndex )
{
mPlacementIndex = placementIndex;
QgsProject::instance()->writeEntry( "NorthArrow", "/Placement", mPlacementIndex );
}

void QgsNorthArrowPlugin::setEnabled( bool theBool )
{
mEnable = theBool;
QgsProject::instance()->writeEntry( "NorthArrow", "/Enabled", mEnable );
}

void QgsNorthArrowPlugin::setAutomatic( bool theBool )
{
mAutomatic = theBool;
QgsProject::instance()->writeEntry( "NorthArrow", "/Automatic", mAutomatic );
if ( mAutomatic )
calculateNorthDirection();
}

bool QgsNorthArrowPlugin::calculateNorthDirection()
bool QgsDecorationNorthArrow::calculateNorthDirection()
{
QgsMapCanvas& mapCanvas = *( qGisInterface->mapCanvas() );
QgsMapCanvas* mapCanvas = QgisApp::instance()->mapCanvas();

bool goodDirn = false;

if ( mapCanvas.layerCount() > 0 )
if ( mapCanvas->layerCount() > 0 )
{
QgsCoordinateReferenceSystem outputCRS = mapCanvas.mapRenderer()->destinationCrs();
QgsCoordinateReferenceSystem outputCRS = mapCanvas->mapRenderer()->destinationCrs();

if ( outputCRS.isValid() && !outputCRS.geographicFlag() )
{
Expand All @@ -306,7 +219,7 @@ bool QgsNorthArrowPlugin::calculateNorthDirection()

QgsCoordinateTransform transform( outputCRS, ourCRS );

QgsRectangle extent = mapCanvas.extent();
QgsRectangle extent = mapCanvas->extent();
QgsPoint p1( extent.center() );
// A point a bit above p1. XXX assumes that y increases up!!
// May need to involve the maptopixel transform if this proves
Expand Down Expand Up @@ -388,75 +301,3 @@ bool QgsNorthArrowPlugin::calculateNorthDirection()
}
return goodDirn;
}

//! Set icons to the current theme
void QgsNorthArrowPlugin::setCurrentTheme( QString theThemeName )
{
Q_UNUSED( theThemeName );
QString myCurThemePath = QgsApplication::activeThemePath() + "/plugins/north_arrow.png";
QString myDefThemePath = QgsApplication::defaultThemePath() + "/plugins/north_arrow.png";
QString myQrcPath = ":/north_arrow.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 QgsNorthArrowPlugin( 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;
}

0 comments on commit 5ca2c32

Please sign in to comment.