LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
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 <QDesktopWidget>
13 #include <QKeyEvent>
14 #include <QTimer>
15 #include <QLabel>
16 #include <QPainter>
17 #include <QStyleOptionViewItem>
18 #include <QtDebug>
19 #include "geometry.h"
20 
21 namespace LC::Util
22 {
23  namespace
24  {
25  class AADisplayEventFilter : public QObject
26  {
27  QWidget * const Display_;
28  public:
29  explicit AADisplayEventFilter (QWidget *display)
30  : QObject (display)
31  , Display_ (display)
32  {
33  }
34  protected:
35  bool eventFilter (QObject*, QEvent *event) override
36  {
37  bool shouldClose = false;
38  switch (event->type ())
39  {
40  case QEvent::KeyRelease:
41  shouldClose = static_cast<QKeyEvent*> (event)->key () == Qt::Key_Escape;
42  break;
43  case QEvent::MouseButtonRelease:
44  shouldClose = true;
45  break;
46  default:
47  break;
48  }
49 
50  if (!shouldClose)
51  return false;
52 
53  QTimer::singleShot (0,
54  Display_,
55  &QWidget::close);
56  return true;
57  }
58  };
59  }
60 
61  QLabel* ShowPixmapLabel (const QPixmap& srcPx, const QPoint& pos)
62  {
63  const auto scaleFactor = 0.9;
64  const auto& availGeom = AvailableGeometry (pos).size () * scaleFactor;
65 
66  auto px = srcPx;
67  if (px.size ().width () > availGeom.width () ||
68  px.size ().height () > availGeom.height ())
69  px = px.scaled (availGeom, Qt::KeepAspectRatio, Qt::SmoothTransformation);
70 
71  const auto label = new QLabel;
72  label->setWindowFlags (Qt::Tool);
73  label->setAttribute (Qt::WA_DeleteOnClose);
74  label->setFixedSize (px.size ());
75  label->setPixmap (px);
76  label->show ();
77  label->activateWindow ();
78  label->installEventFilter (new AADisplayEventFilter (label));
79  label->move (pos);
80  return label;
81  }
82 
83  QColor TintColors (const QColor& c1, const QColor& c2, double alpha)
84  {
85  QColor color;
86  color.setRedF (alpha * c1.redF () + (1 - alpha) * c2.redF ());
87  color.setGreenF (alpha * c1.greenF () + (1 - alpha) * c2.greenF ());
88  color.setBlueF (alpha * c1.blueF () + (1 - alpha) * c2.blueF ());
89  return color;
90  }
91 
92  QString ElideProgressBarText (const QString& text, const QStyleOptionViewItem& option)
93  {
94  return option.fontMetrics.elidedText (text, Qt::ElideRight, option.rect.width ());
95  }
96 
97  void TintPalette (QWidget *widget, const QColor& color, double alpha, const QList<QPalette::ColorRole>& roles)
98  {
99  auto palette = widget->palette ();
100  for (auto role : roles)
101  palette.setColor (role, TintColors (palette.color (role), color, alpha));
102  widget->setPalette (palette);
103  }
104 
105  QString FormatName (const QString& name)
106  {
107  return "<em>" + name + "</em>";
108  }
109 
110  QPixmap DrawOverlayText (QPixmap px,
111  const QString& text, QFont font, const QPen& pen, const QBrush& brush)
112  {
113  const auto& iconSize = px.size () / px.devicePixelRatio ();
114 
115  const auto fontHeight = iconSize.height () * 0.45;
116  const auto minFontHeight = 6.0;
117  font.setPixelSize (static_cast<int> (std::max (minFontHeight, fontHeight)));
118 
119  const QFontMetrics fm (font);
120  const auto width = fm.horizontalAdvance (text) + 2. * iconSize.width () / 10.;
121  const auto height = fm.height () + 2. * iconSize.height () / 10.;
122  const bool tooSmall = width > iconSize.width ();
123 
124  const QRectF textRect (iconSize.width () - width, iconSize.height () - height, width, height);
125 
126  QPainter p (&px);
127  p.setBrush (brush);
128  p.setFont (font);
129  p.setPen (pen);
130  p.setRenderHint (QPainter::Antialiasing);
131  p.setRenderHint (QPainter::TextAntialiasing);
132  p.drawRoundedRect (textRect, 4, 4);
133  p.drawText (textRect,
134  Qt::AlignCenter,
135  tooSmall ? QStringLiteral ("#") : text);
136  p.end ();
137 
138  return px;
139  }
140 }
LC::Util::FormatName
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:111
LC::Util::ElideProgressBarText
QString ElideProgressBarText(const QString &text, const QStyleOptionViewItem &option)
Definition: util.cpp:98
LC::Util::AvailableGeometry
QRect AvailableGeometry(const QPoint &p)
Definition: geometry.cpp:72
geometry.h
QList
Definition: ianrulesstorage.h:14
LC::Util::TintPalette
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:103
LC::Util::DrawOverlayText
QPixmap DrawOverlayText(QPixmap px, const QString &text, QFont font, const QPen &pen, const QBrush &brush)
Definition: util.cpp:116
LC::Util
Definition: icoreproxy.h:33
LC::Util::TintColors
QColor TintColors(const QColor &c1, const QColor &c2, double alpha)
Mixes two colors with the given weights.
Definition: util.cpp:89
LC::Util::oral::sph::max
constexpr detail::AggregateType< detail::AggregateFunction::Max, Ptr > max
Definition: oral.h:972
LC::Util::ShowPixmapLabel
QLabel * ShowPixmapLabel(const QPixmap &srcPx, const QPoint &pos)
Shows a pixmap at the given pos.
Definition: util.cpp:67
util.h