Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Widgetfactory mulibuffer point #299

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 122 additions & 72 deletions geest/gui/widgets/geest_config_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
QSpinBox,
QDoubleSpinBox,
QComboBox,
QButtonGroup
)
from qgis.PyQt.QtCore import pyqtSignal
from qgis.gui import QgsMapLayerComboBox

# from qgis.core import QgsVectorLayer, QgsRasterLayer
from qgis.core import QgsProviderRegistry
from qgis.core import QgsProviderRegistry, QgsVectorLayer
from .geest_widget_factory import GeestWidgetFactory


Expand All @@ -32,18 +31,14 @@ def create_widgets(self):
self.setLayout(layout)

print("Calling GeestWidgetFactory.create_widgets")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No print

widgets_container = GeestWidgetFactory.create_widgets(
self.original_config, self
)
widgets_container = GeestWidgetFactory.create_widgets(self.original_config, self)

if widgets_container is None:
print("GeestWidgetFactory.create_widgets returned None")
return

if not isinstance(widgets_container, QWidget):
print(
f"GeestWidgetFactory.create_widgets returned unexpected type: {type(widgets_container)}"
)
print(f"GeestWidgetFactory.create_widgets returned unexpected type: {type(widgets_container)}")
return

if widgets_container.layout() is None:
Expand All @@ -68,58 +63,105 @@ def recursive_find_and_store_widgets(self, widget, depth=0):
print(" " * depth + f"Examining widget: {type(widget).__name__}")
use_key = widget.property("use_key")
if use_key:
if use_key not in self.widgets:
self.widgets[use_key] = {}
if isinstance(widget, QRadioButton):
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 not in self.widgets:
self.widgets[use_key] = {}
elif isinstance(widget, (QLineEdit, QSpinBox, QDoubleSpinBox, QComboBox, QgsMapLayerComboBox)):
self.widgets[use_key]["widget"] = widget
print(
" " * depth + f"Stored {type(widget).__name__} for key: {use_key}"
)

print(" " * depth + f"Stored {type(widget).__name__} for key: {use_key}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No print through out

elif isinstance(widget, QWidget) and widget.property("widget_type") == "multibuffer":
self.widgets[use_key]["widget"] = widget
print(" " * depth + f"Stored multibuffer widget for key: {use_key}")
elif isinstance(widget, QWidget) and widget.findChild(QgsMapLayerComboBox) and widget.findChild(QComboBox):
self.widgets[use_key]["widget"] = widget
print(" " * depth + f"Stored composite widget (polygon_layer_with_field_selector) for key: {use_key}")
if widget.layout():
for i in range(widget.layout().count()):
item = widget.layout().itemAt(i)
if item.widget():
self.recursive_find_and_store_widgets(item.widget(), depth + 1)
print(f"Current widgets dictionary: {self.widgets}")

def setup_connections(self):
print("Setting up connections")
for key, widgets in self.widgets.items():
radio = widgets.get("radio")
widget = widgets.get("widget")
if radio:
radio.toggled.connect(
lambda checked, k=key: self.handle_option_change(k, checked)
)
radio.toggled.connect(lambda checked, k=key: self.handle_option_change(k, checked))
print(f"Set up radio connection for {key}")
if widget:
print(f"Setting up connection for widget type: {type(widget).__name__} for key: {key}")
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)
)
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)
)
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)
)
widget.valueChanged.connect(lambda value, k=key: self.update_sub_widget_state(k, value))
elif isinstance(widget, QComboBox):
widget.currentTextChanged.connect(
lambda text, k=key: self.update_sub_widget_state(k, text)
)
widget.currentTextChanged.connect(lambda text, k=key: self.update_sub_widget_state(k, text))
elif isinstance(widget, QWidget) and widget.findChild(QgsMapLayerComboBox) and widget.findChild(
QComboBox):
layer_selector = widget.findChild(QgsMapLayerComboBox)
field_selector = widget.findChild(QComboBox)

def update_fields(layer):
self.populate_field_selector(layer, field_selector)
self.update_polygon_layer_and_field(key, layer, field_selector)

layer_selector.layerChanged.connect(update_fields)
field_selector.currentTextChanged.connect(
lambda text, k=key, ls=layer_selector: self.update_polygon_layer_and_field(k,
ls.currentLayer(),
field_selector))
elif widget.property("widget_type") == "multibuffer":
travel_mode_group = widget.travel_mode_group
measurement_group = widget.measurement_group
increment_edit = widget.increment_edit

travel_mode_group.buttonClicked.connect(lambda btn, k=key: self.update_multibuffer_state(k))
measurement_group.buttonClicked.connect(lambda btn, k=key: self.update_multibuffer_state(k))
increment_edit.textChanged.connect(lambda text, k=key: self.update_multibuffer_state(k))

print(f"Set up widget connection for {key}: {type(widget).__name__}")

def populate_field_selector(self, layer, field_selector):
if isinstance(layer, QgsVectorLayer):
field_selector.clear()
field_selector.addItems([field.name() for field in layer.fields()])
print(f"Populated field selector with: {[field.name() for field in layer.fields()]}")
else:
print(f"Invalid layer type for populating field selector: {type(layer)}")

def update_polygon_layer_and_field(self, key, layer, field):
print(f"update_polygon_layer_and_field called for {key}")
print(f"Layer: {layer.name() if layer else 'None'}")
print(f"Field: {field}")

if layer and isinstance(layer, QgsVectorLayer) and field:
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:
value = f"{path};{field}"
print(f"Setting {key} to {value}")
self.modified_config[key] = value
else:
print(f"Unable to determine path for layer {layer.name()} with provider {provider_key}")
self.modified_config[key] = ""
else:
print(f"No valid layer or field selected for {key}")
self.modified_config[key] = ""

print(f"Modified config after update_polygon_layer_and_field: {self.modified_config}")
self.stateChanged.emit(self.get_state())

def update_layer_path(self, key, layer):
print(f"update_layer_path called for {key}") # Debug print
if layer:
Expand All @@ -128,29 +170,49 @@ def update_layer_path(self, key, layer):
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")
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}"
)
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):
print(f"handle_option_change called for {option}, checked={checked}")
if checked:
for key, widgets in self.widgets.items():
widget = widgets.get("widget")
if widget:
widget.setEnabled(key == option)

for key in self.widgets.keys():
self.modified_config[key] = 1 if key == option else 0

if key == option:
if widget is None:
print(f"No widget found for {key}")
self.modified_config[key] = 1
elif isinstance(widget, QWidget) and hasattr(widget, 'get_selections'):
print(f"Handling polygon_layer_with_field_selector for {key}")
layer, field = widget.get_selections()
if layer and field:
self.update_polygon_layer_and_field(key, layer, field)
else:
print(f"No layer or field selected for {key}")
elif isinstance(widget, QgsMapLayerComboBox):
print(f"Handling QgsMapLayerComboBox for {key}")
self.update_layer_path(key, widget.currentLayer())
elif isinstance(widget, QWidget) and widget.property("widget_type") == "multibuffer":
print(f"Handling multibuffer for {key}")
self.update_multibuffer_state(key)
else:
print(f"Setting {key} to 1")
self.modified_config[key] = 1
else:
print(f"Setting {key} to 0")
self.modified_config[key] = 0
else:
print(f"Setting {option} to 0 (unchecked)")
self.modified_config[option] = 0
print(f"Modified config after handle_option_change: {self.modified_config}")
self.stateChanged.emit(self.get_state())

def update_sub_widget_state(self, option, value):
Expand All @@ -162,38 +224,26 @@ def update_sub_widget_state(self, option, value):
self.modified_config[option] = "0"
self.stateChanged.emit(self.get_state())

def get_state(self):
return self.modified_config.copy()
def update_multibuffer_state(self, key):
widget = self.widgets[key]["widget"]
travel_mode = "Driving" if widget.travel_mode_group.checkedButton().text() == "Driving" else "Walking"
measurement = "Distance" if widget.measurement_group.checkedButton().text() == "Distance" else "Time"
increments = widget.increment_edit.text()

# If increments is empty, use the default value
if not increments:
increments = self.original_config.get("Default Multi Buffer Distances", "")

def reset_to_original(self):
self.modified_config = self.original_config.copy()
self.update_widgets_from_config()
self.modified_config[key] = f"{travel_mode};{measurement};{increments}"
self.stateChanged.emit(self.get_state())

def update_widgets_from_config(self):
for key, value in self.modified_config.items():
if key in self.widgets:
widgets = self.widgets[key]
radio = widgets.get("radio")
widget = widgets.get("widget")
if radio:
radio.setChecked(bool(value))
if widget:
if isinstance(widget, QLineEdit):
widget.setText(str(value))
elif isinstance(widget, (QSpinBox, QDoubleSpinBox)):
widget.setValue(
float(value)
if isinstance(widget, QDoubleSpinBox)
else int(value)
)
elif isinstance(widget, (QComboBox, QgsMapLayerComboBox)):
widget.setCurrentText(str(value))
def get_state(self):
return self.modified_config.copy()

def dump_widget_hierarchy(self, widget, level=0):
output = []
output.append(" " * level + f"{widget.__class__.__name__}")
if hasattr(widget, "layout") and widget.layout():
if hasattr(widget, 'layout') and widget.layout():
for i in range(widget.layout().count()):
item = widget.layout().itemAt(i)
if item.widget():
Expand Down
Loading
Loading