QGIS/plugins/north_arrow/plugingui.cpp
g_j_m 2f3d623a44 Bug fix for qgis crash when starting the northarrow gui after loading it.
Bug was the
same as for earlier fix with plugins, but with gui part. Will do the gui
parts of the other plugins in due course


git-svn-id: http://svn.osgeo.org/qgis/trunk@3207 c8812cc2-4d05-0410-92ff-de0c093fc19c
2005-04-24 00:22:35 +00:00

174 lines
5.3 KiB
C++

/***************************************************************************
* 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 "plugingui.h"
//qt includes
#include <qapplication.h>
#include <qpainter.h>
#include <qlabel.h>
#include <iostream>
#include <qspinbox.h>
#include <qslider.h>
#include <qtabwidget.h>
#include <qcombobox.h>
#include <qcheckbox.h>
//standard includes
#include <math.h>
QgsNorthArrowPluginGui::QgsNorthArrowPluginGui() : QgsNorthArrowPluginGuiBase()
{
//temporary hack until this is implemented
tabNorthArrowOptions->removePage( tabIcon );
rotatePixmap(0);
}
QgsNorthArrowPluginGui::QgsNorthArrowPluginGui( QWidget* parent , const char* name , bool modal , WFlags fl )
: QgsNorthArrowPluginGuiBase( parent, name, modal, fl )
{
//temporary hack until this is implemented
tabNorthArrowOptions->removePage( tabIcon );
}
QgsNorthArrowPluginGui::~QgsNorthArrowPluginGui()
{
}
void QgsNorthArrowPluginGui::pbnOK_clicked()
{
// Hide the dialog
hide();
//close the dialog
emit rotationChanged(sliderRotation->value());
emit changePlacement(cboPlacement->currentText());
emit enableNorthArrow(cboxShow->isChecked());
done(1);
}
void QgsNorthArrowPluginGui::pbnCancel_clicked()
{
close(1);
}
void QgsNorthArrowPluginGui::setRotation(int theInt)
{
rotatePixmap(theInt);
sliderRotation->setValue(theInt);
}
void QgsNorthArrowPluginGui::setPlacement(QString thePlacementQString)
{
cboPlacement->setCurrentText(tr(thePlacementQString));
}
void QgsNorthArrowPluginGui::setEnabled(bool theBool)
{
cboxShow->setChecked(theBool);
}
//overides function byt the same name created in .ui
void QgsNorthArrowPluginGui::spinSize_valueChanged( int theInt)
{
}
//overides function byt the same name created in .ui
void QgsNorthArrowPluginGui::sliderRotation_valueChanged( int theInt)
{
rotatePixmap(theInt);
}
void QgsNorthArrowPluginGui::rotatePixmap(int theRotationInt)
{
QPixmap myQPixmap;
#if defined(WIN32) || defined(Q_OS_MACX)
QString PKGDATAPATH = qApp->applicationDirPath() + "/share/qgis";
#endif
QString myFileNameQString = QString(PKGDATAPATH) + QString("/images/north_arrows/default.png");
//std::cout << "Trying to load " << myFileNameQString << std::cout;
if (myQPixmap.load(myFileNameQString))
{
QPixmap myPainterPixmap(myQPixmap.height(),myQPixmap.width());
myPainterPixmap.fill();
QPainter myQPainter;
myQPainter.begin(&myPainterPixmap);
double centerXDouble = myQPixmap.width()/2;
double centerYDouble = myQPixmap.height()/2;
//save the current canvas rotation
myQPainter.save();
//myQPainter.translate( (int)centerXDouble, (int)centerYDouble );
//rotate the canvas
myQPainter.rotate( theRotationInt );
//work out how to shift the image so that it appears in the center of the canvas
//(x cos a + y sin a - x, -x sin a + y cos a - y)
const double PI = 3.14159265358979323846;
double myRadiansDouble = (PI/180) * theRotationInt;
int xShift = static_cast<int>((
(centerXDouble * cos(myRadiansDouble)) +
(centerYDouble * sin(myRadiansDouble))
) - centerXDouble);
int yShift = static_cast<int>((
(-centerXDouble * sin(myRadiansDouble)) +
(centerYDouble * cos(myRadiansDouble))
) - centerYDouble);
//draw the pixmap in the proper position
myQPainter.drawPixmap(xShift,yShift,myQPixmap);
//unrotate the canvase again
myQPainter.restore();
myQPainter.end();
//determine the center of the canvas given that we will bitblt form the origin of the narrow
int myCenterXInt = static_cast<int>((pixmapLabel->width()-myQPixmap.width())/2);
int myCenterYInt = static_cast<int>((pixmapLabel->height()-myQPixmap.height())/2);
bitBlt ( pixmapLabel, myCenterXInt,myCenterYInt, &myPainterPixmap, 0, 0, -1 , -1, Qt::CopyROP, false);
//pixmapLabel1->setPixmap(myPainterPixmap);
}
else
{
QPixmap myPainterPixmap(200,200);
myPainterPixmap.fill();
QPainter myQPainter;
myQPainter.begin(&myPainterPixmap);
QFont myQFont("time", 18, QFont::Bold);
myQPainter.setFont(myQFont);
myQPainter.setPen(Qt::red);
myQPainter.drawText(10, 20, QString("Pixmap Not Found"));
myQPainter.end();
pixmapLabel->setPixmap(myPainterPixmap);
}
}
// Called when the widget needs to be updated.
//
void QgsNorthArrowPluginGui::paintEvent( QPaintEvent * thePaintEvent)
{
rotatePixmap(sliderRotation->value());
}
//
// Called when the widget has been resized.
//
void QgsNorthArrowPluginGui::resizeEvent( QResizeEvent * theResizeEvent)
{
rotatePixmap(sliderRotation->value());
}