Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / vcl / qt5 / Qt5Widget.cxx
blob6b082eef1c1f6121b85b4accb65e4a927272e8fe
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "Qt5Widget.hxx"
21 #include <Qt5Widget.moc>
23 #include "Qt5Frame.hxx"
24 #include "Qt5Graphics.hxx"
25 #include "Qt5Tools.hxx"
27 #include <QtGui/QFocusEvent>
28 #include <QtGui/QImage>
29 #include <QtGui/QKeyEvent>
30 #include <QtGui/QMouseEvent>
31 #include <QtGui/QPainter>
32 #include <QtGui/QPaintEvent>
33 #include <QtGui/QShowEvent>
34 #include <QtGui/QWheelEvent>
36 #include <cairo.h>
37 #include <headless/svpgdi.hxx>
39 Qt5Widget::Qt5Widget(Qt5Frame& rFrame, QWidget* parent, Qt::WindowFlags f)
40 : QWidget(parent, f)
41 , m_pFrame(&rFrame)
43 create();
44 setMouseTracking(true);
45 setFocusPolicy(Qt::StrongFocus);
48 Qt5Widget::~Qt5Widget() {}
50 void Qt5Widget::paintEvent(QPaintEvent* pEvent)
52 QPainter p(this);
53 if (m_pFrame->m_bUseCairo)
55 cairo_surface_t* pSurface = m_pFrame->m_pSurface.get();
56 cairo_surface_flush(pSurface);
58 QImage aImage(cairo_image_surface_get_data(pSurface), size().width(), size().height(),
59 Qt5_DefaultFormat32);
60 p.drawImage(pEvent->rect().topLeft(), aImage, pEvent->rect());
62 else
63 p.drawImage(pEvent->rect().topLeft(), *m_pFrame->m_pQImage, pEvent->rect());
66 void Qt5Widget::resizeEvent(QResizeEvent*)
68 if (m_pFrame->m_bUseCairo)
70 int width = size().width();
71 int height = size().height();
72 cairo_surface_t* pSurface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
73 cairo_surface_set_user_data(pSurface, SvpSalGraphics::getDamageKey(),
74 &m_pFrame->m_aDamageHandler, nullptr);
75 m_pFrame->m_pSvpGraphics->setSurface(pSurface, basegfx::B2IVector(width, height));
76 m_pFrame->m_pSurface.reset(pSurface);
78 else
80 QImage* pImage = new QImage(size(), Qt5_DefaultFormat32);
81 m_pFrame->m_pQt5Graphics->ChangeQImage(pImage);
82 m_pFrame->m_pQImage.reset(pImage);
85 m_pFrame->maGeometry.nWidth = size().width();
86 m_pFrame->maGeometry.nHeight = size().height();
88 m_pFrame->CallCallback(SalEvent::Resize, nullptr);
91 void Qt5Widget::handleMouseButtonEvent(QMouseEvent* pEvent, bool bReleased)
93 SalMouseEvent aEvent;
94 switch (pEvent->button())
96 case Qt::LeftButton:
97 aEvent.mnButton = MOUSE_LEFT;
98 break;
99 case Qt::MidButton:
100 aEvent.mnButton = MOUSE_MIDDLE;
101 break;
102 case Qt::RightButton:
103 aEvent.mnButton = MOUSE_RIGHT;
104 break;
105 default:
106 return;
109 aEvent.mnTime = pEvent->timestamp();
110 aEvent.mnX = static_cast<long>(pEvent->pos().x());
111 aEvent.mnY = static_cast<long>(pEvent->pos().y());
112 aEvent.mnCode = GetKeyModCode(pEvent->modifiers()) | GetMouseModCode(pEvent->buttons());
114 SalEvent nEventType;
115 if (bReleased)
116 nEventType = SalEvent::MouseButtonUp;
117 else
118 nEventType = SalEvent::MouseButtonDown;
119 m_pFrame->CallCallback(nEventType, &aEvent);
122 void Qt5Widget::mousePressEvent(QMouseEvent* pEvent) { handleMouseButtonEvent(pEvent, false); }
124 void Qt5Widget::mouseReleaseEvent(QMouseEvent* pEvent) { handleMouseButtonEvent(pEvent, true); }
126 void Qt5Widget::mouseMoveEvent(QMouseEvent* pEvent)
128 SalMouseEvent aEvent;
129 aEvent.mnTime = pEvent->timestamp();
130 aEvent.mnX = pEvent->pos().x();
131 aEvent.mnY = pEvent->pos().y();
132 aEvent.mnCode = GetKeyModCode(pEvent->modifiers()) | GetMouseModCode(pEvent->buttons());
133 aEvent.mnButton = 0;
135 m_pFrame->CallCallback(SalEvent::MouseMove, &aEvent);
136 pEvent->accept();
139 void Qt5Widget::wheelEvent(QWheelEvent* pEvent)
141 SalWheelMouseEvent aEvent;
143 aEvent.mnTime = pEvent->timestamp();
144 aEvent.mnX = pEvent->pos().x();
145 aEvent.mnY = pEvent->pos().y();
146 aEvent.mnCode = GetKeyModCode(pEvent->modifiers()) | GetMouseModCode(pEvent->buttons());
148 int nDelta = pEvent->angleDelta().x();
149 aEvent.mbHorz = true;
150 if (!nDelta)
152 nDelta = pEvent->angleDelta().y();
153 aEvent.mbHorz = false;
155 if (!nDelta)
156 return;
157 nDelta /= 8;
159 aEvent.mnDelta = nDelta;
160 aEvent.mnNotchDelta = nDelta > 0 ? 1 : -1;
161 aEvent.mnScrollLines = 3;
163 m_pFrame->CallCallback(SalEvent::WheelMouse, &aEvent);
164 pEvent->accept();
167 void Qt5Widget::moveEvent(QMoveEvent*) { m_pFrame->CallCallback(SalEvent::Move, nullptr); }
169 void Qt5Widget::showEvent(QShowEvent*)
171 QSize aSize(m_pFrame->m_pQWidget->size());
172 SalPaintEvent aPaintEvt(0, 0, aSize.width(), aSize.height(), true);
173 m_pFrame->CallCallback(SalEvent::Paint, &aPaintEvt);
176 static sal_uInt16 GetKeyCode(int keyval)
178 sal_uInt16 nCode = 0;
179 if (keyval >= Qt::Key_0 && keyval <= Qt::Key_9)
180 nCode = KEY_0 + (keyval - Qt::Key_0);
181 else if (keyval >= Qt::Key_A && keyval <= Qt::Key_Z)
182 nCode = KEY_A + (keyval - Qt::Key_A);
183 else if (keyval >= Qt::Key_F1 && keyval <= Qt::Key_F26)
184 nCode = KEY_F1 + (keyval - Qt::Key_F1);
185 else
187 switch (keyval)
189 case Qt::Key_Down:
190 nCode = KEY_DOWN;
191 break;
192 case Qt::Key_Up:
193 nCode = KEY_UP;
194 break;
195 case Qt::Key_Left:
196 nCode = KEY_LEFT;
197 break;
198 case Qt::Key_Right:
199 nCode = KEY_RIGHT;
200 break;
201 case Qt::Key_Home:
202 nCode = KEY_HOME;
203 break;
204 case Qt::Key_End:
205 nCode = KEY_END;
206 break;
207 case Qt::Key_PageUp:
208 nCode = KEY_PAGEUP;
209 break;
210 case Qt::Key_PageDown:
211 nCode = KEY_PAGEDOWN;
212 break;
213 case Qt::Key_Return:
214 nCode = KEY_RETURN;
215 break;
216 case Qt::Key_Escape:
217 nCode = KEY_ESCAPE;
218 break;
219 case Qt::Key_Tab:
220 nCode = KEY_TAB;
221 break;
222 case Qt::Key_Backspace:
223 nCode = KEY_BACKSPACE;
224 break;
225 case Qt::Key_Space:
226 nCode = KEY_SPACE;
227 break;
228 case Qt::Key_Insert:
229 nCode = KEY_INSERT;
230 break;
231 case Qt::Key_Delete:
232 nCode = KEY_DELETE;
233 break;
234 case Qt::Key_Plus:
235 nCode = KEY_ADD;
236 break;
237 case Qt::Key_Minus:
238 nCode = KEY_SUBTRACT;
239 break;
240 case Qt::Key_Asterisk:
241 nCode = KEY_MULTIPLY;
242 break;
243 case Qt::Key_Slash:
244 nCode = KEY_DIVIDE;
245 break;
246 case Qt::Key_Period:
247 nCode = KEY_POINT;
248 break;
249 case Qt::Key_Comma:
250 nCode = KEY_COMMA;
251 break;
252 case Qt::Key_Less:
253 nCode = KEY_LESS;
254 break;
255 case Qt::Key_Greater:
256 nCode = KEY_GREATER;
257 break;
258 case Qt::Key_Equal:
259 nCode = KEY_EQUAL;
260 break;
261 case Qt::Key_Find:
262 nCode = KEY_FIND;
263 break;
264 case Qt::Key_Menu:
265 nCode = KEY_CONTEXTMENU;
266 break;
267 case Qt::Key_Help:
268 nCode = KEY_HELP;
269 break;
270 case Qt::Key_Undo:
271 nCode = KEY_UNDO;
272 break;
273 case Qt::Key_Redo:
274 nCode = KEY_REPEAT;
275 break;
276 case Qt::Key_Cancel:
277 nCode = KEY_F11;
278 break;
279 case Qt::Key_AsciiTilde:
280 nCode = KEY_TILDE;
281 break;
282 case Qt::Key_QuoteLeft:
283 nCode = KEY_QUOTELEFT;
284 break;
285 case Qt::Key_BracketLeft:
286 nCode = KEY_BRACKETLEFT;
287 break;
288 case Qt::Key_BracketRight:
289 nCode = KEY_BRACKETRIGHT;
290 break;
291 case Qt::Key_Semicolon:
292 nCode = KEY_SEMICOLON;
293 break;
294 case Qt::Key_Copy:
295 nCode = KEY_COPY;
296 break;
297 case Qt::Key_Cut:
298 nCode = KEY_CUT;
299 break;
300 case Qt::Key_Open:
301 nCode = KEY_OPEN;
302 break;
303 case Qt::Key_Paste:
304 nCode = KEY_PASTE;
305 break;
309 return nCode;
312 bool Qt5Widget::handleKeyEvent(QKeyEvent* pEvent, bool bDown)
314 SalKeyEvent aEvent;
316 aEvent.mnCharCode = (pEvent->text().isEmpty() ? 0 : pEvent->text().at(0).unicode());
317 aEvent.mnRepeat = 0;
318 aEvent.mnCode = GetKeyCode(pEvent->key());
319 aEvent.mnCode |= GetKeyModCode(pEvent->modifiers());
321 bool bStopProcessingKey;
322 if (bDown)
323 bStopProcessingKey = m_pFrame->CallCallback(SalEvent::KeyInput, &aEvent);
324 else
325 bStopProcessingKey = m_pFrame->CallCallback(SalEvent::KeyUp, &aEvent);
326 return bStopProcessingKey;
329 void Qt5Widget::keyPressEvent(QKeyEvent* pEvent)
331 if (handleKeyEvent(pEvent, true))
332 pEvent->accept();
335 void Qt5Widget::keyReleaseEvent(QKeyEvent* pEvent)
337 if (handleKeyEvent(pEvent, false))
338 pEvent->accept();
341 void Qt5Widget::focusInEvent(QFocusEvent*) { m_pFrame->CallCallback(SalEvent::GetFocus, nullptr); }
343 void Qt5Widget::focusOutEvent(QFocusEvent*)
345 m_pFrame->CallCallback(SalEvent::LoseFocus, nullptr);
348 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */