Skip to content

Commit

Permalink
Use native QGIS widget for extent, follow project projection
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed Dec 6, 2023
1 parent 6fde186 commit c4187b1
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 85 deletions.
6 changes: 4 additions & 2 deletions lizmap/dialogs/dock_html_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

from lizmap.qgis_plugin_tools.tools.i18n import tr
from lizmap.qgis_plugin_tools.tools.resources import resources_path
from lizmap.tools import qgis_version

try:
from qgis.PyQt.QtWebKitWidgets import QWebView
Expand Down Expand Up @@ -103,8 +104,9 @@ def __init__(self, parent, *__args):
self.feature.featureChanged.connect(self.update_html)
self.feature.setShowBrowserButtons(True)

# We don't have a better signal to listen to
QgsProject.instance().dirtySet.connect(self.update_html)
if qgis_version() >= 32000:
# We don't have a better signal to listen to
QgsProject.instance().dirtySet.connect(self.update_html)

self.update_html()

Expand Down
13 changes: 13 additions & 0 deletions lizmap/dialogs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ def __init__(self, parent=None, is_dev_version=True):
pixmap = pixmap.scaled(100, 100, Qt.KeepAspectRatio)
self.label_lizmap_logo.setPixmap(pixmap)

# Initial extent widget
if qgis_version() >= 31800:
self.widget_initial_extent.setMapCanvas(iface.mapCanvas(), False)
else:
self.widget_initial_extent.setMapCanvas(iface.mapCanvas())
self.widget_initial_extent.setOutputCrs(self.project.crs())
self.widget_initial_extent.setOriginalExtent(iface.mapCanvas().extent(), self.project.crs())
self.project.crsChanged.connect(self.project_crs_changed)

if WEBKIT_AVAILABLE:
self.dataviz_viewer = QWebView()
else:
Expand Down Expand Up @@ -340,6 +349,10 @@ def __init__(self, parent=None, is_dev_version=True):
def check_results(self) -> TableCheck:
return self.table_checks

def project_crs_changed(self):
""" When the project CRS has changed. """
self.widget_initial_extent.setOutputCrs(self.project.crs())

@staticmethod
def open_pg_service_help():
""" Open the PG service documentation. """
Expand Down
2 changes: 1 addition & 1 deletion lizmap/lizmap_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __init__(self, project, fix_json=False):
'wType': 'text', 'type': 'list', 'default': []
},
'initialExtent': {
'wType': 'text', 'type': 'floatlist', 'default': []
'wType': 'extent', 'type': 'floatlist', 'default': []
},
'googleKey': {
'wType': 'text', 'type': 'string', 'default': ''
Expand Down
76 changes: 37 additions & 39 deletions lizmap/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
QgsMapLayerProxyModel,
QgsProject,
QgsRasterLayer,
QgsRectangle,
QgsSettings,
QgsVectorLayer,
QgsWkbTypes,
Expand Down Expand Up @@ -423,7 +424,7 @@ def write_log_message(message, tag, level):
self.global_options['minScale']['widget'] = self.dlg.inMinScale
self.global_options['maxScale']['widget'] = self.dlg.inMaxScale
self.global_options['acl']['widget'] = self.dlg.inAcl
self.global_options['initialExtent']['widget'] = self.dlg.inInitialExtent
self.global_options['initialExtent']['widget'] = self.dlg.widget_initial_extent
self.global_options['googleKey']['widget'] = self.dlg.inGoogleKey
self.global_options['googleHybrid']['widget'] = self.dlg.cbGoogleHybrid
self.global_options['googleSatellite']['widget'] = self.dlg.cbGoogleSatellite
Expand Down Expand Up @@ -1119,10 +1120,7 @@ def initGui(self):

# initial extent
self.dlg.btSetExtentFromProject.clicked.connect(self.set_initial_extent_from_project)
self.dlg.btSetExtentFromCanvas.clicked.connect(self.set_initial_extent_from_canvas)

self.dlg.btSetExtentFromProject.setIcon(QIcon(":images/themes/default/propertyicons/overlay.svg"))
self.dlg.btSetExtentFromCanvas.setIcon(QIcon(":images/themes/default/mLayoutItemMap.svg"))

# Dataviz options
for item in Theme:
Expand Down Expand Up @@ -1656,6 +1654,17 @@ def read_cfg_file(self, skip_tables=False) -> dict:
else:
item['widget'].setText(str(json_options[key]))

if item['wType'] == 'extent':
if key in json_options:
extent = QgsRectangle(
json_options[key][0],
json_options[key][1],
json_options[key][2],
json_options[key][3]
)
item['widget'].setOriginalExtent(extent, self.project.crs())
item['widget'].setOutputExtentFromOriginal()

if item['wType'] == 'wysiwyg':
item['widget'].set_html_content(str(item['default']))

Expand Down Expand Up @@ -1867,40 +1876,18 @@ def get_qgis_layer_by_id(self, my_id) -> Optional[QgsMapLayer]:
return None

def set_initial_extent_from_project(self):
"""
Get the project WMS advertised extent
and set the initial x minimum, y minimum, x maximum, y maximum
in the map options tab
"""
p_wms_extent = self.project.readListEntry('WMSExtent', '')[0]
if len(p_wms_extent) > 1:
extent = '{}, {}, {}, {}'.format(
p_wms_extent[0],
p_wms_extent[1],
p_wms_extent[2],
p_wms_extent[3]
)
self.dlg.inInitialExtent.setText(extent)
""" Set extent from QGIS server properties with the WMS advertised extent. """
# The default extent widget does not have an input : QGIS server WMS properties
wms_extent = self.project.readListEntry('WMSExtent', '')[0]
if len(wms_extent) < 1:
return

wms_extent = [float(i) for i in wms_extent]
extent = QgsRectangle(wms_extent[0], wms_extent[1], wms_extent[2], wms_extent[3])
self.dlg.widget_initial_extent.setOutputExtentFromUser(
extent, self.iface.mapCanvas().mapSettings().destinationCrs())
LOGGER.info('Setting extent from the project')

def set_initial_extent_from_canvas(self):
"""
Get the map canvas extent
and set the initial x minimum, y minimum, x maximum, y maximum
in the map options tab
"""
# Get map canvas extent
extent = self.iface.mapCanvas().extent()
initial_extent = '{}, {}, {}, {}'.format(
extent.xMinimum(),
extent.yMinimum(),
extent.xMaximum(),
extent.yMaximum()
)
self.dlg.inInitialExtent.setText(initial_extent)
LOGGER.info('Setting extent from the canvas')

def remove_selected_layer_from_table(self, key):
"""
Remove a layer from the list of layers
Expand Down Expand Up @@ -3204,16 +3191,16 @@ def project_config_file(
liz2json["layers"] = dict()

# projection
projection = self.iface.mapCanvas().mapSettings().destinationCrs()
projection = self.dlg.widget_initial_extent.outputCrs()
liz2json['options']['projection'] = dict()
liz2json['options']['projection']['proj4'] = projection.toProj()
liz2json['options']['projection']['ref'] = projection.authid()

# wms extent
# WMS extent from project properties, QGIS server tab
liz2json['options']['bbox'] = self.project.readListEntry('WMSExtent', '')[0]

# set initialExtent values if not defined
if not self.dlg.inInitialExtent.text():
if not self.dlg.widget_initial_extent.outputExtent().isNull():
self.set_initial_extent_from_project()

# gui user defined options
Expand Down Expand Up @@ -3246,6 +3233,9 @@ def project_config_file(
if item['wType'] == 'fields':
input_value = item['widget'].currentField()

if item['wType'] == 'extent':
input_value = item['widget'].outputExtent()

# Cast value depending of data type
if item['type'] == 'string':
if item['wType'] in ('text', 'textarea'):
Expand All @@ -3257,7 +3247,15 @@ def project_config_file(
if item['type'] == 'intlist':
input_value = [int(a) for a in input_value.split(', ') if a.isdigit()]
elif item['type'] == 'floatlist':
input_value = [float(a) for a in input_value.split(', ')]
if item['wType'] != 'extent':
input_value = [float(a) for a in input_value.split(', ')]
else:
input_value = [
input_value.xMinimum(),
input_value.yMinimum(),
input_value.xMaximum(),
input_value.yMaximum(),
]
else:
input_value = [a.strip() for a in input_value.split(',') if a.strip()]

Expand Down
59 changes: 17 additions & 42 deletions lizmap/resources/ui/ui_lizmap.ui
Original file line number Diff line number Diff line change
Expand Up @@ -1084,49 +1084,10 @@ This is different to the map maximum extent (defined in QGIS project properties,
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_20">
<item>
<widget class="QLabel" name="label_24">
<property name="text">
<string>Extent</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="inInitialExtent">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
<widget class="QgsExtentWidget" name="widget_initial_extent" native="true"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_19">
<item>
<widget class="QLabel" name="label_62">
<property name="text">
<string>Set the extent from</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btSetExtentFromProject">
<property name="toolTip">
<string>In the project properties, then QGIS server tab, WMS section, there is an advertised extent</string>
</property>
<property name="text">
<string>project properties, QGIS server tab</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btSetExtentFromCanvas">
<property name="text">
<string>map canvas</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_17">
<property name="orientation">
Expand All @@ -1140,6 +1101,16 @@ This is different to the map maximum extent (defined in QGIS project properties,
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="btSetExtentFromProject">
<property name="toolTip">
<string>In the project properties, then QGIS server tab, WMS section, there is an advertised extent</string>
</property>
<property name="text">
<string>Set the extent from project properties, QGIS server tab</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
Expand Down Expand Up @@ -5094,6 +5065,12 @@ This is different to the map maximum extent (defined in QGIS project properties,
<extends>QTableWidget</extends>
<header>lizmap.widgets.check_project</header>
</customwidget>
<customwidget>
<class>QgsExtentWidget</class>
<extends>QWidget</extends>
<header>qgis.gui</header>
<container>1</container>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>mOptionsListWidget</tabstop>
Expand All @@ -5113,9 +5090,7 @@ This is different to the map maximum extent (defined in QGIS project properties,
<tabstop>inMapScales</tabstop>
<tabstop>inMinScale</tabstop>
<tabstop>inMaxScale</tabstop>
<tabstop>inInitialExtent</tabstop>
<tabstop>btSetExtentFromProject</tabstop>
<tabstop>btSetExtentFromCanvas</tabstop>
<tabstop>cbHideHeader</tabstop>
<tabstop>cbHideMenu</tabstop>
<tabstop>cbHideLegend</tabstop>
Expand Down
2 changes: 1 addition & 1 deletion lizmap/test/test_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def _setup_empty_project(self):
# as it will be called by read_cfg_file and also the UI is set in read_cfg_file
config = lizmap.read_cfg_file(skip_tables=True)

lizmap.set_initial_extent_from_canvas()
lizmap.dlg.widget_initial_extent.setOutputExtentFromLayer(layer)

# Config is empty in the CFG file because it's a new project
self.assertDictEqual({}, config)
Expand Down

0 comments on commit c4187b1

Please sign in to comment.