Skip to content

Commit

Permalink
Fixes QgsMapLayerComboBox handling in GeestConfigWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
hennie-k committed Sep 19, 2024
1 parent 1381f14 commit 1f1cc1c
Showing 1 changed file with 43 additions and 13 deletions.
56 changes: 43 additions & 13 deletions geest/gui/widgets/geest_config_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
)
from qgis.PyQt.QtCore import pyqtSignal
from qgis.gui import QgsMapLayerComboBox
#from qgis.core import QgsVectorLayer, QgsRasterLayer
from qgis.core import QgsProviderRegistry
from .geest_widget_factory import GeestWidgetFactory


Expand Down Expand Up @@ -57,14 +59,15 @@ def recursive_find_and_store_widgets(self, widget, depth=0):
use_key = widget.property("use_key")
if use_key:
if isinstance(widget, QRadioButton):
self.widgets[use_key] = {"radio": widget}
if use_key not in self.widgets:
self.widgets[use_key] = {}
self.widgets[use_key]["radio"] = widget
print(" " * depth + f"Stored QRadioButton for key: {use_key}")
elif isinstance(widget, (QLineEdit, QSpinBox, QDoubleSpinBox, QComboBox, QgsMapLayerComboBox)):
if use_key in self.widgets:
self.widgets[use_key]["widget"] = widget
print(" " * depth + f"Stored {type(widget).__name__} for key: {use_key}")
else:
print(f"Found widget for {use_key} but no associated radio button")
if use_key not in self.widgets:
self.widgets[use_key] = {}
self.widgets[use_key]["widget"] = widget
print(" " * depth + f"Stored {type(widget).__name__} for key: {use_key}")

if widget.layout():
for i in range(widget.layout().count()):
Expand All @@ -79,15 +82,37 @@ def setup_connections(self):
widget = widgets.get("widget")
if radio:
radio.toggled.connect(lambda checked, k=key: self.handle_option_change(k, checked))
print(f"Set up radio connection for {key}")
if widget:
if isinstance(widget, QLineEdit):
if isinstance(widget, QgsMapLayerComboBox):
print(f"Setting up connection for QgsMapLayerComboBox: {key}")
widget.layerChanged.connect(lambda layer, k=key: self.update_layer_path(k, layer))
elif isinstance(widget, QLineEdit):
widget.textChanged.connect(lambda text, k=key: self.update_sub_widget_state(k, text))
elif isinstance(widget, (QSpinBox, QDoubleSpinBox)):
widget.valueChanged.connect(lambda value, k=key: self.update_sub_widget_state(k, value))
elif isinstance(widget, (QComboBox, QgsMapLayerComboBox)):
widget.currentIndexChanged.connect(
lambda index, k=key: self.update_sub_widget_state(k, widget.currentText()))
print(f"Set up connection for {key}")
elif isinstance(widget, QComboBox):
widget.currentTextChanged.connect(lambda text, k=key: self.update_sub_widget_state(k, text))
print(f"Set up widget connection for {key}: {type(widget).__name__}")

def update_layer_path(self, key, layer):
print(f"update_layer_path called for {key}") # Debug print
if layer:
provider_key = layer.providerType()
uri = layer.dataProvider().dataSourceUri()
print(f"Layer URI: {uri}")
decoded = QgsProviderRegistry.instance().decodeUri(provider_key, uri)
print(f"Decoded URI: {decoded}")
path = decoded.get('path') or decoded.get('url') or decoded.get('layerName')
if path:
print(f"Path found: {path}")
self.update_sub_widget_state(key, path)
else:
print(f"Unable to determine path for layer {layer.name()} with provider {provider_key}")
self.update_sub_widget_state(key, uri) # Fallback to using the full URI
else:
print(f"No layer selected for {key}")
self.update_sub_widget_state(key, None)

def handle_option_change(self, option, checked):
if checked:
Expand All @@ -102,8 +127,13 @@ def handle_option_change(self, option, checked):
self.stateChanged.emit(self.get_state())

def update_sub_widget_state(self, option, value):
self.modified_config[option] = value
self.stateChanged.emit(self.get_state())
if value is not None:
self.modified_config[option] = value
self.stateChanged.emit(self.get_state())
else:
print(f"Received None value for option: {option}")
self.modified_config[option] = "0"
self.stateChanged.emit(self.get_state())

def get_state(self):
return self.modified_config.copy()
Expand Down

0 comments on commit 1f1cc1c

Please sign in to comment.