Skip to content

Commit

Permalink
Merge pull request #326 from kartoza/timlinux/issue286
Browse files Browse the repository at this point in the history
Queue Manager . save polygons to study area file too so we can iterate over them for processing.
  • Loading branch information
timlinux authored Sep 27, 2024
2 parents a2794ab + da5e2a4 commit cd09f26
Show file tree
Hide file tree
Showing 7 changed files with 303 additions and 97 deletions.
16 changes: 15 additions & 1 deletion geest/core/study_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ def process_study_area(self) -> None:

# Add the 'study_area_bboxes' layer to the QGIS map after processing is complete
self.add_layer_to_map("study_area_bboxes")
self.add_layer_to_map("study_area_polygons")

# Create and add the VRT of all generated raster masks if in raster mode
if self.mode == "raster":
Expand Down Expand Up @@ -235,14 +236,27 @@ def process_singlepart_geometry(
[QgsField("area_name", QVariant.String)],
QgsWkbTypes.Polygon,
)

# Transform the geometry to the output CRS
crs_src: QgsCoordinateReferenceSystem = self.layer.crs()
transform: QgsCoordinateTransform = QgsCoordinateTransform(
crs_src, self.output_crs, QgsProject.instance()
)
geom.transform(transform)

# Create a feature for the original part
study_area_polygon: QgsFeature = QgsFeature()
study_area_polygon.setGeometry(geom)
study_area_polygon.setAttributes([area_name])
# Always save the study area bounding boxes regardless of mode
self.save_to_geopackage(
[study_area_polygon],
"study_area_polygons",
[QgsField("area_name", QVariant.String)],
QgsWkbTypes.Polygon,
)


# Process the geometry based on the selected mode
if self.mode == "vector":
QgsMessageLog.logMessage(
Expand Down
6 changes: 3 additions & 3 deletions geest/core/workflows/dont_use_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ def execute(self):
"""
Executes the workflow, reporting progress through the feedback object and checking for cancellation.
"""
QgsMessageLog.logMessage("Executing 'dont use'", "Custom Workflows", Qgis.Info)
QgsMessageLog.logMessage("Executing 'dont use'", tag="Geest", level=Qgis.Info)

steps = 10
for i in range(steps):
if self.feedback.isCanceled():
QgsMessageLog.logMessage(
"Dont use workflow canceled.", "Custom Workflows", Qgis.Warning
"Dont use workflow canceled.", tag="Geest", level=Qgis.Warning
)
return False

Expand All @@ -39,6 +39,6 @@ def execute(self):

self.attributes["result"] = "Dont use workflow completed"
QgsMessageLog.logMessage(
"Dont use workflow workflow completed", "Custom Workflows", Qgis.Info
"Dont use workflow workflow completed", tag="Geest", level=Qgis.Info
)
return True
178 changes: 145 additions & 33 deletions geest/gui/geest_dock.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,165 @@
QVBoxLayout,
QWidget,
)
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtCore import Qt, QSettings, QByteArray
from qgis.core import QgsMessageLog
from typing import Optional
from .setup_panel import SetupPanel
from .tree_panel import TreePanel


class GeestDock(QDockWidget):
def __init__(self, parent=None, json_file=None):
def __init__(
self, parent: Optional[QWidget] = None, json_file: Optional[str] = None
) -> None:
"""
Initializes the GeestDock with a parent and an optional JSON file.
Sets up the main widget, tabs, and restores previous geometry.
:param parent: The parent widget for the dock.
:param json_file: Path to a JSON file used for the TreePanel.
"""
super().__init__(parent)

self.setWindowTitle("Geest") # Set the title of the dock
self.json_file: Optional[str] = json_file

self.json_file = json_file

# Main widget and layout for the dock
main_widget = QWidget()
layout = QVBoxLayout(main_widget)
# Initialize main widget and layout for the dock
main_widget: QWidget = QWidget()
layout: QVBoxLayout = QVBoxLayout(main_widget)
layout.setContentsMargins(0, 0, 0, 0) # Remove margins for a cleaner look
layout.setSpacing(0) # Remove spacing between elements

# Create a tab widget
self.tab_widget = QTabWidget()
self.tab_widget: QTabWidget = QTabWidget()
self.tab_widget.setTabPosition(QTabWidget.North) # Tabs at the top
self.tab_widget.setDocumentMode(True) # Cleaner look for the tabs
self.tab_widget.setMovable(False) # Prevent tabs from being moved

# Create and add the "Project" tab (SetupPanel)
self.setup_widget = SetupPanel()
project_tab = QWidget()
project_layout = QVBoxLayout(project_tab)
project_layout.setContentsMargins(0, 0, 0, 0) # Minimize padding
project_layout.addWidget(self.setup_widget)
self.tab_widget.addTab(project_tab, "Project")

# Create and add the "Inputs" tab (TreePanel)
self.tree_widget = TreePanel(json_file=self.json_file)
inputs_tab = QWidget()
inputs_layout = QVBoxLayout(inputs_tab)
inputs_layout.setContentsMargins(0, 0, 0, 0) # Minimize padding
inputs_layout.addWidget(self.tree_widget)
self.tab_widget.addTab(inputs_tab, "Inputs")

# Add the tab widget to the main layout
layout.addWidget(self.tab_widget)
main_widget.setLayout(layout)

# Set the main widget as the widget for the dock
self.setWidget(main_widget)

# Optionally, start with the first tab selected (optional)
self.tab_widget.setCurrentIndex(0)
try:
# Create and add the "Project" tab (SetupPanel)
self.setup_widget: SetupPanel = SetupPanel()
project_tab: QWidget = QWidget()
project_layout: QVBoxLayout = QVBoxLayout(project_tab)
project_layout.setContentsMargins(0, 0, 0, 0) # Minimize padding
project_layout.addWidget(self.setup_widget)
self.tab_widget.addTab(project_tab, "Project")

# Create and add the "Inputs" tab (TreePanel)
self.tree_widget: TreePanel = TreePanel(json_file=self.json_file)
inputs_tab: QWidget = QWidget()
inputs_layout: QVBoxLayout = QVBoxLayout(inputs_tab)
inputs_layout.setContentsMargins(0, 0, 0, 0) # Minimize padding
inputs_layout.addWidget(self.tree_widget)
self.tab_widget.addTab(inputs_tab, "Inputs")

# Add the tab widget to the main layout
layout.addWidget(self.tab_widget)

# Set the main widget as the widget for the dock
self.setWidget(main_widget)

# Start with the first tab selected
self.tab_widget.setCurrentIndex(0)

# Customize allowed areas for docking
self.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
self.setFeatures(
QDockWidget.DockWidgetClosable | QDockWidget.DockWidgetMovable
)

# Connect tab change event if custom logic is needed when switching tabs
self.tab_widget.currentChanged.connect(self.on_tab_changed)

# Restore the geometry of the dock
self.restore_geometry()

QgsMessageLog.logMessage("GeestDock initialized successfully.", "Geest")

except Exception as e:
QgsMessageLog.logMessage(
f"Error initializing GeestDock: {str(e)}",
"Geest",
level=QgsMessageLog.CRITICAL,
)

def on_tab_changed(self, index: int) -> None:
"""
Handle tab change events and log the tab switch.
:param index: The index of the newly selected tab.
"""
if index == 0:
QgsMessageLog.logMessage("Switched to Project tab", "Geest")
elif index == 1:
QgsMessageLog.logMessage("Switched to Tree tab", "Geest")
self.tree_widget.set_working_directory(self.setup_widget.working_dir)

def load_json_file(self, json_file: str) -> None:
"""
Load a new JSON file into the TreePanel.
:param json_file: The path to the new JSON file to be loaded.
"""
try:
self.json_file = json_file
self.tree_widget.load_data_from_json(json_file)
QgsMessageLog.logMessage(f"Loaded JSON file: {json_file}", "Geest")
except Exception as e:
QgsMessageLog.logMessage(
f"Error loading JSON file: {str(e)}",
"Geest",
level=QgsMessageLog.CRITICAL,
)

def restore_geometry(self) -> None:
"""
Restore the saved geometry and state using QSettings.
"""
try:
settings: QSettings = QSettings("ESMAP", "Geest")
geometry: Optional[QByteArray] = settings.value("GeestDock/geometry")
if geometry is not None:
self.restoreGeometry(geometry)
QgsMessageLog.logMessage("Restored geometry from settings.", "Geest")
else:
QgsMessageLog.logMessage("No geometry to restore.", "Geest")

except Exception as e:
QgsMessageLog.logMessage(
f"Error restoring geometry: {str(e)}",
"Geest",
level=QgsMessageLog.CRITICAL,
)

def closeEvent(self, event) -> None:
"""
Save the geometry of the dock when it closes.
:param event: The close event that triggered this method.
"""
try:
self.save_geometry()
QgsMessageLog.logMessage("Saved geometry on close.", "Geest")
except Exception as e:
QgsMessageLog.logMessage(
f"Error saving geometry: {str(e)}",
"Geest",
level=QgsMessageLog.CRITICAL,
)
super().closeEvent(event)

def save_geometry(self) -> None:
"""
Save the current geometry using QSettings.
"""
try:
settings: QSettings = QSettings("ESMAP", "Geest")
settings.setValue("GeestDock/geometry", self.saveGeometry())
QgsMessageLog.logMessage("Geometry saved successfully.", "Geest")
except Exception as e:
QgsMessageLog.logMessage(
f"Error saving geometry: {str(e)}",
"Geest",
level=QgsMessageLog.CRITICAL,
)
Loading

0 comments on commit cd09f26

Please sign in to comment.