4

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

  • 1
    Welcome to gis.stackexchange! Please note that a good question on this site is expected to show some degree of research on your part, i.e. what you have tried and - if applicable - code so far. For more info, you can check our faq. – underdark Dec 7 '17 at 19:21
  • Apologies for the short question, I've edited it slightly. :-) – samwise Dec 7 '17 at 19:28
  • Only for clarify,where is the copyright text in QGIS? – Fran Raga Dec 17 '17 at 20:58
  • 1
    @FranciscoRaga, View -> Decorations > Copyright Label – user35594 Dec 17 '17 at 21:09
6
+100

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

  • 1
    Thanks. Is there a way to achieve this, without the necessity to save and reload the project? – samwise Dec 18 '17 at 13:07
  • 1
    Update my answer using other alternative – Fran Raga Dec 18 '17 at 15:48
  • 2
    +1, Your method works fantastically well. I only used your function without having to read or write entries to QgsProject (and replacing parameters like mQFontsize with the values themselves). Great job :) – Joseph Dec 19 '17 at 10:13
  • @FranciscoRaga, do you know how do you move the decoration from bottom-right to top-middle? – user35594 Jan 13 '18 at 13:25
  • 1
    @FranciscoRaga, figured it out, myXOffset = (myWidth / 2) - (size.width() / 2) – user35594 Jan 13 '18 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.