-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathhistory_object.py
295 lines (242 loc) · 11.7 KB
/
history_object.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
import customtkinter as ctk
from tkinter import PhotoImage
import tkinter as tk
from settings import AppearanceSettings
from services import ThemeManager
from utils import ImageUtility, FileUtility
from typing import Callable
from PIL import Image
import webbrowser
import os
class HistoryObject(ctk.CTkFrame):
def __init__(
self,
master: ctk.CTkFrame = None,
width: int = 130,
no: int = None,
channel: str = None,
title: str = None,
url: str = None,
thumbnail_path_normal: str = None,
thumbnail_path_hover: str = None,
add_to_download_callback: Callable = None,
remove_callback: Callable = None,
download_date: str = None):
super().__init__(master=master, width=width, border_width=1)
self.thumbnail_button: tk.Button = None
self.label_frame: ctk.CTkFrame = None
self.title_label: ctk.CTkLabel = None
self.channel_label: ctk.CTkLabel = None
self.download_date_label: ctk.CTkLabel = None
self.remove_callback: Callable = remove_callback
self.width = width
self.height = width / 16 * 9 + 15 * 3 + 20
self.no = no
self.channel = channel
self.title = title
self.url = url
self.thumbnail_path_normal = thumbnail_path_normal
self.thumbnail_path_hover = thumbnail_path_hover
self.download_date = download_date
self.thumbnail_normal: PhotoImage = None
self.thumbnail_hover: PhotoImage = None
self.default_thumbnail_used = False
self.add_to_download_callback = add_to_download_callback
self.create_widgets()
self.place_widgets()
self.set_data()
self.set_widgets_sizes()
self.set_widgets_fonts()
self.set_widgets_accent_color()
self.tk_widgets_colors()
self.set_widgets_colors()
self.bind_widgets_events()
ThemeManager.register_widget(self)
def get_resized_thumbnail(self, thumbnail_path: str):
thumbnail_size_for_video_history_object = (
int(self.width) - 5,
int((self.width/ 16*9))
)
thumbnail_image = Image.open(thumbnail_path)
thumbnail_image = ImageUtility.resize_image(
image=thumbnail_image,
new_size=thumbnail_size_for_video_history_object
)
thumbnail_path = FileUtility.get_available_file_name("temp\\thumbnails\\history.png")
thumbnail_image.save(FileUtility.get_available_file_name(thumbnail_path))
return thumbnail_path
def get_default_thumbnail(self):
...
def configure_default_thumbnails(self):
...
def set_data(self):
if os.path.exists(self.thumbnail_path_normal) and self.thumbnail_path_normal != "":
self.thumbnail_normal = PhotoImage(file=self.get_resized_thumbnail(self.thumbnail_path_normal))
self.thumbnail_hover = PhotoImage(file=self.get_resized_thumbnail(self.thumbnail_path_hover))
else:
self.default_thumbnail_used = True
self.get_default_thumbnail()
self.thumbnail_button.configure(image=self.thumbnail_normal)
self.title_label.configure(text=self.title)
self.channel_label.configure(text=self.channel)
self.download_date_label.configure(text=self.download_date)
def bind_widgets_events(self):
def on_mouse_enter_self(event):
try:
self.configure(border_color=AppearanceSettings.settings["root"]["accent_color"]["hover"])
self.thumbnail_button.configure(image=self.thumbnail_hover)
except:
pass
def on_mouse_leave_self(event):
try:
self.configure(border_color=AppearanceSettings.settings["root"]["accent_color"]["normal"])
self.thumbnail_button.configure(image=self.thumbnail_normal)
except:
pass
self.bind("<Enter>", on_mouse_enter_self)
self.bind("<Leave>", on_mouse_leave_self)
self.thumbnail_button.bind("<Enter>", on_mouse_enter_self)
self.thumbnail_button.bind("<Leave>", on_mouse_leave_self)
self.title_label.bind("<Enter>", on_mouse_enter_self)
self.title_label.bind("<Leave>", on_mouse_leave_self)
self.channel_label.bind("<Enter>", on_mouse_enter_self)
self.channel_label.bind("<Leave>", on_mouse_leave_self)
self.download_date_label.bind("<Enter>", on_mouse_enter_self)
self.download_date_label.bind("<Leave>", on_mouse_leave_self)
def on_mouse_enter_remove_btn(_event):
try:
self.remove_btn.configure(
fg_color=AppearanceSettings.settings["video_object"]["error_color"]["hover"],
text_color=AppearanceSettings.settings["video_object"]["remove_btn_text_color"]["hover"]
)
except Exception as error:
pass
def on_mouse_leave_remove_btn(_event):
try:
self.remove_btn.configure(
fg_color=AppearanceSettings.settings["video_object"]["error_color"]["normal"],
text_color=AppearanceSettings.settings["video_object"]["remove_btn_text_color"]["normal"]
)
except Exception as error:
pass
self.remove_btn.bind("<Enter>", on_mouse_enter_remove_btn)
self.remove_btn.bind("<Leave>", on_mouse_leave_remove_btn)
def create_widgets(self):
self.thumbnail_button = tk.Button(master=self, text="", relief="sunken", bd=0, cursor="hand2", command=lambda: webbrowser.open(self.url))
self.remove_btn = ctk.CTkButton(master=self, text="X", command=self.kill, hover=False, corner_radius=4)
self.label_frame = ctk.CTkFrame(master=self)
self.title_label = ctk.CTkLabel(master=self.label_frame, text="", justify="left", anchor="w")
self.channel_label = ctk.CTkLabel(master=self.label_frame, text="", justify="left", anchor="w")
self.download_date_label = ctk.CTkLabel(master=self.label_frame, text="", justify="left", anchor="e")
self.download_btn = ctk.CTkButton(master=self.label_frame, text="Download", command=lambda: self.add_to_download_callback(self.url))
def place_widgets(self):
scale = AppearanceSettings.settings["scale_r"]
self.remove_btn.place(x=self.width-2, y=5 , anchor="ne")
self.thumbnail_button.place(x=1, y=3)
y = (self.width) / 16 * 9 + 10
self.label_frame.place(x=4, y=y)
y=0
self.title_label.place(x=0, y=y)
self.channel_label.place(x=0, y=(y + 15) * scale)
self.download_date_label.place(x=0, y=(y + 30) * scale)
self.download_btn.place(x=0, y=(y + 45) * scale)
def set_widgets_colors(self):
"""Set colors for widgets."""
self.configure(fg_color=AppearanceSettings.settings["video_object"]["fg_color"]["normal"])
self.label_frame.configure(fg_color=AppearanceSettings.settings["video_object"]["fg_color"]["normal"])
self.remove_btn.configure(
fg_color=AppearanceSettings.settings["video_object"]["error_color"]["normal"],
text_color=AppearanceSettings.settings["video_object"]["remove_btn_text_color"]["normal"]
)
def tk_widgets_colors(self):
self.thumbnail_button.configure(
bg=ThemeManager.get_color_based_on_theme_mode(
AppearanceSettings.settings["video_object"]["fg_color"]["normal"]
),
disabledforeground=ThemeManager.get_color_based_on_theme_mode(
AppearanceSettings.settings["video_object"]["text_color"]["normal"]
),
activebackground=ThemeManager.get_color_based_on_theme_mode(
AppearanceSettings.settings["video_object"]["fg_color"]["normal"]
)
)
def update_widgets_colors(self):
"""Update colors for widgets."""
self.tk_widgets_colors()
if self.default_thumbnail_used:
self.configure_default_thumbnails()
def set_widgets_accent_color(self):
"""Set accent color for widgets."""
self.configure(border_color=AppearanceSettings.settings["root"]["accent_color"]["normal"])
self.download_btn.configure(
fg_color=AppearanceSettings.settings["root"]["accent_color"]["normal"],
hover_color=AppearanceSettings.settings["root"]["accent_color"]["hover"]
)
def update_widgets_accent_color(self):
"""Update accent color for widgets."""
self.set_widgets_accent_color()
def set_widgets_sizes(self):
"""Set sizes for widgets."""
scale = AppearanceSettings.settings["scale_r"]
width = self.width
height = 15 * scale
self.remove_btn.configure(width=20 * scale, height=20 * scale, border_spacing=0)
self.title_label.configure(height=height, width=width - 8)
self.channel_label.configure(height=height, width=width - 8)
self.download_date_label.configure(height=height, width=width - 8)
self.download_btn.configure(
width = width - 8,
height = 20 * scale
)
self.label_frame.configure(width=width-8, height=65*scale)
widget_height = ((self.width/ 16*9)) + (3 * (15 * scale)) + (20 * scale) + 14
self.configure(
width = self.width,
height = widget_height
)
def set_widgets_fonts(self):
"""Set fonts for widgets."""
scale = AppearanceSettings.settings["scale_r"]
self.remove_btn.configure(font=("arial", 10 * scale, "bold"))
self.title_label.configure(font=('arial', int(11 * scale), 'bold'))
self.channel_label.configure(font=('arial', int(10 * scale), 'bold'))
self.download_date_label.configure(font=('arial', int(10 * scale), 'normal'))
self.download_btn.configure(font=('arial', int(11 * scale), 'bold'))
def kill(self):
if self.remove_callback is not None:
self.remove_callback(self)
def __del__(self):
# Unregister widget from ThemeManager
ThemeManager.unregister_widget(self)
# Clean up all attributes by setting them to None or deleting them
del self.thumbnail_button
del self.label_frame
del self.title_label
del self.channel_label
del self.download_date_label
del self.remove_btn
del self.thumbnail_normal
del self.thumbnail_hover
del self.default_thumbnail_used
del self.add_to_download_callback
del self.remove_callback
del self.no
del self.channel
del self.title
del self.url
del self.thumbnail_path_normal
del self.thumbnail_path_hover
del self.download_date
del self.width
del self.height
def destroy(self):
try:
self.thumbnail_button.destroy()
self.label_frame.destroy()
self.title_label.destroy()
self.channel_label.destroy()
self.download_date_label.destroy()
self.__del__()
return super().destroy()
except Exception as error:
print("history_object.py L-284", error)