refacto update ; pretify code
This commit is contained in:
parent
6bd3ee31dc
commit
fbb1c913ba
11 changed files with 594 additions and 310 deletions
|
|
@ -21,6 +21,7 @@ from warchron.view.ui.ui_battle_result_dialog import Ui_battleResultDialog
|
|||
|
||||
# utils...
|
||||
|
||||
|
||||
def select_if_exists(combo, value):
|
||||
if value is None:
|
||||
return
|
||||
|
|
@ -28,17 +29,21 @@ def select_if_exists(combo, value):
|
|||
if idx != -1:
|
||||
combo.setCurrentIndex(idx)
|
||||
|
||||
|
||||
def format_war_label(war) -> str:
|
||||
return f"{war.name} ({war.year})"
|
||||
|
||||
|
||||
def format_campaign_label(camp) -> str:
|
||||
return f"{camp.name} ({calendar.month_name[camp.month]})"
|
||||
|
||||
|
||||
def format_round_label(round, index: int) -> str:
|
||||
if index is None:
|
||||
return ""
|
||||
return f"Round {index}"
|
||||
|
||||
|
||||
class View(QtWidgets.QMainWindow, Ui_MainWindow):
|
||||
def __init__(self, parent=None):
|
||||
super(View, self).__init__(parent)
|
||||
|
|
@ -51,24 +56,46 @@ class View(QtWidgets.QMainWindow, Ui_MainWindow):
|
|||
self.on_delete_item = None
|
||||
self.show_details(None)
|
||||
self.playersTable.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
|
||||
self.playersTable.customContextMenuRequested.connect(self._on_players_table_context_menu)
|
||||
self.playersTable.customContextMenuRequested.connect(
|
||||
self._on_players_table_context_menu
|
||||
)
|
||||
self.warsTree.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
|
||||
self.warsTree.customContextMenuRequested.connect(self._on_wars_tree_context_menu)
|
||||
self.warsTree.customContextMenuRequested.connect(
|
||||
self._on_wars_tree_context_menu
|
||||
)
|
||||
self.addCampaignBtn.clicked.connect(self._on_add_campaign_clicked)
|
||||
self.addRoundBtn.clicked.connect(self._on_add_round_clicked)
|
||||
# Pages
|
||||
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.campaignParticipantsTable.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
|
||||
self.campaignParticipantsTable.customContextMenuRequested.connect(self._on_campaign_participants_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.campaignParticipantsTable.setContextMenuPolicy(
|
||||
Qt.ContextMenuPolicy.CustomContextMenu
|
||||
)
|
||||
self.campaignParticipantsTable.customContextMenuRequested.connect(
|
||||
self._on_campaign_participants_table_context_menu
|
||||
)
|
||||
self.sectorsTable.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
|
||||
self.sectorsTable.customContextMenuRequested.connect(self._on_sectors_table_context_menu)
|
||||
self.sectorsTable.customContextMenuRequested.connect(
|
||||
self._on_sectors_table_context_menu
|
||||
)
|
||||
self.choicesTable.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
|
||||
self.choicesTable.customContextMenuRequested.connect(self._on_choices_table_context_menu)
|
||||
self.choicesTable.customContextMenuRequested.connect(
|
||||
self._on_choices_table_context_menu
|
||||
)
|
||||
self.battlesTable.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
|
||||
self.battlesTable.customContextMenuRequested.connect(self._on_battles_table_context_menu)
|
||||
self.battlesTable.customContextMenuRequested.connect(
|
||||
self._on_battles_table_context_menu
|
||||
)
|
||||
|
||||
def _emit_selection_changed(self, current, previous):
|
||||
if not self.on_tree_selection_changed:
|
||||
|
|
@ -76,10 +103,12 @@ class View(QtWidgets.QMainWindow, Ui_MainWindow):
|
|||
if not current:
|
||||
self.on_tree_selection_changed(None)
|
||||
return
|
||||
self.on_tree_selection_changed({
|
||||
"type": current.data(0, ROLE_TYPE),
|
||||
"id": current.data(0, ROLE_ID),
|
||||
})
|
||||
self.on_tree_selection_changed(
|
||||
{
|
||||
"type": current.data(0, ROLE_TYPE),
|
||||
"id": current.data(0, ROLE_ID),
|
||||
}
|
||||
)
|
||||
|
||||
def get_current_tab(self) -> str:
|
||||
index = self.tabWidget.currentIndex()
|
||||
|
|
@ -89,7 +118,7 @@ class View(QtWidgets.QMainWindow, Ui_MainWindow):
|
|||
return "wars"
|
||||
return ""
|
||||
|
||||
# General popups
|
||||
# General popups
|
||||
|
||||
def closeEvent(self, event: QCloseEvent):
|
||||
if self.on_close_callback:
|
||||
|
|
@ -101,23 +130,17 @@ class View(QtWidgets.QMainWindow, Ui_MainWindow):
|
|||
|
||||
def ask_open_file(self) -> Path | None:
|
||||
filename, _ = QFileDialog.getOpenFileName(
|
||||
self,
|
||||
"Open war history",
|
||||
"",
|
||||
"WarChron files (*.json)"
|
||||
self, "Open war history", "", "WarChron files (*.json)"
|
||||
)
|
||||
return Path(filename) if filename else None
|
||||
|
||||
def ask_save_file(self) -> Path | None:
|
||||
filename, _ = QFileDialog.getSaveFileName(
|
||||
self,
|
||||
"Save war history",
|
||||
"",
|
||||
"WarChron files (*.json)"
|
||||
self, "Save war history", "", "WarChron files (*.json)"
|
||||
)
|
||||
return Path(filename) if filename else None
|
||||
|
||||
# Players view
|
||||
# Players view
|
||||
|
||||
def _on_players_table_context_menu(self, pos):
|
||||
item = self.playersTable.itemAt(pos)
|
||||
|
|
@ -146,7 +169,7 @@ class View(QtWidgets.QMainWindow, Ui_MainWindow):
|
|||
table.setItem(row, 0, play_item)
|
||||
table.resizeColumnsToContents()
|
||||
|
||||
# Wars view
|
||||
# Wars view
|
||||
|
||||
def _on_add_campaign_clicked(self):
|
||||
if self.on_add_campaign:
|
||||
|
|
@ -216,6 +239,7 @@ class View(QtWidgets.QMainWindow, Ui_MainWindow):
|
|||
if walk(item.child(i)):
|
||||
return True
|
||||
return False
|
||||
|
||||
for i in range(self.warsTree.topLevelItemCount()):
|
||||
if walk(self.warsTree.topLevelItem(i)):
|
||||
return
|
||||
|
|
@ -224,10 +248,7 @@ class View(QtWidgets.QMainWindow, Ui_MainWindow):
|
|||
item = self.warsTree.currentItem()
|
||||
if not item:
|
||||
return None
|
||||
return {
|
||||
"type": item.data(0, ROLE_TYPE),
|
||||
"id": item.data(0, ROLE_ID)
|
||||
}
|
||||
return {"type": item.data(0, ROLE_TYPE), "id": item.data(0, ROLE_ID)}
|
||||
|
||||
def show_details(self, item_type: str | None):
|
||||
if item_type == ItemType.WAR:
|
||||
|
|
@ -239,7 +260,7 @@ class View(QtWidgets.QMainWindow, Ui_MainWindow):
|
|||
else:
|
||||
self.selectedDetailsStack.setCurrentWidget(self.pageEmpty)
|
||||
|
||||
# War page
|
||||
# War page
|
||||
|
||||
def _on_objectives_table_context_menu(self, pos):
|
||||
item = self.objectivesTable.itemAt(pos)
|
||||
|
|
@ -305,7 +326,7 @@ class View(QtWidgets.QMainWindow, Ui_MainWindow):
|
|||
table.setItem(row, 1, fact_item)
|
||||
table.resizeColumnsToContents()
|
||||
|
||||
# Campaign page
|
||||
# Campaign page
|
||||
|
||||
def _on_sectors_table_context_menu(self, pos):
|
||||
item = self.sectorsTable.itemAt(pos)
|
||||
|
|
@ -347,13 +368,19 @@ class View(QtWidgets.QMainWindow, Ui_MainWindow):
|
|||
self.campaignName.setText(name)
|
||||
self.campaignMonth.setText(calendar.month_name[month])
|
||||
|
||||
def display_campaign_sectors(self, sectors: list[tuple[str, str, str, str, str, str]]):
|
||||
def display_campaign_sectors(
|
||||
self, sectors: list[tuple[str, str, str, str, str, str]]
|
||||
):
|
||||
table = self.sectorsTable
|
||||
table.clearContents()
|
||||
table.setRowCount(len(sectors))
|
||||
for row, (name, round_index, major, minor, influence, pid) in enumerate(sectors):
|
||||
for row, (name, round_index, major, minor, influence, pid) in enumerate(
|
||||
sectors
|
||||
):
|
||||
name_item = QtWidgets.QTableWidgetItem(name)
|
||||
round_item = QtWidgets.QTableWidgetItem(format_round_label(None, round_index))
|
||||
round_item = QtWidgets.QTableWidgetItem(
|
||||
format_round_label(None, round_index)
|
||||
)
|
||||
major_item = QtWidgets.QTableWidgetItem(major)
|
||||
minor_item = QtWidgets.QTableWidgetItem(minor)
|
||||
influence_item = QtWidgets.QTableWidgetItem(influence)
|
||||
|
|
@ -365,7 +392,9 @@ class View(QtWidgets.QMainWindow, Ui_MainWindow):
|
|||
table.setItem(row, 4, influence_item)
|
||||
table.resizeColumnsToContents()
|
||||
|
||||
def display_campaign_participants(self, participants: list[tuple[str, str, str, str]]):
|
||||
def display_campaign_participants(
|
||||
self, participants: list[tuple[str, str, str, str]]
|
||||
):
|
||||
table = self.campaignParticipantsTable
|
||||
table.clearContents()
|
||||
table.setRowCount(len(participants))
|
||||
|
|
@ -379,7 +408,7 @@ class View(QtWidgets.QMainWindow, Ui_MainWindow):
|
|||
table.setItem(row, 2, theme_item)
|
||||
table.resizeColumnsToContents()
|
||||
|
||||
# Round page
|
||||
# Round page
|
||||
|
||||
def _on_choices_table_context_menu(self, pos):
|
||||
item = self.choicesTable.itemAt(pos)
|
||||
|
|
@ -422,7 +451,9 @@ class View(QtWidgets.QMainWindow, Ui_MainWindow):
|
|||
table = self.choicesTable
|
||||
table.clearContents()
|
||||
table.setRowCount(len(participants))
|
||||
for row, (participant, priority, secondary, choice_id) in enumerate(participants):
|
||||
for row, (participant, priority, secondary, choice_id) in enumerate(
|
||||
participants
|
||||
):
|
||||
participant_item = QtWidgets.QTableWidgetItem(participant)
|
||||
priority_item = QtWidgets.QTableWidgetItem(priority)
|
||||
secondary_item = QtWidgets.QTableWidgetItem(secondary)
|
||||
|
|
@ -446,6 +477,7 @@ class View(QtWidgets.QMainWindow, Ui_MainWindow):
|
|||
table.setItem(row, 2, player_2_item)
|
||||
table.resizeColumnsToContents()
|
||||
|
||||
|
||||
class PlayerDialog(QDialog):
|
||||
def __init__(self, parent=None, *, default_name: str = ""):
|
||||
super().__init__(parent)
|
||||
|
|
@ -456,8 +488,11 @@ class PlayerDialog(QDialog):
|
|||
def get_player_name(self) -> str:
|
||||
return self.ui.playerName.text().strip()
|
||||
|
||||
|
||||
class WarDialog(QDialog):
|
||||
def __init__(self, parent=None, default_name: str = "", default_year: int | None = None):
|
||||
def __init__(
|
||||
self, parent=None, default_name: str = "", default_year: int | None = None
|
||||
):
|
||||
super().__init__(parent)
|
||||
self.ui = Ui_warDialog()
|
||||
self.ui.setupUi(self)
|
||||
|
|
@ -467,12 +502,15 @@ class WarDialog(QDialog):
|
|||
|
||||
def get_war_name(self) -> str:
|
||||
return self.ui.warName.text().strip()
|
||||
|
||||
|
||||
def get_war_year(self) -> int:
|
||||
return int(self.ui.warYear.value())
|
||||
|
||||
|
||||
class CampaignDialog(QDialog):
|
||||
def __init__(self, parent=None, default_name: str = "", default_month: int | None = None):
|
||||
def __init__(
|
||||
self, parent=None, default_name: str = "", default_month: int | None = None
|
||||
):
|
||||
super().__init__(parent)
|
||||
self.ui = Ui_campaignDialog()
|
||||
self.ui.setupUi(self)
|
||||
|
|
@ -485,7 +523,8 @@ class CampaignDialog(QDialog):
|
|||
|
||||
def get_campaign_month(self) -> int:
|
||||
return int(self.ui.campaignMonth.value())
|
||||
|
||||
|
||||
|
||||
class ObjectiveDialog(QDialog):
|
||||
def __init__(self, parent=None, *, default_name="", default_description=""):
|
||||
super().__init__(parent)
|
||||
|
|
@ -499,9 +538,18 @@ class ObjectiveDialog(QDialog):
|
|||
|
||||
def get_objective_description(self) -> str:
|
||||
return self.ui.objectiveDescription.toPlainText().strip()
|
||||
|
||||
|
||||
|
||||
class WarParticipantDialog(QDialog):
|
||||
def __init__(self, parent=None, *, players: list, default_player_id=None, default_faction="", editable_player=True):
|
||||
def __init__(
|
||||
self,
|
||||
parent=None,
|
||||
*,
|
||||
players: list,
|
||||
default_player_id=None,
|
||||
default_faction="",
|
||||
editable_player=True,
|
||||
):
|
||||
super().__init__(parent)
|
||||
self.ui = Ui_warParticipantDialog()
|
||||
self.ui.setupUi(self)
|
||||
|
|
@ -516,9 +564,19 @@ class WarParticipantDialog(QDialog):
|
|||
|
||||
def get_participant_faction(self) -> str:
|
||||
return self.ui.faction.text().strip()
|
||||
|
||||
|
||||
|
||||
class CampaignParticipantDialog(QDialog):
|
||||
def __init__(self, parent=None, *, participants: list[ParticipantOption], default_participant_id=None, default_leader="", default_theme="", editable_player=True):
|
||||
def __init__(
|
||||
self,
|
||||
parent=None,
|
||||
*,
|
||||
participants: list[ParticipantOption],
|
||||
default_participant_id=None,
|
||||
default_leader="",
|
||||
default_theme="",
|
||||
editable_player=True,
|
||||
):
|
||||
super().__init__(parent)
|
||||
self.ui = Ui_campaignParticipantDialog()
|
||||
self.ui.setupUi(self)
|
||||
|
|
@ -533,13 +591,25 @@ class CampaignParticipantDialog(QDialog):
|
|||
return self.ui.playerComboBox.currentData()
|
||||
|
||||
def get_participant_leader(self) -> str:
|
||||
return self.ui.leader.text().strip()
|
||||
|
||||
return self.ui.leader.text().strip()
|
||||
|
||||
def get_participant_theme(self) -> str:
|
||||
return self.ui.theme.text().strip()
|
||||
|
||||
|
||||
|
||||
class SectorDialog(QDialog):
|
||||
def __init__(self, parent=None, *, default_name="", rounds: list, default_round_id=None, objectives: list, default_major_id=None, default_minor_id=None, default_influence_id=None):
|
||||
def __init__(
|
||||
self,
|
||||
parent=None,
|
||||
*,
|
||||
default_name="",
|
||||
rounds: list,
|
||||
default_round_id=None,
|
||||
objectives: list,
|
||||
default_major_id=None,
|
||||
default_minor_id=None,
|
||||
default_influence_id=None,
|
||||
):
|
||||
super().__init__(parent)
|
||||
self.ui = Ui_sectorDialog()
|
||||
self.ui.setupUi(self)
|
||||
|
|
@ -562,25 +632,30 @@ class SectorDialog(QDialog):
|
|||
return self.ui.sectorName.text().strip()
|
||||
|
||||
def get_round_id(self) -> str:
|
||||
return self.ui.roundComboBox.currentData()
|
||||
return self.ui.roundComboBox.currentData()
|
||||
|
||||
def get_major_id(self) -> str:
|
||||
return self.ui.majorComboBox.currentData()
|
||||
|
||||
return self.ui.majorComboBox.currentData()
|
||||
|
||||
def get_minor_id(self) -> str:
|
||||
return self.ui.minorComboBox.currentData()
|
||||
return self.ui.minorComboBox.currentData()
|
||||
|
||||
def get_influence_id(self) -> str:
|
||||
return self.ui.influenceComboBox.currentData()
|
||||
return self.ui.influenceComboBox.currentData()
|
||||
|
||||
|
||||
class ChoicesDialog(QDialog):
|
||||
def __init__(self, parent = None, *,
|
||||
participants: list,
|
||||
default_participant_id = None,
|
||||
sectors: list,
|
||||
default_priority_id = None,
|
||||
default_secondary_id = None,
|
||||
default_comment = None):
|
||||
def __init__(
|
||||
self,
|
||||
parent=None,
|
||||
*,
|
||||
participants: list,
|
||||
default_participant_id=None,
|
||||
sectors: list,
|
||||
default_priority_id=None,
|
||||
default_secondary_id=None,
|
||||
default_comment=None,
|
||||
):
|
||||
super().__init__(parent)
|
||||
self.ui = Ui_choicesDialog()
|
||||
self.ui.setupUi(self)
|
||||
|
|
@ -598,32 +673,37 @@ class ChoicesDialog(QDialog):
|
|||
self.ui.choiceComment.setPlainText(default_comment)
|
||||
|
||||
def get_participant_id(self) -> str:
|
||||
return self.ui.playerComboBox.currentData()
|
||||
return self.ui.playerComboBox.currentData()
|
||||
|
||||
def get_priority_id(self) -> str:
|
||||
return self.ui.priorityComboBox.currentData()
|
||||
|
||||
return self.ui.priorityComboBox.currentData()
|
||||
|
||||
def get_secondary_id(self) -> str:
|
||||
return self.ui.secondaryComboBox.currentData()
|
||||
return self.ui.secondaryComboBox.currentData()
|
||||
|
||||
def get_comment(self) -> str:
|
||||
return self.ui.choiceComment.toPlainText().strip()
|
||||
return self.ui.choiceComment.toPlainText().strip()
|
||||
|
||||
|
||||
class BattlesDialog(QDialog):
|
||||
def __init__(self, parent = None, *,
|
||||
sectors: list,
|
||||
default_sector_id = None,
|
||||
players: list,
|
||||
default_player_1_id = None,
|
||||
default_player_2_id = None,
|
||||
default_winner_id = None,
|
||||
default_score = None,
|
||||
default_victory_condition = None,
|
||||
default_comment = None):
|
||||
def __init__(
|
||||
self,
|
||||
parent=None,
|
||||
*,
|
||||
sectors: list,
|
||||
default_sector_id=None,
|
||||
players: list,
|
||||
default_player_1_id=None,
|
||||
default_player_2_id=None,
|
||||
default_winner_id=None,
|
||||
default_score=None,
|
||||
default_victory_condition=None,
|
||||
default_comment=None,
|
||||
):
|
||||
super().__init__(parent)
|
||||
self.ui = Ui_battleResultDialog()
|
||||
self.ui.setupUi(self)
|
||||
for sect in sectors:
|
||||
for sect in sectors:
|
||||
self.ui.sectorComboBox.addItem(sect.name, sect.id)
|
||||
select_if_exists(self.ui.sectorComboBox, default_sector_id)
|
||||
self.ui.sectorComboBox.setEnabled(False)
|
||||
|
|
@ -645,16 +725,16 @@ class BattlesDialog(QDialog):
|
|||
self.ui.battleComment.setPlainText(default_comment)
|
||||
|
||||
def get_sector_id(self) -> str:
|
||||
return self.ui.sectorComboBox.currentData()
|
||||
return self.ui.sectorComboBox.currentData()
|
||||
|
||||
def get_player_1_id(self) -> str:
|
||||
return self.ui.player1ComboBox.currentData()
|
||||
|
||||
return self.ui.player1ComboBox.currentData()
|
||||
|
||||
def get_player_2_id(self) -> str:
|
||||
return self.ui.player2ComboBox.currentData()
|
||||
|
||||
return self.ui.player2ComboBox.currentData()
|
||||
|
||||
def get_winner_id(self) -> str:
|
||||
return self.ui.winnerComboBox.currentData()
|
||||
return self.ui.winnerComboBox.currentData()
|
||||
|
||||
def get_score(self) -> str:
|
||||
return self.ui.score.text().strip()
|
||||
|
|
@ -663,4 +743,4 @@ class BattlesDialog(QDialog):
|
|||
return self.ui.victoryCondition.text().strip()
|
||||
|
||||
def get_comment(self) -> str:
|
||||
return self.ui.battleComment.toPlainText().strip()
|
||||
return self.ui.battleComment.toPlainText().strip()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue