LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 <QDesktopWidget>
37 #include <QLabel>
38 #include <QtDebug>
39 
40 namespace LeechCraft
41 {
42 namespace Util
43 {
44  QPoint FitRectScreen (QPoint pos, const QSize& size, FitFlags flags, const QPoint& shiftAdd)
45  {
46  return FitRect (pos, size, QApplication::desktop ()->screenGeometry (pos), flags, shiftAdd);
47  }
48 
49  QPoint FitRect (QPoint pos, const QSize& size, const QRect& geometry,
50  FitFlags flags, const QPoint& shiftAdd)
51  {
52  int xDiff = std::max (0, pos.x () + size.width () - (geometry.width () + geometry.x ()));
53  if (!xDiff)
54  xDiff = std::min (0, pos.x () - geometry.x ());
55  int yDiff = std::max (0, pos.y () + size.height () - (geometry.height () + geometry.y ()));
56  if (!yDiff)
57  yDiff = std::min (0, pos.y () - geometry.y ());
58 
59  if (flags & FitFlag::NoOverlap)
60  {
61  auto overlapFixer = [] (int& diff, int dim)
62  {
63  if (diff > 0)
64  diff = dim > diff ? dim : diff;
65  };
66 
67  if (QRect (pos - QPoint (xDiff, yDiff), size).contains (pos) && yDiff < size.height ())
68  overlapFixer (yDiff, size.height ());
69  if (QRect (pos - QPoint (xDiff, yDiff), size).contains (pos) && xDiff < size.width ())
70  overlapFixer (xDiff, size.width ());
71  }
72 
73  if (xDiff)
74  pos.rx () -= xDiff + shiftAdd.x ();
75  if (yDiff)
76  pos.ry () -= yDiff + shiftAdd.y ();
77 
78  return pos;
79  }
80 
81  namespace
82  {
83  class AADisplayEventFilter : public QObject
84  {
85  QWidget *Display_;
86  public:
87  AADisplayEventFilter (QWidget *display)
88  : QObject (display)
89  , Display_ (display)
90  {
91  }
92  protected:
93  bool eventFilter (QObject*, QEvent *event)
94  {
95  bool shouldClose = false;
96  switch (event->type ())
97  {
98  case QEvent::KeyRelease:
99  shouldClose = static_cast<QKeyEvent*> (event)->key () == Qt::Key_Escape;
100  break;
101  case QEvent::MouseButtonRelease:
102  shouldClose = true;
103  break;
104  default:
105  break;
106  }
107 
108  if (!shouldClose)
109  return false;
110 
111  QTimer::singleShot (0,
112  Display_,
113  SLOT (close ()));
114  return true;
115  }
116  };
117  }
118 
119  QLabel* ShowPixmapLabel (const QPixmap& srcPx, const QPoint& pos)
120  {
121  const auto& availGeom = QApplication::desktop ()->availableGeometry (pos).size () * 0.9;
122 
123  auto px = srcPx;
124  if (px.size ().width () > availGeom.width () ||
125  px.size ().height () > availGeom.height ())
126  px = px.scaled (availGeom, Qt::KeepAspectRatio, Qt::SmoothTransformation);
127 
128  auto label = new QLabel;
129  label->setWindowFlags (Qt::Tool);
130  label->setAttribute (Qt::WA_DeleteOnClose);
131  label->setFixedSize (px.size ());
132  label->setPixmap (px);
133  label->show ();
134  label->activateWindow ();
135  label->installEventFilter (new AADisplayEventFilter (label));
136  label->move (pos);
137  return label;
138  }
139 }
140 }
QLabel * ShowPixmapLabel(const QPixmap &srcPx, const QPoint &pos)
Shows a pixmap at the given pos.
Definition: util.cpp:119
QPoint FitRectScreen(QPoint pos, const QSize &size, FitFlags flags, const QPoint &shiftAdd)
Tries to fit a rectangle (like a dialog or popup) into screen.
Definition: util.cpp:44
QWidget * Display_
Definition: util.cpp:85
QPoint FitRect(QPoint pos, const QSize &size, const QRect &geometry, FitFlags flags, const QPoint &shiftAdd)
Tries to fit a rectangle (like a dialog or popup) into geometry.
Definition: util.cpp:49