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
promotiontaskmenu.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
"promotiontaskmenu_p.h"
6
#
include
"qdesigner_promotiondialog_p.h"
7
#
include
"widgetfactory_p.h"
8
#
include
"metadatabase_p.h"
9
#
include
"widgetdatabase_p.h"
10
#
include
"qdesigner_command_p.h"
11
#
include
"signalslotdialog_p.h"
12
#
include
"qdesigner_objectinspector_p.h"
13
#
include
"abstractintrospection_p.h"
14
15
#
include
<
QtDesigner
/
abstractformwindow
.
h
>
16
#
include
<
QtDesigner
/
abstractformwindowcursor
.
h
>
17
#
include
<
QtDesigner
/
abstractlanguage
.
h
>
18
#
include
<
QtDesigner
/
abstractformeditor
.
h
>
19
#
include
<
QtDesigner
/
qextensionmanager
.
h
>
20
21
#
include
<
QtWidgets
/
qwidget
.
h
>
22
#
include
<
QtWidgets
/
qmenu
.
h
>
23
24
#
include
<
QtGui
/
qaction
.
h
>
25
26
#
include
<
QtCore
/
qdebug
.
h
>
27
28
QT_BEGIN_NAMESPACE
29
30
static
QAction *
separatorAction
(QObject *parent)
31
{
32
QAction *rc =
new
QAction(parent);
33
rc->setSeparator(
true
);
34
return
rc;
35
}
36
37
static
inline
QDesignerLanguageExtension
*
languageExtension
(QDesignerFormEditorInterface *core)
38
{
39
return
qt_extension<QDesignerLanguageExtension*>(core->extensionManager(), core);
40
}
41
42
namespace
qdesigner_internal
{
43
44
PromotionTaskMenu
::
PromotionTaskMenu
(
QWidget
*
widget
,
Mode
mode
,
QObject
*
parent
) :
45
QObject
(
parent
),
46
m_mode
(
mode
),
47
m_widget
(
widget
),
48
m_globalEditAction
(
new
QAction
(
tr
(
"Promoted widgets..."
),
this
)),
49
m_EditPromoteToAction
(
new
QAction
(
tr
(
"Promote to ..."
),
this
)),
50
m_EditSignalsSlotsAction
(
new
QAction
(
tr
(
"Change signals/slots..."
),
this
)),
51
m_promoteLabel
(
tr
(
"Promote to"
)),
52
m_demoteLabel
(
tr
(
"Demote to %1"
))
53
{
54
connect
(
m_globalEditAction
, &
QAction
::
triggered
,
this
, &
PromotionTaskMenu
::
slotEditPromotedWidgets
);
55
connect
(
m_EditPromoteToAction
, &
QAction
::
triggered
,
this
, &
PromotionTaskMenu
::
slotEditPromoteTo
);
56
connect
(
m_EditSignalsSlotsAction
, &
QAction
::
triggered
,
this
, &
PromotionTaskMenu
::
slotEditSignalsSlots
);
57
}
58
59
PromotionTaskMenu
::
Mode
PromotionTaskMenu
::
mode
()
const
60
{
61
return
m_mode
;
62
}
63
64
void
PromotionTaskMenu
::
setMode
(
Mode
m
)
65
{
66
m_mode
=
m
;
67
}
68
69
void
PromotionTaskMenu
::
setWidget
(
QWidget
*
widget
)
70
{
71
m_widget
=
widget
;
72
}
73
74
void
PromotionTaskMenu
::
setPromoteLabel
(
const
QString
&
promoteLabel
)
75
{
76
m_promoteLabel
=
promoteLabel
;
77
}
78
79
void
PromotionTaskMenu
::
setEditPromoteToLabel
(
const
QString
&
promoteEditLabel
)
80
{
81
m_EditPromoteToAction
->
setText
(
promoteEditLabel
);
82
}
83
84
void
PromotionTaskMenu
::
setDemoteLabel
(
const
QString
&
demoteLabel
)
85
{
86
m_demoteLabel
=
demoteLabel
;
87
}
88
89
PromotionTaskMenu
::
PromotionState
PromotionTaskMenu
::
createPromotionActions
(
QDesignerFormWindowInterface
*
formWindow
)
90
{
91
// clear out old
92
if
(!
m_promotionActions
.
isEmpty
()) {
93
qDeleteAll
(
m_promotionActions
);
94
m_promotionActions
.
clear
();
95
}
96
// No promotion of main container
97
if
(
formWindow
->
mainContainer
() ==
m_widget
)
98
return
NotApplicable
;
99
100
// Check for a homogenous selection
101
const
PromotionSelectionList
promotionSelection
=
promotionSelectionList
(
formWindow
);
102
103
if
(
promotionSelection
.
isEmpty
())
104
return
NoHomogenousSelection
;
105
106
QDesignerFormEditorInterface
*
core
=
formWindow
->
core
();
107
// if it is promoted: demote only.
108
if
(
isPromoted
(
formWindow
->
core
(),
m_widget
)) {
109
const
QString
label
=
m_demoteLabel
.
arg
(
promotedExtends
(
core
,
m_widget
));
110
QAction
*
demoteAction
=
new
QAction
(
label
,
this
);
111
connect
(
demoteAction
, &
QAction
::
triggered
,
this
, &
PromotionTaskMenu
::
slotDemoteFromCustomWidget
);
112
m_promotionActions
.
push_back
(
demoteAction
);
113
return
CanDemote
;
114
}
115
// figure out candidates
116
const
QString
baseClassName
=
WidgetFactory
::
classNameOf
(
core
,
m_widget
);
117
const
WidgetDataBaseItemList
candidates
=
promotionCandidates
(
core
->
widgetDataBase
(),
baseClassName
);
118
if
(
candidates
.
isEmpty
()) {
119
// Is this thing promotable at all?
120
return
QDesignerPromotionDialog
::
baseClassNames
(
core
->
promotion
()).
contains
(
baseClassName
) ?
CanPromote
:
NotApplicable
;
121
}
122
123
QMenu
*
candidatesMenu
=
new
QMenu
();
124
// Create a sub menu
125
// Set up actions and map class names
126
for
(
auto
*
item
:
candidates
) {
127
const
QString
customClassName
=
item
->
name
();
128
candidatesMenu
->
addAction
(
customClassName
,
129
this
, [
this
,
customClassName
] {
this
->
slotPromoteToCustomWidget
(
customClassName
); });
130
}
131
// Sub menu action
132
QAction
*
subMenuAction
=
new
QAction
(
m_promoteLabel
,
this
);
133
subMenuAction
->
setMenu
(
candidatesMenu
);
134
m_promotionActions
.
push_back
(
subMenuAction
);
135
return
CanPromote
;
136
}
137
138
void
PromotionTaskMenu
::
addActions
(
unsigned
separatorFlags
,
ActionList
&
actionList
)
139
{
140
addActions
(
formWindow
(),
separatorFlags
,
actionList
);
141
}
142
143
void
PromotionTaskMenu
::
addActions
(
QDesignerFormWindowInterface
*
fw
,
unsigned
flags
,
144
ActionList
&
actionList
)
145
{
146
Q_ASSERT
(
m_widget
);
147
const
auto
previousSize
=
actionList
.
size
();
148
const
PromotionState
promotionState
=
createPromotionActions
(
fw
);
149
150
// Promotion candidates/demote
151
actionList
+=
m_promotionActions
;
152
153
// Edit action depending on context
154
switch
(
promotionState
) {
155
case
CanPromote
:
156
actionList
+=
m_EditPromoteToAction
;
157
break
;
158
case
CanDemote
:
159
if
(!(
flags
&
SuppressGlobalEdit
))
160
actionList
+=
m_globalEditAction
;
161
if
(!
languageExtension
(
fw
->
core
())) {
162
actionList
+=
separatorAction
(
this
);
163
actionList
+=
m_EditSignalsSlotsAction
;
164
}
165
break
;
166
default
:
167
if
(!(
flags
&
SuppressGlobalEdit
))
168
actionList
+=
m_globalEditAction
;
169
break
;
170
}
171
// Add separators if required
172
if
(
actionList
.
size
() >
previousSize
) {
173
if
(
flags
&
LeadingSeparator
)
174
actionList
.
insert
(
previousSize
,
separatorAction
(
this
));
175
if
(
flags
&
TrailingSeparator
)
176
actionList
+=
separatorAction
(
this
);
177
}
178
}
179
180
void
PromotionTaskMenu
::
addActions
(
QDesignerFormWindowInterface
*
fw
,
unsigned
flags
,
QMenu
*
menu
)
181
{
182
ActionList
actionList
;
183
addActions
(
fw
,
flags
,
actionList
);
184
menu
->
addActions
(
actionList
);
185
}
186
187
void
PromotionTaskMenu
::
addActions
(
unsigned
flags
,
QMenu
*
menu
)
188
{
189
addActions
(
formWindow
(),
flags
,
menu
);
190
}
191
192
void
PromotionTaskMenu
::
promoteTo
(
QDesignerFormWindowInterface
*
fw
,
const
QString
&
customClassName
)
193
{
194
Q_ASSERT
(
m_widget
);
195
PromoteToCustomWidgetCommand
*
cmd
=
new
PromoteToCustomWidgetCommand
(
fw
);
196
cmd
->
init
(
promotionSelectionList
(
fw
),
customClassName
);
197
fw
->
commandHistory
()->
push
(
cmd
);
198
}
199
200
201
void
PromotionTaskMenu
::
slotPromoteToCustomWidget
(
const
QString
&
customClassName
)
202
{
203
promoteTo
(
formWindow
(),
customClassName
);
204
}
205
206
void
PromotionTaskMenu
::
slotDemoteFromCustomWidget
()
207
{
208
QDesignerFormWindowInterface
*
fw
=
formWindow
();
209
const
PromotionSelectionList
promotedWidgets
=
promotionSelectionList
(
fw
);
210
Q_ASSERT
(!
promotedWidgets
.
isEmpty
() &&
isPromoted
(
fw
->
core
(),
promotedWidgets
.
constFirst
()));
211
212
// ### use the undo stack
213
DemoteFromCustomWidgetCommand
*
cmd
=
new
DemoteFromCustomWidgetCommand
(
fw
);
214
cmd
->
init
(
promotedWidgets
);
215
fw
->
commandHistory
()->
push
(
cmd
);
216
}
217
218
void
PromotionTaskMenu
::
slotEditPromoteTo
()
219
{
220
Q_ASSERT
(
m_widget
);
221
// Check whether invoked over a promotable widget
222
QDesignerFormWindowInterface
*
fw
=
formWindow
();
223
QDesignerFormEditorInterface
*
core
=
fw
->
core
();
224
const
QString
base_class_name
=
WidgetFactory
::
classNameOf
(
core
,
m_widget
);
225
Q_ASSERT
(
QDesignerPromotionDialog
::
baseClassNames
(
core
->
promotion
()).
contains
(
base_class_name
));
226
// Show over promotable widget
227
QString
promoteToClassName
;
228
QDialog
*
promotionEditor
=
nullptr
;
229
if
(
QDesignerLanguageExtension
*
lang
=
languageExtension
(
core
))
230
promotionEditor
=
lang
->
createPromotionDialog
(
core
,
base_class_name
, &
promoteToClassName
,
fw
);
231
if
(!
promotionEditor
)
232
promotionEditor
=
new
QDesignerPromotionDialog
(
core
,
fw
,
base_class_name
, &
promoteToClassName
);
233
if
(
promotionEditor
->
exec
() ==
QDialog
::
Accepted
&& !
promoteToClassName
.
isEmpty
()) {
234
promoteTo
(
fw
,
promoteToClassName
);
235
}
236
delete
promotionEditor
;
237
}
238
239
void
PromotionTaskMenu
::
slotEditPromotedWidgets
()
240
{
241
// Global context, show over non-promotable widget
242
QDesignerFormWindowInterface
*
fw
=
formWindow
();
243
if
(!
fw
)
244
return
;
245
editPromotedWidgets
(
fw
->
core
(),
fw
);
246
}
247
248
PromotionTaskMenu
::
PromotionSelectionList
PromotionTaskMenu
::
promotionSelectionList
(
QDesignerFormWindowInterface
*
formWindow
)
const
249
{
250
// In multi selection mode, check for a homogenous selection (same class, same promotion state)
251
// and return the list if this is the case. Also make sure m_widget
252
// is the last widget in the list so that it is re-selected as the last
253
// widget by the promotion commands.
254
255
PromotionSelectionList
rc
;
256
257
if
(
m_mode
!=
ModeSingleWidget
) {
258
QDesignerFormEditorInterface
*
core
=
formWindow
->
core
();
259
const
QDesignerIntrospectionInterface
*
intro
=
core
->
introspection
();
260
const
QString
className
=
intro
->
metaObject
(
m_widget
)->
className
();
261
const
bool
promoted
=
isPromoted
(
formWindow
->
core
(),
m_widget
);
262
// Just in case someone plugged an old-style Object Inspector
263
if
(
QDesignerObjectInspector
*
designerObjectInspector
=
qobject_cast
<
QDesignerObjectInspector
*>(
core
->
objectInspector
())) {
264
Selection
s
;
265
designerObjectInspector
->
getSelection
(
s
);
266
// Find objects of similar state
267
const
QWidgetList
&
source
=
m_mode
==
ModeManagedMultiSelection
?
s
.
managed
:
s
.
unmanaged
;
268
for
(
auto
*
w
:
source
) {
269
if
(
w
!=
m_widget
) {
270
// Selection state mismatch
271
if
(
intro
->
metaObject
(
w
)->
className
() !=
className
||
isPromoted
(
core
,
w
) !=
promoted
)
272
return
PromotionSelectionList
();
273
rc
.
push_back
(
w
);
274
}
275
}
276
}
277
}
278
279
rc
.
push_back
(
m_widget
);
280
return
rc
;
281
}
282
283
QDesignerFormWindowInterface
*
PromotionTaskMenu
::
formWindow
()
const
284
{
285
// Use the QObject overload of QDesignerFormWindowInterface::findFormWindow since that works
286
// for QDesignerMenus also.
287
QObject
*
o
=
m_widget
;
288
QDesignerFormWindowInterface
*
result
=
QDesignerFormWindowInterface
::
findFormWindow
(
o
);
289
Q_ASSERT
(
result
!=
nullptr
);
290
return
result
;
291
}
292
293
void
PromotionTaskMenu
::
editPromotedWidgets
(
QDesignerFormEditorInterface
*
core
,
QWidget
*
parent
) {
294
QDesignerLanguageExtension
*
lang
=
languageExtension
(
core
);
295
// Show over non-promotable widget
296
QDialog
*
promotionEditor
=
nullptr
;
297
if
(
lang
)
298
lang
->
createPromotionDialog
(
core
,
parent
);
299
if
(!
promotionEditor
)
300
promotionEditor
=
new
QDesignerPromotionDialog
(
core
,
parent
);
301
promotionEditor
->
exec
();
302
delete
promotionEditor
;
303
}
304
305
void
PromotionTaskMenu
::
slotEditSignalsSlots
()
306
{
307
QDesignerFormWindowInterface
*
fw
=
formWindow
();
308
if
(!
fw
)
309
return
;
310
SignalSlotDialog
::
editPromotedClass
(
fw
->
core
(),
m_widget
,
fw
);
311
}
312
}
// namespace qdesigner_internal
313
314
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
separatorAction
static QT_BEGIN_NAMESPACE QAction * separatorAction(QObject *parent)
Definition
promotiontaskmenu.cpp:30
languageExtension
static QDesignerLanguageExtension * languageExtension(QDesignerFormEditorInterface *core)
Definition
promotiontaskmenu.cpp:37
qttools
src
designer
src
lib
shared
promotiontaskmenu.cpp
Generated on
for Qt by
1.16.1