######################################################################################################################
# Copyright (C) 2017-2022 Spine project consortium
# Copyright Spine Toolbox contributors
# This file is part of Spine Toolbox.
# Spine Toolbox is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General
# Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option)
# any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
# Public License for more details. You should have received a copy of the GNU Lesser General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
######################################################################################################################
"""Classes for custom QListView."""
from textwrap import fill
from PySide6.QtCore import Qt, Signal, Slot, QMimeData
from PySide6.QtGui import QDrag, QIcon, QPainter, QBrush, QColor, QIconEngine, QCursor
from PySide6.QtWidgets import QToolButton, QApplication, QToolTip
[docs]class ProjectItemDragMixin:
"""Custom class with dragging support."""
[docs] drag_about_to_start = Signal()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._reset()
[docs] def _reset(self):
self.drag_start_pos = None
self.pixmap = None
self.mime_data = None
self.setCursor(Qt.OpenHandCursor)
[docs] def mousePressEvent(self, event):
super().mousePressEvent(event)
self.setCursor(Qt.ClosedHandCursor)
[docs] def mouseMoveEvent(self, event):
"""Start dragging action if needed"""
super().mouseMoveEvent(event)
if not event.buttons() & Qt.LeftButton:
return
if not self.drag_start_pos:
return
if (event.position().toPoint() - self.drag_start_pos).manhattanLength() < QApplication.startDragDistance():
return
drag = QDrag(self)
drag.setPixmap(self.pixmap)
drag.setMimeData(self.mime_data)
drag.setHotSpot(self.pixmap.rect().center())
self.drag_start_pos = None
self.pixmap = None
self.mime_data = None
self.drag_about_to_start.emit()
drag.exec()
[docs] def mouseReleaseEvent(self, event):
"""Forget drag start position"""
super().mouseReleaseEvent(event)
self._reset()
[docs] def enterEvent(self, event):
super().enterEvent(event)
self.setCursor(Qt.OpenHandCursor)
[docs]class ShadeMixin:
[docs] def paintEvent(self, ev):
painter = QPainter(self)
brush = QBrush(QColor(255, 255, 255, a=96))
rect = ev.rect()
painter.fillRect(rect, brush)
painter.end()
super().paintEvent(ev)
[docs]class _ChoppedIcon(QIcon):
def __init__(self, icon, size):
self._engine = _ChoppedIconEngine(icon, size)
super().__init__(self._engine)
[docs] def update(self):
self._engine.update()
[docs]class _ChoppedIconEngine(QIconEngine):
def __init__(self, icon, size):
super().__init__()
self._pixmap = None
self._icon = icon
self._size = size
self.update()
[docs] def update(self):
self._pixmap = self._icon.pixmap(self._icon.actualSize(self._size))
[docs] def pixmap(self, size, mode, state):
return self._pixmap