Updated renderNorthArrow slot to receive and draw to the qpainter pointer passed by the mapcanvas::renderComplete signal. This allows the north arrow plugin to draw onto the map even when it is being rendered to a device other than the mapCanvas' own pixmap member.

git-svn-id: http://svn.osgeo.org/qgis/trunk@1502 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
timlinux 2004-06-01 01:04:54 +00:00
parent f4f5e228ed
commit c20fc2ecf9
2 changed files with 85 additions and 90 deletions

View File

@ -66,9 +66,9 @@ static const QgisPlugin::PLUGINTYPE type_ = QgisPlugin::UI;
* @param _qI Pointer to the QGIS interface object * @param _qI Pointer to the QGIS interface object
*/ */
Plugin::Plugin(QgisApp * theQGisApp, QgisIface * theQgisInterFace): Plugin::Plugin(QgisApp * theQGisApp, QgisIface * theQgisInterFace):
qgisMainWindowPointer(theQGisApp), qgisMainWindowPointer(theQGisApp),
qGisInterface(theQgisInterFace), qGisInterface(theQgisInterFace),
QgisPlugin(name_,description_,version_,type_) QgisPlugin(name_,description_,version_,type_)
{ {
mRotationInt=0; mRotationInt=0;
mPlacement=tr("Bottom Left"); mPlacement=tr("Bottom Left");
@ -76,7 +76,6 @@ QgisPlugin(name_,description_,version_,type_)
Plugin::~Plugin() Plugin::~Plugin()
{ {
} }
/* /*
@ -97,7 +96,7 @@ void Plugin::initGui()
// Connect the action to the run // Connect the action to the run
connect(myQActionPointer, SIGNAL(activated()), this, SLOT(run())); connect(myQActionPointer, SIGNAL(activated()), this, SLOT(run()));
//render the arrow each time the map is rendered //render the arrow each time the map is rendered
connect(qGisInterface->getMapCanvas(), SIGNAL(renderComplete()), this, SLOT(renderNorthArrow())); connect(qGisInterface->getMapCanvas(), SIGNAL(renderComplete(QPainter *)), this, SLOT(renderNorthArrow(QPainter *)));
// Add the toolbar // Add the toolbar
toolBarPointer = new QToolBar((QMainWindow *) qgisMainWindowPointer, "Decorations"); toolBarPointer = new QToolBar((QMainWindow *) qgisMainWindowPointer, "Decorations");
toolBarPointer->setLabel("North Arrow"); toolBarPointer->setLabel("North Arrow");
@ -134,92 +133,86 @@ void Plugin::refreshCanvas()
qGisInterface->getMapCanvas()->refresh(); qGisInterface->getMapCanvas()->refresh();
} }
void Plugin::renderNorthArrow() void Plugin::renderNorthArrow(QPainter * theQPainter)
{ {
#ifdef QGISDEBUG
//Large IF statement controlled by enable check box std::cout << "Rendering n-arrow" << std::endl;
if (mEnable) #endif
{ //Large IF statement controlled by enable check box
QPixmap myQPixmap; //to store the north arrow image in if (mEnable)
QString myFileNameQString = QString(PKGDATAPATH) +
QString("/images/north_arrows/default.png");
//std::cout << "Trying to load " << myFileNameQString << std::cout;
if (myQPixmap.load(myFileNameQString))
{ {
// myPainterPixmap.fill(); QPixmap myQPixmap; //to store the north arrow image in
QPainter myQPainter; QString myFileNameQString = QString(PKGDATAPATH) +
myQPainter.begin(qGisInterface->getMapCanvas()->canvasPixmap()); QString("/images/north_arrows/default.png");
//std::cout << "Trying to load " << myFileNameQString << std::cout;
if (myQPixmap.load(myFileNameQString))
{
double centerXDouble = myQPixmap.width()/2; double centerXDouble = myQPixmap.width()/2;
double centerYDouble = myQPixmap.height()/2; double centerYDouble = myQPixmap.height()/2;
//save the current canvas rotation //save the current canvas rotation
myQPainter.save(); theQPainter->save();
// //
//work out how to shift the image so that it rotates //work out how to shift the image so that it rotates
// properly about its center // properly about its center
//(x cos a + y sin a - x, -x sin a + y cos a - y) //(x cos a + y sin a - x, -x sin a + y cos a - y)
// //
const double PI = 3.14159265358979323846; const double PI = 3.14159265358979323846;
double myRadiansDouble = (PI/180) * mRotationInt; double myRadiansDouble = (PI/180) * mRotationInt;
int xShift = static_cast<int>(( int xShift = static_cast<int>((
(centerXDouble * cos(myRadiansDouble)) + (centerXDouble * cos(myRadiansDouble)) +
(centerYDouble * sin(myRadiansDouble)) (centerYDouble * sin(myRadiansDouble))
) - centerXDouble); ) - centerXDouble);
int yShift = static_cast<int>(( int yShift = static_cast<int>((
(-centerXDouble * sin(myRadiansDouble)) + (-centerXDouble * sin(myRadiansDouble)) +
(centerYDouble * cos(myRadiansDouble)) (centerYDouble * cos(myRadiansDouble))
) - centerYDouble); ) - centerYDouble);
//Get canvas dimensions //Get canvas dimensions
int myHeight = qGisInterface->getMapCanvas()->height(); int myHeight = qGisInterface->getMapCanvas()->height();
int myWidth = qGisInterface->getMapCanvas()->width(); int myWidth = qGisInterface->getMapCanvas()->width();
#ifdef QGISDEBUG #ifdef QGISDEBUG
std::cout << "Rendering n-arrow at " << mPlacement << std::endl; std::cout << "Rendering n-arrow at " << mPlacement << std::endl;
#endif #endif
//Determine placement of label from form combo box //Determine placement of label from form combo box
if (mPlacement==tr("Bottom Left")) if (mPlacement==tr("Bottom Left"))
{ {
myQPainter.translate(0,myHeight-myQPixmap.height()); theQPainter->translate(0,myHeight-myQPixmap.height());
} }
else if (mPlacement==tr("Top Right")) else if (mPlacement==tr("Top Right"))
{ {
myQPainter.translate(myWidth-myQPixmap.width(),0); theQPainter->translate(myWidth-myQPixmap.width(),0);
} }
else if (mPlacement==tr("Bottom Right")) else if (mPlacement==tr("Bottom Right"))
{ {
myQPainter.translate(myWidth-myQPixmap.width(), theQPainter->translate(myWidth-myQPixmap.width(),
myHeight-myQPixmap.height()); myHeight-myQPixmap.height());
} }
else // defaulting to top left else // defaulting to top left
{ {
//no need to translate for TL corner because we're already at the origin //no need to translate for TL corner because we're already at the origin
myQPainter.translate(0, 0); theQPainter->translate(0, 0);
} }
//rotate the canvas by the north arrow rotation amount //rotate the canvas by the north arrow rotation amount
myQPainter.rotate( mRotationInt ); theQPainter->rotate( mRotationInt );
//Now we can actually do the drawing //Now we can actually do the drawing
myQPainter.drawPixmap(xShift,yShift,myQPixmap); theQPainter->drawPixmap(xShift,yShift,myQPixmap);
//unrotate the canvase again //unrotate the canvase again
myQPainter.restore(); theQPainter->restore();
myQPainter.end();
//bitBlt ( qGisInterface->getMapCanvas()->canvasPixmap(), 0, 0, &myPainterPixmap, 0, 0, -1 , -1, Qt::CopyROP, false); //bitBlt ( qGisInterface->getMapCanvas()->canvasPixmap(), 0, 0, &myPainterPixmap, 0, 0, -1 , -1, Qt::CopyROP, false);
}
else
{
QFont myQFont("time", 32, QFont::Bold);
theQPainter->setFont(myQFont);
theQPainter->setPen(Qt::black);
theQPainter->drawText(10, 20, QString("Pixmap Not Found"));
}
} }
else
{
//myPainterPixmap.fill();
QPainter myQPainter;
myQPainter.begin(qGisInterface->getMapCanvas()->canvasPixmap());
QFont myQFont("time", 32, QFont::Bold);
myQPainter.setFont(myQFont);
myQPainter.setPen(Qt::black);
myQPainter.drawText(10, 20, QString("Pixmap Not Found"));
myQPainter.end();
}
}
} }
// Unload the plugin by cleaning up the GUI // Unload the plugin by cleaning up the GUI

View File

@ -21,6 +21,8 @@
#define PLUGIN #define PLUGIN
#include "../qgisplugin.h" #include "../qgisplugin.h"
#include <qwidget.h> #include <qwidget.h>
#include <qpainter.h>
#include "../../src/qgisapp.h" #include "../../src/qgisapp.h"
@ -47,7 +49,7 @@ class Plugin:public QObject, public QgisPlugin
//! Show the dialog box //! Show the dialog box
void run(); void run();
// draw some arbitary text to the screen // draw some arbitary text to the screen
void renderNorthArrow(); void renderNorthArrow(QPainter *);
//! Run when the user has set a new rotation //! Run when the user has set a new rotation
void rotationChanged(int); void rotationChanged(int);
//! Refresh the map display using the mapcanvas exported via the plugin interface //! Refresh the map display using the mapcanvas exported via the plugin interface