-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.py
More file actions
317 lines (250 loc) · 10.8 KB
/
Copy pathinterface.py
File metadata and controls
317 lines (250 loc) · 10.8 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import sys
import json
import os
from pathlib import Path
from PySide6.QtWidgets import QApplication, QWidget, QMenu, QFileDialog
from PySide6.QtCore import Qt, QPoint, QThread, Signal, QTimer
from PySide6.QtGui import QColor, QPainter, QAction, QPixmap, QDragEnterEvent, QDropEvent, QPen
from core import GhostConverter
from config import ConfigJson
class ConversionThread(QThread):
"""
Thread dedicated to PDF conversion to avoid blocking the UI
"""
finished = Signal()
error = Signal(str)
def __init__(self, fichier_path: str, fichier_sortie: str, niveau: str):
super().__init__()
self.fichier_path = fichier_path
self.fichier_sortie = fichier_sortie
self.niveau = niveau
def run(self):
try:
converter = GhostConverter(self.fichier_path, self.fichier_sortie, self.niveau)
converter.launch()
self.finished.emit()
except Exception as e:
self.error.emit(str(e))
class CarreRouge(QWidget):
def __init__(self):
super().__init__()
config_json = ConfigJson(Path("config.json"))
self.config_json_path = config_json.determine_config_path()
# Loading the configuration
self.config = self.charger_config()
# Window configuration
self.setWindowTitle("DropPDF")
self.setFixedSize(100, 100)
# Remove window borders and force foreground
self.setWindowFlags(
Qt.WindowType.FramelessWindowHint |
Qt.WindowType.WindowStaysOnTopHint
)
# Allow mouse tracking for right click
self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.customContextMenuRequested.connect(self.afficher_menu)
# Position the square in the bottom right corner of the screen
screen_rect = QApplication.primaryScreen().geometry()
screen_width = screen_rect.width()
screen_height = screen_rect.height()
pos_x = screen_width - self.width() - 100
pos_y = screen_height - self.height() - 100
self.move(pos_x, pos_y)
# Determine current level and image from configuration
self.niveau_actuel = next(iter(self.config["current"].keys()))
self.image_actuelle = self.config["current"][self.niveau_actuel]
# Build the full path of the image
chemin_image = os.path.join("img", self.image_actuelle)
if os.path.exists(chemin_image):
self.pixmap = QPixmap(chemin_image)
else:
self.pixmap = None
# --- Loading animation state ---
self.is_converting = False
self.spinner_angle = 0
# Timer for the spinner animation (updates every 30ms ≈ 33fps)
self.spinner_timer = QTimer(self)
self.spinner_timer.timeout.connect(self._update_spinner)
# Active conversion threads (kept alive until finished)
self._conversion_threads = []
# Enable drag and drop
self.setAcceptDrops(True)
def _update_spinner(self):
"""Advances the spinner angle and triggers a repaint"""
self.spinner_angle = (self.spinner_angle + 8) % 360
self.update()
def _start_spinner(self):
"""Switches the widget into loading mode"""
self.is_converting = True
self.spinner_angle = 0
self.spinner_timer.start(30)
self.update()
def _stop_spinner(self):
"""Switches the widget back to normal mode if no conversion is running"""
# Only stop when every thread has finished
alive = [t for t in self._conversion_threads if t.isRunning()]
self._conversion_threads = alive
if not alive:
self.is_converting = False
self.spinner_timer.stop()
self.update()
def charger_config(self):
try:
with open(self.config_json_path, "r") as fichier:
return json.load(fichier)
except (FileNotFoundError, json.JSONDecodeError):
return {
"path": "",
"pics": {"high": "pdf.jpg", "medium": "pdflow.jpg", "low": "pdfmedium.jpg"},
"current": {"medium": "pdflow.jpg"}
}
def sauvegarder_config(self):
try:
with open(self.config_json_path, "w") as fichier:
json.dump(self.config, fichier, indent=2)
except Exception as e:
print(f"Error saving configuration: {e}")
def changer_image(self, niveau):
if niveau in self.config["pics"]:
self.niveau_actuel = niveau
image_nom = self.config["pics"][niveau]
self.image_actuelle = image_nom
self.config["current"] = {niveau: image_nom}
self.sauvegarder_config()
chemin_image = os.path.join("img", image_nom)
if os.path.exists(chemin_image):
self.pixmap = QPixmap(chemin_image)
else:
self.pixmap = None
self.update()
def paintEvent(self, event):
peintre = QPainter(self)
peintre.setRenderHint(QPainter.RenderHint.Antialiasing)
if self.is_converting:
self._draw_spinner(peintre)
elif self.pixmap and not self.pixmap.isNull():
peintre.drawPixmap(0, 0, self.width(), self.height(), self.pixmap)
else:
peintre.setBrush(QColor(255, 0, 0))
peintre.drawRect(0, 0, self.width(), self.height())
def _draw_spinner(self, painter: QPainter):
"""Draws an animated spinner centred in the widget"""
w, h = self.width(), self.height()
# Dark background
painter.setBrush(QColor(30, 30, 30))
painter.setPen(Qt.PenStyle.NoPen)
painter.drawRect(0, 0, w, h)
# Spinner arc
margin = 14
rect_size = min(w, h) - margin * 2
cx = (w - rect_size) // 2
cy = (h - rect_size) // 2
# Faint track circle
track_pen = QPen(QColor(80, 80, 80), 6, Qt.PenStyle.SolidLine, Qt.PenCapStyle.RoundCap)
painter.setPen(track_pen)
painter.setBrush(Qt.BrushStyle.NoBrush)
painter.drawEllipse(cx, cy, rect_size, rect_size)
# Animated arc (gradient-like via two arcs)
arc_pen = QPen(QColor(220, 60, 60), 6, Qt.PenStyle.SolidLine, Qt.PenCapStyle.RoundCap)
painter.setPen(arc_pen)
from PySide6.QtCore import QRect
rect = QRect(cx, cy, rect_size, rect_size)
start_angle = (90 - self.spinner_angle) * 16 # Qt uses 1/16th degrees
span_angle = -270 * 16 # 270° arc
painter.drawArc(rect, start_angle, span_angle)
# Small "PDF" label below the spinner
painter.setPen(QColor(200, 200, 200))
font = painter.font()
font.setPointSize(7)
font.setBold(True)
painter.setFont(font)
#painter.drawText(0, h - 6, w, 10, Qt.AlignmentFlag.AlignHCenter, "conversion…")
def parcourir_dossier(self):
dossier = QFileDialog.getExistingDirectory(
self,
"Select a folder",
self.config.get("path", ""),
QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks
)
if dossier:
self.config["path"] = dossier
self.sauvegarder_config()
def afficher_menu(self, position):
menu = QMenu(self)
high_action = QAction("High", self)
high_action.triggered.connect(lambda: self.changer_image("high"))
menu.addAction(high_action)
medium_action = QAction("Medium", self)
medium_action.triggered.connect(lambda: self.changer_image("medium"))
menu.addAction(medium_action)
low_action = QAction("Low", self)
low_action.triggered.connect(lambda: self.changer_image("low"))
menu.addAction(low_action)
menu.addSeparator()
parcourir_action = QAction("Browse", self)
parcourir_action.triggered.connect(self.parcourir_dossier)
menu.addAction(parcourir_action)
menu.addSeparator()
quitter_action = QAction("Quit", self)
quitter_action.triggered.connect(QApplication.quit)
menu.addAction(quitter_action)
menu.exec(self.mapToGlobal(position))
def mousePressEvent(self, event):
if event.button() == Qt.MouseButton.LeftButton:
self.starting_position = QPoint(event.position().x(), event.position().y())
def mouseMoveEvent(self, event):
if hasattr(self, 'starting_position'):
delta = QPoint(event.position().x() - self.starting_position.x(),
event.position().y() - self.starting_position.y())
self.move(self.x() + delta.x(), self.y() + delta.y())
def mouseReleaseEvent(self, event):
if event.button() == Qt.MouseButton.LeftButton:
if hasattr(self, 'starting_position'):
delattr(self, 'starting_position')
def dragEnterEvent(self, event: QDragEnterEvent):
if event.mimeData().hasUrls():
for url in event.mimeData().urls():
if url.toLocalFile().lower().endswith('.pdf'):
event.acceptProposedAction()
return
def dropEvent(self, event: QDropEvent):
for url in event.mimeData().urls():
fichier_path = url.toLocalFile()
if fichier_path.lower().endswith('.pdf'):
self.compresser_pdf(fichier_path)
def compresser_pdf(self, fichier_path: str):
niveau = self.niveau_actuel
dossier_sortie = self.config.get("path", "")
if not dossier_sortie:
dossier_sortie = os.path.dirname(os.path.abspath(__file__))
nom_fichier = os.path.basename(fichier_path)
nom_base, _ = os.path.splitext(nom_fichier)
fichier_entrant = str(Path(fichier_path).absolute())
fichier_sortie = str(Path(dossier_sortie) / f"{nom_base}.pdf")
print(f"File compression: {fichier_entrant}")
print(f"Compression level: {niveau}")
print(f"Output file: {fichier_sortie}")
# Start the spinner before launching the thread
self._start_spinner()
# Create and configure the worker thread
thread = ConversionThread(fichier_entrant, fichier_sortie, niveau)
thread.finished.connect(self._on_conversion_finished)
thread.error.connect(self._on_conversion_error)
# Keep a reference so the thread is not garbage-collected
self._conversion_threads.append(thread)
thread.start()
def _on_conversion_finished(self):
print("Conversion completed.")
self._stop_spinner()
def _on_conversion_error(self, message: str):
print(f"Error while compressing: {message}")
self._stop_spinner()
# Application entry point
if __name__ == "__main__":
app = QApplication(sys.argv)
fenetre = CarreRouge()
fenetre.show()
try:
sys.exit(app.exec())
except AttributeError:
sys.exit(app.exec_())