LeechCraft 0.6.70-18808-g3467692359
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
util.cpp
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#include "util.h"
10#include <QSize>
11#include <QApplication>
12#include <QKeyEvent>
13#include <QTimer>
14#include <QLabel>
15#include <QPainter>
16#include <QStyleOptionViewItem>
17#include <QtDebug>
18#include <util/sll/prelude.h>
19#include <util/sll/qtutil.h>
20#include "geometry.h"
21
22namespace LC::Util
23{
24 namespace
25 {
26 class AADisplayEventFilter : public QObject
27 {
28 QWidget * const Display_;
29 public:
30 explicit AADisplayEventFilter (QWidget *display)
31 : QObject (display)
32 , Display_ (display)
33 {
34 }
35 protected:
36 bool eventFilter (QObject*, QEvent *event) override
37 {
38 bool shouldClose = false;
39 switch (event->type ())
40 {
41 case QEvent::KeyRelease:
42 shouldClose = static_cast<QKeyEvent*> (event)->key () == Qt::Key_Escape;
43 break;
44 case QEvent::MouseButtonRelease:
45 shouldClose = true;
46 break;
47 default:
48 break;
49 }
50
51 if (!shouldClose)
52 return false;
53
54 QTimer::singleShot (0,
55 Display_,
56 &QWidget::close);
57 return true;
58 }
59 };
60 }
61
62 QLabel* ShowPixmapLabel (const QPixmap& srcPx, const QPoint& centerPos)
63 {
64 const auto scaleFactor = 0.9;
65 const auto& availGeom = AvailableGeometry (centerPos);
66 const auto& availSize = availGeom.size () * scaleFactor;
67
68 auto px = srcPx;
69 if (px.size ().width () > availSize.width () ||
70 px.size ().height () > availSize.height ())
71 px = px.scaled (availSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
72
73 auto topLeftPos = centerPos - QPoint { px.size ().width (), px.size ().height () } /2;
74 if (!availGeom.contains (topLeftPos))
75 {
76 topLeftPos.setX (std::max (topLeftPos.x (), availGeom.left ()));
77 topLeftPos.setY (std::max (topLeftPos.y (), availGeom.top ()));
78 }
79
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);
85 label->show ();
86 label->activateWindow ();
87 label->installEventFilter (new AADisplayEventFilter (label));
88 label->move (topLeftPos);
89 return label;
90 }
91
92 QColor TintColors (const QColor& c1, const QColor& c2, double alpha)
93 {
94 QColor color;
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 ());
98 return color;
99 }
100
101 QString ElideProgressBarText (const QString& text, const QStyleOptionViewItem& option)
102 {
103 return option.fontMetrics.elidedText (text, Qt::ElideRight, option.rect.width ());
104 }
105
106 void TintPalette (QWidget *widget, const QColor& color, double alpha, const QList<QPalette::ColorRole>& roles)
107 {
108 auto palette = widget->palette ();
109 for (auto role : roles)
110 palette.setColor (role, TintColors (palette.color (role), color, alpha));
111 widget->setPalette (palette);
112 }
113
114 QString FormatName (const QString& name)
115 {
116 return "<em>" + name + "</em>";
117 }
118
119 QString FormatHumanReadableList (QStringList items)
120 {
121 switch (items.size ())
122 {
123 case 0:
124 return {};
125 case 1:
126 return items [0];
127 default:
128 {
129 const auto& last = items.takeLast ();
130 //: %1 is a comma-separated list having at least one item, %2 is the final item
131 return QObject::tr ("%1 and %2").arg (items.join (u", "_qsv), last);
132 }
133 }
134 }
135
136 QPixmap DrawOverlayText (QPixmap px,
137 const QString& text, QFont font, const QPen& pen, const QBrush& brush)
138 {
139 const auto& iconSize = px.size () / px.devicePixelRatio ();
140
141 const auto fontHeight = iconSize.height () * 0.45;
142 const auto minFontHeight = 6.0;
143 font.setPixelSize (static_cast<int> (std::max (minFontHeight, fontHeight)));
144
145 const QFontMetrics fm (font);
146 const auto width = fm.horizontalAdvance (text) + 2. * iconSize.width () / 10.;
147 const auto height = fm.height () + 2. * iconSize.height () / 10.;
148 const bool tooSmall = width > iconSize.width ();
149
150 const QRectF textRect (iconSize.width () - width, iconSize.height () - height, width, height);
151
152 QPainter p (&px);
153 p.setBrush (brush);
154 p.setFont (font);
155 p.setPen (pen);
156 p.setRenderHint (QPainter::Antialiasing);
157 p.setRenderHint (QPainter::TextAntialiasing);
158 p.drawRoundedRect (textRect, 4, 4);
159 p.drawText (textRect,
160 Qt::AlignCenter,
161 tooSmall ? QStringLiteral ("#") : text);
162 p.end ();
163
164 return px;
165 }
166
167 // https://bugreports.qt.io/browse/QTBUG-53550
168 QIcon FixupTrayIcon (const QIcon& icon)
169 {
170 if (!icon.availableSizes ().isEmpty ())
171 return icon;
172
173 constexpr auto pxSize = 256;
174 return QIcon { icon.pixmap (pxSize, pxSize) };
175 }
176
177 namespace
178 {
179 QString MakeFileDialogFilterImpl (auto&& entries)
180 {
181 const auto toString = [] (const auto& e) { return e.Description_ + " (*." + e.Extension_ + ")"; };
182 return MapAs<QList> (entries, toString).join (";;"_ql);
183 }
184 }
185
186 QString MakeFileDialogFilter (std::initializer_list<FileDialogFilterEntry> entries)
187 {
188 return MakeFileDialogFilterImpl (entries);
189 }
190
192 {
193 return MakeFileDialogFilterImpl (entries);
194 }
195}
char * toString(const char *name, const T &t)
Definition common.h:38
QLabel * ShowPixmapLabel(const QPixmap &srcPx, const QPoint &centerPos)
Shows a pixmap at the given pos.
Definition util.cpp:62
QColor TintColors(const QColor &c1, const QColor &c2, double alpha)
Mixes two colors with the given weights.
Definition util.cpp:92
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.
Definition util.cpp:106
QString ElideProgressBarText(const QString &text, const QStyleOptionViewItem &option)
Definition util.cpp:101
QRect AvailableGeometry(const QPoint &p)
Definition geometry.cpp:66
QPixmap DrawOverlayText(QPixmap px, const QString &text, QFont font, const QPen &pen, const QBrush &brush)
Definition util.cpp:136
QString FormatHumanReadableList(QStringList items)
Definition util.cpp:119
QString MakeFileDialogFilter(std::initializer_list< FileDialogFilterEntry > entries)
Definition util.cpp:186
QIcon FixupTrayIcon(const QIcon &icon)
Definition util.cpp:168
QString FormatName(const QString &name)
HTML-formats the name to let the user know it is not a part of the fixed dialog text.
Definition util.cpp:114
auto MapAs(Container &&c, F &&f) noexcept(noexcept(std::is_nothrow_invocable_v< F, decltype(*c.begin())>))
Definition prelude.h:111