-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectModel.py
222 lines (179 loc) · 6.99 KB
/
ProjectModel.py
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# Blending Images
# ------------------------------------------
# Модель данных для работы с проектами
# ------------------------------------------
from PySide6.QtCore import QAbstractListModel, QModelIndex, Qt, Signal, Slot, QPoint, QDir, QUrl
from PySide6.QtGui import QPainter, QColor, QImage
from Database import Database
from misc import FileWorker
class ProjectModel(QAbstractListModel):
error = Signal(str, arguments=['error'])
imgReady = Signal(QUrl, arguments=['file'])
def __init__(self, parent=None):
super().__init__(parent)
self.db = Database('prj')
self.fw = FileWorker("PMFW")
self.data_list = []
self.col1 = Qt.UserRole + 1
self.col2 = Qt.UserRole + 2
self.col3 = Qt.UserRole + 3
self.col4 = Qt.UserRole + 4
self.col5 = Qt.UserRole + 5
self.col6 = Qt.UserRole + 6
self.col7 = Qt.UserRole + 7
self.col8 = Qt.UserRole + 8
self.col9 = Qt.UserRole + 9
self.filter = None
self.loadModel()
# -- перегрузка стандартных функций
@Slot(result=int)
def rowCount(self, parent=QModelIndex):
return len(self.data_list)
def data(self, index, role=Qt.DisplayRole):
row = index.row()
card = self.data_list[row]
if index.isValid():
if role == self.col1:
return card.get('id')
if role == self.col2:
return card.get('name')
if role == self.col3:
return card.get('width')
if role == self.col4:
return card.get('height')
if role == self.col5:
return card.get('rows')
if role == self.col6:
return card.get('columns')
if role == self.col7:
return card.get('file')
if role == self.col8:
return card.get('bg')
if role == self.col9:
return card.get('upd')
return str()
def roleNames(self):
return {
self.col1 : b"id",
self.col2 : b"name",
self.col3 : b"width",
self.col4 : b"height",
self.col5 : b"rows",
self.col6 : b"columns",
self.col7 : b"file",
self.col8 : b"bg",
self.col9 : b"upd",
}
#----------------------------
@Slot()
def loadModel(self):
self.beginResetModel()
self.data_list.clear()
res = self.db.db_get(self.db.TABLE_PROJECT, self.filter)
if res.get('r'):
self.data_list = res.get('data')
else:
self.error.emit(res.get('message'))
self.endResetModel()
# сохранение
# цвет background передается как тип QColor, перед сохранением преобразовываем в HEX
@Slot(dict, result=bool)
def save(self, card :dict):
bg = card['bg']
# for item's generator
rows = int(card['rows'])
columns = int(card['columns'])
create = True
if card['id'] > 0:
create = False
# self.db.db_del(0, self.db.TABLE_ITEMS, filter=card['id'])
#-------
card['bg'] = bg.name()
res = self.db.db_save(card, self.db.TABLE_PROJECT)
if res.get('r'):
self.loadModel()
project_id = int(res.get('id'))
if create:
# ITEMS GENERATION
for r in range(0, rows, 1):
for c in range(0, columns, 1):
d = {
'id': 0,
'project':project_id,
'row':r,
'col':c,
'file':0
}
self.db.db_save(d, self.db.TABLE_ITEMS)
#------
return True
else:
self.error.emit(res.get('message'))
return False
# --удаление элементов
@Slot(result=bool)
def delete(self):
res = self.db.db_del(self.currentID, self.db.TABLE_PROJECT)
if res.get('r'):
self.db.db_del(0, self.db.TABLE_ITEMS, self.currentID)
self.fw.deleleProjectPreview(self.currentID)
self.loadModel()
return True
else:
self.error.emit(res.get('message'))
return False
# -- опреление текущего элемента
@Slot(int)
def setCurrent(self, i: int):
self.currentID = self.data_list[i].get('id')
self.currentCard = self.data_list[i]
self.makeImage()
# -- получить элемент по индексу и названию
@Slot(int, str, result=str)
def get(self, index:int, item:str):
return str(self.data_list[index].get(item))
# -- установить фильтр
@Slot(str)
def setFilter(self, f:str):
self.filter = f
self.loadModel()
# -- создать превью
def makeImage(self):
self.fw.removePreview()
card = self.currentCard
map_items = []
x = self.db.db_get(self.db.TABLE_ITEMS, card.get('id'))
if x['r']:
_data = x['data']
for r in range (0, card.get('rows'), 1):
_row = {}
for item in _data:
if item['row'] == r:
_id = item['id']
_file = item['file']
if _file == 0:
_type = False
_display = card.get('bg')
else:
_type = True
_display = self.fw.getUrl(self.db.getFile(_file))
_row[item['col']] = {'id':_id, 'file':_file, 'displayType':_type, 'display': _display, 'selected': False}
map_items.append(_row)
# Создание изображения размером, соответствующим вашему проекту
image = QImage(card.get('columns') * 76, card.get('rows') * 85, QImage.Format_RGB32)
image.fill(QColor(card.get('bg'))) # Заливка фона цветом проекта
painter = QPainter(image)
for r in range(0, card.get('rows')):
data_row = map_items[r]
for c in range(0, card.get('columns')):
card_item = data_row.get(c)
x = c * 76
y = r * 85
if card_item['displayType']:
img = QImage()
img.load(self.fw.getPathByURL(card_item['display']))
painter.drawImage(QPoint(x, y), img)
painter.end()
r = image.save("preview" + str(self.currentID) + ".png", "PNG")
pd = QDir(QDir.toNativeSeparators(QDir.currentPath() + "/preview" + str(self.currentID) + ".png"))
self.imgReady.emit(self.fw.getUrl(pd.path()))