-
Notifications
You must be signed in to change notification settings - Fork 2
/
file.py
116 lines (93 loc) · 3.2 KB
/
file.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
# -*- coding: utf-8 -*-
import enum
import os
from PySide6.QtWidgets import QCheckBox
class FileType(enum.Enum):
SrcFile = 0
DstFile = 1
SrcDir = 2
DstDir = 3
class FileManager:
def __init__(self):
self.file_id = 0
self.all_files = []
self.checked_files = []
self.id2file = {}
self.file2id = {}
self.widget2id = {}
self.src2dst = {}
def gen_id(self):
self.file_id += 1
return self.file_id
def get_file_id(self, file):
return self.file2id.get(file)
def get_widget_id(self, widget):
return self.widget2id.get(widget)
def get_check_widget(self, file_id):
check_widget = None
for (k,v) in self.widget2id.items():
if v == file_id and isinstance(k, QCheckBox):
check_widget = k
break
return check_widget
def get_file(self, widget, file_type):
result = None
file_id = self.get_widget_id(widget)
if file_id:
file = self.id2file.get(file_id)
if file_type == FileType.SrcFile:
result = file
elif file_type == FileType.DstFile:
result = self.src2dst.get(file)
elif file_type == FileType.SrcDir:
result = os.path.dirname(file)
elif file_type == FileType.DstDir:
dst_file = self.src2dst.get(file)
if dst_file:
result = os.path.dirname(dst_file)
return result
def remove_file(self, widget):
file_id = self.get_widget_id(widget)
if file_id:
del self.widget2id[widget]
file = self.id2file.get(file_id)
if file:
del self.id2file[file_id]
del self.file2id[file]
self.all_files.remove(file)
if file in self.checked_files:
self.checked_files.remove(file)
if file in self.src2dst:
del self.src2dst[file]
def add_widget(self, widget, file_id):
self.widget2id[widget] = file_id
def add_file(self, file_id, file):
self.id2file[file_id] = file
self.file2id[file] = file_id
self.all_files.append(file)
self.checked_files.append(file)
def add_convert(self, src_file, dst_file):
self.src2dst[src_file] = dst_file
def set_checked(self, widget, checked):
pdf_file = self.get_file(widget, FileType.SrcFile)
if checked:
if pdf_file not in self.checked_files:
self.checked_files.append(pdf_file)
else:
if pdf_file in self.checked_files:
self.checked_files.remove(pdf_file)
def set_all_checked(self, widgets, checked):
self.checked_files = []
if checked:
for widget in widgets:
pdf_file = self.get_file(widget, FileType.SrcFile)
self.checked_files.append(pdf_file)
def exists(self, file):
return file in self.all_files
def clear(self):
self.all_files = []
self.checked_files = []
self.id2file = {}
self.file2id = {}
self.widget2id = {}
self.src2dst = {}