Skip to content

Commit

Permalink
Fixed bug with context menus in tree
Browse files Browse the repository at this point in the history
  • Loading branch information
timlinux committed Sep 30, 2024
1 parent 4018851 commit cadb95a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
2 changes: 1 addition & 1 deletion geest/core/workflows/dont_use_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def execute(self):
self.feedback.setProgress(
(i + 1) / steps * 100
) # Report progress in percentage
time.sleep(1) # Simulate a task
pass

self.attributes["result"] = "Dont use workflow completed"
QgsMessageLog.logMessage(
Expand Down
2 changes: 1 addition & 1 deletion geest/core/workflows/raster_layer_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def execute(self):
self._feedback.setProgress(
(i + 1) / steps * 100
) # Report progress in percentage
time.sleep(1) # Simulate a task
pass

self._attributes["result"] = "Spatial analysis completed"
QgsMessageLog.logMessage(
Expand Down
24 changes: 24 additions & 0 deletions geest/gui/geest_treeview.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,30 @@ def recurse_tree(item):
}
return json_data

def clear_factor_weightings(self, dimension_item):
"""Clear all weightings for factors under the given dimension."""
for i in range(dimension_item.childCount()):
factor_item = dimension_item.child(i)
factor_item.setData(2, "0.00")
# After clearing, update the dimension's total weighting
dimension_item.setData(2, "0.00")
self.update_font_color(dimension_item, QColor(Qt.red))
self.layoutChanged.emit()

def auto_assign_factor_weightings(self, dimension_item):
"""Auto-assign weightings evenly across all factors under the dimension."""
num_factors = dimension_item.childCount()
if num_factors == 0:
return
factor_weighting = 1 / num_factors
for i in range(num_factors):
factor_item = dimension_item.child(i)
factor_item.setData(2, f"{factor_weighting:.2f}")
# Update the dimensions's total weighting
dimension_item.setData(2, "1.00")
self.update_font_color(dimension_item, QColor(Qt.green))
self.layoutChanged.emit()

def clear_layer_weightings(self, factor_item):
"""Clear all weightings for layers under the given factor."""
for i in range(factor_item.childCount()):
Expand Down
25 changes: 18 additions & 7 deletions geest/gui/tree_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def open_context_menu(self, position: QPoint):
item = index.internalPointer()

# Check the role of the item directly from the stored role
if item.role == "dimension" and editing:
if item.role == "dimension":
# Context menu for dimensions
add_factor_action = QAction("Add Factor", self)
remove_dimension_action = QAction("Remove Dimension", self)
Expand All @@ -244,13 +244,23 @@ def open_context_menu(self, position: QPoint):
remove_dimension_action.triggered.connect(
lambda: self.model.remove_item(item)
)

clear_action = QAction("Clear Factor Weightings", self)
auto_assign_action = QAction("Auto Assign Factor Weightings", self)
clear_action.triggered.connect(
lambda: self.model.clear_factor_weightings(item)
)
auto_assign_action.triggered.connect(
lambda: self.model.auto_assign_factor_weightings(item)
)
# Add actions to menu
menu = QMenu(self)
menu.addAction(add_factor_action)
menu.addAction(remove_dimension_action)
menu.addAction(clear_action)
menu.addAction(auto_assign_action)
if editing:
menu.addAction(add_factor_action)
menu.addAction(remove_dimension_action)

elif item.role == "factor" and editing:
elif item.role == "factor":
# Context menu for factors
add_layer_action = QAction("Add Layer", self)
remove_factor_action = QAction("Remove Factor", self)
Expand All @@ -269,8 +279,9 @@ def open_context_menu(self, position: QPoint):

# Add actions to menu
menu = QMenu(self)
menu.addAction(add_layer_action)
menu.addAction(remove_factor_action)
if editing:
menu.addAction(add_layer_action)
menu.addAction(remove_factor_action)
menu.addAction(clear_action)
menu.addAction(auto_assign_action)

Expand Down

0 comments on commit cadb95a

Please sign in to comment.