#!/usr/bin/env python3
# SPDX-License-Identifier: WTFPL
# a Qt app that displays an image always-on-top like a sticky note

# /// script
# dependencies = ["PyQt6"]
# ///

from argparse import ArgumentParser
import sys

from PyQt6.QtCore import Qt
from PyQt6.QtGui import QPixmap
from PyQt6.QtWidgets import QLabel, QApplication


class Display(QLabel):
	hover_opacity = 1
	opacity = 1

	def __init__(self, pix):
		super(Display, self).__init__()

		self.setPixmap(pix)
		self.setFocusPolicy(Qt.FocusPolicy.NoFocus)
		self.setWindowFlags(self.windowFlags() | Qt.WindowType.WindowStaysOnTopHint | Qt.WindowType.WindowDoesNotAcceptFocus)

	def enterEvent(self, ev):
		self.setWindowOpacity(self.hover_opacity)

	def leaveEvent(self, ev):
		self.setWindowOpacity(self.opacity)

	def focusInEvent(self, ev):
		self.clearFocus()

	def mousePressEvent(self, ev):
		self.clicked = ev.position()

	def mouseMoveEvent(self, ev):
		self.move((ev.globalPosition() - self.clicked).toPoint())


if __name__ == '__main__':
	app = QApplication(sys.argv)

	parser = ArgumentParser(
		description="app that displays an image always-on-top like a sticky note",
	)
	parser.add_argument(
		"--borderless", action="store_const", const=True,
		help="Hide image window border",
	)
	parser.add_argument(
		"--hover-opacity", type=float, metavar="NUMBER",
		help="When the mouse cursor hovers the image, set image opacity to"
		+ " NUMBER (between 0 and 1). Default: use the same as --opacity",
	)
	parser.add_argument(
		"--opacity", type=float, default=1,
		help="Set image opacity to NUMBER (between 0 and 1). Default: 1",
	)
	parser.add_argument("file", help="image to display")
	args = parser.parse_args(app.arguments()[1:])

	pix = QPixmap(args.file)
	if pix.isNull():
		parser.error('invalid image file %r' % args.file)

	d = Display(pix)
	d.setWindowTitle('Sticky image: %r' % args.file)
	if args.hover_opacity is None:
		args.hover_opacity = args.opacity
	if args.borderless:
		d.setWindowFlags(d.windowFlags() | Qt.WindowType.FramelessWindowHint)

	d.hover_opacity = args.hover_opacity
	d.opacity = args.opacity
	d.setWindowOpacity(d.opacity)

	d.show()
	app.exec()
