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
|
import sys
from os.path import basename
import webbrowser
from PySide6.QtCore import Qt, QSize, QTimer
from PySide6.QtWidgets import (
QApplication, QMainWindow,
QVBoxLayout, QHBoxLayout,
QWidget, QFileDialog,
QLabel, QPushButton,
QCheckBox, QMessageBox,
)
from Konvert import ls_size, process_dir
APP_VERSION = "0.1.0"
class DirectoryPicker(QWidget):
def __init__(self):
super().__init__()
self.selected_dir = ""
layout = QHBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(QLabel("Folder:"))
self.select_btn = QPushButton("Browse")
self.select_btn.clicked.connect(self.select_dir)
layout.addWidget(self.select_btn)
self.setLayout(layout)
def select_dir(self):
self.selected_dir = str(QFileDialog.getExistingDirectory(self, "Select a Folder"))
if self.selected_dir == "":
self.select_btn.setText("Browse")
else:
self.select_btn.setText(basename(self.selected_dir))
class DirectoryInfo(QWidget):
def __init__(self):
super().__init__()
self.size = 0
layout = QVBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
self.directory_name_label = QLabel("")
self.directory_name_label.setWordWrap(True)
layout.addWidget(self.directory_name_label)
self.directory_size_label = QLabel("")
self.directory_size_label.setWordWrap(True)
layout.addWidget(self.directory_size_label)
self.setLayout(layout)
def update_data(self, _dir, recurse=False):
if _dir == "":
self.directory_name_label.setText("")
self.directory_size_label.setText("")
self.size = 0
return
self.size = ls_size(_dir, recurse)
self.directory_name_label.setText(f"{_dir}")
self.directory_size_label.setText(f"{self.size} Image(s) Found.")
class KonvertAboutWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle(f"About - Konvert {APP_VERSION}")
self.setFixedSize(QSize(300, 180))
layout = QVBoxLayout()
label = QLabel(
"Konvert - Easy way to bulk convert image files.\n" +
f"Version - {APP_VERSION}\n\n" +
"Copyright (c) 2024 Vidhu Kant Sharma\n" +
"This program comes with absolutely no warranty.\n" +
"See GNU GPL Version 3 or later for more details."
)
layout.addWidget(label)
view_source_btn = QPushButton("View Source Code")
view_source_btn.clicked.connect(lambda: webbrowser.open("https://git.vidhukant.com/konvert"))
layout.addWidget(view_source_btn)
view_license_btn = QPushButton("View License")
view_license_btn.clicked.connect(lambda: webbrowser.open("https://git.vidhukant.com/konvert/tree/LICENSE"))
layout.addWidget(view_license_btn)
self.setLayout(layout)
class KonvertMainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Konvert")
self.setFixedSize(QSize(350, 230))
self.about_window = None
self.recurse = True
self.delete_original = False
menu = self.menuBar()
about_menubar_btn = menu.addAction("&About")
about_menubar_btn.triggered.connect(lambda: self.toggle_about_page())
exit_menubar_btn = menu.addAction("&Exit")
exit_menubar_btn.triggered.connect(lambda: self.close())
layout = QVBoxLayout()
self.directory_picker = DirectoryPicker()
self.directory_picker.select_btn.clicked.connect(self.update_dir)
layout.addWidget(self.directory_picker)
recurse_checkbox = QCheckBox()
recurse_checkbox.setText("Also search inside subfolders")
recurse_checkbox.setCheckState(Qt.CheckState.Checked)
recurse_checkbox.stateChanged.connect(self.handle_recurse_checkbox)
layout.addWidget(recurse_checkbox)
delete_checkbox = QCheckBox()
delete_checkbox.setText("Delete original files")
delete_checkbox.setCheckState(Qt.CheckState.Unchecked)
delete_checkbox.stateChanged.connect(self.handle_delete_checkbox)
layout.addWidget(delete_checkbox)
self.directory_info = DirectoryInfo()
layout.addWidget(self.directory_info)
self.start_btn = QPushButton("Convert")
self.start_btn.setDisabled(True)
self.start_btn.clicked.connect(self.start)
layout.addWidget(self.start_btn)
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
def _start(self):
process_dir(self.directory_picker.selected_dir, self.recurse, self.delete_original)
success_text = f"Successfully converted {self.directory_info.size} HEIC image(s) to PNG."
if self.delete_original:
success_text += " Also deleted the original file(s)."
# show success dialog
dialog = QMessageBox(self)
dialog.setWindowTitle("Yee Haw!!!")
dialog.setText(success_text)
dialog.exec()
# clear selected dir and stuff
self.start_btn.setText("Convert")
self.directory_picker.select_btn.setText("Browse")
self.directory_picker.selected_dir = ""
self.update_dir()
def start(self):
# disable the button
self.start_btn.setText("Processing... Please Wait")
self.start_btn.setDisabled(True)
QTimer.singleShot(500, lambda: self._start())
def update_dir(self):
selected_dir = self.directory_picker.selected_dir
self.directory_info.update_data(selected_dir, self.recurse)
self.start_btn.setDisabled(self.directory_info.size < 1)
def handle_recurse_checkbox(self, state):
self.recurse = (state == Qt.CheckState.Checked.value)
self.update_dir()
def handle_delete_checkbox(self, state):
self.delete_original = (state == Qt.CheckState.Checked.value)
def toggle_about_page(self):
if self.about_window is None:
self.about_window = KonvertAboutWindow()
self.about_window.show()
else:
self.about_window.close()
self.about_window = None
if __name__ == "__main__":
app = QApplication(sys.argv)
window = KonvertMainWindow()
window.show()
app.exec()
|