RazorAutostartEntry => LxQt::AutostartEntry
[lxde/liblxqt.git] / rotatedwidget.cpp
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
34 #include <QApplication>
35
36 #include <QMouseEvent>
37
38 #ifndef QT_NO_WHEELEVENT
39 #include <QWheelEvent>
40 #endif
41
42 #include <QResizeEvent>
43
44
45 RotatedWidget::RotatedWidget(QWidget &content, QWidget *parent, Qt::WindowFlags f)
46 : QWidget(parent, f)
47 , mContent(&content)
48 , mOrigin(Qt::TopLeftCorner)
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)
58 {
59 mContent->setParent(this);
60 }
61
62 Qt::Corner RotatedWidget::origin() const
63 {
64 return mOrigin;
65 }
66
67 void RotatedWidget::setOrigin(Qt::Corner newOrigin)
68 {
69 if (mOrigin != newOrigin)
70 {
71 if (mOrigin == Qt::TopLeftCorner)
72 mContent->hide();
73
74 mOrigin = newOrigin;
75 adjustContentSize();
76 update();
77
78 if (mOrigin == Qt::TopLeftCorner)
79 mContent->show();
80 }
81 }
82
83 QWidget * RotatedWidget::content() const
84 {
85 return mContent;
86 }
87
88 void RotatedWidget::adjustContentSize()
89 {
90 mContent->adjustSize();
91
92 QSize before = size();
93
94 adjustSize();
95
96 if (before != size())
97 updateGeometry();
98 }
99
100 QSize RotatedWidget::adjustedSize(QSize size) const
101 {
102 switch (mOrigin)
103 {
104 case Qt::TopLeftCorner:
105 case Qt::BottomRightCorner:
106 break;
107
108 case Qt::TopRightCorner:
109 case Qt::BottomLeftCorner:
110 size.transpose();
111 break;
112 }
113
114 return size;
115 }
116
117 QPoint RotatedWidget::adjustedPoint(QPoint point) const
118 {
119 switch (mOrigin)
120 {
121 case Qt::TopLeftCorner:
122 case Qt::BottomRightCorner:
123 break;
124
125 case Qt::TopRightCorner:
126 case Qt::BottomLeftCorner:
127 point = QPoint(point.y(), point.x());
128 break;
129 }
130
131 return point;
132 }
133
134 QSize RotatedWidget::minimumSizeHint() const
135 {
136 return adjustedSize(mContent->minimumSizeHint());
137 }
138
139 QSize RotatedWidget::sizeHint() const
140 {
141 return adjustedSize(mContent->sizeHint());
142 }
143
144 void RotatedWidget::paintEvent(QPaintEvent */*event*/)
145 {
146 if (mOrigin == Qt::TopLeftCorner)
147 return;
148
149 QSize sz = mContent->size();
150
151 QPainter painter(this);
152
153 QTransform transform;
154 QPoint originPoint;
155 switch (mOrigin)
156 {
157 case Qt::TopLeftCorner:
158 // transform.rotate(0.0);
159 // originPoint = QPoint(0.0, 0.0);
160 break;
161
162 case Qt::TopRightCorner:
163 transform.rotate(90.0);
164 originPoint = QPoint(0.0, -sz.height());
165 break;
166
167 case Qt::BottomRightCorner:
168 transform.rotate(180.0);
169 originPoint = QPoint(-sz.width(), -sz.height());
170 break;
171
172 case Qt::BottomLeftCorner:
173 transform.rotate(270.0);
174 originPoint = QPoint(-sz.width(), 0.0);
175 break;
176 }
177 painter.setTransform(transform);
178 mContent->render(&painter, originPoint, QRegion(), RenderFlags(DrawChildren));
179 }
180
181 void RotatedWidget::mousePressEvent(QMouseEvent *event)
182 {
183 if (!mTransferMousePressEvent)
184 {
185 event->ignore();
186 return;
187 }
188
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
200 void RotatedWidget::mouseReleaseEvent(QMouseEvent *event)
201 {
202 if (!mTransferMouseReleaseEvent)
203 {
204 event->ignore();
205 return;
206 }
207
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
219 void RotatedWidget::mouseDoubleClickEvent(QMouseEvent *event)
220 {
221 if (!mTransferMouseDoubleClickEvent)
222 {
223 event->ignore();
224 return;
225 }
226
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
238 void RotatedWidget::mouseMoveEvent(QMouseEvent *event)
239 {
240 if (!mTransferMouseMoveEvent)
241 {
242 event->ignore();
243 return;
244 }
245
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
258 void RotatedWidget::wheelEvent(QWheelEvent *event)
259 {
260 if (!mTransferWheelEvent)
261 {
262 event->ignore();
263 return;
264 }
265
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
278 void RotatedWidget::enterEvent(QEvent *event)
279 {
280 if (!mTransferEnterEvent)
281 {
282 event->ignore();
283 return;
284 }
285
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
296 void RotatedWidget::leaveEvent(QEvent *event)
297 {
298 if (!mTransferLeaveEvent)
299 {
300 event->ignore();
301 return;
302 }
303
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
314 void 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 }