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