11#include <QApplication>
16#include <QStyleOptionViewItem>
26 class AADisplayEventFilter :
public QObject
28 QWidget *
const Display_;
30 explicit AADisplayEventFilter (QWidget *display)
36 bool eventFilter (QObject*, QEvent *event)
override
38 bool shouldClose =
false;
39 switch (event->type ())
41 case QEvent::KeyRelease:
42 shouldClose =
static_cast<QKeyEvent*
> (event)->key () == Qt::Key_Escape;
44 case QEvent::MouseButtonRelease:
54 QTimer::singleShot (0,
64 const auto scaleFactor = 0.9;
66 const auto& availSize = availGeom.size () * scaleFactor;
69 if (px.size ().width () > availSize.width () ||
70 px.size ().height () > availSize.height ())
71 px = px.scaled (availSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
73 auto topLeftPos = centerPos - QPoint { px.size ().width (), px.size ().height () } /2;
74 if (!availGeom.contains (topLeftPos))
76 topLeftPos.setX (std::max (topLeftPos.x (), availGeom.left ()));
77 topLeftPos.setY (std::max (topLeftPos.y (), availGeom.top ()));
80 const auto label =
new QLabel;
81 label->setWindowFlags (Qt::Tool);
82 label->setAttribute (Qt::WA_DeleteOnClose);
83 label->setFixedSize (px.size ());
84 label->setPixmap (px);
86 label->activateWindow ();
87 label->installEventFilter (
new AADisplayEventFilter (label));
88 label->move (topLeftPos);
92 QColor
TintColors (
const QColor& c1,
const QColor& c2,
double alpha)
95 color.setRedF (alpha * c1.redF () + (1 - alpha) * c2.redF ());
96 color.setGreenF (alpha * c1.greenF () + (1 - alpha) * c2.greenF ());
97 color.setBlueF (alpha * c1.blueF () + (1 - alpha) * c2.blueF ());
103 return option.fontMetrics.elidedText (text, Qt::ElideRight, option.rect.width ());
108 auto palette = widget->palette ();
109 for (
auto role : roles)
110 palette.setColor (role,
TintColors (palette.color (role), color, alpha));
111 widget->setPalette (palette);
116 return "<em>" + name +
"</em>";
120 const QString& text, QFont font,
const QPen& pen,
const QBrush& brush)
122 const auto& iconSize = px.size () / px.devicePixelRatio ();
124 const auto fontHeight = iconSize.height () * 0.45;
125 const auto minFontHeight = 6.0;
126 font.setPixelSize (
static_cast<int> (std::max (minFontHeight, fontHeight)));
128 const QFontMetrics fm (font);
129 const auto width = fm.horizontalAdvance (text) + 2. * iconSize.width () / 10.;
130 const auto height = fm.height () + 2. * iconSize.height () / 10.;
131 const bool tooSmall = width > iconSize.width ();
133 const QRectF textRect (iconSize.width () - width, iconSize.height () - height, width, height);
139 p.setRenderHint (QPainter::Antialiasing);
140 p.setRenderHint (QPainter::TextAntialiasing);
141 p.drawRoundedRect (textRect, 4, 4);
142 p.drawText (textRect,
144 tooSmall ? QStringLiteral (
"#") : text);
153 if (!icon.availableSizes ().isEmpty ())
156 constexpr auto pxSize = 256;
157 return QIcon { icon.pixmap (pxSize, pxSize) };
162 QString MakeFileDialogFilterImpl (
auto&& entries)
164 const auto toString = [] (
const auto& e) {
return e.Description_ +
" (*." + e.Extension_ +
")"; };
171 return MakeFileDialogFilterImpl (entries);
176 return MakeFileDialogFilterImpl (entries);
char * toString(const char *name, const T &t)
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)
QString MakeFileDialogFilter(std::initializer_list< FileDialogFilterEntry > entries)
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.
auto MapAs(Container &&c, F &&f) noexcept(noexcept(std::is_nothrow_invocable_v< F, decltype(*c.begin())>))