Skip to content

Commit 3a1d47f

Browse files
duncan-rnyalldawson
authored andcommitted
Updated decoration items to use QgsUnitSelectionWidget.
Applies to north arrow, copyright and scalebar. - Changed dialogs to use QgsUnitSelectionWidget. - Added QgsSymbolV2::OutputUnit enum to decorationitem. - Updated to use mm, pixel, or percentage units.
1 parent a89c183 commit 3a1d47f

11 files changed

+393
-276
lines changed

src/app/qgsdecorationcopyright.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ QgsDecorationCopyright::QgsDecorationCopyright( QObject* parent )
4747
, mMarginVertical( 0 )
4848
{
4949
mPlacement = BottomRight;
50+
mMarginUnit = QgsSymbolV2::Pixel;
5051

5152
setName( "Copyright Label" );
5253
// initialise default values in the gui
@@ -112,10 +113,30 @@ void QgsDecorationCopyright::render( QPainter * theQPainter )
112113

113114
float myXOffset( 0 ), myYOffset( 0 );
114115

115-
myXOffset = int(( float( myWidth - size.width() )
116-
/ 100. ) * float( mMarginHorizontal ) );
117-
myYOffset = int(( float( myHeight - size.height() )
118-
/ 100. ) * float( mMarginVertical ) );
116+
// Set margin according to selected units
117+
switch ( mMarginUnit )
118+
{
119+
case 0: // Millimetres
120+
{
121+
int myPixelsInchX = theQPainter->device()->logicalDpiX();
122+
int myPixelsInchY = theQPainter->device()->logicalDpiY();
123+
myXOffset = int(( float( myPixelsInchX ) * INCHES_TO_MM ) * float( mMarginHorizontal ) );
124+
myYOffset = int(( float( myPixelsInchY ) * INCHES_TO_MM ) * float( mMarginVertical ) );
125+
break;
126+
}
127+
case 3: // Pixels
128+
myXOffset = mMarginHorizontal;
129+
myYOffset = mMarginVertical;
130+
break;
131+
case 4: // Percentage
132+
myXOffset = int(( float( myWidth - size.width() )
133+
/ 100. ) * float( mMarginHorizontal ) );
134+
myYOffset = int(( float( myHeight - size.height() )
135+
/ 100. ) * float( mMarginVertical ) );
136+
break;
137+
default: // Use default of top left
138+
break;
139+
}
119140
//Determine placement of label from form combo box
120141
switch ( mPlacement )
121142
{

src/app/qgsdecorationcopyrightdialog.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ QgsDecorationCopyrightDialog::QgsDecorationCopyrightDialog( QgsDecorationCopyrig
4949
cboPlacement->setCurrentIndex( cboPlacement->findData( mDeco.placement() ) );
5050
spnHorizontal->setValue( mDeco.mMarginHorizontal );
5151
spnVertical->setValue( mDeco.mMarginVertical );
52+
wgtUnitSelection->setUnits( QgsSymbolV2::OutputUnitList() << QgsSymbolV2::MM << QgsSymbolV2::Percentage << QgsSymbolV2::Pixel );
53+
wgtUnitSelection->setUnit( mDeco.mMarginUnit );
54+
5255
// color
5356
pbnColorChooser->setColor( mDeco.mLabelQColor );
5457
pbnColorChooser->setContext( "gui" );
@@ -91,6 +94,7 @@ void QgsDecorationCopyrightDialog::apply()
9194
mDeco.mLabelQString = txtCopyrightText->toPlainText();
9295
mDeco.mLabelQColor = pbnColorChooser->color();
9396
mDeco.setPlacement( static_cast< QgsDecorationItem::Placement>( cboPlacement->itemData( cboPlacement->currentIndex() ).toInt() ) );
97+
mDeco.mMarginUnit = wgtUnitSelection->unit();
9498
mDeco.mMarginHorizontal = spnHorizontal->value();
9599
mDeco.mMarginVertical = spnVertical->value();
96100
mDeco.setEnabled( grpEnable->isChecked() );

src/app/qgsdecorationitem.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ QgsDecorationItem::QgsDecorationItem( QObject* parent )
4646
: QObject( parent )
4747
, mEnabled( false )
4848
, mPlacement( TopLeft )
49+
, mMarginUnit( QgsSymbolV2::Pixel )
4950
{
5051
}
5152

@@ -65,14 +66,17 @@ void QgsDecorationItem::projectRead()
6566
QgsDebugMsg( "Entered" );
6667
mEnabled = QgsProject::instance()->readBoolEntry( mNameConfig, "/Enabled", false );
6768
mPlacement = static_cast< Placement >( QgsProject::instance()->readNumEntry( mNameConfig, "/Placement", static_cast< int >( mPlacement ) ) );
69+
mMarginUnit = QgsSymbolLayerV2Utils::decodeOutputUnit( QgsProject::instance()->readEntry( mNameConfig, "/MarginUnit", QgsSymbolLayerV2Utils::encodeOutputUnit( mMarginUnit ) ) );
6870
}
6971

7072
void QgsDecorationItem::saveToProject()
7173
{
7274
QgsDebugMsg( "Entered" );
7375
QgsProject::instance()->writeEntry( mNameConfig, "/Enabled", mEnabled );
7476
QgsProject::instance()->writeEntry( mNameConfig, "/Placement", static_cast< int >( mPlacement ) );
77+
QgsProject::instance()->writeEntry( mNameConfig, "/MarginUnit", QgsSymbolLayerV2Utils::encodeOutputUnit( mMarginUnit ) );
7578
}
79+
7680
void QgsDecorationItem::setName( const char *name )
7781
{
7882
mName = name;

src/app/qgsdecorationitem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <QObject>
2222
#include "qgslogger.h"
23+
#include "qgssymbollayerv2.h"
2324

2425
class QPainter;
2526

@@ -82,6 +83,9 @@ class APP_EXPORT QgsDecorationItem: public QObject
8283

8384
//! Placement of the decoration
8485
Placement mPlacement;
86+
//! Units used for the decoration placement margin
87+
QgsSymbolV2::OutputUnit mMarginUnit;
88+
const double INCHES_TO_MM = 0.0393700787402;
8589

8690
QString mName;
8791
QString mNameConfig;

src/app/qgsdecorationnortharrow.cpp

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ QgsDecorationNorthArrow::QgsDecorationNorthArrow( QObject* parent )
6262
, mMarginVertical( 0 )
6363
{
6464
mPlacement = BottomLeft;
65+
mMarginUnit = QgsSymbolV2::Pixel;
66+
6567
setName( "North Arrow" );
6668
projectRead();
6769
}
@@ -138,27 +140,47 @@ void QgsDecorationNorthArrow::render( QPainter * theQPainter )
138140

139141
//QgsDebugMsg("Rendering north arrow at " + mPlacementLabels.at(mPlacementIndex));
140142

141-
// Calculate the margin percentage values
142-
int myPercentageWidth = int((( float( myWidth ) - float( myQPixmap.width() ) )
143-
/ 100. ) * float( mMarginHorizontal ) );
144-
int myPercentageHeight = int((( float( myHeight ) - float( myQPixmap.height() ) )
145-
/ 100. ) * float( mMarginVertical ) );
146-
143+
// Set margin according to selected units
144+
int myXOffset = 0;
145+
int myYOffset = 0;
146+
switch ( mMarginUnit )
147+
{
148+
case 0: // Millimetres
149+
{
150+
int myPixelsInchX = theQPainter->device()->logicalDpiX();
151+
int myPixelsInchY = theQPainter->device()->logicalDpiY();
152+
myXOffset = int(( float( myPixelsInchX ) * INCHES_TO_MM ) * float( mMarginHorizontal ) );
153+
myYOffset = int(( float( myPixelsInchY ) * INCHES_TO_MM ) * float( mMarginVertical ) );
154+
break;
155+
}
156+
case 3: // Pixels
157+
myXOffset = mMarginHorizontal - 5; // Minus 5 to shift tight into corner
158+
myYOffset = mMarginVertical - 5;
159+
break;
160+
case 4: // Percentage
161+
myXOffset = int((( float( myWidth ) - float( myQPixmap.width() ) )
162+
/ 100. ) * float( mMarginHorizontal ) );
163+
myYOffset = int((( float( myHeight ) - float( myQPixmap.height() ) )
164+
/ 100. ) * float( mMarginVertical ) );
165+
break;
166+
default: // Use default of top left
167+
break;
168+
}
147169
//Determine placement of label from form combo box
148170
switch ( mPlacement )
149171
{
150172
case BottomLeft:
151-
theQPainter->translate( myPercentageWidth, myHeight - myPercentageHeight - myQPixmap.height() );
173+
theQPainter->translate( myXOffset, myHeight - myYOffset - myQPixmap.height() );
152174
break;
153175
case TopLeft:
154-
theQPainter->translate( myPercentageWidth, myPercentageHeight );
176+
theQPainter->translate( myXOffset, myYOffset );
155177
break;
156178
case TopRight:
157-
theQPainter->translate( myWidth - myPercentageWidth - myQPixmap.width(), myPercentageHeight );
179+
theQPainter->translate( myWidth - myXOffset - myQPixmap.width(), myYOffset );
158180
break;
159181
case BottomRight:
160-
theQPainter->translate( myWidth - myPercentageWidth - myQPixmap.width(),
161-
myHeight - myPercentageHeight - myQPixmap.height() );
182+
theQPainter->translate( myWidth - myXOffset - myQPixmap.width(),
183+
myHeight - myYOffset - myQPixmap.height() );
162184
break;
163185
default:
164186
{

src/app/qgsdecorationnortharrowdialog.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ QgsDecorationNorthArrowDialog::QgsDecorationNorthArrowDialog( QgsDecorationNorth
4646
cboPlacement->setCurrentIndex( cboPlacement->findData( mDeco.placement() ) );
4747
spinHorizontal->setValue( mDeco.mMarginHorizontal );
4848
spinVertical->setValue( mDeco.mMarginVertical );
49+
wgtUnitSelection->setUnits( QgsSymbolV2::OutputUnitList() << QgsSymbolV2::MM << QgsSymbolV2::Percentage << QgsSymbolV2::Pixel );
50+
wgtUnitSelection->setUnit( mDeco.mMarginUnit );
4951

5052
// enabled
5153
grpEnable->setChecked( mDeco.enabled() );
@@ -91,6 +93,7 @@ void QgsDecorationNorthArrowDialog::apply()
9193
{
9294
mDeco.mRotationInt = sliderRotation->value();
9395
mDeco.setPlacement( static_cast< QgsDecorationItem::Placement>( cboPlacement->itemData( cboPlacement->currentIndex() ).toInt() ) );
96+
mDeco.mMarginUnit = wgtUnitSelection->unit();
9497
mDeco.setEnabled( grpEnable->isChecked() );
9598
mDeco.mAutomatic = cboxAutomatic->isChecked();
9699
mDeco.mMarginHorizontal = spinHorizontal->value();

src/app/qgsdecorationscalebar.cpp

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ QgsDecorationScaleBar::QgsDecorationScaleBar( QObject* parent )
5454
, mMarginHorizontal( 0 )
5555
, mMarginVertical( 0 )
5656
{
57-
mPlacement = TopRight;
57+
mPlacement = TopLeft;
58+
mMarginUnit = QgsSymbolV2::Pixel;
5859
mStyleLabels << tr( "Tick Down" ) << tr( "Tick Up" )
5960
<< tr( "Bar" ) << tr( "Box" );
6061

@@ -247,16 +248,39 @@ void QgsDecorationScaleBar::render( QPainter * theQPainter )
247248
//Calculate total width of scale bar and label
248249
double myTotalScaleBarWidth = myScaleBarWidth + myFontWidth;
249250

250-
//Calculate percentage page width for use in placing item
251251
int myMarginW = 10;
252252
int myMarginH = 20;
253-
float myMarginDoubledW = float( myMarginW * 2 );
254-
float myMarginDoubledH = float( myMarginH * 2 );
255-
int myOriginX = int((( float( myCanvasWidth - myMarginDoubledW ) - myTotalScaleBarWidth )
253+
int myOriginX = 0;
254+
int myOriginY = 0;
255+
256+
// Set margin according to selected units
257+
switch ( mMarginUnit )
258+
{
259+
case 0: // Millimetres
260+
{
261+
int myPixelsInchX = theQPainter->device()->logicalDpiX();
262+
int myPixelsInchY = theQPainter->device()->logicalDpiY();
263+
myOriginX = int(( float( myPixelsInchX ) * INCHES_TO_MM ) * float( mMarginHorizontal ) );
264+
myOriginY = int(( float( myPixelsInchY ) * INCHES_TO_MM ) * float( mMarginVertical ) );
265+
break;
266+
}
267+
case 3: // Pixels
268+
myOriginX = mMarginHorizontal - 5.; // Minus 5 to shift tight into corner
269+
myOriginY = mMarginVertical - 5.;
270+
break;
271+
case 4: // Percentage
272+
{
273+
float myMarginDoubledW = float( myMarginW * 2 );
274+
float myMarginDoubledH = float( myMarginH * 2 );
275+
myOriginX = int((( float( myCanvasWidth - myMarginDoubledW ) - myTotalScaleBarWidth )
256276
/ 100. ) * float( mMarginHorizontal ) );
257-
int myOriginY = int((( float( myCanvasHeight - myMarginDoubledH ) )
277+
myOriginY = int((( float( myCanvasHeight - myMarginDoubledH ) )
258278
/ 100. ) * float( mMarginVertical ) );
259-
279+
break;
280+
}
281+
default: // Use default of top left
282+
break;
283+
}
260284
//Determine the origin of scale bar depending on placement selected
261285
switch ( mPlacement )
262286
{

src/app/qgsdecorationscalebardialog.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,20 @@ QgsDecorationScaleBarDialog::QgsDecorationScaleBarDialog( QgsDecorationScaleBar&
5151

5252
chkSnapping->setChecked( mDeco.mSnapping );
5353

54+
// placement
5455
cboPlacement->addItem( tr( "Top left" ), QgsDecorationItem::TopLeft );
5556
cboPlacement->addItem( tr( "Top right" ), QgsDecorationItem::TopRight );
5657
cboPlacement->addItem( tr( "Bottom left" ), QgsDecorationItem::BottomLeft );
5758
cboPlacement->addItem( tr( "Bottom right" ), QgsDecorationItem::BottomRight );
5859
cboPlacement->setCurrentIndex( cboPlacement->findData( mDeco.placement() ) );
5960
spnHorizontal->setValue( mDeco.mMarginHorizontal );
6061
spnVertical->setValue( mDeco.mMarginVertical );
62+
wgtUnitSelection->setUnits( QgsSymbolV2::OutputUnitList() << QgsSymbolV2::MM << QgsSymbolV2::Percentage << QgsSymbolV2::Pixel );
63+
wgtUnitSelection->setUnit( mDeco.mMarginUnit );
6164

6265
grpEnable->setChecked( mDeco.enabled() );
6366

67+
// style
6468
cboStyle->clear();
6569
cboStyle->addItems( mDeco.mStyleLabels );
6670

@@ -85,6 +89,7 @@ void QgsDecorationScaleBarDialog::on_buttonBox_helpRequested()
8589
void QgsDecorationScaleBarDialog::apply()
8690
{
8791
mDeco.setPlacement( static_cast< QgsDecorationItem::Placement>( cboPlacement->itemData( cboPlacement->currentIndex() ).toInt() ) );
92+
mDeco.mMarginUnit = wgtUnitSelection->unit();
8893
mDeco.mMarginHorizontal = spnHorizontal->value();
8994
mDeco.mMarginVertical = spnVertical->value();
9095
mDeco.mPreferredSize = spnSize->value();

0 commit comments

Comments
 (0)