Onze verontschuldigingen, dit bericht is alleen beschikbaar in Amerikaans Engels.
Join the Conversation
3 Comments
A plugin would be greatly appreciated and would help me from time to time! Additionally, for a Qgis Grandpa like me, I would go for the plugin anytime over the Python Console just out of usability.
I’d love to see this as a plugin! Also thanks for the background on the embedded widgets, I hope this spurs on some development of more plugins using them
Bij gebruik van bovenstaande code liep ik tegen wat problemen op. Uiteindelijk gebruik ik nu deze code die vooralsnog probleemloos loopt:
def __init__(self, layer):
# layout the widget itself: a label, slider and checkbox
QWidget.__init__(self)
label = QLabel("Zoomniveau")
self.slider = QSlider(Qt.Horizontal)
self.ckbox = QCheckBox()
self.ckbox.setToolTip("Zoom alleen naar beschikbare niveau's")
layout = QHBoxLayout()
layout.addWidget(label)
layout.addWidget(self.slider)
layout.addWidget(self.ckbox)
self.setLayout(layout)
self.layer = layer
self.resolutions = layer.dataProvider().property('resolutions')
self.mapCanvas = iface.mapCanvas()
self.slider.setRange(0, len(self.resolutions)-1)
self.slider.setTickInterval(1)
self.slider.setTickPosition(QSlider.TicksBothSides)
# invert because 'highest' value == most zoomed out == 0
self.slider.setInvertedAppearance(True)
self.slider.setPageStep(1)
self.slider.setTracking(False)
self.slider.setEnabled(True)
# set the right value of the slider now
self.on_scale_changed(self.mapCanvas.scale())
# connect signals
self.mapCanvas.scaleChanged.connect(self.on_scale_changed)
self.slider.valueChanged.connect(self.on_value_changed)
self.ckbox.stateChanged.connect(self.on_chkbox_change)
def on_chkbox_change(self, state):
# force a rescaling OR not
self.on_scale_changed(self.mapCanvas.scale())
def on_scale_changed(self, scale):
if len(self.resolutions)==0:
return
try:
# enable or disabe the 'fixed zooms' checkbox
self.ckbox.setVisible(self.layer.crs().srsid() == \
iface.mapCanvas().mapSettings().destinationCrs().srsid())
mupp = self.mapCanvas.mapUnitsPerPixel()
r = 0
for i in range(0, len(self.resolutions)):
r = i
if self.resolutions[i] > mupp:
if i > 0 \
and (self.resolutions[i]-mupp > mupp-self.resolutions[i-1]):
r = i-1
break
# only do fixed zooms if the chkbox is checked AND visible
if self.ckbox.isVisible() and self.ckbox.isChecked() \
and not math.isclose(self.resolutions[r], mupp, rel_tol=1e-5):
self.mapCanvas.zoomByFactor(self.resolutions[r] / \
self.mapCanvas.mapUnitsPerPixel())
return
self.slider.blockSignals(True)
self.slider.setValue(r)
self.slider.setToolTip('Z: {:.3f}\nResolution: {:.3f}'.\
format(self.slider.maximum() - r, self.resolutions[r]))
self.slider.blockSignals(False)
except:
pass
def on_value_changed(self, value):
if len(self.resolutions)==0:
return
self.mapCanvas.zoomByFactor(self.resolutions[value] / \
self.mapCanvas.mapUnitsPerPixel())
A plugin would be greatly appreciated and would help me from time to time! Additionally, for a Qgis Grandpa like me, I would go for the plugin anytime over the Python Console just out of usability.
I’d love to see this as a plugin! Also thanks for the background on the embedded widgets, I hope this spurs on some development of more plugins using them
Bij gebruik van bovenstaande code liep ik tegen wat problemen op. Uiteindelijk gebruik ik nu deze code die vooralsnog probleemloos loopt:
def __init__(self, layer):
# layout the widget itself: a label, slider and checkbox
QWidget.__init__(self)
label = QLabel("Zoomniveau")
self.slider = QSlider(Qt.Horizontal)
self.ckbox = QCheckBox()
self.ckbox.setToolTip("Zoom alleen naar beschikbare niveau's")
layout = QHBoxLayout()
layout.addWidget(label)
layout.addWidget(self.slider)
layout.addWidget(self.ckbox)
self.setLayout(layout)
self.layer = layer
self.resolutions = layer.dataProvider().property('resolutions')
self.mapCanvas = iface.mapCanvas()
self.slider.setRange(0, len(self.resolutions)-1)
self.slider.setTickInterval(1)
self.slider.setTickPosition(QSlider.TicksBothSides)
# invert because 'highest' value == most zoomed out == 0
self.slider.setInvertedAppearance(True)
self.slider.setPageStep(1)
self.slider.setTracking(False)
self.slider.setEnabled(True)
# set the right value of the slider now
self.on_scale_changed(self.mapCanvas.scale())
# connect signals
self.mapCanvas.scaleChanged.connect(self.on_scale_changed)
self.slider.valueChanged.connect(self.on_value_changed)
self.ckbox.stateChanged.connect(self.on_chkbox_change)
def on_chkbox_change(self, state):
# force a rescaling OR not
self.on_scale_changed(self.mapCanvas.scale())
def on_scale_changed(self, scale):
if len(self.resolutions)==0:
return
try:
# enable or disabe the 'fixed zooms' checkbox
self.ckbox.setVisible(self.layer.crs().srsid() == \
iface.mapCanvas().mapSettings().destinationCrs().srsid())
mupp = self.mapCanvas.mapUnitsPerPixel()
r = 0
for i in range(0, len(self.resolutions)):
r = i
if self.resolutions[i] > mupp:
if i > 0 \
and (self.resolutions[i]-mupp > mupp-self.resolutions[i-1]):
r = i-1
break
# only do fixed zooms if the chkbox is checked AND visible
if self.ckbox.isVisible() and self.ckbox.isChecked() \
and not math.isclose(self.resolutions[r], mupp, rel_tol=1e-5):
self.mapCanvas.zoomByFactor(self.resolutions[r] / \
self.mapCanvas.mapUnitsPerPixel())
return
self.slider.blockSignals(True)
self.slider.setValue(r)
self.slider.setToolTip('Z: {:.3f}\nResolution: {:.3f}'.\
format(self.slider.maximum() - r, self.resolutions[r]))
self.slider.blockSignals(False)
except:
pass
def on_value_changed(self, value):
if len(self.resolutions)==0:
return
self.mapCanvas.zoomByFactor(self.resolutions[value] / \
self.mapCanvas.mapUnitsPerPixel())