void QgsDecorationNorthArrowDialog::rotatePixmap( int theRotationInt ) { QPixmap myQPixmap; QString myFileNameQString = ":/images/north_arrows/default.png"; // QgsDebugMsg(QString("Trying to load %1").arg(myFileNameQString)); if ( myQPixmap.load( myFileNameQString ) ) { QPixmap myPainterPixmap( myQPixmap.height(), myQPixmap.width() ); myPainterPixmap.fill(); QPainter myQPainter; myQPainter.begin( &myPainterPixmap ); myQPainter.setRenderHint( QPainter::SmoothPixmapTransform ); double centerXDouble = myQPixmap.width() / 2.0; double centerYDouble = myQPixmap.height() / 2.0; //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 canvas again myQPainter.restore(); myQPainter.end(); pixmapLabel->setPixmap( myPainterPixmap ); } else { QPixmap myPainterPixmap( 200, 200 ); myPainterPixmap.fill(); QPainter myQPainter; myQPainter.begin( &myPainterPixmap ); QFont myQFont( "time", 12, QFont::Bold ); myQPainter.setFont( myQFont ); myQPainter.setPen( Qt::red ); myQPainter.drawText( 10, 20, tr( "Pixmap not found" ) ); myQPainter.end(); pixmapLabel->setPixmap( myPainterPixmap ); } }
void QgsDecorationNorthArrowDialog::drawNorthArrow() { int rotation = spinAngle->value(); double maxLength = 64; QSvgRenderer svg; const QByteArray &svgContent = QgsApplication::svgCache()->svgContent( mDeco.svgPath(), maxLength, pbnChangeColor->color(), pbnChangeOutlineColor->color(), 1.0, 1.0 ); svg.load( svgContent ); if ( svg.isValid() ) { QSize size( maxLength, maxLength ); QRectF viewBox = svg.viewBoxF(); if ( viewBox.height() > viewBox.width() ) { size.setWidth( maxLength * viewBox.width() / viewBox.height() ); } else { size.setHeight( maxLength * viewBox.height() / viewBox.width() ); } QPixmap myPainterPixmap( maxLength, maxLength ); myPainterPixmap.fill(); QPainter myQPainter; myQPainter.begin( &myPainterPixmap ); myQPainter.setRenderHint( QPainter::SmoothPixmapTransform ); double centerXDouble = size.width() / 2.0; double centerYDouble = size.height() / 2.0; //save the current canvas rotation myQPainter.save(); myQPainter.translate( ( maxLength - size.width() ) / 2, ( maxLength - size.height() ) / 2 ); //rotate the canvas myQPainter.rotate( rotation ); //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) double myRadiansDouble = ( M_PI / 180 ) * rotation; int xShift = static_cast<int>( ( ( centerXDouble * std::cos( myRadiansDouble ) ) + ( centerYDouble * std::sin( myRadiansDouble ) ) ) - centerXDouble ); int yShift = static_cast<int>( ( ( -centerXDouble * std::sin( myRadiansDouble ) ) + ( centerYDouble * std::cos( myRadiansDouble ) ) ) - centerYDouble ); //draw the pixmap in the proper position myQPainter.translate( xShift, yShift ); svg.render( &myQPainter, QRectF( 0, 0, size.width(), size.height() ) ); //unrotate the canvas again myQPainter.restore(); myQPainter.end(); pixmapLabel->setPixmap( myPainterPixmap ); } else { QPixmap myPainterPixmap( 200, 200 ); myPainterPixmap.fill(); QPainter myQPainter; myQPainter.begin( &myPainterPixmap ); QFont myQFont( QStringLiteral( "time" ), 12, QFont::Bold ); myQPainter.setFont( myQFont ); myQPainter.setPen( Qt::red ); myQPainter.drawText( 10, 20, tr( "Pixmap not found" ) ); myQPainter.end(); pixmapLabel->setPixmap( myPainterPixmap ); } }