LeechCraft 0.6.70-17609-g3dde4097dd
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 QPixmap DrawOverlayText (QPixmap px,
120 const QString& text, QFont font, const QPen& pen, const QBrush& brush)
121 {
122 const auto& iconSize = px.size () / px.devicePixelRatio ();
123
124 const auto fontHeight = iconSize.height () * 0.45;
125 const auto minFontHeight = 6.0;
126 font.setPixelSize (static_cast<int> (std::max (minFontHeight, fontHeight)));
127
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 ();
132
133 const QRectF textRect (iconSize.width () - width, iconSize.height () - height, width, height);
134
135 QPainter p (&px);
136 p.setBrush (brush);
137 p.setFont (font);
138 p.setPen (pen);
139 p.setRenderHint (QPainter::Antialiasing);
140 p.setRenderHint (QPainter::TextAntialiasing);
141 p.drawRoundedRect (textRect, 4, 4);
142 p.drawText (textRect,
143 Qt::AlignCenter,
144 tooSmall ? QStringLiteral ("#") : text);
145 p.end ();
146
147 return px;
148 }
149
150 // https://bugreports.qt.io/browse/QTBUG-53550
151 QIcon FixupTrayIcon (const QIcon& icon)
152 {
153 if (!icon.availableSizes ().isEmpty ())
154 return icon;
155
156 constexpr auto pxSize = 256;
157 return QIcon { icon.pixmap (pxSize, pxSize) };
158 }
159
160 namespace
161 {
162 QString MakeFileDialogFilterImpl (auto&& entries)
163 {
164 const auto toString = [] (const auto& e) { return e.Description_ + " (*." + e.Extension_ + ")"; };
165 return MapAs<QList> (entries, toString).join (";;"_ql);
166 }
167 }
168
169 QString MakeFileDialogFilter (std::initializer_list<FileDialogFilterEntry> entries)
170 {
171 return MakeFileDialogFilterImpl (entries);
172 }
173
175 {
176 return MakeFileDialogFilterImpl (entries);
177 }
178}
char * toString(const char *name, const T &t)
Definition common.h:55
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:119
QString MakeFileDialogFilter(std::initializer_list< FileDialogFilterEntry > entries)
Definition util.cpp:169
QIcon FixupTrayIcon(const QIcon &icon)
Definition util.cpp:151
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