-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWidget.py
137 lines (121 loc) · 6.63 KB
/
MainWidget.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
from PyQt5 import QtCore, QtGui, QtWidgets
from urllib.request import urlopen
from mainwindow import Ui_MainWindow
from tabs import vars
from tabs.__init__ import Pyroman
import ui.header, ui.tableWidget, ui.label
class MainWidget(QtWidgets.QMainWindow):
def __init__(self):
super(MainWidget, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.preview_label = ui.label.MangaPreviewLabel()
self.initialize()
self.controller = Pyroman(self)
self.event_connect()
self.tab_list = ['Manga', 'Image', 'Magazine']
self.current_tab = self.tab_list[0]
self.button_mapper = {}
self._magazine_checkboxes = []
def initialize(self):
self.ui.edit_source.setText(vars.default_source_path)
self.ui.edit_dest.setText(vars.default_manga_dest_path)
self.ui.tableWidgetManga = ui.tableWidget.tableWidgetManga(self.ui.tab)
self.ui.tableWidgetManga.scroll_preview_func = self.preview_label.scroll_preview
self.ui.horizontalLayout.removeWidget(self.ui.tableWidgetImage)
self.ui.tableWidgetImage = ui.tableWidget.tableWidgetImage(self.ui.horizontalLayoutWidget)
self.ui.horizontalLayout.insertWidget(0, self.ui.tableWidgetImage)
self.ui.tableWidgetMagazine.verticalHeader().hide()
self.ui.tableWidgetMagazine.horizontalHeader().hide()
self.ui.label_work_preview.setAlignment(QtCore.Qt.AlignCenter)
def event_connect(self):
self.ui.btn_parse.clicked.connect(lambda: self.controller.on_parse(
self.ui.edit_source.text(), self.ui.edit_dest.text(), self.ui.tabWidget.currentIndex()
))
#self.ui.btn_move_all.clicked.connect(self.controller.on_move_all)
self.ui.pushButton_gen.clicked.connect(lambda: self.controller.magazine_tab.on_create_multiple_folders(
self._magazine_checkboxes, self.ui.edit_dest.text()
))
self.ui.tableWidgetManga.cellDoubleClicked.connect(self.controller.on_open_folder)
self.ui.tableWidgetManga.cellEntered.connect(self.controller.on_preview_manga)
self.ui.tableWidgetManga.cellExited.connect(self.controller.on_preview_mange_terminate)
self.ui.tableWidgetManga.keyPressEvent = self.controller.table_key_event
self.ui.tableWidgetImage.cellPressed.connect(self.controller.on_show_image)
self.ui.tabWidget.currentChanged.connect(self.on_change_tab)
self.ui.tableWidgetMagazine.itemSelectionChanged.connect(self.on_work_selected_in_magazine)
self.ui.pushButtonMove.clicked.connect(lambda: self.controller.on_move_image(self.ui.tableWidgetImage.selectedItems()))
def set_tableWidget_items(self, table_data):
(current_table, func) = {
self.tab_list[0]: (self.ui.tableWidgetManga,
lambda self, table_data: self._set_manga_table(self.ui.tableWidgetManga, table_data)),
self.tab_list[1]: (self.ui.tableWidgetImage,
lambda self, table_data: self._set_image_table(self.ui.tableWidgetImage, table_data)),
self.tab_list[2]: (self.ui.tableWidgetMagazine,
lambda self, table_data: self._set_magazine_table(self.ui.tableWidgetMagazine, table_data))
}.get(self.current_tab)
if not current_table:
return
current_table.setRowCount(len(table_data))
func(self, table_data)
current_table.update()
def _set_manga_table(self, current_table, table_data):
def create_move_button():
button = QtWidgets.QPushButton(current_table)
button.setText('Move')
self.button_mapper[button] = row_idx
button.clicked.connect(self.controller.manga_tab.move_to_dest)
return button
self.button_mapper = {}
for row_idx, row in enumerate(table_data):
for column_idx, cell in enumerate(row):
current_table.setItem(row_idx, column_idx, QtWidgets.QTableWidgetItem(cell))
duplicated = row[2]
if not duplicated:
button = create_move_button()
current_table.setCellWidget(row_idx, 3, button)
def _set_image_table(self, current_table, table_data):
for row_idx, row in enumerate(table_data):
for column_idx, cell in enumerate(row):
current_table.setItem(row_idx, column_idx, QtWidgets.QTableWidgetItem(cell))
def _set_magazine_table(self, current_table, table_data):
def create_checkbox_item(text):
cb = QtWidgets.QCheckBox(text)
self._magazine_checkboxes.append(cb)
cb.stateChanged.connect(self.on_magazine_checked_state_changed)
return cb
self._magazine_checkboxes = []
for row_idx, row in enumerate(table_data):
for column_idx, cell in enumerate(row):
if column_idx == 0:
current_table.setCellWidget(row_idx, column_idx, create_checkbox_item(cell))
else:
current_table.setItem(row_idx, column_idx, QtWidgets.QTableWidgetItem(cell))
header = ui.header.MagazineHeader(self.ui.tableWidgetMagazine)
header.check_all_delegate = self.on_check_all_magazine
self.ui.tableWidgetMagazine.setHorizontalHeader(header)
self.ui.tableWidgetMagazine.setSelectionBehavior(QtWidgets.QTableWidget.SelectRows)
w = self.ui.tableWidgetMagazine.width()
self.ui.tableWidgetMagazine.setColumnWidth(0, w * 4 / 10)
self.ui.tableWidgetMagazine.setColumnWidth(1, w * 5 / 10)
def on_change_tab(self, idx):
self.ui.edit_dest.setText(vars.dest_path_dict[idx])
self.ui.edit_source.setText(vars.source_path_dict[idx])
self.current_tab = self.tab_list[idx]
def on_check_all_magazine(self, state):
for cb in self._magazine_checkboxes:
cb.setCheckState(state)
def on_magazine_checked_state_changed(self, state):
if state == 0:
self.ui.tableWidgetMagazine.horizontalHeader().is_on = False
self.ui.tableWidgetMagazine.horizontalHeader().updateSection(0)
def on_work_selected_in_magazine(self):
row_idx = self.ui.tableWidgetMagazine.selectedItems()[0].row()
image_url = self.controller.magazine_tab.get_image_url(row_idx)
data = urlopen(image_url).read()
pixmap = QtGui.QPixmap()
pixmap.loadFromData(data)
self.ui.label_work_preview.setPixmap(pixmap)
def going_to_show_image(self, image_path):
pixmap = QtGui.QPixmap(image_path)
scaled_pixmap = pixmap.scaled(self.ui.label_image.size(), QtCore.Qt.KeepAspectRatio)
self.ui.label_image.setPixmap(scaled_pixmap)