add/edit/delet objectives and participants in war

This commit is contained in:
Maxime Réaux 2026-01-28 16:25:40 +01:00
parent e33dec40be
commit 495a5adb98
14 changed files with 1088 additions and 322 deletions

View file

@ -11,6 +11,8 @@ from warchron.view.ui.ui_main_window import Ui_MainWindow
from warchron.view.ui.ui_player_dialog import Ui_playerDialog
from warchron.view.ui.ui_war_dialog import Ui_warDialog
from warchron.view.ui.ui_campaign_dialog import Ui_campaignDialog
from warchron.view.ui.ui_objective_dialog import Ui_objectiveDialog
from warchron.view.ui.ui_participant_dialog import Ui_participantDialog
class View(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
@ -25,6 +27,10 @@ class View(QtWidgets.QMainWindow, Ui_MainWindow):
self.show_details(None)
self.playersTable.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.playersTable.customContextMenuRequested.connect(self._on_players_table_context_menu)
self.warParticipantsTable.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.warParticipantsTable.customContextMenuRequested.connect(self._on_war_participants_table_context_menu)
self.objectivesTable.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.objectivesTable.customContextMenuRequested.connect(self._on_objectives_table_context_menu)
self.warsTree.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.warsTree.customContextMenuRequested.connect(self._on_wars_tree_context_menu)
self.addCampaignBtn.clicked.connect(self._on_add_campaign_clicked)
@ -41,46 +47,15 @@ class View(QtWidgets.QMainWindow, Ui_MainWindow):
"id": current.data(0, ROLE_ID),
})
def _on_players_table_context_menu(self, pos):
item = self.playersTable.itemAt(pos)
if not item:
return
player_id = item.data(Qt.ItemDataRole.UserRole)
menu = QMenu(self)
edit_action = menu.addAction("Edit")
delete_action = menu.addAction("Delete")
action = menu.exec(self.playersTable.viewport().mapToGlobal(pos))
if action == edit_action and self.on_edit_item:
self.on_edit_item(ItemType.PLAYER, player_id)
elif action == delete_action and self.on_delete_item:
self.on_delete_item(ItemType.PLAYER, player_id)
def get_current_tab(self) -> str:
index = self.tabWidget.currentIndex()
if index == 0:
return "players"
elif index == 1:
return "wars"
return ""
def _on_wars_tree_context_menu(self, pos):
item = self.warsTree.itemAt(pos)
if not item:
return
item_type = item.data(0, ROLE_TYPE)
item_id = item.data(0, ROLE_ID)
menu = QMenu(self)
edit_action = None
if item_type != ItemType.ROUND:
edit_action = menu.addAction("Edit")
delete_action = menu.addAction("Delete")
action = menu.exec(self.warsTree.viewport().mapToGlobal(pos))
if action == edit_action:
if self.on_edit_item:
self.on_edit_item(item_type, item_id)
elif action == delete_action:
if self.on_delete_item:
self.on_delete_item(item_type, item_id)
def _on_add_campaign_clicked(self):
if self.on_add_campaign:
self.on_add_campaign()
def _on_add_round_clicked(self):
if self.on_add_round:
self.on_add_round()
# General popups
def closeEvent(self, event: QCloseEvent):
if self.on_close_callback:
@ -108,6 +83,26 @@ class View(QtWidgets.QMainWindow, Ui_MainWindow):
)
return Path(filename) if filename else None
# Players view
def _on_players_table_context_menu(self, pos):
item = self.playersTable.itemAt(pos)
if not item:
return
row = item.row()
name_item = self.playersTable.item(row, 0)
if not name_item:
return
player_id = name_item.data(Qt.ItemDataRole.UserRole)
menu = QMenu(self)
edit_action = menu.addAction("Edit")
delete_action = menu.addAction("Delete")
action = menu.exec(self.playersTable.viewport().mapToGlobal(pos))
if action == edit_action and self.on_edit_item:
self.on_edit_item(ItemType.PLAYER, player_id)
elif action == delete_action and self.on_delete_item:
self.on_delete_item(ItemType.PLAYER, player_id)
def display_players(self, players: list):
table = self.playersTable
table.setRowCount(len(players))
@ -117,8 +112,43 @@ class View(QtWidgets.QMainWindow, Ui_MainWindow):
table.setItem(row, 0, play_item)
table.resizeColumnsToContents()
# Wars view
def _on_add_campaign_clicked(self):
if self.on_add_campaign:
self.on_add_campaign()
def _on_add_round_clicked(self):
if self.on_add_round:
self.on_add_round()
def set_add_campaign_enabled(self, enabled: bool):
self.addCampaignBtn.setEnabled(enabled)
def set_add_round_enabled(self, enabled: bool):
self.addRoundBtn.setEnabled(enabled)
def _on_wars_tree_context_menu(self, pos):
item = self.warsTree.itemAt(pos)
if not item:
return
item_type = item.data(0, ROLE_TYPE)
item_id = item.data(0, ROLE_ID)
menu = QMenu(self)
edit_action = None
if item_type != ItemType.ROUND:
edit_action = menu.addAction("Edit")
delete_action = menu.addAction("Delete")
action = menu.exec(self.warsTree.viewport().mapToGlobal(pos))
if action == edit_action:
if self.on_edit_item:
self.on_edit_item(item_type, item_id)
elif action == delete_action:
if self.on_delete_item:
self.on_delete_item(item_type, item_id)
def _format_war_label(self, war) -> str:
return f"{war.name} ({war.name} ({war.year}))"
return f"{war.name} ({war.year})"
def _format_campaign_label(self, camp) -> str:
return f"{camp.name} ({calendar.month_name[camp.month]})"
@ -149,26 +179,21 @@ class View(QtWidgets.QMainWindow, Ui_MainWindow):
tree.currentItemChanged.connect(self._emit_selection_changed)
tree.expandAll()
def show_details(self, item_type: str | None):
if item_type == ItemType.WAR:
self.selectedDetailsStack.setCurrentWidget(self.pageWar)
elif item_type == ItemType.CAMPAIGN:
self.selectedDetailsStack.setCurrentWidget(self.pageCampaign)
elif item_type == ItemType.ROUND:
self.selectedDetailsStack.setCurrentWidget(self.pageRound)
else:
self.selectedDetailsStack.setCurrentWidget(self.pageEmpty)
def show_war_details(self, *, name: str, year: int):
self.warName.setText(name)
self.warYear.setText(year)
def show_campaign_details(self, *, name: str, month: int):
self.campaignName.setText(name)
self.campaignMonth.setText(calendar.month_name[month])
def show_round_details(self, *, index: int):
self.roundNb.setText(f"Round {index}")
def select_tree_item(self, *, item_type: ItemType, item_id: str):
def walk(item: QTreeWidgetItem):
if (
item.data(0, ROLE_TYPE) == item_type
and item.data(0, ROLE_ID) == item_id
):
self.warsTree.setCurrentItem(item)
return True
for i in range(item.childCount()):
if walk(item.child(i)):
return True
return False
for i in range(self.warsTree.topLevelItemCount()):
if walk(self.warsTree.topLevelItem(i)):
return
def get_selected_tree_item(self):
item = self.warsTree.currentItem()
@ -179,20 +204,94 @@ class View(QtWidgets.QMainWindow, Ui_MainWindow):
"id": item.data(0, ROLE_ID)
}
def set_add_campaign_enabled(self, enabled: bool):
self.addCampaignBtn.setEnabled(enabled)
def show_details(self, item_type: str | None):
if item_type == ItemType.WAR:
self.selectedDetailsStack.setCurrentWidget(self.pageWar)
elif item_type == ItemType.CAMPAIGN:
self.selectedDetailsStack.setCurrentWidget(self.pageCampaign)
elif item_type == ItemType.ROUND:
self.selectedDetailsStack.setCurrentWidget(self.pageRound)
else:
self.selectedDetailsStack.setCurrentWidget(self.pageEmpty)
def set_add_round_enabled(self, enabled: bool):
self.addRoundBtn.setEnabled(enabled)
# War page
def get_current_tab(self) -> str:
index = self.tabWidget.currentIndex()
if index == 0:
return "players"
elif index == 1:
return "wars"
return ""
def _on_objectives_table_context_menu(self, pos):
item = self.objectivesTable.itemAt(pos)
if not item:
return
row = item.row()
name_item = self.objectivesTable.item(row, 0)
if not name_item:
return
objective_id = name_item.data(Qt.ItemDataRole.UserRole)
menu = QMenu(self)
edit_action = menu.addAction("Edit")
delete_action = menu.addAction("Delete")
action = menu.exec(self.objectivesTable.viewport().mapToGlobal(pos))
if action == edit_action and self.on_edit_item:
self.on_edit_item(ItemType.OBJECTIVE, objective_id)
elif action == delete_action and self.on_delete_item:
self.on_delete_item(ItemType.OBJECTIVE, objective_id)
def _on_war_participants_table_context_menu(self, pos):
item = self.warParticipantsTable.itemAt(pos)
if not item:
return
row = item.row()
name_item = self.warParticipantsTable.item(row, 0)
if not name_item:
return
participant_id = name_item.data(Qt.ItemDataRole.UserRole)
menu = QMenu(self)
edit_action = menu.addAction("Edit")
delete_action = menu.addAction("Delete")
action = menu.exec(self.warParticipantsTable.viewport().mapToGlobal(pos))
if action == edit_action and self.on_edit_item:
self.on_edit_item(ItemType.WAR_PARTICIPANT, participant_id)
elif action == delete_action and self.on_delete_item:
self.on_delete_item(ItemType.WAR_PARTICIPANT, participant_id)
def show_war_details(self, *, name: str, year: int):
self.warName.setText(name)
self.warYear.setText(str(year))
def display_war_objectives(self, objectives: list):
table = self.objectivesTable
table.clearContents()
table.setRowCount(len(objectives))
for row, obj in enumerate(objectives):
name_item = QtWidgets.QTableWidgetItem(obj.name)
desc_item = QtWidgets.QTableWidgetItem(obj.description)
name_item.setData(Qt.ItemDataRole.UserRole, obj.id)
table.setItem(row, 0, name_item)
table.setItem(row, 1, desc_item)
table.resizeColumnsToContents()
def display_war_participants(self, participants: list[tuple[str, str, str]]):
table = self.warParticipantsTable
table.clearContents()
table.setRowCount(len(participants))
for row, (name, faction, pid) in enumerate(participants):
name_item = QtWidgets.QTableWidgetItem(name)
fact_item = QtWidgets.QTableWidgetItem(faction)
name_item.setData(Qt.ItemDataRole.UserRole, pid)
table.setItem(row, 0, name_item)
table.setItem(row, 1, fact_item)
table.resizeColumnsToContents()
# Campaign page
def show_campaign_details(self, *, name: str, month: int):
self.campaignName.setText(name)
self.campaignMonth.setText(calendar.month_name[month])
# Round page
def show_round_details(self, *, index: int):
self.roundNb.setText(f"Round {index}")
class PlayerDialog(QDialog):
def __init__(self, parent=None, *, default_name: str = ""):
super().__init__(parent)
@ -231,4 +330,38 @@ class CampaignDialog(QDialog):
return self.ui.campaignName.text().strip()
def get_campaign_month(self) -> int:
return int(self.ui.campaignMonth.value())
return int(self.ui.campaignMonth.value())
class ObjectiveDialog(QDialog):
def __init__(self, parent=None, *, default_name="", default_description=""):
super().__init__(parent)
self.ui = Ui_objectiveDialog()
self.ui.setupUi(self)
self.ui.objectiveName.setText(default_name)
self.ui.objectiveDescription.setPlainText(default_description)
def get_objective_name(self) -> str:
return self.ui.objectiveName.text().strip()
def get_objective_description(self) -> str:
return self.ui.objectiveDescription.toPlainText().strip()
class ParticipantDialog(QDialog):
def __init__(self, parent=None, *, players: list, default_player_id=None, default_faction="", editable_player=True):
super().__init__(parent)
self.ui = Ui_participantDialog()
self.ui.setupUi(self)
for player in players:
self.ui.playerComboBox.addItem(player.name, player.id)
if default_player_id:
index = self.ui.playerComboBox.findData(default_player_id)
if index != -1:
self.ui.playerComboBox.setCurrentIndex(index)
self.ui.playerComboBox.setEnabled(editable_player)
self.ui.faction.setText(default_faction)
def get_player_id(self) -> str:
return self.ui.playerComboBox.currentData()
def get_participant_faction(self) -> str:
return self.ui.faction.text().strip()