11#include <QApplication>
16#include <QStyleOptionViewItem>
24 class AADisplayEventFilter :
public QObject
26 QWidget *
const Display_;
28 explicit AADisplayEventFilter (QWidget *display)
34 bool eventFilter (QObject*, QEvent *event)
override
36 bool shouldClose =
false;
37 switch (event->type ())
39 case QEvent::KeyRelease:
40 shouldClose =
static_cast<QKeyEvent*
> (event)->key () == Qt::Key_Escape;
42 case QEvent::MouseButtonRelease:
52 QTimer::singleShot (0,
62 const auto scaleFactor = 0.9;
64 const auto& availSize = availGeom.size () * scaleFactor;
67 if (px.size ().width () > availSize.width () ||
68 px.size ().height () > availSize.height ())
69 px = px.scaled (availSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
71 auto topLeftPos = centerPos - QPoint { px.size ().width (), px.size ().height () } /2;
72 if (!availGeom.contains (topLeftPos))
74 topLeftPos.setX (std::max (topLeftPos.x (), availGeom.left ()));
75 topLeftPos.setY (std::max (topLeftPos.y (), availGeom.top ()));
78 const auto label =
new QLabel;
79 label->setWindowFlags (Qt::Tool);
80 label->setAttribute (Qt::WA_DeleteOnClose);
81 label->setFixedSize (px.size ());
82 label->setPixmap (px);
84 label->activateWindow ();
85 label->installEventFilter (
new AADisplayEventFilter (label));
86 label->move (topLeftPos);
90 QColor
TintColors (
const QColor& c1,
const QColor& c2,
double alpha)
93 color.setRedF (alpha * c1.redF () + (1 - alpha) * c2.redF ());
94 color.setGreenF (alpha * c1.greenF () + (1 - alpha) * c2.greenF ());
95 color.setBlueF (alpha * c1.blueF () + (1 - alpha) * c2.blueF ());
101 return option.fontMetrics.elidedText (text, Qt::ElideRight, option.rect.width ());
106 auto palette = widget->palette ();
107 for (
auto role : roles)
108 palette.setColor (role,
TintColors (palette.color (role), color, alpha));
109 widget->setPalette (palette);
114 return "<em>" + name +
"</em>";
118 const QString& text, QFont font,
const QPen& pen,
const QBrush& brush)
120 const auto& iconSize = px.size () / px.devicePixelRatio ();
122 const auto fontHeight = iconSize.height () * 0.45;
123 const auto minFontHeight = 6.0;
124 font.setPixelSize (
static_cast<int> (std::max (minFontHeight, fontHeight)));
126 const QFontMetrics fm (font);
127 const auto width = fm.horizontalAdvance (text) + 2. * iconSize.width () / 10.;
128 const auto height = fm.height () + 2. * iconSize.height () / 10.;
129 const bool tooSmall = width > iconSize.width ();
131 const QRectF textRect (iconSize.width () - width, iconSize.height () - height, width, height);
137 p.setRenderHint (QPainter::Antialiasing);
138 p.setRenderHint (QPainter::TextAntialiasing);
139 p.drawRoundedRect (textRect, 4, 4);
140 p.drawText (textRect,
142 tooSmall ? QStringLiteral (
"#") : text);
151 if (!icon.availableSizes ().isEmpty ())
154 constexpr auto pxSize = 256;
155 return QIcon { icon.pixmap (pxSize, pxSize) };
QLabel * ShowPixmapLabel(const QPixmap &srcPx, const QPoint ¢erPos)
Shows a pixmap at the given pos.
QColor TintColors(const QColor &c1, const QColor &c2, double alpha)
Mixes two colors with the given weights.
void TintPalette(QWidget *widget, const QColor &color, double alpha, const QList< QPalette::ColorRole > &roles)
Mixes some of the widget's palette roles with the given color.
QString ElideProgressBarText(const QString &text, const QStyleOptionViewItem &option)
QRect AvailableGeometry(const QPoint &p)
QPixmap DrawOverlayText(QPixmap px, const QString &text, QFont font, const QPen &pen, const QBrush &brush)
QIcon FixupTrayIcon(const QIcon &icon)
QString FormatName(const QString &name)
HTML-formats the name to let the user know it is not a part of the fixed dialog text.