11#include <QAbstractItemView>
12#include <QApplication>
13#include <QItemSelectionModel>
18#include <QStyleOptionViewItem>
28 class AADisplayEventFilter :
public QObject
30 QWidget *
const Display_;
32 explicit AADisplayEventFilter (QWidget *display)
38 bool eventFilter (QObject*, QEvent *event)
override
40 bool shouldClose =
false;
41 switch (event->type ())
43 case QEvent::KeyRelease:
44 shouldClose =
static_cast<QKeyEvent*
> (event)->key () == Qt::Key_Escape;
46 case QEvent::MouseButtonRelease:
56 QTimer::singleShot (0,
66 const auto scaleFactor = 0.9;
68 const auto& availSize = availGeom.size () * scaleFactor;
71 if (px.size ().width () > availSize.width () ||
72 px.size ().height () > availSize.height ())
73 px = px.scaled (availSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
75 auto topLeftPos = centerPos - QPoint { px.size ().width (), px.size ().height () } /2;
76 if (!availGeom.contains (topLeftPos))
78 topLeftPos.setX (std::max (topLeftPos.x (), availGeom.left ()));
79 topLeftPos.setY (std::max (topLeftPos.y (), availGeom.top ()));
82 const auto label =
new QLabel;
83 label->setWindowFlags (Qt::Tool);
84 label->setAttribute (Qt::WA_DeleteOnClose);
85 label->setFixedSize (px.size ());
86 label->setPixmap (px);
88 label->activateWindow ();
89 label->installEventFilter (
new AADisplayEventFilter (label));
90 label->move (topLeftPos);
96 view.selectionModel ()->setCurrentIndex (index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
99 QColor
TintColors (
const QColor& c1,
const QColor& c2,
double alpha)
102 color.setRedF (alpha * c1.redF () + (1 - alpha) * c2.redF ());
103 color.setGreenF (alpha * c1.greenF () + (1 - alpha) * c2.greenF ());
104 color.setBlueF (alpha * c1.blueF () + (1 - alpha) * c2.blueF ());
110 return option.fontMetrics.elidedText (text, Qt::ElideRight, option.rect.width ());
115 auto palette = widget->palette ();
116 for (
auto role : roles)
117 palette.setColor (role,
TintColors (palette.color (role), color, alpha));
118 widget->setPalette (palette);
123 return "<i>" + name +
"</i>";
128 switch (items.size ())
136 const auto& last = items.takeLast ();
138 return QObject::tr (
"%1 and %2").arg (items.join (u
", "_qsv), last);
144 const QString& text, QFont font,
const QPen& pen,
const QBrush& brush)
146 const auto& iconSize = px.size () / px.devicePixelRatio ();
148 const auto fontHeight = iconSize.height () * 0.45;
149 const auto minFontHeight = 6.0;
150 font.setPixelSize (
static_cast<int> (std::max (minFontHeight, fontHeight)));
152 const QFontMetrics fm (font);
153 const auto width = fm.horizontalAdvance (text) + 2. * iconSize.width () / 10.;
154 const auto height = fm.height () + 2. * iconSize.height () / 10.;
155 const bool tooSmall = width > iconSize.width ();
157 const QRectF textRect (iconSize.width () - width, iconSize.height () - height, width, height);
163 p.setRenderHint (QPainter::Antialiasing);
164 p.setRenderHint (QPainter::TextAntialiasing);
165 p.drawRoundedRect (textRect, 4, 4);
166 p.drawText (textRect,
168 tooSmall ? QStringLiteral (
"#") : text);
177 if (!icon.availableSizes ().isEmpty ())
180 constexpr auto pxSize = 256;
181 return QIcon { icon.pixmap (pxSize, pxSize) };
186 QString MakeFileDialogFilterImpl (
auto&& entries)
188 const auto toString = [] (
const auto& e) {
return e.Description_ +
" (*." + e.Extension_ +
")"; };
195 return MakeFileDialogFilterImpl (entries);
200 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.
void SetCurrentRow(QAbstractItemView &view, const QModelIndex &index)
Makes the given row current and the only selected one.
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 FormatHumanReadableList(QStringList items)
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())>))