22
33import os
44
5- from PyQt5 .QtWidgets import QWidget , QTableWidgetItem , QMessageBox
5+ from PyQt5 .QtWidgets import QMessageBox , QVBoxLayout , QWidget
66from qgis .PyQt import uic
77
8+ from loopstructural .gui .modelling .stratigraphic_column import StratColumnWidget
9+
810
911class UserDefinedSorterWidget (QWidget ):
1012 """Widget for creating a user-defined stratigraphic column.
1113
12- This widget allows users to manually define the stratigraphic order
13- of units from youngest to oldest .
14+ This widget uses the LoopStructural StratigraphicColumn widget
15+ and links it to the data manager for integration with the model .
1416 """
1517
1618 def __init__ (self , parent = None , data_manager = None ):
@@ -24,80 +26,43 @@ def __init__(self, parent=None, data_manager=None):
2426 Data manager for accessing shared data.
2527 """
2628 super ().__init__ (parent )
29+
30+ if data_manager is None :
31+ raise ValueError ("data_manager must be provided" )
32+
2733 self .data_manager = data_manager
2834
2935 # Load the UI file
30- ui_path = os .path .join (os .path .dirname (__file__ ), "user_defined_sorter_widget.ui" )
31- uic .loadUi (ui_path , self )
32-
33- # Connect signals
34- self .addRowButton .clicked .connect (self ._add_row )
35- self .removeRowButton .clicked .connect (self ._remove_row )
36- self .moveUpButton .clicked .connect (self ._move_up )
37- self .moveDownButton .clicked .connect (self ._move_down )
38- self .runButton .clicked .connect (self ._run_sorter )
39-
40- # Initialize with a few empty rows
41- for _ in range (3 ):
42- self ._add_row ()
43-
44- def _add_row (self ):
45- """Add a new row to the stratigraphic column table."""
46- row_count = self .stratiColumnTable .rowCount ()
47- self .stratiColumnTable .insertRow (row_count )
48- self .stratiColumnTable .setItem (row_count , 0 , QTableWidgetItem ("" ))
49-
50- def _remove_row (self ):
51- """Remove the selected row from the stratigraphic column table."""
52- current_row = self .stratiColumnTable .currentRow ()
53- if current_row >= 0 :
54- self .stratiColumnTable .removeRow (current_row )
55-
56- def _move_up (self ):
57- """Move the selected row up in the stratigraphic column table."""
58- current_row = self .stratiColumnTable .currentRow ()
59- if current_row > 0 :
60- # Get current row data
61- item = self .stratiColumnTable .takeItem (current_row , 0 )
62-
63- # Remove current row
64- self .stratiColumnTable .removeRow (current_row )
65-
66- # Insert row above
67- self .stratiColumnTable .insertRow (current_row - 1 )
68- self .stratiColumnTable .setItem (current_row - 1 , 0 , item )
69-
70- # Select the moved row
71- self .stratiColumnTable .setCurrentCell (current_row - 1 , 0 )
72-
73- def _move_down (self ):
74- """Move the selected row down in the stratigraphic column table."""
75- current_row = self .stratiColumnTable .currentRow ()
76- if current_row >= 0 and current_row < self .stratiColumnTable .rowCount () - 1 :
77- # Get current row data
78- item = self .stratiColumnTable .takeItem (current_row , 0 )
79-
80- # Remove current row
81- self .stratiColumnTable .removeRow (current_row )
82-
83- # Insert row below
84- self .stratiColumnTable .insertRow (current_row + 1 )
85- self .stratiColumnTable .setItem (current_row + 1 , 0 , item )
86-
87- # Select the moved row
88- self .stratiColumnTable .setCurrentCell (current_row + 1 , 0 )
36+
37+ # Create and add the StratigraphicColumn widget to the UI
38+ self .strat_column_widget = StratColumnWidget (parent = self , data_manager = self .data_manager )
39+
40+ # Add the stratigraphic column widget to the UI layout
41+ # Assuming the UI has a placeholder widget or layout for this
42+ if hasattr (self , 'stratiColumnWidget' ):
43+ # If the UI has a widget called stratiColumnWidget, use its layout
44+ layout = self .stratiColumnWidget .layout ()
45+ if layout is None :
46+ layout = QVBoxLayout (self .stratiColumnWidget )
47+ layout .addWidget (self .strat_column_widget )
48+ else :
49+ # Otherwise, add it to the main layout
50+ main_layout = self .layout ()
51+ if main_layout is None :
52+ main_layout = QVBoxLayout (self )
53+ main_layout .addWidget (self .strat_column_widget )
8954
9055 def _run_sorter (self ):
91- """Run the user-defined stratigraphic sorter algorithm."""
92- from qgis .core import QgsProcessingFeedback
56+ """Run the user-defined stratigraphic sorter algorithm.
57+
58+ This method will use the stratigraphic column from the StratColumnWidget
59+ that is already linked to the data manager, ensuring the model is updated.
60+ """
9361 from qgis import processing
62+ from qgis .core import QgsProcessingFeedback
9463
95- # Get stratigraphic column data
96- strati_column = []
97- for row in range (self .stratiColumnTable .rowCount ()):
98- item = self .stratiColumnTable .item (row , 0 )
99- if item and item .text ().strip ():
100- strati_column .append (item .text ().strip ())
64+ # Get stratigraphic column data from the data manager
65+ strati_column = self .get_stratigraphic_column ()
10166
10267 if not strati_column :
10368 QMessageBox .warning (
@@ -114,9 +79,7 @@ def _run_sorter(self):
11479 # Run the algorithm
11580 try :
11681 feedback = QgsProcessingFeedback ()
117- result = processing .run (
118- "plugin_map2loop:loop_sorter_2" , params , feedback = feedback
119- )
82+ result = processing .run ("plugin_map2loop:loop_sorter_2" , params , feedback = feedback )
12083
12184 if result :
12285 QMessageBox .information (
@@ -131,33 +94,44 @@ def _run_sorter(self):
13194 QMessageBox .critical (self , "Error" , f"An error occurred: { str (e )} " )
13295
13396 def get_stratigraphic_column (self ):
134- """Get the current stratigraphic column.
97+ """Get the current stratigraphic column from the data manager .
13598
13699 Returns
137100 -------
138101 list
139102 List of unit names from youngest to oldest.
140103 """
141- strati_column = []
142- for row in range (self .stratiColumnTable .rowCount ()):
143- item = self .stratiColumnTable .item (row , 0 )
144- if item and item .text ().strip ():
145- strati_column .append (item .text ().strip ())
146- return strati_column
104+ if hasattr (self , 'data_manager' ) and self .data_manager is not None :
105+ strati_column = self .data_manager .get_stratigraphic_column ()
106+ # Extract unit names in order
107+ unit_names = []
108+ for element in strati_column .order :
109+ if hasattr (element , 'name' ):
110+ unit_names .append (element .name )
111+ return unit_names
112+ return []
147113
148114 def set_stratigraphic_column (self , units ):
149- """Set the stratigraphic column.
115+ """Set the stratigraphic column in the data manager .
150116
151117 Parameters
152118 ----------
153119 units : list
154120 List of unit names from youngest to oldest.
155121 """
156- # Clear existing rows
157- self .stratiColumnTable .setRowCount (0 )
158-
159- # Add new rows
160- for unit in units :
161- row_count = self .stratiColumnTable .rowCount ()
162- self .stratiColumnTable .insertRow (row_count )
163- self .stratiColumnTable .setItem (row_count , 0 , QTableWidgetItem (unit ))
122+ if not hasattr (self , 'data_manager' ) or self .data_manager is None :
123+ raise ValueError ("data_manager is not initialized" )
124+
125+ # Clear the existing column
126+ self .data_manager ._stratigraphic_column .clear ()
127+
128+ # Add each unit to the stratigraphic column
129+ for unit_name in units :
130+ self .data_manager .add_to_stratigraphic_column (
131+ {'type' : 'unit' , 'name' : unit_name , 'colour' : None }
132+ )
133+
134+ # The callback is already called by add_to_stratigraphic_column
135+ # But we still update the display to be safe
136+ if hasattr (self , 'strat_column_widget' ):
137+ self .strat_column_widget .update_display ()
0 commit comments