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