-
Notifications
You must be signed in to change notification settings - Fork 1
/
layout_designer_hooks.py
159 lines (135 loc) · 5.98 KB
/
layout_designer_hooks.py
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
# -*- coding: utf-8 -*-
"""Layout Designer Hooks
.. note:: 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.
"""
__author__ = '(C) 2021 by Nyall Dawson'
__date__ = '28/05/2021'
__copyright__ = 'Copyright 2021, North Road'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from functools import partial
from qgis.PyQt.QtCore import (
QObject
)
from qgis.PyQt.QtGui import (
QKeySequence
)
from qgis.PyQt.QtWidgets import (
QAction,
)
from qgis.core import (
QgsLayoutItem,
QgsLayoutItemMap,
check,
QgsAbstractValidityCheck,
QgsValidityCheckResult
)
from qgis.gui import (
QgisInterface,
QgsLayoutDesignerInterface
)
from cartography_tools.gui.gui_utils import GuiUtils
class LayoutDesignerHooks(QObject):
"""
Hooks for customizing layout designers
"""
def init_gui(self, iface: QgisInterface):
"""
Initializes the hooks
"""
iface.layoutDesignerOpened.connect(self.designer_opened)
iface.layoutDesignerWillBeClosed.connect(self.designer_will_be_closed)
def unload(self, iface: QgisInterface):
"""
Unloads the hooks
"""
iface.layoutDesignerOpened.disconnect(self.designer_opened)
iface.layoutDesignerWillBeClosed.disconnect(self.designer_will_be_closed)
def designer_opened(self, designer: QgsLayoutDesignerInterface):
"""
Called whenever a new layout designer window is opened
"""
toggle_unplaced_labels_action = QAction(self.tr('Show Unplaced Labels on Maps'), parent=designer)
toggle_unplaced_labels_action.setCheckable(True)
toggle_unplaced_labels_action.setIcon(GuiUtils.get_icon(
'show_unplaced_labels.svg'))
# determine initial check state
layout = designer.layout()
maps = [item for item in layout.items() if isinstance(item, QgsLayoutItemMap)]
initial_checked = bool(maps) and all(m.mapFlags() & QgsLayoutItemMap.MapItemFlag.ShowUnplacedLabels for m in maps)
toggle_unplaced_labels_action.setChecked(initial_checked)
toggle_unplaced_labels_action.toggled.connect(partial(self.toggle_unplaced_labels, designer))
toggle_unplaced_labels_action.setShortcut(QKeySequence('Ctrl+Shift+U'))
tb = designer.actionsToolbar()
tb.addSeparator()
tb.addAction(toggle_unplaced_labels_action)
designer.viewMenu().addSeparator()
designer.viewMenu().addAction(toggle_unplaced_labels_action)
def designer_will_be_closed(self, designer: QgsLayoutDesignerInterface):
"""
Called whenever a layout designer is closed
"""
def toggle_unplaced_labels(self, designer: QgsLayoutDesignerInterface, checked: bool):
"""
Toggles unplaced label visibility for the specified designer
"""
layout = designer.layout()
maps = [item for item in layout.items() if isinstance(item, QgsLayoutItemMap)]
for m in maps:
flags = m.mapFlags()
if checked:
flags |= QgsLayoutItemMap.MapItemFlag.ShowUnplacedLabels
else:
flags &= ~QgsLayoutItemMap.MapItemFlag.ShowUnplacedLabels
m.setMapFlags(flags)
m.invalidateCache()
@check.register(type=QgsAbstractValidityCheck.Type.TypeLayoutCheck)
def layout_map_unplaced_labels_check(context, feedback):
layout = context.layout
results = []
for i in layout.items():
if isinstance(i, QgsLayoutItemMap) and i.mapFlags() & QgsLayoutItemMap.MapItemFlag.ShowUnplacedLabels:
res = QgsValidityCheckResult()
res.type = QgsValidityCheckResult.Type.Warning
res.title = 'Unplaced labels are visible'
res.detailedDescription = 'The map item {} currently has unplaced labels shown. This setting is only suitable for draft exports.'.format(
i.displayName())
results.append(res)
return results
@check.register(type=QgsAbstractValidityCheck.Type.TypeLayoutCheck)
def layout_map_rasterized_check(context, feedback):
layout = context.layout
results = []
if layout.customProperty('rasterize', False):
res = QgsValidityCheckResult()
res.type = QgsValidityCheckResult.Type.Warning
res.title = 'Layout export will be rasterized'
res.detailedDescription = 'The layout is set to be completely rasterized, even when exporting to vector formats such as PDF or SVG.'
results.append(res)
return results
@check.register(type=QgsAbstractValidityCheck.Type.TypeLayoutCheck)
def layout_item_rasterized_check(context, feedback):
layout = context.layout
results = []
for i in layout.items():
if isinstance(i, QgsLayoutItem):
if i.requiresRasterization():
res = QgsValidityCheckResult()
res.type = QgsValidityCheckResult.Type.Warning
res.title = 'Layout export will be rasterized'
res.detailedDescription = 'The item {} uses settings (e.g. blending modes) which require the whole layout to be rasterized during export. This ' \
'will degrade the quality of the output.'.format(
i.displayName().replace('<', '<'))
results.append(res)
elif i.containsAdvancedEffects():
res = QgsValidityCheckResult()
res.type = QgsValidityCheckResult.Type.Warning
res.title = 'Item will be rasterized'
res.detailedDescription = 'The item {} uses settings (e.g. transparency) which require this item to be rasterized during export. This ' \
'will degrade the quality of the output.'.format(
i.displayName().replace('<', '<'))
results.append(res)
return results