7

Is there a possibility, i.e. API, for PyQGIS to access / modify the copyright decoration (View -> Decorations > Copyright Label) in QGIS?

I've looked in the Python QGIS API (http://geoapis.sourcepole.com/qgispyapi/qgsproject), however, the read/write methods are only for plugin settings, as far as I can tell from the examples I found, e.g. at https://docs.qgis.org/2.14/de/docs/pyqgis_developer_cookbook/settings.html.

Unfortunately, at this point I don't know what to search for and hope somebody can point me in the right direction. Hints are also welcome at: https://github.com/geometalab/Vector-Tiles-Reader-QGIS-Plugin/issues/158

user35594
  • 555
  • 1
  • 5
  • 22
samwise
  • 171
  • 3

1 Answers1

9

This is a trick for change the Copyright Label.This value is established at the project level,and the project has to be saved.

The steps:

  1. Change settings
  2. Save project
  3. Reload Project

Code

from PyQt4.QtCore import QSettings

QgsProject.instance().writeEntry( "CopyrightLabel", "/FontName" , "Sans Serif" );
QgsProject.instance().writeEntry( "CopyrightLabel", "/FontSize" , 9 );
QgsProject.instance().writeEntry( "CopyrightLabel", "/Label" , "New Copyright" );
QgsProject.instance().writeEntry( "CopyrightLabel", "/Color" , "#ff9e00" );
QgsProject.instance().writeEntry( "CopyrightLabel", "/MarginH" , 0 );
QgsProject.instance().writeEntry( "CopyrightLabel", "/MarginV" , 0 );
QgsProject.instance().writeEntry( "CopyrightLabel", "/Enabled" , True );

#Save Project
QgsProject.instance().write()

extent = iface.mapCanvas().extent()

#Reload project
def set_extent():
    iface.mapCanvas().setExtent(extent)
    iface.projectRead.disconnect(set_extent)
    print('Loaded using new copyright label')

filename = QgsProject.instance().fileName()
iface.projectRead.connect(set_extent)
iface.addProject(filename)

enter image description here

Update: without Save/reload project:

Only need override this QGIS Core method using something like that.

from PyQt4.QtCore import *
from PyQt4.QtGui import *

#Save To project
QgsProject.instance().writeEntry( "CopyrightLabel", "/FontName" , "Sans Serif" );
QgsProject.instance().writeEntry( "CopyrightLabel", "/FontSize" , 9 );
QgsProject.instance().writeEntry( "CopyrightLabel", "/Label" , "New Copyright" );
QgsProject.instance().writeEntry( "CopyrightLabel", "/Color" , "#FF0000" );
QgsProject.instance().writeEntry( "CopyrightLabel", "/MarginH" , 0 );
QgsProject.instance().writeEntry( "CopyrightLabel", "/MarginV" , 0 );

QgsProject.instance().writeEntry( "CopyrightLabel","/Enabled", True );
QgsProject.instance().writeEntry( "CopyrightLabel","/Placement" , 3 );
QgsProject.instance().writeEntry( "CopyrightLabel", "/MarginUnit", 'MM' );

#Project Read
mQFont = QgsProject.instance().readEntry( "CopyrightLabel", "/FontName");
mQFontsize = QgsProject.instance().readEntry( "CopyrightLabel", "/FontSize");
mLabelQString = QgsProject.instance().readEntry( "CopyrightLabel", "/Label" );
mMarginHorizontal = QgsProject.instance().readNumEntry( "CopyrightLabel","/MarginH");
mMarginVertical = QgsProject.instance().readNumEntry( "CopyrightLabel", "/MarginV");
mLabelQColor = QgsProject.instance().readEntry( "CopyrightLabel","/Color" );
QgsApplication.instance().processEvents()

INCHES_TO_MM = 0.0393700787402

def _onRenderComplete(p):
    myHeight = p.device().height()
    myWidth = p.device().width()
    text = QTextDocument()
    font = QFont()
    font.setFamily(mQFont[0])
    font.setPointSize(int(mQFontsize[0]))
    text.setDefaultFont(font)
    style = "<style type=\"text/css\"> p {color: " +mLabelQColor[0] + "}</style>"
    text.setHtml( style + "<p>" + mLabelQString[0] + "</p>" )
    size = text.size()

    #Case 1
    #QgsSymbolV2::MM because I'm savig "/MarginUnit", 'MM'
    myPixelsInchX = p.device().logicalDpiX()
    myPixelsInchY = p.device().logicalDpiY()
    myXOffset = myPixelsInchX * INCHES_TO_MM * int(mMarginHorizontal[0])
    myYOffset = myPixelsInchY * INCHES_TO_MM * int(mMarginVertical[0])

    #Bottom Right
    myYOffset = myHeight - myYOffset - size.height()
    myXOffset = myWidth - myXOffset - size.width()

    p.translate( myXOffset, myYOffset )
    worldMatrix = p.worldMatrix()
    text.drawContents(p)
    p.setWorldMatrix( worldMatrix )


iface.mapCanvas().renderComplete.connect(_onRenderComplete)
iface.mapCanvas().refresh()

And the result is a copyright on the Bottom Right.

enter image description here

I hope this helps you

Fran Raga
  • 7,838
  • 3
  • 26
  • 47