Skip to content
Permalink
Browse files

Cleanup decoration code

  • Loading branch information
nyalldawson committed Jan 6, 2016
1 parent 03ec7ed commit 36b0f6567d25d2acccc4054ac5cbc879354dc66d
@@ -43,11 +43,10 @@ email : tim@linfiniti.com

QgsDecorationCopyright::QgsDecorationCopyright( QObject* parent )
: QgsDecorationItem( parent )
, mMarginHorizontal( 0 )
, mMarginVertical( 0 )
{
mPlacementLabels << tr( "Bottom Left" ) << tr( "Top Left" )
<< tr( "Top Right" ) << tr( "Bottom Right" );
mMarginHorizontal = 0;
mMarginVertical = 0;
mPlacement = BottomRight;

setName( "Copyright Label" );
// initialise default values in the gui
@@ -69,7 +68,6 @@ void QgsDecorationCopyright::projectRead()
// mQFont.setPointSize( QgsProject::instance()->readNumEntry( "CopyrightLabel", "/FontSize", 9 ) );
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
@@ -83,7 +81,6 @@ void QgsDecorationCopyright::saveToProject()
prj->writeEntry( mNameConfig, "/FontSize", mQFont.pointSize() );
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 );
}
@@ -124,23 +121,23 @@ void QgsDecorationCopyright::render( QPainter * theQPainter )
myYOffset = int(( float( myHeight - size.height() )
/ 100. ) * float( mMarginVertical ) );
//Determine placement of label from form combo box
switch ( mPlacementIndex )
switch ( mPlacement )
{
case 0: // Bottom Left. myXOffset is set above
case BottomLeft: // Bottom Left. myXOffset is set above
myYOffset = myHeight - myYOffset - size.height();
break;
case 1: // Top left. Already setup above
case TopLeft: // Top left. Already setup above
break;
case 2: // Top Right. myYOffset is set above
case TopRight: // Top Right. myYOffset is set above
myXOffset = myWidth - myXOffset - size.width();
break;
case 3: // Bottom Right
case BottomRight: // Bottom Right
//Define bottom right hand corner start point
myYOffset = myHeight - myYOffset - size.height();
myXOffset = myWidth - myXOffset - size.width();
break;
default:
QgsDebugMsg( QString( "Unknown placement index of %1" ).arg( mPlacementIndex ) );
QgsDebugMsg( QString( "Unknown placement index of %1" ).arg( static_cast<int>( mPlacement ) ) );
}

//Paint label to canvas
@@ -57,9 +57,6 @@ class APP_EXPORT QgsDecorationCopyright : public QgsDecorationItem
QString mLabelQString;
//! This is the color for the copyright label
QColor mLabelQColor;
//! 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;
@@ -37,9 +37,11 @@ QgsDecorationCopyrightDialog::QgsDecorationCopyrightDialog( QgsDecorationCopyrig
// text
txtCopyrightText->setPlainText( mDeco.mLabelQString );
// placement
cboPlacement->clear();
cboPlacement->addItems( mDeco.mPlacementLabels );
cboPlacement->setCurrentIndex( mDeco.mPlacementIndex );
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 );
// color
@@ -64,7 +66,7 @@ void QgsDecorationCopyrightDialog::on_buttonBox_accepted()
mDeco.mQFont = txtCopyrightText->currentFont();
mDeco.mLabelQString = txtCopyrightText->toPlainText();
mDeco.mLabelQColor = pbnColorChooser->color();
mDeco.mPlacementIndex = cboPlacement->currentIndex();
mDeco.setPlacement( static_cast< QgsDecorationItem::Placement>( cboPlacement->itemData( cboPlacement->currentIndex() ).toInt() ) );
mDeco.mMarginHorizontal = spnHorizontal->value();
mDeco.mMarginVertical = spnVertical->value();
mDeco.setEnabled( grpEnable->isChecked() );
@@ -94,10 +94,10 @@ void QgsDecorationGrid::projectRead()
QgsDecorationItem::projectRead();

mEnabled = QgsProject::instance()->readBoolEntry( mNameConfig, "/Enabled", false );
mMapUnits = ( QGis::UnitType ) QgsProject::instance()->readNumEntry( mNameConfig, "/MapUnits",
QGis::UnknownUnit );
mGridStyle = ( GridStyle ) QgsProject::instance()->readNumEntry( mNameConfig, "/Style",
QgsDecorationGrid::Line );
mMapUnits = static_cast< QGis::UnitType >( QgsProject::instance()->readNumEntry( mNameConfig, "/MapUnits",
QGis::UnknownUnit ) );
mGridStyle = static_cast< GridStyle >( QgsProject::instance()->readNumEntry( mNameConfig, "/Style",
QgsDecorationGrid::Line ) );
mGridIntervalX = QgsProject::instance()->readDoubleEntry( mNameConfig, "/IntervalX", 10 );
mGridIntervalY = QgsProject::instance()->readDoubleEntry( mNameConfig, "/IntervalY", 10 );
mGridOffsetX = QgsProject::instance()->readDoubleEntry( mNameConfig, "/OffsetX", 0 );
@@ -107,8 +107,8 @@ void QgsDecorationGrid::projectRead()
// mGridAnnotationPosition = ( GridAnnotationPosition ) QgsProject::instance()->readNumEntry( mNameConfig,
// "/AnnotationPosition", 0 );
mGridAnnotationPosition = InsideMapFrame; // don't allow outside frame, doesn't make sense
mGridAnnotationDirection = ( GridAnnotationDirection ) QgsProject::instance()->readNumEntry( mNameConfig,
"/AnnotationDirection", 0 );
mGridAnnotationDirection = static_cast< GridAnnotationDirection >( QgsProject::instance()->readNumEntry( mNameConfig,
"/AnnotationDirection", 0 ) );
QString fontStr = QgsProject::instance()->readEntry( mNameConfig, "/AnnotationFont", "" );
if ( fontStr != "" )
{
@@ -164,8 +164,8 @@ void QgsDecorationGrid::saveToProject()
{
QgsDecorationItem::saveToProject();
QgsProject::instance()->writeEntry( mNameConfig, "/Enabled", mEnabled );
QgsProject::instance()->writeEntry( mNameConfig, "/MapUnits", ( int ) mMapUnits );
QgsProject::instance()->writeEntry( mNameConfig, "/Style", ( int ) mGridStyle );
QgsProject::instance()->writeEntry( mNameConfig, "/MapUnits", static_cast< int >( mMapUnits ) );
QgsProject::instance()->writeEntry( mNameConfig, "/Style", static_cast< int >( mGridStyle ) );
QgsProject::instance()->writeEntry( mNameConfig, "/IntervalX", mGridIntervalX );
QgsProject::instance()->writeEntry( mNameConfig, "/IntervalY", mGridIntervalY );
QgsProject::instance()->writeEntry( mNameConfig, "/OffsetX", mGridOffsetX );
@@ -174,7 +174,7 @@ void QgsDecorationGrid::saveToProject()
// missing mGridPen, but should use styles anyway
QgsProject::instance()->writeEntry( mNameConfig, "/ShowAnnotation", mShowGridAnnotation );
// QgsProject::instance()->writeEntry( mNameConfig, "/AnnotationPosition", ( int ) mGridAnnotationPosition );
QgsProject::instance()->writeEntry( mNameConfig, "/AnnotationDirection", ( int ) mGridAnnotationDirection );
QgsProject::instance()->writeEntry( mNameConfig, "/AnnotationDirection", static_cast< int >( mGridAnnotationDirection ) );
QgsProject::instance()->writeEntry( mNameConfig, "/AnnotationFont", mGridAnnotationFont.toString() );
QgsProject::instance()->writeEntry( mNameConfig, "/AnnotationFrameDistance", mAnnotationFrameDistance );
QgsProject::instance()->writeEntry( mNameConfig, "/AnnotationPrecision", mGridAnnotationPrecision );
@@ -595,7 +595,7 @@ int QgsDecorationGrid::xGridLines( QList< QPair< qreal, QLineF > >& lines ) cons
Q_ASSERT( fabs( len - lineWest.length() ) < 1e-6 ); // no shear

double roundCorrection = mapBoundingRect.top() > 0 ? 1.0 : 0.0;
double dist = ( int )(( mapBoundingRect.top() - mGridOffsetY ) / mGridIntervalY + roundCorrection ) * mGridIntervalY + mGridOffsetY;
double dist = static_cast< int >(( mapBoundingRect.top() - mGridOffsetY ) / mGridIntervalY + roundCorrection ) * mGridIntervalY + mGridOffsetY;
dist = dist - mapBoundingRect.top();
while ( dist < len )
{
@@ -642,7 +642,7 @@ int QgsDecorationGrid::yGridLines( QList< QPair< qreal, QLineF > >& lines ) cons

const QRectF& mapBoundingRect = mapPolygon.boundingRect();
double roundCorrection = mapBoundingRect.left() > 0 ? 1.0 : 0.0;
double dist = ( int )(( mapBoundingRect.left() - mGridOffsetX ) / mGridIntervalX + roundCorrection ) * mGridIntervalX + mGridOffsetX;
double dist = static_cast< int >(( mapBoundingRect.left() - mGridOffsetX ) / mGridIntervalX + roundCorrection ) * mGridIntervalX + mGridOffsetX;
dist = dist - mapBoundingRect.left();
while ( dist < len )
{
@@ -767,7 +767,7 @@ bool QgsDecorationGrid::isDirty()
// or if interval is 0
if ( mMapUnits == QGis::UnknownUnit ||
mMapUnits != QgisApp::instance()->mapCanvas()->mapSettings().mapUnits() ||
mGridIntervalX == 0 || mGridIntervalY == 0 )
qgsDoubleNear( mGridIntervalX, 0.0 ) || qgsDoubleNear( mGridIntervalY, 0.0 ) )
return true;
return false;
}
@@ -796,15 +796,15 @@ bool QgsDecorationGrid::getIntervalFromExtent( double* values, bool useXAxis )
else
interval = ( extent.yMaximum() - extent.yMinimum() ) / 5;
QgsDebugMsg( QString( "interval: %1" ).arg( interval ) );
if ( interval != 0 )
if ( !qgsDoubleNear( interval, 0.0 ) )
{
double interval2 = 0;
int factor = pow( 10, floor( log10( interval ) ) );
if ( factor != 0 )
{
interval2 = qRound( interval / factor ) * factor;
QgsDebugMsg( QString( "interval2: %1" ).arg( interval2 ) );
if ( interval2 != 0 )
if ( !qgsDoubleNear( interval2, 0.0 ) )
interval = interval2;
}
}
@@ -74,9 +74,9 @@ void QgsDecorationGridDialog::updateGuiElements()
mOffsetXEdit->setText( QString::number( mDeco.gridOffsetX() ) );
mOffsetYEdit->setText( QString::number( mDeco.gridOffsetY() ) );

mGridTypeComboBox->setCurrentIndex(( int ) mDeco.gridStyle() );
mGridTypeComboBox->setCurrentIndex( static_cast< int >( mDeco.gridStyle() ) );
mDrawAnnotationCheckBox->setChecked( mDeco.showGridAnnotation() );
mAnnotationDirectionComboBox->setCurrentIndex(( int ) mDeco.gridAnnotationDirection() );
mAnnotationDirectionComboBox->setCurrentIndex( static_cast< int >( mDeco.gridAnnotationDirection() ) );
mCoordinatePrecisionSpinBox->setValue( mDeco.gridAnnotationPrecision() );

mDistanceToMapFrameSpinBox->setValue( mDeco.annotationFrameDistance() );
@@ -44,8 +44,9 @@

QgsDecorationItem::QgsDecorationItem( QObject* parent )
: QObject( parent )
, mEnabled( false )
, mPlacement( TopLeft )
{
mEnabled = false;
}

QgsDecorationItem::~QgsDecorationItem()
@@ -63,12 +64,14 @@ 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 ) ) );
}

void QgsDecorationItem::saveToProject()
{
QgsDebugMsg( "Entered" );
QgsProject::instance()->writeEntry( mNameConfig, "/Enabled", mEnabled );
QgsProject::instance()->writeEntry( mNameConfig, "/Placement", static_cast< int >( mPlacement ) );
}
void QgsDecorationItem::setName( const char *name )
{
@@ -27,6 +27,16 @@ class APP_EXPORT QgsDecorationItem: public QObject
{
Q_OBJECT
public:

//! Item placements
enum Placement
{
BottomLeft = 0,
TopLeft,
TopRight,
BottomRight,
};

//! Constructor
QgsDecorationItem( QObject* parent = nullptr );
//! Destructor
@@ -35,6 +45,16 @@ class APP_EXPORT QgsDecorationItem: public QObject
void setEnabled( bool enabled ) { mEnabled = enabled; }
bool enabled() const { return mEnabled; }

/** Returns the current placement for the item.
* @see setPlacement()
*/
Placement placement() const { return mPlacement; }

/** Sets the placement of the item.
* @see placement()
*/
void setPlacement( Placement placement ) { mPlacement = placement; }

void update();

signals:
@@ -59,6 +79,9 @@ class APP_EXPORT QgsDecorationItem: public QObject
/** True if decoration item has to be displayed*/
bool mEnabled;

//! Placement of the decoration
Placement mPlacement;

QString mName;
QString mNameConfig;
QString mNameTranslated;
@@ -56,14 +56,12 @@ const double QgsDecorationNorthArrow::TOL = 1e-8;
*/
QgsDecorationNorthArrow::QgsDecorationNorthArrow( QObject* parent )
: QgsDecorationItem( parent )
, mRotationInt( 0 )
, mAutomatic( true )
, mMarginHorizontal( 0 )
, mMarginVertical( 0 )
{
mRotationInt = 0;
mAutomatic = true;
mPlacementLabels << tr( "Bottom Left" ) << tr( "Top Left" )
<< tr( "Top Right" ) << tr( "Bottom Right" );
mMarginHorizontal = 0;
mMarginVertical = 0;

mPlacement = BottomLeft;
setName( "North Arrow" );
projectRead();
}
@@ -76,7 +74,6 @@ void QgsDecorationNorthArrow::projectRead()
{
QgsDecorationItem::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 );
@@ -86,7 +83,6 @@ void QgsDecorationNorthArrow::saveToProject()
{
QgsDecorationItem::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 );
@@ -153,18 +149,18 @@ void QgsDecorationNorthArrow::render( QPainter * theQPainter )
/ 100. ) * float( mMarginVertical ) );

//Determine placement of label from form combo box
switch ( mPlacementIndex )
switch ( mPlacement )
{
case 0: // Bottom Left
case BottomLeft:
theQPainter->translate( myPercentageWidth, myHeight - myPercentageHeight - myQPixmap.height() );
break;
case 1: // Top Left
case TopLeft:
theQPainter->translate( myPercentageWidth, myPercentageHeight );
break;
case 2: // Top Right
case TopRight:
theQPainter->translate( myWidth - myPercentageWidth - myQPixmap.width(), myPercentageHeight );
break;
case 3: // Bottom Right
case BottomRight:
theQPainter->translate( myWidth - myPercentageWidth - myQPixmap.width(),
myHeight - myPercentageHeight - myQPixmap.height() );
break;
@@ -65,9 +65,6 @@ class APP_EXPORT QgsDecorationNorthArrow: public QgsDecorationItem

//! enable or disable the automatic setting of the arrow direction
bool mAutomatic;
// The placement index and translated text
int mPlacementIndex;
QStringList mPlacementLabels;
//! margin values
int mMarginHorizontal;
int mMarginVertical;
@@ -36,9 +36,11 @@ QgsDecorationNorthArrowDialog::QgsDecorationNorthArrowDialog( QgsDecorationNorth
spinAngle->setValue( mDeco.mRotationInt );

// placement
cboPlacement->clear();
cboPlacement->addItems( mDeco.mPlacementLabels );
cboPlacement->setCurrentIndex( mDeco.mPlacementIndex );
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 );

@@ -63,7 +65,7 @@ void QgsDecorationNorthArrowDialog::on_buttonBox_helpRequested()
void QgsDecorationNorthArrowDialog::on_buttonBox_accepted()
{
mDeco.mRotationInt = sliderRotation->value();
mDeco.mPlacementIndex = cboPlacement->currentIndex();
mDeco.setPlacement( static_cast< QgsDecorationItem::Placement>( cboPlacement->itemData( cboPlacement->currentIndex() ).toInt() ) );
mDeco.setEnabled( grpEnable->isChecked() );
mDeco.mAutomatic = cboxAutomatic->isChecked();
mDeco.mMarginHorizontal = spinHorizontal->value();

0 comments on commit 36b0f65

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