-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqgsdecorationnortharrowdialog.cpp
172 lines (146 loc) · 6.12 KB
/
qgsdecorationnortharrowdialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/***************************************************************************
* Copyright (C) 2003 by Tim Sutton *
* *
* 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 "qgsdecorationnortharrowdialog.h"
#include "qgsdecorationnortharrow.h"
#include "qgslogger.h"
#include "qgshelp.h"
#include "qgssettings.h"
#include <QPainter>
#include <cmath>
#include <QDialogButtonBox>
#include <QPushButton>
QgsDecorationNorthArrowDialog::QgsDecorationNorthArrowDialog( QgsDecorationNorthArrow &deco, QWidget *parent )
: QDialog( parent )
, mDeco( deco )
{
setupUi( this );
QgsSettings settings;
restoreGeometry( settings.value( QStringLiteral( "Windows/DecorationNorthArrow/geometry" ) ).toByteArray() );
QPushButton *applyButton = buttonBox->button( QDialogButtonBox::Apply );
connect( applyButton, SIGNAL( clicked() ), this, SLOT( apply() ) );
// rotation
rotatePixmap( mDeco.mRotationInt );
// signal/slot connection defined in 'designer' causes the slider to
// be moved to reflect the change in the spinbox.
spinAngle->setValue( mDeco.mRotationInt );
// placement
cboPlacement->addItem( tr( "Top left" ), QgsDecorationItem::TopLeft );
cboPlacement->addItem( tr( "Top right" ), QgsDecorationItem::TopRight );
cboPlacement->addItem( tr( "Bottom left" ), QgsDecorationItem::BottomLeft );
cboPlacement->addItem( tr( "Bottom right" ), QgsDecorationItem::BottomRight );
cboPlacement->setCurrentIndex( cboPlacement->findData( mDeco.placement() ) );
spinHorizontal->setValue( mDeco.mMarginHorizontal );
spinVertical->setValue( mDeco.mMarginVertical );
wgtUnitSelection->setUnits( QgsUnitTypes::RenderUnitList() << QgsUnitTypes::RenderMillimeters << QgsUnitTypes::RenderPercentage << QgsUnitTypes::RenderPixels );
wgtUnitSelection->setUnit( mDeco.mMarginUnit );
// enabled
grpEnable->setChecked( mDeco.enabled() );
// automatic
cboxAutomatic->setChecked( mDeco.mAutomatic );
}
QgsDecorationNorthArrowDialog::~QgsDecorationNorthArrowDialog()
{
QgsSettings settings;
settings.setValue( QStringLiteral( "Windows/DecorationNorthArrow/geometry" ), saveGeometry() );
}
void QgsDecorationNorthArrowDialog::on_buttonBox_helpRequested()
{
QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#north-arrow" ) );
}
void QgsDecorationNorthArrowDialog::on_buttonBox_accepted()
{
apply();
accept();
}
void QgsDecorationNorthArrowDialog::on_buttonBox_rejected()
{
reject();
}
void QgsDecorationNorthArrowDialog::on_spinAngle_valueChanged( int spinAngle )
{
Q_UNUSED( spinAngle );
}
void QgsDecorationNorthArrowDialog::on_sliderRotation_valueChanged( int rotationValue )
{
rotatePixmap( rotationValue );
}
void QgsDecorationNorthArrowDialog::apply()
{
mDeco.mRotationInt = sliderRotation->value();
mDeco.setPlacement( static_cast< QgsDecorationItem::Placement>( cboPlacement->currentData().toInt() ) );
mDeco.mMarginUnit = wgtUnitSelection->unit();
mDeco.setEnabled( grpEnable->isChecked() );
mDeco.mAutomatic = cboxAutomatic->isChecked();
mDeco.mMarginHorizontal = spinHorizontal->value();
mDeco.mMarginVertical = spinVertical->value();
mDeco.update();
}
void QgsDecorationNorthArrowDialog::rotatePixmap( int rotationInt )
{
QPixmap myQPixmap;
QString myFileNameQString = QStringLiteral( ":/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( rotationInt );
//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 ) * rotationInt;
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( 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 );
}
}
//
// Called when the widget has been resized.
//
void QgsDecorationNorthArrowDialog::resizeEvent( QResizeEvent *resizeEvent )
{
Q_UNUSED( resizeEvent );
rotatePixmap( sliderRotation->value() );
}