forked from linuxdeepin/qt5platform-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdinputselectionhandle.cpp
99 lines (80 loc) · 2.55 KB
/
dinputselectionhandle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// SPDX-FileCopyrightText: 2020 - 2022 Uniontech Software Technology Co.,Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "dinputselectionhandle.h"
#include "ddesktopinputselectioncontrol.h"
#include <QImageReader>
#include <QGuiApplication>
#include <QPainter>
#include <QPalette>
#include <QMouseEvent>
DPP_BEGIN_NAMESPACE
DInputSelectionHandle::DInputSelectionHandle(HandlePosition position, DDesktopInputSelectionControl *pControl)
: QRasterWindow()
, m_position(position)
, m_pInputSelectionControl(pControl)
{
setFlags(Qt::ToolTip | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::WindowDoesNotAcceptFocus);
QSurfaceFormat format;
format.setAlphaBufferSize(8);
setFormat(format);
updateImage(position);
}
DInputSelectionHandle::HandlePosition DInputSelectionHandle::handlePosition() const
{
return m_position;
}
void DInputSelectionHandle::setHandlePosition(HandlePosition position)
{
if (m_position == position) {
return;
}
m_position = position;
updateImage(position);
update();
}
QSize DInputSelectionHandle::handleImageSize() const
{
return m_image.size() / devicePixelRatio();
}
void DInputSelectionHandle::updateImage(HandlePosition position)
{
QImage handle;
QImageReader reader(position == Up ? ":/up_handle.svg" : ":/down_handle.svg");
QSize image_size(reader.size());
reader.setScaledSize(image_size * devicePixelRatio());
reader.read(&handle);
m_image = handle;
m_image.setDevicePixelRatio(devicePixelRatio());
}
void DInputSelectionHandle::paintEvent(QPaintEvent *pe)
{
Q_UNUSED(pe);
QPainter painter(this);
QImage image(m_image);
const QSize szDelta = size() - image.size();
QPainter pa(&image);
pa.setCompositionMode(QPainter::CompositionMode_SourceIn);
pa.fillRect(image.rect(), qApp->palette().highlight());
// center image onto window
painter.drawImage(QPointF(szDelta.width(), szDelta.height()) / 2, image);
}
void DInputSelectionHandle::mousePressEvent(QMouseEvent *event)
{
if (QWindow *focusWindow = QGuiApplication::focusWindow()) {
QCoreApplication::sendEvent(focusWindow, event);
}
}
void DInputSelectionHandle::mouseReleaseEvent(QMouseEvent *event)
{
if (QWindow *focusWindow = QGuiApplication::focusWindow()) {
QCoreApplication::sendEvent(focusWindow, event);
}
}
void DInputSelectionHandle::mouseMoveEvent(QMouseEvent *event)
{
if (QWindow *focusWindow = QGuiApplication::focusWindow()) {
QCoreApplication::sendEvent(focusWindow, event);
}
}
DPP_END_NAMESPACE