Skip to content

Commit

Permalink
We need to fix the widget factory
Browse files Browse the repository at this point in the history
WIP for  #331
  • Loading branch information
timlinux committed Sep 28, 2024
1 parent fec261c commit 452260e
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 1,160 deletions.
39 changes: 39 additions & 0 deletions geest/gui/indicator_config_widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from qgis.PyQt.QtWidgets import QWidget, QVBoxLayout, QButtonGroup
from qgis.core import QgsMessageLog
from .indicator_widget_factory import RadioButtonFactory


class IndicatorConfigWidget(QWidget):
"""
Widget for configuring indicators based on a dictionary.
"""
def __init__(self, attributes_dict: dict) -> None:
super().__init__()
self.attributes_dict = attributes_dict
self.layout: QVBoxLayout = QVBoxLayout()
self.button_group: QButtonGroup = QButtonGroup(self)

try:
self.create_radio_buttons(attributes_dict)
except Exception as e:
QgsMessageLog.logMessage(f"Error in create_radio_buttons: {e}", "Geest")

self.setLayout(self.layout)

def create_radio_buttons(self, attributes_dict: dict) -> None:
"""
Uses the factory to create radio buttons from attributes dictionary.
"""
for key, value in attributes_dict.items():
radio_button_widget = RadioButtonFactory.create_radio_button(key, value)
if radio_button_widget:
self.button_group.addButton(radio_button_widget)
self.layout.addWidget(radio_button_widget.get_container())
radio_button_widget.data_changed.connect(self.update_attributes)

def update_attributes(self, new_data: dict) -> None:
"""
Updates the attributes dictionary with new data from radio buttons.
"""
self.attributes_dict.update(new_data)
QgsMessageLog.logMessage(f"Updated attributes dictionary: {self.attributes_dict}", "Geest")
24 changes: 24 additions & 0 deletions geest/gui/indicator_widget_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from qgis.core import QgsMessageLog
from .widgets.indicator_index_score_widget import IndexScoreRadioButton
from .widgets.widget_radio_button import WidgetRadioButton


class RadioButtonFactory:
"""
Factory class for creating radio buttons based on key-value pairs.
"""
@staticmethod
def create_radio_button(key: str, value: int):
"""
Factory method to create a radio button based on key-value pairs.
"""
try:
if key == "UseIndexScore" and value == 1:
return IndexScoreRadioButton("IndexScore")
elif key == "UseWidget" and value == 1:
return WidgetRadioButton("Widget")
else:
return None
except Exception as e:
QgsMessageLog.logMessage(f"Error in create_radio_button: {e}", "Geest")
return None
19 changes: 10 additions & 9 deletions geest/gui/layer_detail_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
from qgis.PyQt.QtCore import Qt, pyqtSignal
from .toggle_switch import ToggleSwitch
from geest.utilities import resources_path
from geest.gui.widgets import GeestConfigWidget

from .indicator_config_widget import IndicatorConfigWidget

class LayerDetailDialog(QDialog):
"""Dialog to show layer properties, with a Markdown editor and preview for the 'indicator' field."""
Expand Down Expand Up @@ -237,13 +236,15 @@ def get_widget_for_value(self, key, value):
return line_edit

def add_config_widgets(self, layout):
config_widget = GeestConfigWidget(self.layer_data)
if config_widget.widgets:
layout.addWidget(config_widget)
# connect to the stateChanged signal
config_widget.stateChanged.connect(self.handle_config_change)
else:
print("No configuration widgets were created for this layer.")
config_widget = IndicatorConfigWidget(self.layer_data)
#if config_widget.widgets:
# layout.addWidget(config_widget)
# # connect to the stateChanged signal
# #config_widget.stateChanged.connect(self.handle_config_change)
#else:
# QgsMessageLog.logMessage(
# "No configuration widgets were created for this layer.",
# tag="Geest", level=Qgis.CRITICAL)

def handle_config_change(self, new_config):
"""Optionally handle configuration changes."""
Expand Down
8 changes: 1 addition & 7 deletions geest/gui/widgets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
from .geest_config_widget import GeestConfigWidget
from .geest_widget_factory import GeestWidgetFactory

__all__ = [
"GeestConfigWidget",
"GeestWidgetFactory",
]
# Widgets package initialization file
50 changes: 50 additions & 0 deletions geest/gui/widgets/base_indicator_widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from qgis.PyQt.QtWidgets import QRadioButton, QHBoxLayout, QWidget
from qgis.PyQt.QtCore import pyqtSignal
from qgis.core import QgsMessageLog


class BaseIndicatorWidget(QRadioButton):
"""
Abstract base class for radio buttons with internal widgets.
"""
data_changed = pyqtSignal(dict)

def __init__(self, label_text: str) -> None:
super().__init__(label_text)
self.container: QWidget = QWidget()
self.layout: QHBoxLayout = QHBoxLayout(self.container)
self.layout.addWidget(self)

try:
self.add_internal_widgets()
except Exception as e:
QgsMessageLog.logMessage(f"Error in add_internal_widgets: {e}", "Geest")

def add_internal_widgets(self) -> None:
"""
Add internal widgets; to be implemented by subclasses.
"""
raise NotImplementedError("Subclasses must implement add_internal_widgets.")

def get_container(self) -> QWidget:
"""
Returns the container holding the radio button and its internal widgets.
"""
return self.container

def get_data(self) -> dict:
"""
Method to get data from internal widgets.
To be implemented by subclasses.
"""
raise NotImplementedError("Subclasses must implement get_data.")

def update_data(self) -> None:
"""
Gathers data from internal widgets and emits the data_changed signal.
"""
try:
data = self.get_data()
self.data_changed.emit(data)
except Exception as e:
QgsMessageLog.logMessage(f"Error in update_data: {e}", "Geest")
87 changes: 0 additions & 87 deletions geest/gui/widgets/classify_poly_into_classes_widget.py

This file was deleted.

Loading

0 comments on commit 452260e

Please sign in to comment.