-
Notifications
You must be signed in to change notification settings - Fork 0
/
zQueue.py
112 lines (101 loc) · 3.75 KB
/
zQueue.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
#coding: utf-8
from functools import partial
import tkinter as tk
from tkinter import ttk
from Tooltip import Tooltip
from path import Path
from zEntryPopup import zEntryPopup
from zLog import get_logger
logger = get_logger("zTextureReducer")
class zQueue:
def __init__(self, master: tk.BaseWidget, **kwargs):
self.container = ttk.LabelFrame(
master,
text="Queue",
width=500,
height=100)
self.container.pack(side="top", expand=True, fill="both")
# Selection callback
self.get_selected = kwargs.get("add_callback", None)
# Controls
self.controls = tk.Frame(self.container)
self.add_button = ttk.Button(
self.controls,
text="Add",
command=self._add_cb)
self.add_tooltip = Tooltip(
self.add_button,
"Add selected images from workspace to the queue")
self.add_button.pack(side="left")
self.remove_button = ttk.Button(
self.controls,
text="Remove",
command=self._remove_cb)
self.remove_tooltip = Tooltip(
self.remove_button,
"Remove selected images from the queue")
self.remove_button.pack(side="left")
self.remove_all_button = ttk.Button(
self.controls,
text="Remove All",
command=self.remove_all)
self.remove_all_button.pack(side="left")
self.controls.pack(side="top", expand=False, anchor="w")
# Treeview
self.queue = ttk.Treeview(self.container, selectmode="extended")
self.queue["columns"] = ("1")
self.queue.heading("#0", text="Files to process", anchor=tk.W)
self.queue.heading("1", text="Rename to", anchor=tk.W)
self.queue["displaycolumns"] = ["1"]
self.queue.bind("<Double-1>", self._on_dclick_item)
self.queue.pack(side="top", expand=True, fill="both")
def _add_cb(self):
if self.get_selected is not None:
selection = self.get_selected()
for item in selection:
fp = Path(item)
self.queue.insert(
"",
"end",
text=item,
values=("{}".format(fp.stem))
)
def _remove_cb(self):
print(self.queue.selection())
print(*self.queue.selection())
self.queue.delete(*self.queue.selection())
def _on_dclick_item(self, *args, **kwargs):
"""
Method bind to double click
used to rename a file
"""
event = args[0]
item = self.queue.identify_row(event.y)
if self.queue.identify_column(event.x) == "#1" and item != '':
px, py, width, height = self.queue.bbox(item, "#1")
pady = height // 2
values = self.queue.item(item, "values")
value = values[0] if values != '' else ''
self.popUp = zEntryPopup(
self.queue,
value,
partial(self.set_rename, item))
self.popUp.place(x=px, y=py+pady, anchor=tk.W, relwidth=1)
def set_rename(self, item, name_value, *ignore):
self.queue.item(item, values=(name_value))
def remove_all(self):
self.queue.delete(*self.queue.get_children())
def get_queue(self):
ret = []
for item in self.queue.get_children():
## TODO
## Rework algo to properly handle renaming option
src = self.queue.item(item, 'text')
name = ''
values = self.queue.item(item, 'values')
if values != '':
name = values[0]
else:
name = Path(src).stem
ret.append((src, name))
return ret