|
| 1 | +/*************************************************************************** |
| 2 | + plugin.cpp |
| 3 | + Import tool for various worldmap analysis output files |
| 4 | +Functions: |
| 5 | +
|
| 6 | +------------------- |
| 7 | +begin : Jan 21, 2004 |
| 8 | +copyright : (C) 2004 by Tim Sutton |
| 9 | +email : tim@linfiniti.com |
| 10 | +
|
| 11 | + ***************************************************************************/ |
| 12 | + |
| 13 | +/*************************************************************************** |
| 14 | + * * |
| 15 | + * This program is free software; you can redistribute it and/or modify * |
| 16 | + * it under the terms of the GNU General Public License as published by * |
| 17 | + * the Free Software Foundation; either version 2 of the License, or * |
| 18 | + * (at your option) any later version. * |
| 19 | + * * |
| 20 | + ***************************************************************************/ |
| 21 | + |
| 22 | +// includes |
| 23 | + |
| 24 | +#include "qgsdecorationcopyright.h" |
| 25 | +#include "qgsdecorationcopyrightdialog.h" |
| 26 | + |
| 27 | +#include "qgsapplication.h" |
| 28 | +#include "qgslogger.h" |
| 29 | +#include "qgsmapcanvas.h" |
| 30 | +#include "qgsproject.h" |
| 31 | +#include "qgisapp.h" |
| 32 | + |
| 33 | +#include <QPainter> |
| 34 | +#include <QMenu> |
| 35 | +#include <QDate> |
| 36 | +#include <QTextDocument> |
| 37 | +#include <QMatrix> |
| 38 | +#include <QFile> |
| 39 | + |
| 40 | +//non qt includes |
| 41 | +#include <cmath> |
| 42 | + |
| 43 | + |
| 44 | +QgsDecorationCopyright::QgsDecorationCopyright( QObject* parent ) |
| 45 | + : QObject(parent) |
| 46 | +{ |
| 47 | + mPlacementLabels << tr( "Bottom Left" ) << tr( "Top Left" ) |
| 48 | + << tr( "Top Right" ) << tr( "Bottom Right" ); |
| 49 | + |
| 50 | + // initialise default values in the gui |
| 51 | + projectRead(); |
| 52 | +} |
| 53 | + |
| 54 | +QgsDecorationCopyright::~QgsDecorationCopyright() |
| 55 | +{} |
| 56 | + |
| 57 | +void QgsDecorationCopyright::projectRead() |
| 58 | +{ |
| 59 | + QDate now; |
| 60 | + QString defString; |
| 61 | + |
| 62 | + now = QDate::currentDate(); |
| 63 | + defString = "© QGIS " + now.toString( "yyyy" ); |
| 64 | + |
| 65 | + // there is no font setting in the UI, so just use the Qt/QGIS default font (what mQFont gets when created) |
| 66 | + // mQFont.setFamily( QgsProject::instance()->readEntry( "CopyrightLabel", "/FontName", "Sans Serif" ) ); |
| 67 | + // mQFont.setPointSize( QgsProject::instance()->readNumEntry( "CopyrightLabel", "/FontSize", 9 ) ); |
| 68 | + QgsProject* prj = QgsProject::instance(); |
| 69 | + mLabelQString = prj->readEntry( "CopyrightLabel", "/Label", defString ); |
| 70 | + mPlacementIndex = prj->readNumEntry( "CopyrightLabel", "/Placement", 3 ); |
| 71 | + mEnable = prj->readBoolEntry( "CopyrightLabel", "/Enabled", true ); |
| 72 | + mLabelQColor.setNamedColor( prj->readEntry( "CopyrightLabel", "/Color", "#000000" ) ); // default color is black |
| 73 | +} |
| 74 | + |
| 75 | +// Slot called when the buffer menu item is activated |
| 76 | +void QgsDecorationCopyright::run() |
| 77 | +{ |
| 78 | + QgsDecorationCopyrightDialog dlg( *this, QgisApp::instance() ); |
| 79 | + |
| 80 | + if (dlg.exec()) |
| 81 | + { |
| 82 | + saveToProject(); |
| 83 | + QgisApp::instance()->mapCanvas()->refresh(); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +void QgsDecorationCopyright::renderLabel( QPainter * theQPainter ) |
| 89 | +{ |
| 90 | + //Large IF statement to enable/disable copyright label |
| 91 | + if ( mEnable ) |
| 92 | + { |
| 93 | + // need width/height of paint device |
| 94 | + int myHeight = theQPainter->device()->height(); |
| 95 | + int myWidth = theQPainter->device()->width(); |
| 96 | + |
| 97 | + QTextDocument text; |
| 98 | + text.setDefaultFont( mQFont ); |
| 99 | + // To set the text color in a QTextDocument we use a CSS style |
| 100 | + QString style = "<style type=\"text/css\"> p {color: " + |
| 101 | + mLabelQColor.name() + "}</style>"; |
| 102 | + text.setHtml( style + "<p>" + mLabelQString + "</p>" ); |
| 103 | + QSizeF size = text.size(); |
| 104 | + |
| 105 | + float myXOffset( 0 ), myYOffset( 0 ); |
| 106 | + //Determine placement of label from form combo box |
| 107 | + switch ( mPlacementIndex ) |
| 108 | + { |
| 109 | + case 0: // Bottom Left |
| 110 | + //Define bottom left hand corner start point |
| 111 | + myYOffset = myHeight - ( size.height() + 5 ); |
| 112 | + myXOffset = 5; |
| 113 | + break; |
| 114 | + case 1: // Top left |
| 115 | + //Define top left hand corner start point |
| 116 | + myYOffset = 0;; |
| 117 | + myXOffset = 5; |
| 118 | + break; |
| 119 | + case 2: // Top Right |
| 120 | + //Define top right hand corner start point |
| 121 | + myYOffset = 0; |
| 122 | + myXOffset = myWidth - ( size.width() + 5 ); |
| 123 | + break; |
| 124 | + case 3: // Bottom Right |
| 125 | + //Define bottom right hand corner start point |
| 126 | + myYOffset = myHeight - ( size.height() + 5 ); |
| 127 | + myXOffset = myWidth - ( size.width() + 5 ); |
| 128 | + break; |
| 129 | + default: |
| 130 | + QgsDebugMsg( QString( "Unknown placement index of %1" ).arg( mPlacementIndex ) ); |
| 131 | + } |
| 132 | + |
| 133 | + //Paint label to canvas |
| 134 | + QMatrix worldMatrix = theQPainter->worldMatrix(); |
| 135 | + theQPainter->translate( myXOffset, myYOffset ); |
| 136 | + text.drawContents( theQPainter ); |
| 137 | + // Put things back how they were |
| 138 | + theQPainter->setWorldMatrix( worldMatrix ); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +void QgsDecorationCopyright::saveToProject() |
| 143 | +{ |
| 144 | + //save state to the project file..... |
| 145 | + QgsProject* prj = QgsProject::instance(); |
| 146 | + prj->writeEntry( "CopyrightLabel", "/FontName", mQFont.family() ); |
| 147 | + prj->writeEntry( "CopyrightLabel", "/FontSize", mQFont.pointSize() ); |
| 148 | + prj->writeEntry( "CopyrightLabel", "/Label", mLabelQString ); |
| 149 | + prj->writeEntry( "CopyrightLabel", "/Color", mLabelQColor.name() ); |
| 150 | + prj->writeEntry( "CopyrightLabel", "/Placement", mPlacementIndex ); |
| 151 | + prj->writeEntry( "CopyrightLabel", "/Enabled", mEnable ); |
| 152 | +} |
0 commit comments