LeechCraft 0.6.70-17335-ge406ffdcaf
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 "geometry.h"
19
20namespace LC::Util
21{
22 namespace
23 {
24 class AADisplayEventFilter : public QObject
25 {
26 QWidget * const Display_;
27 public:
28 explicit AADisplayEventFilter (QWidget *display)
29 : QObject (display)
30 , Display_ (display)
31 {
32 }
33 protected:
34 bool eventFilter (QObject*, QEvent *event) override
35 {
36 bool shouldClose = false;
37 switch (event->type ())
38 {
39 case QEvent::KeyRelease:
40 shouldClose = static_cast<QKeyEvent*> (event)->key () == Qt::Key_Escape;
41 break;
42 case QEvent::MouseButtonRelease:
43 shouldClose = true;
44 break;
45 default:
46 break;
47 }
48
49 if (!shouldClose)
50 return false;
51
52 QTimer::singleShot (0,
53 Display_,
54 &QWidget::close);
55 return true;
56 }
57 };
58 }
59
60 QLabel* ShowPixmapLabel (const QPixmap& srcPx, const QPoint& centerPos)
61 {
62 const auto scaleFactor = 0.9;
63 const auto& availGeom = AvailableGeometry (centerPos);
64 const auto& availSize = availGeom.size () * scaleFactor;
65
66 auto px = srcPx;
67 if (px.size ().width () > availSize.width () ||
68 px.size ().height () > availSize.height ())
69 px = px.scaled (availSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
70
71 auto topLeftPos = centerPos - QPoint { px.size ().width (), px.size ().height () } /2;
72 if (!availGeom.contains (topLeftPos))
73 {
74 topLeftPos.setX (std::max (topLeftPos.x (), availGeom.left ()));
75 topLeftPos.setY (std::max (topLeftPos.y (), availGeom.top ()));
76 }
77
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);
83 label->show ();
84 label->activateWindow ();
85 label->installEventFilter (new AADisplayEventFilter (label));
86 label->move (topLeftPos);
87 return label;
88 }
89
90 QColor TintColors (const QColor& c1, const QColor& c2, double alpha)
91 {
92 QColor color;
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 ());
96 return color;
97 }
98
99 QString ElideProgressBarText (const QString& text, const QStyleOptionViewItem& option)
100 {
101 return option.fontMetrics.elidedText (text, Qt::ElideRight, option.rect.width ());
102 }
103
104 void TintPalette (QWidget *widget, const QColor& color, double alpha, const QList<QPalette::ColorRole>& roles)
105 {
106 auto palette = widget->palette ();
107 for (auto role : roles)
108 palette.setColor (role, TintColors (palette.color (role), color, alpha));
109 widget->setPalette (palette);
110 }
111
112 QString FormatName (const QString& name)
113 {
114 return "<em>" + name + "</em>";
115 }
116
117 QPixmap DrawOverlayText (QPixmap px,
118 const QString& text, QFont font, const QPen& pen, const QBrush& brush)
119 {
120 const auto& iconSize = px.size () / px.devicePixelRatio ();
121
122 const auto fontHeight = iconSize.height () * 0.45;
123 const auto minFontHeight = 6.0;
124 font.setPixelSize (static_cast<int> (std::max (minFontHeight, fontHeight)));
125
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 ();
130
131 const QRectF textRect (iconSize.width () - width, iconSize.height () - height, width, height);
132
133 QPainter p (&px);
134 p.setBrush (brush);
135 p.setFont (font);
136 p.setPen (pen);
137 p.setRenderHint (QPainter::Antialiasing);
138 p.setRenderHint (QPainter::TextAntialiasing);
139 p.drawRoundedRect (textRect, 4, 4);
140 p.drawText (textRect,
141 Qt::AlignCenter,
142 tooSmall ? QStringLiteral ("#") : text);
143 p.end ();
144
145 return px;
146 }
147
148 // https://bugreports.qt.io/browse/QTBUG-53550
149 QIcon FixupTrayIcon (const QIcon& icon)
150 {
151 if (!icon.availableSizes ().isEmpty ())
152 return icon;
153
154 constexpr auto pxSize = 256;
155 return QIcon { icon.pixmap (pxSize, pxSize) };
156 }
157}
QLabel * ShowPixmapLabel(const QPixmap &srcPx, const QPoint &centerPos)
Shows a pixmap at the given pos.
Definition util.cpp:60
QColor TintColors(const QColor &c1, const QColor &c2, double alpha)
Mixes two colors with the given weights.
Definition util.cpp:90
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:104
QString ElideProgressBarText(const QString &text, const QStyleOptionViewItem &option)
Definition util.cpp:99
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:117
QIcon FixupTrayIcon(const QIcon &icon)
Definition util.cpp:149
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:112