LeechCraft  0.6.70-13729-g7046a9d2a7
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  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "util.h"
31 #include <QSize>
32 #include <QApplication>
33 #include <QDesktopWidget>
34 #include <QKeyEvent>
35 #include <QTimer>
36 #include <QLabel>
37 #include <QStyleOptionViewItem>
38 #include <QtDebug>
39 #include "geometry.h"
40 
41 namespace LC
42 {
43 namespace Util
44 {
45  namespace
46  {
47  class AADisplayEventFilter : public QObject
48  {
49  QWidget *Display_;
50  public:
51  explicit AADisplayEventFilter (QWidget *display)
52  : QObject (display)
53  , Display_ (display)
54  {
55  }
56  protected:
57  bool eventFilter (QObject*, QEvent *event) override
58  {
59  bool shouldClose = false;
60  switch (event->type ())
61  {
62  case QEvent::KeyRelease:
63  shouldClose = static_cast<QKeyEvent*> (event)->key () == Qt::Key_Escape;
64  break;
65  case QEvent::MouseButtonRelease:
66  shouldClose = true;
67  break;
68  default:
69  break;
70  }
71 
72  if (!shouldClose)
73  return false;
74 
75  QTimer::singleShot (0,
76  Display_,
77  SLOT (close ()));
78  return true;
79  }
80  };
81  }
82 
83  QLabel* ShowPixmapLabel (const QPixmap& srcPx, const QPoint& pos)
84  {
85  const auto& availGeom = AvailableGeometry (pos).size () * 0.9;
86 
87  auto px = srcPx;
88  if (px.size ().width () > availGeom.width () ||
89  px.size ().height () > availGeom.height ())
90  px = px.scaled (availGeom, Qt::KeepAspectRatio, Qt::SmoothTransformation);
91 
92  auto label = new QLabel;
93  label->setWindowFlags (Qt::Tool);
94  label->setAttribute (Qt::WA_DeleteOnClose);
95  label->setFixedSize (px.size ());
96  label->setPixmap (px);
97  label->show ();
98  label->activateWindow ();
99  label->installEventFilter (new AADisplayEventFilter (label));
100  label->move (pos);
101  return label;
102  }
103 
104  QColor TintColors (const QColor& c1, const QColor& c2, double alpha)
105  {
106  QColor color;
107  color.setRedF (alpha * c1.redF () + (1 - alpha) * c2.redF ());
108  color.setGreenF (alpha * c1.greenF () + (1 - alpha) * c2.greenF ());
109  color.setBlueF (alpha * c1.blueF () + (1 - alpha) * c2.blueF ());
110  return color;
111  }
112 
113  QString ElideProgressBarText (const QString& text, const QStyleOptionViewItem& option)
114  {
115  return option.fontMetrics.elidedText (text, Qt::ElideRight, option.rect.width ());
116  }
117 
118  void TintPalette (QWidget *widget, const QColor& color, double alpha, const QList<QPalette::ColorRole>& roles)
119  {
120  auto palette = widget->palette ();
121  for (auto role : roles)
122  palette.setColor (role, TintColors (palette.color (role), color, alpha));
123  widget->setPalette (palette);
124  }
125 
126  QString FormatName (const QString& name)
127  {
128  return "<em>" + name + "</em>";
129  }
130 }
131 }
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:126
QString ElideProgressBarText(const QString &text, const QStyleOptionViewItem &option)
Definition: util.cpp:113
QRect AvailableGeometry(const QPoint &p)
Definition: geometry.cpp:87
void TintPalette(QWidget *widget, const QColor &color, double alpha, const QList< QPalette::ColorRole > &roles)
Mixes some of the widget&#39;s palette roles with the given color.
Definition: util.cpp:118
QColor TintColors(const QColor &c1, const QColor &c2, double alpha)
Mixes two colors with the given weights.
Definition: util.cpp:104
QLabel * ShowPixmapLabel(const QPixmap &srcPx, const QPoint &pos)
Shows a pixmap at the given pos.
Definition: util.cpp:83
Definition: constants.h:35