You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

124 lines
5.0 KiB

from PyQt6.QtWidgets import QMainWindow, QVBoxLayout, QWidget, QPushButton, QGraphicsDropShadowEffect
from PyQt6.QtCore import Qt, pyqtSignal, QPoint
from PyQt6.QtGui import QMouseEvent, QColor
import time
class IconWindow(QMainWindow):
"""Окно с круглой иконкой приложения"""
icon_clicked = pyqtSignal()
def __init__(self, parent=None):
super().__init__(parent)
self.parent = parent
self.dragging = False
self.drag_offset = QPoint()
self.setup_ui()
def setup_ui(self):
"""Настраивает интерфейс иконки"""
self.setWindowFlags(Qt.WindowType.FramelessWindowHint |
Qt.WindowType.WindowStaysOnTopHint |
Qt.WindowType.Tool)
self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
self.setFixedSize(70, 70)
self.moved = False
self.start_position = None
self.icon_widget = QWidget()
self.icon_layout = QVBoxLayout(self.icon_widget)
self.icon_layout.setContentsMargins(10, 10, 10, 10)
self.icon_button = QPushButton("P")
self.icon_button.setFixedSize(50, 50)
self.icon_button.setStyleSheet("""
QPushButton {
background-color: rgba(30, 30, 30, 220);
border-radius: 25px;
color: white;
font-size: 22px;
font-weight: bold;
border: 4px solid #8e44ad;
}
QPushButton:hover {
background-color: rgba(0, 0, 0, 100);
}
QPushButton:pressed {
background-color: rgba(0, 0, 0, 100);
}
""")
self.icon_layout.addWidget(self.icon_button)
# self.shadow = QGraphicsDropShadowEffect()
# self.shadow.setBlurRadius(15)
# self.shadow.setColor(QColor('#FFFFFF'))
# self.shadow.setOffset(0, 0)
# self.icon_widget.setGraphicsEffect(self.shadow)
self.setCentralWidget(self.icon_widget)
# Настраиваем обработчики мыши для перемещения
self.icon_button.mousePressEvent = self.mouse_press_event
self.icon_button.mouseMoveEvent = self.mouse_move_event
self.icon_button.mouseReleaseEvent = self.mouse_release_event
def mouse_press_event(self, event: QMouseEvent):
"""Обработчик нажатия мыши на иконку"""
if event.button() == Qt.MouseButton.LeftButton:
self.moved = True
self.start_position = self.icon_button.mapToGlobal(event.position().toPoint())
self.dragging = True
self.drag_offset = event.position().toPoint()
self.icon_button.setStyleSheet("""
QPushButton {
background-color: rgba(0, 0, 0, 220);
border-radius: 25px;
color: white;
font-size: 22px;
font-weight: bold;
border: 3px solid #8e44ad;
}
""")
# self.shadow.setColor(QColor('#de94fd'))
event.accept()
def mouse_move_event(self, event: QMouseEvent):
"""Обработчик движения мыши для иконки"""
if self.dragging and event.buttons() & Qt.MouseButton.LeftButton:
global_pos = self.icon_button.mapToGlobal(event.position().toPoint())
new_pos = global_pos - self.drag_offset + QPoint(-10,-10)
self.move(new_pos)
event.accept()
def mouse_release_event(self, event: QMouseEvent):
"""Обработчик отпускания мыши для иконки"""
if event.button() == Qt.MouseButton.LeftButton and self.dragging:
# Решение конфликта событий mouse_press_event и clicked
end_position = self.icon_button.mapToGlobal(event.position().toPoint())
if abs((self.start_position - end_position).x()) + abs((self.start_position - end_position).y()) < 40:
self.icon_clicked.emit()
self.dragging = False
self.icon_button.setStyleSheet("""
QPushButton {
background-color: rgba(30, 30, 30, 220);
border-radius: 25px;
color: white;
font-size: 22px;
font-weight: bold;
border: 4px solid #8e44ad;
}
QPushButton:hover {
background-color: rgba(0, 0, 0, 100);
}
""")
# self.shadow.setColor(QColor('#FFFFFF'))
event.accept()
def show_icon(self):
"""Показывает иконку в левом нижнем углу"""
screen_geometry = self.screen().geometry()
x_position = screen_geometry.width() - 70
y_position = screen_geometry.height() - 120
self.move(x_position, y_position)
self.show()