-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcso2.2.py
More file actions
119 lines (93 loc) · 3.76 KB
/
Copy pathcso2.2.py
File metadata and controls
119 lines (93 loc) · 3.76 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
import sys
from PyQt5.QtWidgets import QApplication, QLabel, QWidget
from PyQt5.QtGui import QPixmap, QTransform
from PyQt5.QtCore import Qt
class TransparentImageViewer(QWidget):
def __init__(self, image_path: str):
super().__init__()
# Stato
self.mirrored = False
self.current_scale = 1.5
self.start_pos = None
# Finestra: frameless + always on top (NO Qt.Tool)
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
self.setAttribute(Qt.WA_TranslucentBackground)
# Per ricevere eventi tastiera anche su finestra frameless
self.setFocusPolicy(Qt.StrongFocus)
# Blocca menu contestuali
self.setContextMenuPolicy(Qt.NoContextMenu)
# Label immagine
self.label = QLabel(self)
self.label.setContextMenuPolicy(Qt.NoContextMenu)
# Fondamentale: gli eventi mouse devono arrivare al QWidget, non alla QLabel
self.label.setAttribute(Qt.WA_TransparentForMouseEvents)
# Carica immagine
self.original_pixmap = QPixmap(image_path)
# Allinea subito stato e vista (evita il bug della prima rotella)
self.update_pixmap()
# Centra sullo schermo (dopo update_pixmap, così usa le dimensioni reali)
screen = QApplication.primaryScreen().geometry()
x = (screen.width() - self.width()) // 2
y = (screen.height() - self.height()) // 2
self.move(x, y)
def update_pixmap(self):
pixmap = self.original_pixmap
if self.mirrored:
pixmap = pixmap.transformed(QTransform().scale(-1, 1))
new_width = max(10, int(pixmap.width() * self.current_scale))
new_height = max(10, int(pixmap.height() * self.current_scale))
scaled_pixmap = pixmap.scaled(
new_width,
new_height,
Qt.KeepAspectRatio,
Qt.SmoothTransformation,
)
self.label.setPixmap(scaled_pixmap)
self.label.resize(scaled_pixmap.size())
self.resize(scaled_pixmap.size())
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.start_pos = event.globalPos() - self.frameGeometry().topLeft()
event.accept()
elif event.button() == Qt.RightButton:
# IMPORTANTISSIMO: non chiudere qui, chiudi al release (Windows apre menu sul release)
event.accept()
def mouseMoveEvent(self, event):
if self.start_pos is not None:
self.move(event.globalPos() - self.start_pos)
event.accept()
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self.start_pos = None
event.accept()
elif event.button() == Qt.RightButton:
event.accept()
self.close()
def wheelEvent(self, event):
# Robusto: giù = riduci, su = aumenta
if event.angleDelta().y() < 0:
self.current_scale *= 0.9
else:
self.current_scale *= 1.1
self.update_pixmap()
event.accept()
def keyPressEvent(self, event):
if event.key() in (Qt.Key_Left, Qt.Key_Right):
self.mirrored = not self.mirrored
self.update_pixmap()
event.accept()
elif event.key() == Qt.Key_Escape:
self.close()
event.accept()
else:
super().keyPressEvent(event)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Trascina un file PNG sull'eseguibile oppure passa un file PNG come argomento.")
sys.exit(1)
app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(True)
viewer = TransparentImageViewer(sys.argv[1])
viewer.show()
viewer.setFocus() # utile per le frecce appena aperta
sys.exit(app.exec_())