Skip to content

Instantly share code, notes, and snippets.

@ThomasG77

ThomasG77/refactoring.py Secret

Created Feb 28, 2021
Embed
What would you like to do?
Just some ideas to learn about refactoring using functions
# Before
# Crédit
credit_text2 = QgsLayoutItemLabel(layout)
credit_text2.setText("Plan réalisé par le Club agroenvironnemental de l'Estrie")
credit_text2.setFont(QFont("Ms Shell Dlg 2", 11))
credit_text2.setHAlign(Qt.AlignLeft)
credit_text2.setMinimumSize(QgsLayoutSize(133.376, 8.215, QgsUnitTypes.LayoutMillimeters))
credit_text2.attemptMove(QgsLayoutPoint(6, 268.737),page=1)
credit_text3 = QgsLayoutItemLabel(layout)
credit_text3.setText("Plan réalisé par le Club agroenvironnemental de l'Estrie")
credit_text3.setFont(QFont("Ms Shell Dlg 2", 11))
credit_text3.setHAlign(Qt.AlignLeft)
credit_text3.setMinimumSize(QgsLayoutSize(133.376, 8.215, QgsUnitTypes.LayoutMillimeters))
credit_text3.attemptMove(QgsLayoutPoint(6, 268.737),page=2)
# After (could be largely improved, jus to give principles e.g reusing code with functions
def generateLayoutItem(layout, page)
credit_text = QgsLayoutItemLabel(layout)
credit_text.setText("Plan réalisé par le Club agroenvironnemental de l'Estrie")
credit_text.setFont(QFont("Ms Shell Dlg 2", 11))
credit_text.setHAlign(Qt.AlignLeft)
credit_text.setMinimumSize(QgsLayoutSize(133.376, 8.215, QgsUnitTypes.LayoutMillimeters))
credit_text.attemptMove(QgsLayoutPoint(6, 268.737),page=page)
return credit_text
credit_text2 = generateLayoutItem(layout, 1)
credit_text3 = generateLayoutItem(layout, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment