Settings: unify duplicated code
[lxde/liblxqt.git] / lxqtrotatedwidget.cpp
CommitLineData
4182f457
KS
1/* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 *
b9223fe7 4 * LXQt - a lightweight, Qt based, desktop toolset
4182f457
KS
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
b6483430 29#include "lxqtrotatedwidget.h"
4182f457
KS
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
f05ba5af 44using namespace LXQt;
4182f457 45
5cc7332d 46RotatedWidget::RotatedWidget(QWidget &content, QWidget *parent, Qt::WindowFlags f)
4182f457 47 : QWidget(parent, f)
5cc7332d 48 , mContent(&content)
4182f457 49 , mOrigin(Qt::TopLeftCorner)
3c68ef86
KS
50 , mTransferMousePressEvent(false)
51 , mTransferMouseReleaseEvent(false)
52 , mTransferMouseDoubleClickEvent(false)
53 , mTransferMouseMoveEvent(false)
54#ifndef QT_NO_WHEELEVENT
55 , mTransferWheelEvent(false)
56#endif
57 , mTransferEnterEvent(false)
58 , mTransferLeaveEvent(false)
4182f457
KS
59{
60 mContent->setParent(this);
4182f457
KS
61}
62
63Qt::Corner RotatedWidget::origin() const
64{
65 return mOrigin;
66}
67
68void RotatedWidget::setOrigin(Qt::Corner newOrigin)
69{
70 if (mOrigin != newOrigin)
71 {
3c68ef86
KS
72 if (mOrigin == Qt::TopLeftCorner)
73 mContent->hide();
74
4182f457
KS
75 mOrigin = newOrigin;
76 adjustContentSize();
77 update();
3c68ef86
KS
78
79 if (mOrigin == Qt::TopLeftCorner)
80 mContent->show();
4182f457
KS
81 }
82}
83
84QWidget * RotatedWidget::content() const
85{
86 return mContent;
87}
88
89void RotatedWidget::adjustContentSize()
90{
91 mContent->adjustSize();
92
93 QSize before = size();
94
95 adjustSize();
96
97 if (before != size())
98 updateGeometry();
99}
100
43ceadfa 101QSize RotatedWidget::adjustedSize(QSize size) const
4182f457 102{
4182f457
KS
103 switch (mOrigin)
104 {
105 case Qt::TopLeftCorner:
106 case Qt::BottomRightCorner:
107 break;
108
109 case Qt::TopRightCorner:
110 case Qt::BottomLeftCorner:
43ceadfa 111 size.transpose();
4182f457
KS
112 break;
113 }
114
43ceadfa 115 return size;
4182f457
KS
116}
117
43ceadfa 118QPoint RotatedWidget::adjustedPoint(QPoint point) const
4182f457 119{
4182f457
KS
120 switch (mOrigin)
121 {
122 case Qt::TopLeftCorner:
123 case Qt::BottomRightCorner:
124 break;
125
126 case Qt::TopRightCorner:
127 case Qt::BottomLeftCorner:
43ceadfa 128 point = QPoint(point.y(), point.x());
4182f457
KS
129 break;
130 }
131
43ceadfa
KS
132 return point;
133}
134
135QSize RotatedWidget::minimumSizeHint() const
136{
137 return adjustedSize(mContent->minimumSizeHint());
138}
139
140QSize RotatedWidget::sizeHint() const
141{
142 return adjustedSize(mContent->sizeHint());
4182f457
KS
143}
144
145void RotatedWidget::paintEvent(QPaintEvent */*event*/)
146{
3c68ef86
KS
147 if (mOrigin == Qt::TopLeftCorner)
148 return;
149
4182f457 150 QSize sz = mContent->size();
4182f457
KS
151
152 QPainter painter(this);
4182f457
KS
153
154 QTransform transform;
a938e0d0 155 QPoint originPoint;
4182f457
KS
156 switch (mOrigin)
157 {
158 case Qt::TopLeftCorner:
159// transform.rotate(0.0);
a938e0d0 160// originPoint = QPoint(0.0, 0.0);
4182f457
KS
161 break;
162
163 case Qt::TopRightCorner:
164 transform.rotate(90.0);
a938e0d0 165 originPoint = QPoint(0.0, -sz.height());
4182f457
KS
166 break;
167
168 case Qt::BottomRightCorner:
169 transform.rotate(180.0);
a938e0d0 170 originPoint = QPoint(-sz.width(), -sz.height());
4182f457
KS
171 break;
172
173 case Qt::BottomLeftCorner:
174 transform.rotate(270.0);
a938e0d0 175 originPoint = QPoint(-sz.width(), 0.0);
4182f457
KS
176 break;
177 }
4182f457 178 painter.setTransform(transform);
a938e0d0 179 mContent->render(&painter, originPoint, QRegion(), RenderFlags(DrawChildren));
4182f457 180}
b7d6c34d
KS
181
182void RotatedWidget::mousePressEvent(QMouseEvent *event)
183{
3c68ef86
KS
184 if (!mTransferMousePressEvent)
185 {
186 event->ignore();
187 return;
188 }
189
b7d6c34d
KS
190 static bool cascadeCall = false;
191 if (cascadeCall)
192 return;
193 cascadeCall = true;
194
195 QMouseEvent contentEvent(event->type(), adjustedPoint(event->pos()), event->globalPos(), event->button(), event->buttons(), event->modifiers());
196 QApplication::sendEvent(mContent, &contentEvent);
197
198 cascadeCall = false;
199}
200
201void RotatedWidget::mouseReleaseEvent(QMouseEvent *event)
202{
3c68ef86
KS
203 if (!mTransferMouseReleaseEvent)
204 {
205 event->ignore();
206 return;
207 }
208
b7d6c34d
KS
209 static bool cascadeCall = false;
210 if (cascadeCall)
211 return;
212 cascadeCall = true;
213
214 QMouseEvent contentEvent(event->type(), adjustedPoint(event->pos()), event->globalPos(), event->button(), event->buttons(), event->modifiers());
215 QApplication::sendEvent(mContent, &contentEvent);
216
217 cascadeCall = false;
218}
219
220void RotatedWidget::mouseDoubleClickEvent(QMouseEvent *event)
221{
3c68ef86
KS
222 if (!mTransferMouseDoubleClickEvent)
223 {
224 event->ignore();
225 return;
226 }
227
b7d6c34d
KS
228 static bool cascadeCall = false;
229 if (cascadeCall)
230 return;
231 cascadeCall = true;
232
233 QMouseEvent contentEvent(event->type(), adjustedPoint(event->pos()), event->globalPos(), event->button(), event->buttons(), event->modifiers());
234 QApplication::sendEvent(mContent, &contentEvent);
235
236 cascadeCall = false;
237}
238
239void RotatedWidget::mouseMoveEvent(QMouseEvent *event)
240{
3c68ef86
KS
241 if (!mTransferMouseMoveEvent)
242 {
243 event->ignore();
244 return;
245 }
246
b7d6c34d
KS
247 static bool cascadeCall = false;
248 if (cascadeCall)
249 return;
250 cascadeCall = true;
251
252 QMouseEvent contentEvent(event->type(), adjustedPoint(event->pos()), event->globalPos(), event->button(), event->buttons(), event->modifiers());
253 QApplication::sendEvent(mContent, &contentEvent);
254
255 cascadeCall = false;
256}
257
258#ifndef QT_NO_WHEELEVENT
259void RotatedWidget::wheelEvent(QWheelEvent *event)
260{
3c68ef86
KS
261 if (!mTransferWheelEvent)
262 {
263 event->ignore();
264 return;
265 }
266
b7d6c34d
KS
267 static bool cascadeCall = false;
268 if (cascadeCall)
269 return;
270 cascadeCall = true;
271
272 QWheelEvent contentEvent(adjustedPoint(event->pos()), event->globalPos(), event->delta(), event->buttons(), event->modifiers(), event->orientation());
273 QApplication::sendEvent(mContent, &contentEvent);
274
275 cascadeCall = false;
276}
277#endif
278
279void RotatedWidget::enterEvent(QEvent *event)
280{
3c68ef86
KS
281 if (!mTransferEnterEvent)
282 {
283 event->ignore();
284 return;
285 }
286
b7d6c34d
KS
287 static bool cascadeCall = false;
288 if (cascadeCall)
289 return;
290 cascadeCall = true;
291
292 QApplication::sendEvent(mContent, event);
293
294 cascadeCall = false;
295}
296
297void RotatedWidget::leaveEvent(QEvent *event)
298{
3c68ef86
KS
299 if (!mTransferLeaveEvent)
300 {
301 event->ignore();
302 return;
303 }
304
b7d6c34d
KS
305 static bool cascadeCall = false;
306 if (cascadeCall)
307 return;
308 cascadeCall = true;
309
310 QApplication::sendEvent(mContent, event);
311
312 cascadeCall = false;
313}
314
315void RotatedWidget::resizeEvent(QResizeEvent *event)
316{
317 static bool cascadeCall = false;
318 if (cascadeCall)
319 return;
320 cascadeCall = true;
321
322 QResizeEvent contentEvent(adjustedSize(event->size()), adjustedSize(event->oldSize()));
323 QApplication::sendEvent(mContent, &contentEvent);
324
325 cascadeCall = false;
326}