Skip to content
Permalink
Browse files

Added placement margin to decoration items and updated dialogs.

- North arrow, scalebar, and copyright label updated to include a
  margin option on placement.
- All qgsdecorationitem type dialogs amended so that all widgets sit
  within a checkable group box for activating/deactivating the item.
  • Loading branch information
duncan-r committed Dec 22, 2015
1 parent 1340afd commit 47d93078ba276b43c369850bfcbb1c224f26d469
@@ -46,6 +46,8 @@ QgsDecorationCopyright::QgsDecorationCopyright( QObject* parent )
{
mPlacementLabels << tr( "Bottom Left" ) << tr( "Top Left" )
<< tr( "Top Right" ) << tr( "Bottom Right" );
mMarginHorizontal = 0;
mMarginVertical = 0;

setName( "Copyright Label" );
// initialise default values in the gui
@@ -68,6 +70,8 @@ void QgsDecorationCopyright::projectRead()
QgsProject* prj = QgsProject::instance();
mLabelQString = prj->readEntry( mNameConfig, "/Label", defString );
mPlacementIndex = prj->readNumEntry( mNameConfig, "/Placement", 3 );
mMarginHorizontal = QgsProject::instance()->readNumEntry( mNameConfig, "/MarginH", 0 );
mMarginVertical = QgsProject::instance()->readNumEntry( mNameConfig, "/MarginV", 0 );
mLabelQColor.setNamedColor( prj->readEntry( mNameConfig, "/Color", "#000000" ) ); // default color is black
}

@@ -80,6 +84,8 @@ void QgsDecorationCopyright::saveToProject()
prj->writeEntry( mNameConfig, "/Label", mLabelQString );
prj->writeEntry( mNameConfig, "/Color", mLabelQColor.name() );
prj->writeEntry( mNameConfig, "/Placement", mPlacementIndex );
prj->writeEntry( mNameConfig, "/MarginH", mMarginHorizontal );
prj->writeEntry( mNameConfig, "/MarginV", mMarginVertical );
}

// Slot called when the buffer menu item is activated
@@ -112,28 +118,26 @@ void QgsDecorationCopyright::render( QPainter * theQPainter )
QSizeF size = text.size();

float myXOffset( 0 ), myYOffset( 0 );

myXOffset = int(( float( myWidth - size.width() )
/ 100. ) * float( mMarginHorizontal ) );
myYOffset = int(( float( myHeight - size.height() )
/ 100. ) * float( mMarginVertical ) );
//Determine placement of label from form combo box
switch ( mPlacementIndex )
{
case 0: // Bottom Left
//Define bottom left hand corner start point
myYOffset = myHeight - ( size.height() + 5 );
myXOffset = 5;
case 0: // Bottom Left. myXOffset is set above
myYOffset = myHeight - myYOffset - size.height();
break;
case 1: // Top left
//Define top left hand corner start point
myYOffset = 0;
myXOffset = 5;
case 1: // Top left. Already setup above
break;
case 2: // Top Right
//Define top right hand corner start point
myYOffset = 0;
myXOffset = myWidth - ( size.width() + 5 );
case 2: // Top Right. myYOffset is set above
myXOffset = myWidth - myXOffset - size.width();
break;
case 3: // Bottom Right
//Define bottom right hand corner start point
myYOffset = myHeight - ( size.height() + 5 );
myXOffset = myWidth - ( size.width() + 5 );
myYOffset = myHeight - myYOffset - size.height();
myXOffset = myWidth - myXOffset - size.width();
break;
default:
QgsDebugMsg( QString( "Unknown placement index of %1" ).arg( mPlacementIndex ) );
@@ -60,6 +60,9 @@ class APP_EXPORT QgsDecorationCopyright : public QgsDecorationItem
//! Placement of the copyright label - index and translated label names
int mPlacementIndex;
QStringList mPlacementLabels;
//! enable or disable use of position percentage for placement
int mMarginHorizontal;
int mMarginVertical;

friend class QgsDecorationCopyrightDialog;
};
@@ -33,13 +33,15 @@ QgsDecorationCopyrightDialog::QgsDecorationCopyrightDialog( QgsDecorationCopyrig
cboOrientation->hide();
textLabel15->hide();

cboxEnabled->setChecked( mDeco.enabled() );
grpEnable->setChecked( mDeco.enabled() );
// text
txtCopyrightText->setPlainText( mDeco.mLabelQString );
// placement
cboPlacement->clear();
cboPlacement->addItems( mDeco.mPlacementLabels );
cboPlacement->setCurrentIndex( mDeco.mPlacementIndex );
spnHorizontal->setValue( mDeco.mMarginHorizontal );
spnVertical->setValue( mDeco.mMarginVertical );
// color
pbnColorChooser->setColor( mDeco.mLabelQColor );
pbnColorChooser->setContext( "gui" );
@@ -63,7 +65,9 @@ void QgsDecorationCopyrightDialog::on_buttonBox_accepted()
mDeco.mLabelQString = txtCopyrightText->toPlainText();
mDeco.mLabelQColor = pbnColorChooser->color();
mDeco.mPlacementIndex = cboPlacement->currentIndex();
mDeco.setEnabled( cboxEnabled->isChecked() );
mDeco.mMarginHorizontal = spnHorizontal->value();
mDeco.mMarginVertical = spnVertical->value();
mDeco.setEnabled( grpEnable->isChecked() );

accept();
}
@@ -37,7 +37,7 @@ QgsDecorationGridDialog::QgsDecorationGridDialog( QgsDecorationGrid& deco, QWidg
QSettings settings;
// restoreGeometry( settings.value( "/Windows/DecorationGrid/geometry" ).toByteArray() );

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

// mXMinLineEdit->setValidator( new QDoubleValidator( mXMinLineEdit ) );

@@ -67,7 +67,7 @@ void QgsDecorationGridDialog::updateGuiElements()
{
// blockAllSignals( true );

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

mIntervalXEdit->setText( QString::number( mDeco.gridIntervalX() ) );
mIntervalYEdit->setText( QString::number( mDeco.gridIntervalY() ) );
@@ -109,7 +109,7 @@ void QgsDecorationGridDialog::updateGuiElements()
void QgsDecorationGridDialog::updateDecoFromGui()
{
mDeco.setDirty( false );
mDeco.setEnabled( chkEnable->isChecked() );
mDeco.setEnabled( grpEnable->isChecked() );

mDeco.setGridIntervalX( mIntervalXEdit->text().toDouble() );
mDeco.setGridIntervalY( mIntervalYEdit->text().toDouble() );
@@ -61,6 +61,8 @@ QgsDecorationNorthArrow::QgsDecorationNorthArrow( QObject* parent )
mAutomatic = true;
mPlacementLabels << tr( "Bottom Left" ) << tr( "Top Left" )
<< tr( "Top Right" ) << tr( "Bottom Right" );
mMarginHorizontal = 0;
mMarginVertical = 0;

setName( "North Arrow" );
projectRead();
@@ -76,6 +78,8 @@ void QgsDecorationNorthArrow::projectRead()
mRotationInt = QgsProject::instance()->readNumEntry( mNameConfig, "/Rotation", 0 );
mPlacementIndex = QgsProject::instance()->readNumEntry( mNameConfig, "/Placement", 0 );
mAutomatic = QgsProject::instance()->readBoolEntry( mNameConfig, "/Automatic", true );
mMarginHorizontal = QgsProject::instance()->readNumEntry( mNameConfig, "/MarginH", 0 );
mMarginVertical = QgsProject::instance()->readNumEntry( mNameConfig, "/MarginV", 0 );
}

void QgsDecorationNorthArrow::saveToProject()
@@ -84,6 +88,8 @@ void QgsDecorationNorthArrow::saveToProject()
QgsProject::instance()->writeEntry( mNameConfig, "/Rotation", mRotationInt );
QgsProject::instance()->writeEntry( mNameConfig, "/Placement", mPlacementIndex );
QgsProject::instance()->writeEntry( mNameConfig, "/Automatic", mAutomatic );
QgsProject::instance()->writeEntry( mNameConfig, "/MarginH", mMarginHorizontal );
QgsProject::instance()->writeEntry( mNameConfig, "/MarginV", mMarginVertical );
}

// Slot called when the buffer menu item is activated
@@ -140,28 +146,34 @@ 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 ) );

//Determine placement of label from form combo box
switch ( mPlacementIndex )
{
case 0: // Bottom Left
theQPainter->translate( 0, myHeight - myQPixmap.height() );
theQPainter->translate( myPercentageWidth, myHeight - myPercentageHeight - myQPixmap.height() );
break;
case 1: // Top Left
//no need to translate for TL corner because we're already at the origin
theQPainter->translate( 0, 0 );
theQPainter->translate( myPercentageWidth, myPercentageHeight );
break;
case 2: // Top Right
theQPainter->translate( myWidth - myQPixmap.width(), 0 );
theQPainter->translate( myWidth - myPercentageWidth - myQPixmap.width(), myPercentageHeight );
break;
case 3: // Bottom Right
theQPainter->translate( myWidth - myQPixmap.width(),
myHeight - myQPixmap.height() );
theQPainter->translate( myWidth - myPercentageWidth - myQPixmap.width(),
myHeight - myPercentageHeight - myQPixmap.height() );
break;
default:
{
//QgsDebugMsg("Unable to determine where to put north arrow so defaulting to top left");
}
}

//rotate the canvas by the north arrow rotation amount
theQPainter->rotate( mRotationInt );
//Now we can actually do the drawing, and draw a smooth north arrow even when rotated
@@ -68,6 +68,9 @@ class APP_EXPORT QgsDecorationNorthArrow: public QgsDecorationItem
// The placement index and translated text
int mPlacementIndex;
QStringList mPlacementLabels;
//! margin values
int mMarginHorizontal;
int mMarginVertical;

friend class QgsDecorationNorthArrowDialog;
};
@@ -39,9 +39,11 @@ QgsDecorationNorthArrowDialog::QgsDecorationNorthArrowDialog( QgsDecorationNorth
cboPlacement->clear();
cboPlacement->addItems( mDeco.mPlacementLabels );
cboPlacement->setCurrentIndex( mDeco.mPlacementIndex );
spinHorizontal->setValue( mDeco.mMarginHorizontal );
spinVertical->setValue( mDeco.mMarginVertical );

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

// automatic
cboxAutomatic->setChecked( mDeco.mAutomatic );
@@ -62,8 +64,10 @@ void QgsDecorationNorthArrowDialog::on_buttonBox_accepted()
{
mDeco.mRotationInt = sliderRotation->value();
mDeco.mPlacementIndex = cboPlacement->currentIndex();
mDeco.setEnabled( cboxShow->isChecked() );
mDeco.setEnabled( grpEnable->isChecked() );
mDeco.mAutomatic = cboxAutomatic->isChecked();
mDeco.mMarginHorizontal = spinHorizontal->value();
mDeco.mMarginVertical = spinVertical->value();

accept();
}
@@ -57,6 +57,8 @@ QgsDecorationScaleBar::QgsDecorationScaleBar( QObject* parent )
mPlacementIndex = 1;
mStyleLabels << tr( "Tick Down" ) << tr( "Tick Up" )
<< tr( "Bar" ) << tr( "Box" );
mMarginHorizontal = 0;
mMarginVertical = 0;

setName( "Scale Bar" );
projectRead();
@@ -79,6 +81,8 @@ void QgsDecorationScaleBar::projectRead()
int myGreenInt = QgsProject::instance()->readNumEntry( mNameConfig, "/ColorGreenPart", 0 );
int myBlueInt = QgsProject::instance()->readNumEntry( mNameConfig, "/ColorBluePart", 0 );
mColor = QColor( myRedInt, myGreenInt, myBlueInt );
mMarginHorizontal = QgsProject::instance()->readNumEntry( mNameConfig, "/MarginH", 0 );
mMarginVertical = QgsProject::instance()->readNumEntry( mNameConfig, "/MarginV", 0 );
}

void QgsDecorationScaleBar::saveToProject()
@@ -92,6 +96,8 @@ void QgsDecorationScaleBar::saveToProject()
QgsProject::instance()->writeEntry( mNameConfig, "/ColorRedPart", mColor.red() );
QgsProject::instance()->writeEntry( mNameConfig, "/ColorGreenPart", mColor.green() );
QgsProject::instance()->writeEntry( mNameConfig, "/ColorBluePart", mColor.blue() );
QgsProject::instance()->writeEntry( mNameConfig, "/MarginH", mMarginHorizontal );
QgsProject::instance()->writeEntry( mNameConfig, "/MarginV", mMarginVertical );
}


@@ -133,7 +139,6 @@ void QgsDecorationScaleBar::render( QPainter * theQPainter )
// Hard coded sizes
int myMajorTickSize = 8;
int myTextOffsetX = 3;
int myMargin = 20;

QSettings settings;
QGis::UnitType myPreferredUnits = QGis::fromLiteral( settings.value( "/qgis/measure/displayunits", QGis::toLiteral( QGis::Meters ) ).toString() );
@@ -252,26 +257,34 @@ void QgsDecorationScaleBar::render( QPainter * theQPainter )
//Calculate total width of scale bar and label
double myTotalScaleBarWidth = myScaleBarWidth + myFontWidth;

//determine the origin of scale bar depending on placement selected
int myOriginX = myMargin;
int myOriginY = myMargin;
//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 )
/ 100. ) * float( mMarginHorizontal ) );
int myOriginY = int((( float( myCanvasHeight - myMarginDoubledH ) )
/ 100. ) * float( mMarginVertical ) );

//Determine the origin of scale bar depending on placement selected
switch ( mPlacementIndex )
{
case 0: // Bottom Left
myOriginX = myMargin;
myOriginY = myCanvasHeight - myMargin;
myOriginX += myMarginW;
myOriginY = myCanvasHeight - myOriginY - myMarginH;
break;
case 1: // Top Left
myOriginX = myMargin;
myOriginY = myMargin;
myOriginX += myMarginW;
myOriginY += myMarginH;
break;
case 2: // Top Right
myOriginX = myCanvasWidth - (( int ) myTotalScaleBarWidth ) - myMargin;
myOriginY = myMargin;
myOriginX = myCanvasWidth - myOriginX - myMarginW - (( int ) myTotalScaleBarWidth );
myOriginY += myMarginH;
break;
case 3: // Bottom Right
myOriginX = myCanvasWidth - (( int ) myTotalScaleBarWidth ) - myMargin;
myOriginY = myCanvasHeight - myMargin;
myOriginX = myCanvasWidth - myOriginX - myMarginW - (( int ) myTotalScaleBarWidth );
myOriginY = myCanvasHeight - myOriginY - myMarginH;
break;
default:
QgsDebugMsg( "Unable to determine where to put scale bar so defaulting to top left" );
@@ -62,6 +62,9 @@ class APP_EXPORT QgsDecorationScaleBar: public QgsDecorationItem
QStringList mStyleLabels;
//! The scale bar color
QColor mColor;
//! Margin percentage values
int mMarginHorizontal;
int mMarginVertical;

friend class QgsDecorationScaleBarDialog;
};
@@ -50,8 +50,10 @@ QgsDecorationScaleBarDialog::QgsDecorationScaleBarDialog( QgsDecorationScaleBar&
cboPlacement->clear();
cboPlacement->addItems( mDeco.mPlacementLabels );
cboPlacement->setCurrentIndex( mDeco.mPlacementIndex );
spnHorizontal->setValue( mDeco.mMarginHorizontal );
spnVertical->setValue( mDeco.mMarginVertical );

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

cboStyle->clear();
cboStyle->addItems( mDeco.mStyleLabels );
@@ -77,9 +79,11 @@ void QgsDecorationScaleBarDialog::on_buttonBox_helpRequested()
void QgsDecorationScaleBarDialog::on_buttonBox_accepted()
{
mDeco.mPlacementIndex = cboPlacement->currentIndex();
mDeco.mMarginHorizontal = spnHorizontal->value();
mDeco.mMarginVertical = spnVertical->value();
mDeco.mPreferredSize = spnSize->value();
mDeco.mSnapping = chkSnapping->isChecked();
mDeco.setEnabled( chkEnable->isChecked() );
mDeco.setEnabled( grpEnable->isChecked() );
mDeco.mStyleIndex = cboStyle->currentIndex();
mDeco.mColor = pbnChangeColor->color();

0 comments on commit 47d9307

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