Skip to content
Permalink
Browse files

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.
  • Loading branch information
duncan-r authored and nyalldawson committed Jan 12, 2016
1 parent a89c183 commit 3a1d47fef7eded72ab736e75458bd5617caf1427
@@ -47,6 +47,7 @@ QgsDecorationCopyright::QgsDecorationCopyright( QObject* parent )
, mMarginVertical( 0 )
{
mPlacement = BottomRight;
mMarginUnit = QgsSymbolV2::Pixel;

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

float myXOffset( 0 ), myYOffset( 0 );

myXOffset = int(( float( myWidth - size.width() )
/ 100. ) * float( mMarginHorizontal ) );
myYOffset = int(( float( myHeight - size.height() )
/ 100. ) * float( mMarginVertical ) );
// Set margin according to selected units
switch ( mMarginUnit )
{
case 0: // Millimetres
{
int myPixelsInchX = theQPainter->device()->logicalDpiX();
int myPixelsInchY = theQPainter->device()->logicalDpiY();
myXOffset = int(( float( myPixelsInchX ) * INCHES_TO_MM ) * float( mMarginHorizontal ) );
myYOffset = int(( float( myPixelsInchY ) * INCHES_TO_MM ) * float( mMarginVertical ) );
break;
}
case 3: // Pixels
myXOffset = mMarginHorizontal;
myYOffset = mMarginVertical;
break;
case 4: // Percentage
myXOffset = int(( float( myWidth - size.width() )
/ 100. ) * float( mMarginHorizontal ) );
myYOffset = int(( float( myHeight - size.height() )
/ 100. ) * float( mMarginVertical ) );
break;
default: // Use default of top left
break;
}
//Determine placement of label from form combo box
switch ( mPlacement )
{
@@ -49,6 +49,9 @@ QgsDecorationCopyrightDialog::QgsDecorationCopyrightDialog( QgsDecorationCopyrig
cboPlacement->setCurrentIndex( cboPlacement->findData( mDeco.placement() ) );
spnHorizontal->setValue( mDeco.mMarginHorizontal );
spnVertical->setValue( mDeco.mMarginVertical );
wgtUnitSelection->setUnits( QgsSymbolV2::OutputUnitList() << QgsSymbolV2::MM << QgsSymbolV2::Percentage << QgsSymbolV2::Pixel );
wgtUnitSelection->setUnit( mDeco.mMarginUnit );

// color
pbnColorChooser->setColor( mDeco.mLabelQColor );
pbnColorChooser->setContext( "gui" );
@@ -91,6 +94,7 @@ void QgsDecorationCopyrightDialog::apply()
mDeco.mLabelQString = txtCopyrightText->toPlainText();
mDeco.mLabelQColor = pbnColorChooser->color();
mDeco.setPlacement( static_cast< QgsDecorationItem::Placement>( cboPlacement->itemData( cboPlacement->currentIndex() ).toInt() ) );
mDeco.mMarginUnit = wgtUnitSelection->unit();
mDeco.mMarginHorizontal = spnHorizontal->value();
mDeco.mMarginVertical = spnVertical->value();
mDeco.setEnabled( grpEnable->isChecked() );
@@ -46,6 +46,7 @@ QgsDecorationItem::QgsDecorationItem( QObject* parent )
: QObject( parent )
, mEnabled( false )
, mPlacement( TopLeft )
, mMarginUnit( QgsSymbolV2::Pixel )
{
}

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

void QgsDecorationItem::saveToProject()
{
QgsDebugMsg( "Entered" );
QgsProject::instance()->writeEntry( mNameConfig, "/Enabled", mEnabled );
QgsProject::instance()->writeEntry( mNameConfig, "/Placement", static_cast< int >( mPlacement ) );
QgsProject::instance()->writeEntry( mNameConfig, "/MarginUnit", QgsSymbolLayerV2Utils::encodeOutputUnit( mMarginUnit ) );
}

void QgsDecorationItem::setName( const char *name )
{
mName = name;
@@ -20,6 +20,7 @@

#include <QObject>
#include "qgslogger.h"
#include "qgssymbollayerv2.h"

class QPainter;

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

//! Placement of the decoration
Placement mPlacement;
//! Units used for the decoration placement margin
QgsSymbolV2::OutputUnit mMarginUnit;
const double INCHES_TO_MM = 0.0393700787402;

QString mName;
QString mNameConfig;
@@ -62,6 +62,8 @@ QgsDecorationNorthArrow::QgsDecorationNorthArrow( QObject* parent )
, mMarginVertical( 0 )
{
mPlacement = BottomLeft;
mMarginUnit = QgsSymbolV2::Pixel;

setName( "North Arrow" );
projectRead();
}
@@ -138,27 +140,47 @@ void QgsDecorationNorthArrow::render( QPainter * theQPainter )

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

// Calculate the margin percentage values
int myPercentageWidth = int((( float( myWidth ) - float( myQPixmap.width() ) )
/ 100. ) * float( mMarginHorizontal ) );
int myPercentageHeight = int((( float( myHeight ) - float( myQPixmap.height() ) )
/ 100. ) * float( mMarginVertical ) );

// Set margin according to selected units
int myXOffset = 0;
int myYOffset = 0;
switch ( mMarginUnit )
{
case 0: // Millimetres
{
int myPixelsInchX = theQPainter->device()->logicalDpiX();
int myPixelsInchY = theQPainter->device()->logicalDpiY();
myXOffset = int(( float( myPixelsInchX ) * INCHES_TO_MM ) * float( mMarginHorizontal ) );
myYOffset = int(( float( myPixelsInchY ) * INCHES_TO_MM ) * float( mMarginVertical ) );
break;
}
case 3: // Pixels
myXOffset = mMarginHorizontal - 5; // Minus 5 to shift tight into corner
myYOffset = mMarginVertical - 5;
break;
case 4: // Percentage
myXOffset = int((( float( myWidth ) - float( myQPixmap.width() ) )
/ 100. ) * float( mMarginHorizontal ) );
myYOffset = int((( float( myHeight ) - float( myQPixmap.height() ) )
/ 100. ) * float( mMarginVertical ) );
break;
default: // Use default of top left
break;
}
//Determine placement of label from form combo box
switch ( mPlacement )
{
case BottomLeft:
theQPainter->translate( myPercentageWidth, myHeight - myPercentageHeight - myQPixmap.height() );
theQPainter->translate( myXOffset, myHeight - myYOffset - myQPixmap.height() );
break;
case TopLeft:
theQPainter->translate( myPercentageWidth, myPercentageHeight );
theQPainter->translate( myXOffset, myYOffset );
break;
case TopRight:
theQPainter->translate( myWidth - myPercentageWidth - myQPixmap.width(), myPercentageHeight );
theQPainter->translate( myWidth - myXOffset - myQPixmap.width(), myYOffset );
break;
case BottomRight:
theQPainter->translate( myWidth - myPercentageWidth - myQPixmap.width(),
myHeight - myPercentageHeight - myQPixmap.height() );
theQPainter->translate( myWidth - myXOffset - myQPixmap.width(),
myHeight - myYOffset - myQPixmap.height() );
break;
default:
{
@@ -46,6 +46,8 @@ QgsDecorationNorthArrowDialog::QgsDecorationNorthArrowDialog( QgsDecorationNorth
cboPlacement->setCurrentIndex( cboPlacement->findData( mDeco.placement() ) );
spinHorizontal->setValue( mDeco.mMarginHorizontal );
spinVertical->setValue( mDeco.mMarginVertical );
wgtUnitSelection->setUnits( QgsSymbolV2::OutputUnitList() << QgsSymbolV2::MM << QgsSymbolV2::Percentage << QgsSymbolV2::Pixel );
wgtUnitSelection->setUnit( mDeco.mMarginUnit );

// enabled
grpEnable->setChecked( mDeco.enabled() );
@@ -91,6 +93,7 @@ void QgsDecorationNorthArrowDialog::apply()
{
mDeco.mRotationInt = sliderRotation->value();
mDeco.setPlacement( static_cast< QgsDecorationItem::Placement>( cboPlacement->itemData( cboPlacement->currentIndex() ).toInt() ) );
mDeco.mMarginUnit = wgtUnitSelection->unit();
mDeco.setEnabled( grpEnable->isChecked() );
mDeco.mAutomatic = cboxAutomatic->isChecked();
mDeco.mMarginHorizontal = spinHorizontal->value();
@@ -54,7 +54,8 @@ QgsDecorationScaleBar::QgsDecorationScaleBar( QObject* parent )
, mMarginHorizontal( 0 )
, mMarginVertical( 0 )
{
mPlacement = TopRight;
mPlacement = TopLeft;
mMarginUnit = QgsSymbolV2::Pixel;
mStyleLabels << tr( "Tick Down" ) << tr( "Tick Up" )
<< tr( "Bar" ) << tr( "Box" );

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

//Calculate percentage page width for use in placing item
int myMarginW = 10;
int myMarginH = 20;
float myMarginDoubledW = float( myMarginW * 2 );
float myMarginDoubledH = float( myMarginH * 2 );
int myOriginX = int((( float( myCanvasWidth - myMarginDoubledW ) - myTotalScaleBarWidth )
int myOriginX = 0;
int myOriginY = 0;

// Set margin according to selected units
switch ( mMarginUnit )
{
case 0: // Millimetres
{
int myPixelsInchX = theQPainter->device()->logicalDpiX();
int myPixelsInchY = theQPainter->device()->logicalDpiY();
myOriginX = int(( float( myPixelsInchX ) * INCHES_TO_MM ) * float( mMarginHorizontal ) );
myOriginY = int(( float( myPixelsInchY ) * INCHES_TO_MM ) * float( mMarginVertical ) );
break;
}
case 3: // Pixels
myOriginX = mMarginHorizontal - 5.; // Minus 5 to shift tight into corner
myOriginY = mMarginVertical - 5.;
break;
case 4: // Percentage
{
float myMarginDoubledW = float( myMarginW * 2 );
float myMarginDoubledH = float( myMarginH * 2 );
myOriginX = int((( float( myCanvasWidth - myMarginDoubledW ) - myTotalScaleBarWidth )
/ 100. ) * float( mMarginHorizontal ) );
int myOriginY = int((( float( myCanvasHeight - myMarginDoubledH ) )
myOriginY = int((( float( myCanvasHeight - myMarginDoubledH ) )
/ 100. ) * float( mMarginVertical ) );

break;
}
default: // Use default of top left
break;
}
//Determine the origin of scale bar depending on placement selected
switch ( mPlacement )
{
@@ -51,16 +51,20 @@ QgsDecorationScaleBarDialog::QgsDecorationScaleBarDialog( QgsDecorationScaleBar&

chkSnapping->setChecked( mDeco.mSnapping );

// 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() ) );
spnHorizontal->setValue( mDeco.mMarginHorizontal );
spnVertical->setValue( mDeco.mMarginVertical );
wgtUnitSelection->setUnits( QgsSymbolV2::OutputUnitList() << QgsSymbolV2::MM << QgsSymbolV2::Percentage << QgsSymbolV2::Pixel );
wgtUnitSelection->setUnit( mDeco.mMarginUnit );

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

// style
cboStyle->clear();
cboStyle->addItems( mDeco.mStyleLabels );

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

0 comments on commit 3a1d47f

Please sign in to comment.
You can’t perform that action at this time.