-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome.py
357 lines (311 loc) · 16.3 KB
/
home.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# This Python file uses the following encoding: utf-8
import os, sys
from typing import Iterable
import PyQt5.QtCore
import DockWidget
import home_ui
from common.dialog import *
from common.lang import *
from common.books import *
from common import bdd
from CentralBlockTable import *
from InfoPanel import *
from SortingBlockTree import *
from settings import *
from metadata import *
import empty_book
class HomeWindow(home_ui.Ui_home_window, HomeWindowCentralBlock, HomeWindowInfoPanel, HomeWindowSortingBlockTree, MetadataWindow):
def __init__(self, database: bdd.BDD, translation: Lang, argv: list, env_vars: dict):
self.parent = None
super(HomeWindow, self).__init__(self.parent)
self.setupUi(self)
self.setWindowModality(QtCore.Qt.ApplicationModal)
# load vars and base objects
self.app_directory = app_directory
self.currentBook = ''
self.env_vars = env_vars
self.BDD = database
self.lang = translation
self.tools = env_vars['tools']
self.env_vars = self.vars = env_vars['vars']
self.argv = argv
# load memorised sizes
size_tx = self.BDD.get_param('library/windowSize')
pos_tx = self.BDD.get_param('library/windowPos')
size1 = [150, 150]
size2 = [150, 150]
area1 = area2 = PyQt5.QtCore.Qt.LeftDockWidgetArea
try:
size1 = eval(self.BDD.get_param('library/BlockSize_info_block', '[150, 150]'))
size2 = eval(self.BDD.get_param('library/BlockSize_sorting_block', '[150, 150]'))
area1 = int(self.BDD.get_param('library/BlockArea_info_block', PyQt5.QtCore.Qt.LeftDockWidgetArea))
area2 = int(self.BDD.get_param('library/BlockArea_sorting_block', PyQt5.QtCore.Qt.LeftDockWidgetArea))
except Exception as error:
traceback.print_exc()
# load window size
if size_tx is not None and size_tx != '':
size = eval(size_tx)
self.resize(size[0], size[1])
# load window position
if pos_tx is not None and pos_tx != '':
pos = eval(pos_tx)
self.move(pos[0], pos[1])
self.pos()
# load parameters
self.app_lang = None
self.app_style = None
list_test_param = [
{'parameter': 'defaultCover/background', 'default': self.env_vars["default_cover"]["background"]},
{'parameter': 'defaultCover/pattern', 'default': self.env_vars["default_cover"]["pattern"]},
{'parameter': 'defaultCover/pattern_color', 'default': self.env_vars["default_cover"]["pattern_color"]},
{'parameter': 'defaultCover/title', 'default': self.env_vars["default_cover"]["title"]},
{'parameter': 'defaultCover/series', 'default': self.env_vars["default_cover"]["series"]},
{'parameter': 'defaultCover/authors', 'default': self.env_vars["default_cover"]["authors"]},
{'parameter': 'library/lastOpenDir', 'default': app_directory},
{'parameter': 'import_file_separator', 'default': self.env_vars["import_file_separator"]},
{'parameter': 'import_file_template', 'default': self.env_vars['import_file_template']['default']},
{'parameter': 'style', 'default': self.env_vars["default_style"], 'var': 'app_style'},
{'parameter': 'lang', 'default': self.env_vars["default_language"], 'var': 'app_lang'}
]
for obj in list_test_param:
var = self.BDD.get_param(obj['parameter'])
if var is None or var == '':
self.BDD.set_param(obj['parameter'], obj['default'])
var = obj['default']
if 'var' in obj:
if obj['var'] == 'app_style':
self.app_style = var
if obj['var'] == 'app_lang':
self.app_lang = var
self.lang.set_lang(self.app_lang)
self.set_style()
self.set_localisation()
self.set_info_panel()
self.load_books(self.BDD.get_books())
self.header_block_btn_add_book.clicked.connect(self.header_block_btn_add_book_clicked)
self.header_block_btn_create_book.clicked.connect(self.header_block_btn_create_book_clicked)
self.header_block_btn_settings.clicked.connect(self.header_block_btn_settings_clicked)
self.header_block_btn_del_book.clicked.connect(self.header_block_btn_del_book_clicked)
self.central_block_table_define_slots()
self.sorting_block_tree_init()
self.sorting_block_tree_load_data()
self.sorting_block_tree_set_filter('all')
# self.sorting_block_search_area = QtWidgets.QScrollArea()
# self.sorting_block_search_area_contents = QtWidgets.QWidget()
self.info_block.setBDD(self.BDD)
self.sorting_block.setBDD(self.BDD)
self.addDockWidget(area2, self.sorting_block, PyQt5.QtCore.Qt.Vertical)
self.addDockWidget(area1, self.info_block, PyQt5.QtCore.Qt.Vertical)
self.resizeDocks(
(self.sorting_block, self.info_block),
(size2[0], size1[0]),
PyQt5.QtCore.Qt.Horizontal
)
self.sorting_block.setMinimumHeight(size2[1])
self.info_block.setMinimumHeight(size1[1])
def resizeEvent(self, a0: QtGui.QResizeEvent) -> None:
size = self.size()
tx = [size.width(), size.height()].__str__()
self.BDD.set_param('library/windowSize', tx)
def moveEvent(self, a0: QtGui.QMoveEvent) -> None:
pos = self.pos()
tx = [pos.x(), pos.y()].__str__()
self.BDD.set_param('library/windowPos', tx)
def header_block_btn_add_book_clicked(self):
"""
Slot for click on the Add Book Button
:return: void
"""
try:
# load parameters for file import
file_name_template = self.BDD.get_param('import_file_template')
file_name_separator = self.BDD.get_param('import_file_separator')
options = QFileDialog.Options()
# options |= QFileDialog.DontUseNativeDialog
list_files, _ = QFileDialog.getOpenFileNames(
self, self.lang['Library']['AddBookWindowTitle'], self.BDD.get_param('library/lastOpenDir'),
"eBook (*.epub *.epub2 *.epub3 *.cbz *.cbr *.pdf *.mobi);;Texte (*.txt *.doc *.docx *.rtf)",
options=options
)
if len(list_files) > 0:
selected_directory = list_files[0].replace('/', os.sep)
selected_index = selected_directory.rindex(""+os.sep)
selected_directory = selected_directory[0:selected_index]
self.BDD.set_param('library/lastOpenDir', selected_directory)
for file in list_files:
# Call booksTools.insert_book
insert_book(self.BDD, file_name_template, file_name_separator, file)
self.central_block_table.clearSelection()
self.sorting_block_tree_set_filter(self.sorting_block_tree_actual_filter)
except Exception:
traceback.print_exc()
def header_block_btn_create_book_clicked(self):
"""
Slot for click on the Add Book Button
:return: void
"""
try:
empty_ui = empty_book.EmptyBookWindow(self, self.BDD)
ret = empty_ui.open_exec()
if ret is not None:
try:
rmDir(app_directory+os.sep+'tmp')
os.makedirs(app_directory + os.sep + 'tmp')
except Exception:
""
if ret['format'] == 'EPUB':
vol = ret['vol']
cover_style = {
"background": self.BDD.get_param('defaultCover/background'),
"pattern": self.BDD.get_param('defaultCover/pattern'),
"pattern_color": self.BDD.get_param('defaultCover/pattern_color'),
"title": self.BDD.get_param('defaultCover/title'),
"series": self.BDD.get_param('defaultCover/series'),
"authors": self.BDD.get_param('defaultCover/authors'),
}
file = create_epub(
ret['name'], ret['authors'], ret['series'], vol,
file_name_template=self.BDD.get_param('import_file_template'), style=cover_style
)
# load parameters for file import
file_name_template = self.BDD.get_param('import_file_template')
file_name_separator = self.BDD.get_param('import_file_separator')
insert_book(self.BDD, file_name_template, file_name_separator, file)
self.central_block_table.clearSelection()
self.sorting_block_tree_set_filter(self.sorting_block_tree_actual_filter)
except Exception:
traceback.print_exc()
def header_block_btn_settings_clicked(self):
"""
Slot for click on the Settings Button
:return: void
"""
try:
dialog = SettingsWindow(self, self.BDD)
ret = dialog.open_exec()
try:
if ret is not None:
self.app_lang = self.BDD.get_param('lang')
self.app_style = self.BDD.get_param('style')
self.set_localisation()
self.set_style()
if "settings" in self.argv:
self.close()
except Exception:
traceback.print_exc()
except Exception:
traceback.print_exc()
def header_block_btn_del_book_clicked(self):
"""
Slot for click on the Delete book Button
:return: void
"""
try:
if len(self.central_block_table.selectedItems()) > 0:
ret = WarnDialogConfirm(
self.lang['Library']['DialogConfirmDeleteBookWindowTitle'],
self.lang['Library']['DialogConfirmDeleteBookWindowText'],
self.lang['Library']['DialogConfirmDeleteBookBtnYes'],
self.lang['Library']['DialogConfirmDeleteBookBtnNo'],
self
)
if ret is True:
items = self.central_block_table.selectedItems()
self.central_block_table.clearSelection()
for item in items:
if item.column() == 0:
book_id = item.data(99)
for file in self.BDD.get_books(book_id)[0]['files']:
try:
os.remove(file['link'].replace('\\', '/').replace('/', os.sep))
except Exception:
traceback.print_exc()
self.BDD.delete_book(book_id)
# Cleanup all empty folder in data folder
clean_dir('./data')
self.sorting_block_tree_set_filter(self.sorting_block_tree_actual_filter)
self.set_info_panel(None)
except Exception:
traceback.print_exc()
def set_localisation(self):
"""
translate window text with the content of the lang object
:return: void
"""
# Titre fenêtre
self.setWindowTitle(self.lang['Library/WindowTitle'])
# Titres blocks
self.header_block.setWindowTitle(self.lang['Library']['blockHeaderTitle'])
self.sorting_block.setWindowTitle(self.lang['Library']['blockSortTitle'])
self.info_block.setWindowTitle(self.lang['Library']['blockInfoTitle'])
# Boutons du bandeau
self.header_block_btn_add_book.setToolTip(self.lang['Library']['HeaderBlockBtnAddBook'])
self.header_block_btn_create_book.setToolTip(self.lang['Library']['HeaderBlockBtnCreateBook'])
self.header_block_btn_del_book.setToolTip(self.lang['Library']['HeaderBlockBtnDelBook'])
self.header_block_btn_settings.setToolTip(self.lang['Library']['HeaderBlockBtnSettings'])
# Panneau de gauche
self.sorting_block_tree.topLevelItem(0).setText(0, self.lang['Library']['SortingBlockTreeAll'])
self.sorting_block_tree.topLevelItem(0).setText(1, 'all')
self.sorting_block_tree.topLevelItem(1).setText(0, self.lang['Library']['SortingBlockTreeAuthors'])
self.sorting_block_tree.topLevelItem(1).setText(1, 'authors')
self.sorting_block_tree.topLevelItem(2).setText(0, self.lang['Library']['SortingBlockTreeSeries'])
self.sorting_block_tree.topLevelItem(2).setText(1, 'series')
self.sorting_block_tree.topLevelItem(3).setText(0, self.lang['Library']['SortingBlockTreeTags'])
self.sorting_block_tree.topLevelItem(3).setText(1, 'tags')
self.sorting_block_search_label.setText(self.lang['Library']['SortingBlockSearchLabel'])
# Panneau central
self.central_block_table.setColumnCount(6)
item = QtWidgets.QTableWidgetItem()
item.setText(self.lang['Library']['CentralBlockTableTitle'])
self.central_block_table.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
item.setText(self.lang['Library']['CentralBlockTableAuthors'])
self.central_block_table.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
item.setText(self.lang['Library']['CentralBlockTableSeries'])
self.central_block_table.setHorizontalHeaderItem(2, item)
item = QTableAltItem()
item.lock(True)
item.setText(self.lang['Library']['CentralBlockTableTags'])
self.central_block_table.setHorizontalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
item.setText(self.lang['Library']['CentralBlockTableAdded'])
self.central_block_table.setHorizontalHeaderItem(4, item)
item = QtWidgets.QTableWidgetItem()
item.setText(self.lang['Library']['CentralBlockTableModified'])
self.central_block_table.setHorizontalHeaderItem(5, item)
# Panneau de info
self.info_block_title_label.setText(self.lang['Library']['InfoBlockTitleLabel'])
self.info_block_serie_label.setText(self.lang['Library']['InfoBlockSerieLabel'])
self.info_block_authors_label.setText(self.lang['Library']['InfoBlockAuthorsLabel'])
self.info_block_file_formats_label.setText(self.lang['Library']['InfoBlockFileFormatsLabel'])
self.info_block_size_label.setText(self.lang['Library']['InfoBlockSizeLabel'])
def set_style(self):
QDockStyle = get_style_var(self.app_style, 'QDockWidget').replace('../', './')
self.setStyleSheet(get_style_var(self.app_style, 'QMainWindow') + QDockStyle)
self.header_block.setStyleSheet(QDockStyle)
self.header_block_contents2.setStyleSheet(get_style_var(self.app_style, 'QMainWindow'))
self.sorting_block_contents.setStyleSheet(get_style_var(self.app_style, 'QMainWindow'))
self.sorting_block_search_area.setStyleSheet(get_style_var(self.app_style, "sorting_block_search_area"))
self.info_block_contents.setStyleSheet(get_style_var(self.app_style, 'QMainWindow'))
self.sorting_block_tree.setStyleSheet(get_style_var(self.app_style, 'fullTreeView') + "QTreeView{ background:transparent; }")
# self.central_block_table = QTableWidget()
self.central_block_table.horizontalHeader().setStyleSheet(get_style_var(self.app_style, 'QTableWidget'))
self.central_block_table.setStyleSheet(get_style_var(self.app_style, 'QTableWidget'))
self.central_block_table.horizontalHeader().setSortIndicatorShown(True)
icon_names_list = ['book_add', 'book_new', 'book_del', 'settings', 'search']
icon_dir = {}
for name in icon_names_list:
icon_dir[name] = QtGui.QIcon()
icon_dir[name].addPixmap(
QtGui.QPixmap(
get_style_var(self.app_style, 'icons/'+name)
.replace('/', os.sep)
),
QtGui.QIcon.Normal, QtGui.QIcon.Off
)
self.header_block_btn_add_book.setIcon(icon_dir['book_add'])
self.header_block_btn_create_book.setIcon(icon_dir['book_new'])
self.header_block_btn_del_book.setIcon(icon_dir['book_del'])
self.header_block_btn_settings.setIcon(icon_dir['settings'])
self.sorting_block_search_button.setIcon(icon_dir['search'])