RazorAutostartEntry => LxQt::AutostartEntry
[lxde/liblxqt.git] / rotatedwidget.cpp
CommitLineData
4182f457
KS
1/* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 *
4 * Razor - a lightweight, Qt based, desktop toolset
5 * http://razor-qt.org
6 *
7 * Copyright: 2010-2013 Razor team
8 * Authors:
9 * Kuzma Shapran <kuzma.shapran@gmail.com>
10 *
11 * This program or library is free software; you can redistribute it
12 * and/or modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20
21 * You should have received a copy of the GNU Lesser General
22 * Public License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301 USA
25 *
26 * END_COMMON_COPYRIGHT_HEADER */
27
28
29#include "rotatedwidget.h"
30
31#include <QPainter>
32#include <QImage>
33
b7d6c34d
KS
34#include <QApplication>
35
36#include <QMouseEvent>
37
38#ifndef QT_NO_WHEELEVENT
39#include <QWheelEvent>
40#endif
41
42#include <QResizeEvent>
43
4182f457 44
5cc7332d 45RotatedWidget::RotatedWidget(QWidget &content, QWidget *parent, Qt::WindowFlags f)
4182f457 46 : QWidget(parent, f)
5cc7332d 47 , mContent(&content)
4182f457 48 , mOrigin(Qt::TopLeftCorner)
3c68ef86
KS
49 , mTransferMousePressEvent(false)
50 , mTransferMouseReleaseEvent(false)
51 , mTransferMouseDoubleClickEvent(false)
52 , mTransferMouseMoveEvent(false)
53#ifndef QT_NO_WHEELEVENT
54 , mTransferWheelEvent(false)
55#endif
56 , mTransferEnterEvent(false)
57 , mTransferLeaveEvent(false)
4182f457
KS
58{
59 mContent->setParent(this);
4182f457
KS
60}
61
62Qt::Corner RotatedWidget::origin() const
63{
64 return mOrigin;
65}
66
67void RotatedWidget::setOrigin(Qt::Corner newOrigin)
68{
69 if (mOrigin != newOrigin)
70 {
3c68ef86
KS
71 if (mOrigin == Qt::TopLeftCorner)
72 mContent->hide();
73
4182f457
KS
74 mOrigin = newOrigin;
75 adjustContentSize();
76 update();
3c68ef86
KS
77
78 if (mOrigin == Qt::TopLeftCorner)
79 mContent->show();
4182f457
KS
80 }
81}
82
83QWidget * RotatedWidget::content() const
84{
85 return mContent;
86}
87
88void RotatedWidget::adjustContentSize()
89{
90 mContent->adjustSize();
91
92 QSize before = size();
93
94 adjustSize();
95
96 if (before != size())
97 updateGeometry();
98}
99
43ceadfa 100QSize RotatedWidget::adjustedSize(QSize size) const
4182f457 101{
4182f457
KS
102 switch (mOrigin)
103 {
104 case Qt::TopLeftCorner:
105 case Qt::BottomRightCorner:
106 break;
107
108 case Qt::TopRightCorner:
109 case Qt::BottomLeftCorner:
43ceadfa 110 size.transpose();
4182f457
KS
111 break;
112 }
113
43ceadfa 114 return size;
4182f457
KS
115}
116
43ceadfa 117QPoint RotatedWidget::adjustedPoint(QPoint point) const
4182f457 118{
4182f457
KS
119 switch (mOrigin)
120 {
121 case Qt::TopLeftCorner:
122 case Qt::BottomRightCorner:
123 break;
124
125 case Qt::TopRightCorner:
126 case Qt::BottomLeftCorner:
43ceadfa 127 point = QPoint(point.y(), point.x());
4182f457
KS
128 break;
129 }
130
43ceadfa
KS
131 return point;
132}
133
134QSize RotatedWidget::minimumSizeHint() const
135{
136 return adjustedSize(mContent->minimumSizeHint());
137}
138
139QSize RotatedWidget::sizeHint() const
140{
141 return adjustedSize(mContent->sizeHint());
4182f457
KS
142}
143
144void RotatedWidget::paintEvent(QPaintEvent */*event*/)
145{
3c68ef86
KS
146 if (mOrigin == Qt::TopLeftCorner)
147 return;
148
4182f457 149 QSize sz = mContent->size();
4182f457
KS
150
151 QPainter painter(this);
4182f457
KS
152
153 QTransform transform;
a938e0d0 154 QPoint originPoint;
4182f457
KS
155 switch (mOrigin)
156 {
157 case Qt::TopLeftCorner:
158// transform.rotate(0.0);
a938e0d0 159// originPoint = QPoint(0.0, 0.0);
4182f457
KS
160 break;
161
162 case Qt::TopRightCorner:
163 transform.rotate(90.0);
a938e0d0 164 originPoint = QPoint(0.0, -sz.height());
4182f457
KS
165 break;
166
167 case Qt::BottomRightCorner:
168 transform.rotate(180.0);
a938e0d0 169 originPoint = QPoint(-sz.width(), -sz.height());
4182f457
KS
170 break;
171
172 case Qt::BottomLeftCorner:
173 transform.rotate(270.0);
a938e0d0 174 originPoint = QPoint(-sz.width(), 0.0);
4182f457
KS
175 break;
176 }
4182f457 177 painter.setTransform(transform);
a938e0d0 178 mContent->render(&painter, originPoint, QRegion(), RenderFlags(DrawChildren));
4182f457 179}
b7d6c34d
KS
180
181void RotatedWidget::mousePressEvent(QMouseEvent *event)
182{
3c68ef86
KS
183 if (!mTransferMousePressEvent)
184 {
185 event->ignore();
186 return;
187 }
188
b7d6c34d
KS
189 static bool cascadeCall = false;
190 if (cascadeCall)
191 return;
192 cascadeCall = true;
193
194 QMouseEvent contentEvent(event->type(), adjustedPoint(event->pos()), event->globalPos(), event->button(), event->buttons(), event->modifiers());
195 QApplication::sendEvent(mContent, &contentEvent);
196
197 cascadeCall = false;
198}
199
200void RotatedWidget::mouseReleaseEvent(QMouseEvent *event)
201{
3c68ef86
KS
202 if (!mTransferMouseReleaseEvent)
203 {
204 event->ignore();
205 return;
206 }
207
b7d6c34d
KS
208 static bool cascadeCall = false;
209 if (cascadeCall)
210 return;
211 cascadeCall = true;
212
213 QMouseEvent contentEvent(event->type(), adjustedPoint(event->pos()), event->globalPos(), event->button(), event->buttons(), event->modifiers());
214 QApplication::sendEvent(mContent, &contentEvent);
215
216 cascadeCall = false;
217}
218
219void RotatedWidget::mouseDoubleClickEvent(QMouseEvent *event)
220{
3c68ef86
KS
221 if (!mTransferMouseDoubleClickEvent)
222 {
223 event->ignore();
224 return;
225 }
226
b7d6c34d
KS
227 static bool cascadeCall = false;
228 if (cascadeCall)
229 return;
230 cascadeCall = true;
231
232 QMouseEvent contentEvent(event->type(), adjustedPoint(event->pos()), event->globalPos(), event->button(), event->buttons(), event->modifiers());
233 QApplication::sendEvent(mContent, &contentEvent);
234
235 cascadeCall = false;
236}
237
238void RotatedWidget::mouseMoveEvent(QMouseEvent *event)
239{
3c68ef86
KS
240 if (!mTransferMouseMoveEvent)
241 {
242 event->ignore();
243 return;
244 }
245
b7d6c34d
KS
246 static bool cascadeCall = false;
247 if (cascadeCall)
248 return;
249 cascadeCall = true;
250
251 QMouseEvent contentEvent(event->type(), adjustedPoint(event->pos()), event->globalPos(), event->button(), event->buttons(), event->modifiers());
252 QApplication::sendEvent(mContent, &contentEvent);
253
254 cascadeCall = false;
255}
256
257#ifndef QT_NO_WHEELEVENT
258void RotatedWidget::wheelEvent(QWheelEvent *event)
259{
3c68ef86
KS
260 if (!mTransferWheelEvent)
261 {
262 event->ignore();
263 return;
264 }
265
b7d6c34d
KS
266 static bool cascadeCall = false;
267 if (cascadeCall)
268 return;
269 cascadeCall = true;
270
271 QWheelEvent contentEvent(adjustedPoint(event->pos()), event->globalPos(), event->delta(), event->buttons(), event->modifiers(), event->orientation());
272 QApplication::sendEvent(mContent, &contentEvent);
273
274 cascadeCall = false;
275}
276#endif
277
278void RotatedWidget::enterEvent(QEvent *event)
279{
3c68ef86
KS
280 if (!mTransferEnterEvent)
281 {
282 event->ignore();
283 return;
284 }
285
b7d6c34d
KS
286 static bool cascadeCall = false;
287 if (cascadeCall)
288 return;
289 cascadeCall = true;
290
291 QApplication::sendEvent(mContent, event);
292
293 cascadeCall = false;
294}
295
296void RotatedWidget::leaveEvent(QEvent *event)
297{
3c68ef86
KS
298 if (!mTransferLeaveEvent)
299 {
300 event->ignore();
301 return;
302 }
303
b7d6c34d
KS
304 static bool cascadeCall = false;
305 if (cascadeCall)
306 return;
307 cascadeCall = true;
308
309 QApplication::sendEvent(mContent, event);
310
311 cascadeCall = false;
312}
313
314void RotatedWidget::resizeEvent(QResizeEvent *event)
315{
316 static bool cascadeCall = false;
317 if (cascadeCall)
318 return;
319 cascadeCall = true;
320
321 QResizeEvent contentEvent(adjustedSize(event->size()), adjustedSize(event->oldSize()));
322 QApplication::sendEvent(mContent, &contentEvent);
323
324 cascadeCall = false;
325}