-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqgsdecorationcopyright.cpp
166 lines (140 loc) · 5.9 KB
/
qgsdecorationcopyright.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
/***************************************************************************
plugin.cpp
Import tool for various worldmap analysis output files
Functions:
-------------------
begin : Jan 21, 2004
copyright : (C) 2004 by Tim Sutton
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
// includes
#include "qgsdecorationcopyright.h"
#include "qgsdecorationcopyrightdialog.h"
#include "qgsapplication.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsproject.h"
#include "qgisapp.h"
#include <QPainter>
#include <QMenu>
#include <QDate>
#include <QTextDocument>
#include <QMatrix>
#include <QFile>
//non qt includes
#include <cmath>
QgsDecorationCopyright::QgsDecorationCopyright( QObject *parent )
: QgsDecorationItem( parent )
, mMarginHorizontal( 0 )
, mMarginVertical( 0 )
{
mPlacement = BottomRight;
mMarginUnit = QgsUnitTypes::RenderMillimeters;
setName( "Copyright Label" );
// initialize default values in the gui
projectRead();
}
void QgsDecorationCopyright::projectRead()
{
QgsDecorationItem::projectRead();
QDate now = QDate::currentDate();
QString defString = "© QGIS " + now.toString( QStringLiteral( "yyyy" ) );
// there is no font setting in the UI, so just use the Qt/QGIS default font (what mQFont gets when created)
// mQFont.setFamily( QgsProject::instance()->readEntry( "CopyrightLabel", "/FontName", "Sans Serif" ) );
// mQFont.setPointSize( QgsProject::instance()->readNumEntry( "CopyrightLabel", "/FontSize", 9 ) );
QgsProject *prj = QgsProject::instance();
mLabelQString = prj->readEntry( mNameConfig, QStringLiteral( "/Label" ), defString );
mMarginHorizontal = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/MarginH" ), 0 );
mMarginVertical = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/MarginV" ), 0 );
mLabelQColor.setNamedColor( prj->readEntry( mNameConfig, QStringLiteral( "/Color" ), QStringLiteral( "#000000" ) ) ); // default color is black
}
void QgsDecorationCopyright::saveToProject()
{
QgsDecorationItem::saveToProject();
QgsProject *prj = QgsProject::instance();
prj->writeEntry( mNameConfig, QStringLiteral( "/FontName" ), mQFont.family() );
prj->writeEntry( mNameConfig, QStringLiteral( "/FontSize" ), mQFont.pointSize() );
prj->writeEntry( mNameConfig, QStringLiteral( "/Label" ), mLabelQString );
prj->writeEntry( mNameConfig, QStringLiteral( "/Color" ), mLabelQColor.name() );
prj->writeEntry( mNameConfig, QStringLiteral( "/MarginH" ), mMarginHorizontal );
prj->writeEntry( mNameConfig, QStringLiteral( "/MarginV" ), mMarginVertical );
}
// Slot called when the buffer menu item is activated
void QgsDecorationCopyright::run()
{
QgsDecorationCopyrightDialog dlg( *this, QgisApp::instance() );
dlg.exec();
}
void QgsDecorationCopyright::render( QPainter *theQPainter )
{
//Large IF statement to enable/disable copyright label
if ( enabled() )
{
// need width/height of paint device
int myHeight = theQPainter->device()->height();
int myWidth = theQPainter->device()->width();
QTextDocument text;
text.setDefaultFont( mQFont );
// To set the text color in a QTextDocument we use a CSS style
QString style = "<style type=\"text/css\"> p {color: " +
mLabelQColor.name() + "}</style>";
text.setHtml( style + "<p>" + mLabelQString + "</p>" );
QSizeF size = text.size();
float myXOffset( 0 ), myYOffset( 0 );
// Set margin according to selected units
switch ( mMarginUnit )
{
case QgsUnitTypes::RenderMillimeters:
{
int myPixelsInchX = theQPainter->device()->logicalDpiX();
int myPixelsInchY = theQPainter->device()->logicalDpiY();
myXOffset = myPixelsInchX * INCHES_TO_MM * mMarginHorizontal;
myYOffset = myPixelsInchY * INCHES_TO_MM * mMarginVertical;
break;
}
case QgsUnitTypes::RenderPixels:
myXOffset = mMarginHorizontal;
myYOffset = mMarginVertical;
break;
case QgsUnitTypes::RenderPercentage:
myXOffset = ( ( myWidth - size.width() ) / 100. ) * mMarginHorizontal;
myYOffset = ( ( myHeight - size.height() ) / 100. ) * mMarginVertical;
break;
default: // Use default of top left
break;
}
//Determine placement of label from form combo box
switch ( mPlacement )
{
case BottomLeft: // Bottom Left. myXOffset is set above
myYOffset = myHeight - myYOffset - size.height();
break;
case TopLeft: // Top left. Already setup above
break;
case TopRight: // Top Right. myYOffset is set above
myXOffset = myWidth - myXOffset - size.width();
break;
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( static_cast<int>( mPlacement ) ) );
}
//Paint label to canvas
QMatrix worldMatrix = theQPainter->worldMatrix();
theQPainter->translate( myXOffset, myYOffset );
text.drawContents( theQPainter );
// Put things back how they were
theQPainter->setWorldMatrix( worldMatrix );
}
}