-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomeFrame.py
More file actions
129 lines (104 loc) · 4.67 KB
/
HomeFrame.py
File metadata and controls
129 lines (104 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from PySide6.QtCore import Slot, SIGNAL
from PySide6.QtWidgets import (QWidget, QListWidget, QGridLayout, QPushButton, QFileDialog)
from os import path
import pickle as pk
import MW
import Perso_class as Pc
class HomeFrame(QWidget):
""" Widget to select and manage characters """
def __init__(self):
QWidget.__init__(self)
self.grid = QGridLayout(self)
self.setLayout(self.grid)
self.Char_create = QPushButton(self.tr("Créer un personnage")) # bouton de création de personnage
self.Char_import = QPushButton(self.tr("Importer un personnage")) # bouton d'import de personnage
self.Char_modif = QPushButton(self.tr("Consulter le personnage")) # bouton de modification de personnage
self.Char_export = QPushButton(self.tr("Exporter le personnage")) # bouton d'exportation de personnage
self.Char_list = QListWidget() # liste de choix du personnage à consulter
self.grid.addWidget(self.Char_create, 0, 1)
self.grid.addWidget(self.Char_import, 1, 1)
self.grid.addWidget(self.Char_modif, 2, 1)
self.grid.addWidget(self.Char_export, 3, 1)
self.grid.addWidget(self.Char_list, 0, 0, 4, 1)
self.connect(self.Char_create, SIGNAL("clicked()"), self.create_char)
self.connect(self.Char_import, SIGNAL("clicked()"), self.import_char)
self.connect(self.Char_modif, SIGNAL("clicked()"), self.modify_char)
self.connect(self.Char_export, SIGNAL("clicked()"), self.export_char)
self.connect(self.Char_list, SIGNAL("itemDoubleClicked(QListWidgetItem *)"), self.modify_char)
def charlist_reload(self):
"""
Method called to refresh the ListWidget
:return: None
"""
self.Char_list.clear()
for i in self.get_characlist():
self.Char_list.addItem(i.get_name())
@Slot()
def create_char(self):
"""
Slot called to open the pannel for creating characters
:return: None
"""
self.parent().goto_create()
@Slot()
def export_char(self):
"""
Slot called to export characters created to share them with other users
:return: None
"""
if self.Char_list.currentRow() != -1:
perso = self.get_characlist()[self.Char_list.currentRow()]
filename = QFileDialog.getSaveFileName(self, self.tr("Choisissez le fichier de personnage"),
path.dirname(__file__) + "/Personnages/" + perso.get_name(),
self.tr("Tous fichiers (*)"))
if filename[0]:
with open(filename[0], "wb") as fichier:
# on enregistre le personnage
pk.Pickler(fichier).dump(perso)
def get_characlist(self):
"""
Method to get the list of characters from the main window*
:return: list of Pc.player
"""
return self.parent().get_characlist()
@Slot()
def import_char(self):
"""
Slot called to import characters created by other users
:return: None
"""
# on demande le fichier de personnage à importer
filenames = QFileDialog.getOpenFileNames(self, self.tr("Choisissez le fichier de personnage"),
path.dirname(__file__), self.tr("Tous fichiers (*)"))
if filenames[0]:
perso_list = []
for filename in filenames[0]:
with open(filename, "rb") as fichier:
# if file contains a character, it is added to the list in order to be imported
perso = pk.Unpickler(fichier).load()
if type(perso) == Pc.player:
perso_list.append(perso)
self.parent().import_char(perso_list)
self.charlist_reload()
@Slot()
def modify_char(self):
"""
Slot called to display the full characteristics of the selected character
:return: None
"""
if self.Char_list.currentRow() != -1:
self.set_selectedchar(self.Char_list.currentRow()) # indexes in list and ListWidget are equal
self.parent().goto_modify()
def parent(self) -> MW.UIWindow:
"""
Method called to get the parent widget (the main window)
:return: the reference to the parent
"""
return QWidget.parent(self)
def set_selectedchar(self, number: int):
"""
Method called to set the selected character for display, amongthe list
:param number: index of the character to load
:return: None
"""
self.parent().set_selectedchar(number)