Skip to content

Commit 909610d

Browse files
committed
fix: loopstructural/map2loop share the same stratigraphic column widget/backend
1 parent 8a3dbeb commit 909610d

8 files changed

Lines changed: 248 additions & 219 deletions

File tree

loopstructural/gui/map2loop_tools/dialogs.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ def setup_ui(self):
2626

2727
# Replace the run button with dialog buttons
2828
self.widget.runButton.hide()
29-
30-
self.button_box = QDialogButtonBox(
31-
QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self
32-
)
29+
30+
self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self)
3331
self.button_box.accepted.connect(self._run_and_accept)
3432
self.button_box.rejected.connect(self.reject)
3533
layout.addWidget(self.button_box)
@@ -59,10 +57,8 @@ def setup_ui(self):
5957

6058
# Replace the run button with dialog buttons
6159
self.widget.runButton.hide()
62-
63-
self.button_box = QDialogButtonBox(
64-
QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self
65-
)
60+
61+
self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self)
6662
self.button_box.accepted.connect(self._run_and_accept)
6763
self.button_box.rejected.connect(self.reject)
6864
layout.addWidget(self.button_box)
@@ -75,29 +71,29 @@ def _run_and_accept(self):
7571
class UserDefinedSorterDialog(QDialog):
7672
"""Dialog for user-defined stratigraphic column using map2loop classes directly."""
7773

78-
def __init__(self, parent=None):
74+
def __init__(self, parent=None, data_manager=None):
7975
"""Initialize the user-defined sorter dialog."""
8076
super().__init__(parent)
8177
self.setWindowTitle("Map2Loop User-Defined Stratigraphic Column")
78+
self.data_manager = data_manager
79+
8280
self.setup_ui()
8381

8482
def setup_ui(self):
8583
"""Set up the dialog UI."""
8684
from .user_defined_sorter_widget import UserDefinedSorterWidget
8785

8886
layout = QVBoxLayout(self)
89-
self.widget = UserDefinedSorterWidget(self)
87+
self.widget = UserDefinedSorterWidget(self, data_manager=self.data_manager)
9088
layout.addWidget(self.widget)
9189

9290
# Replace the run button with dialog buttons
93-
self.widget.runButton.hide()
94-
95-
self.button_box = QDialogButtonBox(
96-
QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self
97-
)
98-
self.button_box.accepted.connect(self._run_and_accept)
99-
self.button_box.rejected.connect(self.reject)
100-
layout.addWidget(self.button_box)
91+
# self.widget.runButton.hide()
92+
93+
# self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self)
94+
# self.button_box.accepted.connect(self._run_and_accept)
95+
# self.button_box.rejected.connect(self.reject)
96+
# layout.addWidget(self.button_box)
10197

10298
def _run_and_accept(self):
10399
"""Run the sorter and accept dialog if successful."""
@@ -123,10 +119,8 @@ def setup_ui(self):
123119

124120
# Replace the run button with dialog buttons
125121
self.widget.runButton.hide()
126-
127-
self.button_box = QDialogButtonBox(
128-
QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self
129-
)
122+
123+
self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self)
130124
self.button_box.accepted.connect(self._run_and_accept)
131125
self.button_box.rejected.connect(self.reject)
132126
layout.addWidget(self.button_box)
@@ -155,10 +149,8 @@ def setup_ui(self):
155149

156150
# Replace the run button with dialog buttons
157151
self.widget.runButton.hide()
158-
159-
self.button_box = QDialogButtonBox(
160-
QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self
161-
)
152+
153+
self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self)
162154
self.button_box.accepted.connect(self._run_and_accept)
163155
self.button_box.rejected.connect(self.reject)
164156
layout.addWidget(self.button_box)

loopstructural/gui/map2loop_tools/user_defined_sorter_widget.py

Lines changed: 63 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
import os
44

5-
from PyQt5.QtWidgets import QWidget, QTableWidgetItem, QMessageBox
5+
from PyQt5.QtWidgets import QMessageBox, QVBoxLayout, QWidget
66
from qgis.PyQt import uic
77

8+
from loopstructural.gui.modelling.stratigraphic_column import StratColumnWidget
9+
810

911
class 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()

loopstructural/gui/map2loop_tools/user_defined_sorter_widget.ui

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -24,70 +24,6 @@
2424
</property>
2525
</widget>
2626
</item>
27-
<item>
28-
<widget class="QTableWidget" name="stratiColumnTable">
29-
<property name="columnCount">
30-
<number>1</number>
31-
</property>
32-
<column>
33-
<property name="text">
34-
<string>Unit Name</string>
35-
</property>
36-
</column>
37-
</widget>
38-
</item>
39-
<item>
40-
<layout class="QHBoxLayout" name="buttonLayout">
41-
<item>
42-
<widget class="QPushButton" name="addRowButton">
43-
<property name="text">
44-
<string>Add Row</string>
45-
</property>
46-
</widget>
47-
</item>
48-
<item>
49-
<widget class="QPushButton" name="removeRowButton">
50-
<property name="text">
51-
<string>Remove Row</string>
52-
</property>
53-
</widget>
54-
</item>
55-
<item>
56-
<widget class="QPushButton" name="moveUpButton">
57-
<property name="text">
58-
<string>Move Up</string>
59-
</property>
60-
</widget>
61-
</item>
62-
<item>
63-
<widget class="QPushButton" name="moveDownButton">
64-
<property name="text">
65-
<string>Move Down</string>
66-
</property>
67-
</widget>
68-
</item>
69-
<item>
70-
<spacer name="horizontalSpacer">
71-
<property name="orientation">
72-
<enum>Qt::Horizontal</enum>
73-
</property>
74-
<property name="sizeHint" stdset="0">
75-
<size>
76-
<width>40</width>
77-
<height>20</height>
78-
</size>
79-
</property>
80-
</spacer>
81-
</item>
82-
</layout>
83-
</item>
84-
<item>
85-
<widget class="QPushButton" name="runButton">
86-
<property name="text">
87-
<string>Create Stratigraphic Column</string>
88-
</property>
89-
</widget>
90-
</item>
9127
</layout>
9228
</widget>
9329
<resources/>

0 commit comments

Comments
 (0)