1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
37 #include <headless/svpgdi.hxx>
39 Qt5Widget::Qt5Widget(Qt5Frame
& rFrame
, QWidget
* parent
, Qt::WindowFlags f
)
44 setMouseTracking(true);
45 setFocusPolicy(Qt::StrongFocus
);
48 Qt5Widget::~Qt5Widget() {}
50 void Qt5Widget::paintEvent(QPaintEvent
* pEvent
)
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(),
60 p
.drawImage(pEvent
->rect().topLeft(), aImage
, pEvent
->rect());
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
);
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
)
94 switch (pEvent
->button())
97 aEvent
.mnButton
= MOUSE_LEFT
;
100 aEvent
.mnButton
= MOUSE_MIDDLE
;
102 case Qt::RightButton
:
103 aEvent
.mnButton
= MOUSE_RIGHT
;
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());
116 nEventType
= SalEvent::MouseButtonUp
;
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());
135 m_pFrame
->CallCallback(SalEvent::MouseMove
, &aEvent
);
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;
152 nDelta
= pEvent
->angleDelta().y();
153 aEvent
.mbHorz
= false;
159 aEvent
.mnDelta
= nDelta
;
160 aEvent
.mnNotchDelta
= nDelta
> 0 ? 1 : -1;
161 aEvent
.mnScrollLines
= 3;
163 m_pFrame
->CallCallback(SalEvent::WheelMouse
, &aEvent
);
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
);
210 case Qt::Key_PageDown
:
211 nCode
= KEY_PAGEDOWN
;
222 case Qt::Key_Backspace
:
223 nCode
= KEY_BACKSPACE
;
238 nCode
= KEY_SUBTRACT
;
240 case Qt::Key_Asterisk
:
241 nCode
= KEY_MULTIPLY
;
255 case Qt::Key_Greater
:
265 nCode
= KEY_CONTEXTMENU
;
279 case Qt::Key_AsciiTilde
:
282 case Qt::Key_QuoteLeft
:
283 nCode
= KEY_QUOTELEFT
;
285 case Qt::Key_BracketLeft
:
286 nCode
= KEY_BRACKETLEFT
;
288 case Qt::Key_BracketRight
:
289 nCode
= KEY_BRACKETRIGHT
;
291 case Qt::Key_Semicolon
:
292 nCode
= KEY_SEMICOLON
;
312 bool Qt5Widget::handleKeyEvent(QKeyEvent
* pEvent
, bool bDown
)
316 aEvent
.mnCharCode
= (pEvent
->text().isEmpty() ? 0 : pEvent
->text().at(0).unicode());
318 aEvent
.mnCode
= GetKeyCode(pEvent
->key());
319 aEvent
.mnCode
|= GetKeyModCode(pEvent
->modifiers());
321 bool bStopProcessingKey
;
323 bStopProcessingKey
= m_pFrame
->CallCallback(SalEvent::KeyInput
, &aEvent
);
325 bStopProcessingKey
= m_pFrame
->CallCallback(SalEvent::KeyUp
, &aEvent
);
326 return bStopProcessingKey
;
329 void Qt5Widget::keyPressEvent(QKeyEvent
* pEvent
)
331 if (handleKeyEvent(pEvent
, true))
335 void Qt5Widget::keyReleaseEvent(QKeyEvent
* pEvent
)
337 if (handleKeyEvent(pEvent
, false))
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: */