Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdesigner_dnditem.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
// Qt-Security score:significant reason:default
4
5
#
include
"qdesigner_dnditem_p.h"
6
#
include
"formwindowbase_p.h"
7
#
include
<
QtDesigner
/
private
/
ui4_p
.
h
>
8
9
#
include
<
QtGui
/
qpainter
.
h
>
10
#
include
<
QtGui
/
qbitmap
.
h
>
11
#
include
<
QtGui
/
qpixmap
.
h
>
12
#
include
<
QtGui
/
qimage
.
h
>
13
#
include
<
QtWidgets
/
qlabel
.
h
>
14
#
include
<
QtGui
/
qdrag
.
h
>
15
#
include
<
QtGui
/
qcursor
.
h
>
16
#
include
<
QtGui
/
qevent
.
h
>
17
#
include
<
QtGui
/
qrgb
.
h
>
18
19
#
include
<
QtCore
/
qmap
.
h
>
20
21
QT_BEGIN_NAMESPACE
22
23
namespace
qdesigner_internal
{
24
25
QDesignerDnDItem
::
QDesignerDnDItem
(
DropType
type
,
QWidget
*
source
) :
26
m_source
(
source
),
27
m_type
(
type
),
28
m_dom_ui
(
nullptr
),
29
m_widget
(
nullptr
),
30
m_decoration
(
nullptr
)
31
{
32
}
33
34
void
QDesignerDnDItem
::
init
(
DomUI
*
ui
,
QWidget
*
widget
,
QWidget
*
decoration
,
35
const
QPoint
&
global_mouse_pos
)
36
{
37
Q_ASSERT
(
widget
!=
nullptr
||
ui
!=
nullptr
);
38
Q_ASSERT
(
decoration
!=
nullptr
);
39
40
m_dom_ui
=
ui
;
41
m_widget
=
widget
;
42
m_decoration
=
decoration
;
43
44
m_hot_spot
=
global_mouse_pos
-
m_decoration
->
geometry
().
topLeft
();
45
}
46
47
QDesignerDnDItem
::~
QDesignerDnDItem
()
48
{
49
if
(
m_decoration
!=
nullptr
)
50
m_decoration
->
deleteLater
();
51
delete
m_dom_ui
;
52
}
53
54
DomUI
*
QDesignerDnDItem
::
domUi
()
const
55
{
56
return
m_dom_ui
;
57
}
58
59
QWidget
*
QDesignerDnDItem
::
decoration
()
const
60
{
61
return
m_decoration
;
62
}
63
64
QPoint
QDesignerDnDItem
::
hotSpot
()
const
65
{
66
return
m_hot_spot
;
67
}
68
69
QWidget
*
QDesignerDnDItem
::
widget
()
const
70
{
71
return
m_widget
;
72
}
73
74
QDesignerDnDItem
::
DropType
QDesignerDnDItem
::
type
()
const
75
{
76
return
m_type
;
77
}
78
79
QWidget
*
QDesignerDnDItem
::
source
()
const
80
{
81
return
m_source
;
82
}
83
84
void
QDesignerDnDItem
::
setDomUi
(
DomUI
*
dom_ui
)
85
{
86
delete
m_dom_ui
;
87
m_dom_ui
=
dom_ui
;
88
}
89
90
// ---------- QDesignerMimeData
91
92
// Make pixmap transparent on Windows only. Mac is transparent by default, Unix usually does not work.
93
#
ifdef
Q_OS_WIN
94
#
define
TRANSPARENT_DRAG_PIXMAP
95
#
endif
96
97
QDesignerMimeData
::
QDesignerMimeData
(
const
QDesignerDnDItems
&
items
,
QDrag
*
drag
) :
98
m_items
(
items
)
99
{
100
enum
{
Alpha
= 200 };
101
QPoint
decorationTopLeft
;
102
switch
(
m_items
.
size
()) {
103
case
0:
104
break
;
105
case
1: {
106
QWidget
*
deco
=
m_items
.
first
()->
decoration
();
107
decorationTopLeft
=
deco
->
pos
();
108
const
QPixmap
widgetPixmap
=
deco
->
grab
(
QRect
(0, 0, -1, -1));
109
#
ifdef
TRANSPARENT_DRAG_PIXMAP
110
QImage
image
(
widgetPixmap
.
size
(),
QImage
::
Format_ARGB32
);
111
image
.
setDevicePixelRatio
(
widgetPixmap
.
devicePixelRatio
());
112
image
.
fill
(
QColor
(
Qt
::
transparent
).
rgba
());
113
QPainter
painter
(&
image
);
114
painter
.
drawPixmap
(
QPoint
(0, 0),
widgetPixmap
);
115
painter
.
end
();
116
setImageTransparency
(
image
,
Alpha
);
117
drag
->
setPixmap
(
QPixmap
::
fromImage
(
image
));
118
#
else
119
drag
->
setPixmap
(
widgetPixmap
);
120
#
endif
121
}
122
break
;
123
default
: {
124
// determine size of drag decoration by uniting all geometries
125
const
auto
cend
=
m_items
.
cend
();
126
auto
it
=
m_items
.
cbegin
();
127
QRect
unitedGeometry
= (*
it
)->
decoration
()->
geometry
();
128
const
qreal
devicePixelRatio
= (*
it
)->
decoration
()->
devicePixelRatioF
();
129
for
(++
it
;
it
!=
cend
; ++
it
)
130
unitedGeometry
=
unitedGeometry
.
united
((*
it
)->
decoration
()->
geometry
());
131
132
// paint with offset. At the same time, create a mask bitmap, containing widget rectangles.
133
const
QSize
imageSize
= (
QSizeF
(
unitedGeometry
.
size
()) *
devicePixelRatio
).
toSize
();
134
QImage
image
(
imageSize
,
QImage
::
Format_ARGB32
);
135
image
.
setDevicePixelRatio
(
devicePixelRatio
);
136
image
.
fill
(
QColor
(
Qt
::
transparent
).
rgba
());
137
QBitmap
mask
(
imageSize
);
138
mask
.
setDevicePixelRatio
(
devicePixelRatio
);
139
mask
.
clear
();
140
// paint with offset, determine action
141
QPainter
painter
(&
image
);
142
QPainter
maskPainter
(&
mask
);
143
decorationTopLeft
=
unitedGeometry
.
topLeft
();
144
for
(
auto
*
item
:
std
::
as_const
(
m_items
)) {
145
QWidget
*
w
=
item
->
decoration
();
146
const
QPixmap
wp
=
w
->
grab
(
QRect
(0, 0, -1, -1));
147
const
QPoint
pos
=
w
->
pos
() -
decorationTopLeft
;
148
painter
.
drawPixmap
(
pos
,
wp
);
149
maskPainter
.
fillRect
(
QRect
(
pos
,
w
->
size
()),
Qt
::
color1
);
150
}
151
painter
.
end
();
152
maskPainter
.
end
();
153
#
ifdef
TRANSPARENT_DRAG_PIXMAP
154
setImageTransparency
(
image
,
Alpha
);
155
#
endif
156
QPixmap
pixmap
=
QPixmap
::
fromImage
(
image
);
157
pixmap
.
setMask
(
mask
);
158
drag
->
setPixmap
(
pixmap
);
159
}
160
break
;
161
}
162
// determine hot spot and reconstruct the exact starting position as form window
163
// introduces some offset when detecting DnD
164
m_globalStartPos
=
m_items
.
first
()->
decoration
()->
pos
() +
m_items
.
first
()->
hotSpot
();
165
m_hotSpot
=
m_globalStartPos
-
decorationTopLeft
;
166
drag
->
setHotSpot
(
m_hotSpot
);
167
168
drag
->
setMimeData
(
this
);
169
}
170
171
QDesignerMimeData
::~
QDesignerMimeData
()
172
{
173
qDeleteAll
(
m_items
);
174
}
175
176
Qt
::
DropAction
QDesignerMimeData
::
proposedDropAction
()
const
177
{
178
return
m_items
.
first
()->
type
() ==
QDesignerDnDItemInterface
::
CopyDrop
?
Qt
::
CopyAction
:
Qt
::
MoveAction
;
179
}
180
181
Qt
::
DropAction
QDesignerMimeData
::
execDrag
(
const
QDesignerDnDItems
&
items
,
QWidget
*
dragSource
)
182
{
183
if
(
items
.
isEmpty
())
184
return
Qt
::
IgnoreAction
;
185
186
QDrag
*
drag
=
new
QDrag
(
dragSource
);
187
QDesignerMimeData
*
mimeData
=
new
QDesignerMimeData
(
items
,
drag
);
188
189
// Store pointers to widgets that are to be re-shown if a move operation is canceled
190
QWidgetList
reshowWidgets
;
191
for
(
auto
*
item
:
items
) {
192
if
(
QWidget
*
w
=
item
->
widget
()) {
193
if
(
item
->
type
() ==
QDesignerDnDItemInterface
::
MoveDrop
)
194
reshowWidgets
.
push_back
(
w
);
195
}
196
}
197
198
const
Qt
::
DropAction
executedAction
=
drag
->
exec
(
Qt
::
CopyAction
|
Qt
::
MoveAction
,
mimeData
->
proposedDropAction
());
199
200
if
(
executedAction
==
Qt
::
IgnoreAction
) {
201
for
(
QWidget
*
w
:
std
::
as_const
(
reshowWidgets
))
202
w
->
show
();
203
}
204
205
return
executedAction
;
206
}
207
208
209
void
QDesignerMimeData
::
moveDecoration
(
const
QPoint
&
globalPos
)
const
210
{
211
const
QPoint
relativeDistance
=
globalPos
-
m_globalStartPos
;
212
for
(
auto
*
item
:
m_items
) {
213
QWidget
*
w
=
item
->
decoration
();
214
w
->
move
(
w
->
pos
() +
relativeDistance
);
215
}
216
}
217
218
void
QDesignerMimeData
::
removeMovedWidgetsFromSourceForm
(
const
QDesignerDnDItems
&
items
)
219
{
220
QMultiMap
<
FormWindowBase
*,
QWidget
*>
formWidgetMap
;
221
// Find moved widgets per form
222
for
(
auto
*
item
:
items
) {
223
if
(
item
->
type
() ==
QDesignerDnDItemInterface
::
MoveDrop
) {
224
if
(
QWidget
*
w
=
item
->
widget
()) {
225
if
(
FormWindowBase
*
fb
=
qobject_cast
<
FormWindowBase
*>(
item
->
source
()))
226
formWidgetMap
.
insert
(
fb
,
w
);
227
}
228
}
229
}
230
231
const
auto
&
formWindows
=
formWidgetMap
.
uniqueKeys
();
232
for
(
FormWindowBase
*
fb
:
formWindows
)
233
fb
->
deleteWidgetList
(
formWidgetMap
.
values
(
fb
));
234
}
235
236
void
QDesignerMimeData
::
acceptEventWithAction
(
Qt
::
DropAction
desiredAction
,
QDropEvent
*
e
)
237
{
238
if
(
e
->
proposedAction
() ==
desiredAction
) {
239
e
->
acceptProposedAction
();
240
}
else
{
241
e
->
setDropAction
(
desiredAction
);
242
e
->
accept
();
243
}
244
}
245
246
void
QDesignerMimeData
::
acceptEvent
(
QDropEvent
*
e
)
const
247
{
248
acceptEventWithAction
(
proposedDropAction
(),
e
);
249
}
250
251
void
QDesignerMimeData
::
setImageTransparency
(
QImage
&
image
,
int
alpha
)
252
{
253
const
int
height
=
image
.
height
();
254
for
(
int
l
= 0;
l
<
height
;
l
++) {
255
QRgb
*
line
=
reinterpret_cast
<
QRgb
*>(
image
.
scanLine
(
l
));
256
QRgb
*
lineEnd
=
line
+
image
.
width
();
257
for
( ;
line
<
lineEnd
;
line
++) {
258
const
QRgb
rgba
= *
line
;
259
*
line
=
qRgba
(
qRed
(
rgba
),
qGreen
(
rgba
),
qBlue
(
rgba
),
alpha
);
260
}
261
}
262
}
263
264
}
// namespace qdesigner_internal
265
266
QT_END_NAMESPACE
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qdesigner_internal
Auxiliary methods to store/retrieve settings.
Definition
buddyeditor.cpp:67
qttools
src
designer
src
lib
shared
qdesigner_dnditem.cpp
Generated on
for Qt by
1.16.1