Omnidome
Fulldome Mapping Software Toolkit
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TransformedRect.h
Go to the documentation of this file.
1 /* Copyright (c) 2014-2016 "Omnidome" by cr8tr
2  * Dome Mapping Projection Software (http://omnido.me).
3  * Omnidome was created by Michael Winkelmann aka Wilston Oreo (@WilstonOreo)
4  *
5  * This file is part of Omnidome.
6  *
7  * Omnidome is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Affero General Public License for more details.
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef OMNI_UI_MIXIN_TRANSFORMEDRECT_H
21 #define OMNI_UI_MIXIN_TRANSFORMEDRECT_H
22 
23 #include <QRectF>
24 #include <QDebug>
25 
26 namespace omni {
27  namespace ui {
28  namespace mixin {
29  /**@brief Mixin which calculates transformed screen rectangles
30  @detail Used in export output preview and screen setup widgets
31  **/
32  template<typename WIDGET>
34  public:
35  typedef WIDGET widget_type;
36  TransformedRect(widget_type *_widget) : widget_(_widget) {}
37 
38  /// Pure virtual method which returns overall desktop rect
39  virtual QRect desktopRect() const = 0;
40 
41  /// Returns the scaling factor which is needed so that desktopRect_
42  // fits into window
43  float scalingFactor() const
44  {
45  if (!widget_) return 0.0;
46 
47  auto _windowRect = widget_->rect();
48 
49  if ((_windowRect.height() == 0) ||
50  (_windowRect.width() == 0)) return 0.0;
51 
52  QRect _desktopRect = this->desktopRect();
53 
54  if ((_desktopRect.height() == 0) ||
55  (_desktopRect.width() == 0)) return 0.0;
56 
57  float _rectAspect = float(_desktopRect.width()) /
58  float(_desktopRect.height());
59  float _windowAspect = float(_windowRect.width()) /
60  float(_windowRect.height());
61 
62  float _factor = 1.0f;
63 
64  if (_windowAspect < _rectAspect) {
65  _factor = float(_windowRect.width()) / _desktopRect.width();
66  } else {
67  _factor = float(_windowRect.height()) / _desktopRect.height();
68  }
69 
70  return _factor * zoom();
71  }
72 
73  /// Returns transformed bounding rect which fits into window and keeps
74  // aspect ratio
75  inline QRectF transformedRect() const
76  {
77  auto _windowRect = widget_->rect();
78  auto _zoom = scalingFactor();
79  QRect _desktopRect = this->desktopRect();
80 
81  return QRectF(
82  0.5 * (_windowRect.width() - _zoom * (_desktopRect.width())),
83  0.5 * (_windowRect.height() - _zoom * (_desktopRect.height())),
84  _zoom * _desktopRect.width(),
85  _zoom * _desktopRect.height());
86  }
87 
88  /// Returns Transformed copy of the rect, based on transformed desktop
89  // rect
90  inline QRectF transformedRect(QRectF const& _rect) const
91  {
92  auto _zoom = scalingFactor();
93 
94  /// Transformed desktop rect
95  auto _desktopRect = transformedRect();
96 
97  return QRectF(
98  _desktopRect.x() + _rect.x() * _zoom,
99  _desktopRect.y() + _rect.y() * _zoom,
100  _rect.width() * _zoom,
101  _rect.height() * _zoom);
102  }
103 
104  /// Return zoom factor
105  float zoom() const {
106  return zoom_;
107  }
108 
109  /// Set zoom factor
110  void setZoom(float _zoom)
111  {
112  zoom_ = _zoom;
113  widget_->update();
114  }
115 
116  private:
117  widget_type *widget_ = nullptr;
118  float zoom_ = 0.9;
119  };
120  }
121  }
122 }
123 
124 #endif /* OMNI_UI_MIXIN_TRANSFORMEDRECT_H */
QRectF transformedRect(QRectF const &_rect) const
Returns Transformed copy of the rect, based on transformed desktop.
Definition: TransformedRect.h:90
Mixin which calculates transformed screen rectangles Used in export output preview and screen setup ...
Definition: TransformedRect.h:33
float zoom_
Definition: TransformedRect.h:118
virtual QRect desktopRect() const =0
Pure virtual method which returns overall desktop rect.
float zoom() const
Return zoom factor.
Definition: TransformedRect.h:105
QRectF transformedRect() const
Returns transformed bounding rect which fits into window and keeps.
Definition: TransformedRect.h:75
float scalingFactor() const
Returns the scaling factor which is needed so that desktopRect_.
Definition: TransformedRect.h:43
TransformedRect(widget_type *_widget)
Definition: TransformedRect.h:36
void setZoom(float _zoom)
Set zoom factor.
Definition: TransformedRect.h:110
widget_type * widget_
Definition: TransformedRect.h:117
WIDGET widget_type
Definition: TransformedRect.h:35