from qtpy.QtCore import Qt, QSize
from qtpy.QtWidgets import QGridLayout, QLabel, QStyle, QDialogButtonBox, QSizePolicy, QWidget, QApplication
from qtpy.QtGui import QIcon
from . import FramelessWindow
[docs]class FramelessMessageBox(FramelessWindow):
"""Frameless messagebox."""
def __init__(self, icon=None, parent=None):
super(FramelessMessageBox, self).__init__(parent)
self.setResizingEnabled(False)
self.resize(QSize(350, 150))
policy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
policy.setHeightForWidth(True)
self.setSizePolicy(policy)
self.__messagewidget = QWidget(self)
self.__messagewidget.setContentsMargins(11, 11, 11, 11)
self.__grid = QGridLayout(self.__messagewidget)
self.__grid.setContentsMargins(0, 0, 0, 0)
self.__grid.setVerticalSpacing(8)
self.__grid.setHorizontalSpacing(0)
self.__messagewidget.setLayout(self.__grid)
self.__iconLabel = QLabel(self.__messagewidget)
self.__iconLabel.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
self.__iconLabel.setScaledContents(True)
self.__grid.addWidget(self.__iconLabel, 0, 0, 2, 1, Qt.AlignTop | Qt.AlignLeft)
self.__textLabel = QLabel(self.__messagewidget)
self.__textLabel.setWordWrap(True)
self.__textLabel.setContentsMargins(2, 0, 0, 0)
self.__textLabel.setIndent(9)
self.__textLabel.setScaledContents(True)
self.__textLabel.setMouseTracking(True)
self.__textLabel.autoFillBackground()
self.__textLabel.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
self.__grid.addWidget(self.__textLabel, 0, 1, 1, 1)
self.__grid.setRowStretch(1, 100)
self.__grid.setRowMinimumHeight(2, 6)
self.__buttonBox = QDialogButtonBox(self.__messagewidget)
self.__buttonBox.setStandardButtons(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
self.__grid.addWidget(self.__buttonBox, 3, 1, 1, 1)
self.addContentWidget(self.__messagewidget)
self.setIcon(icon)
self.showSizeControl(False)
self.setEdgeSnapping(False)
[docs] def setText(self, text: str):
"""Set text for message box."""
self.__textLabel.setText(text)
self.__textLabel.adjustSize()
self.__textLabel.updateGeometry()
self.adjustSize()
[docs] def text(self):
"""This property holds the message box text to be displayed.
The default value of this property is an empty string."""
return self.__textLabel.text()
[docs] def setIcon(self, icon):
"""Set icon for message box.
Args:
icon (QIcon): Message box icon.
"""
if icon:
icon = QApplication.instance().style().standardIcon(icon)
self.__iconLabel.setPixmap(icon.pixmap(QSize(128, 128)))
else:
self.__iconLabel.setPixmap(QIcon().pixmap(QSize(128, 128)))
[docs] def icon(self):
"""This property holds the message box's icon"""
return QIcon(self.__iconLabel.pixmap())
[docs]class FramelessWarningMessageBox(FramelessMessageBox):
def __init__(self, parent=None):
super(FramelessWarningMessageBox, self).__init__(icon=QStyle.SP_MessageBoxWarning, parent=parent)
[docs]class FramelessCriticalMessageBox(FramelessMessageBox):
def __init__(self, parent=None):
super(FramelessCriticalMessageBox, self).__init__(icon=QStyle.SP_MessageBoxCritical, parent=parent)
[docs]class FramelessQuestionMessageBox(FramelessMessageBox):
def __init__(self, parent=None):
super(FramelessQuestionMessageBox, self).__init__(icon=QStyle.SP_MessageBoxQuestion, parent=parent)