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
connectdialog.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
7
8#include <signalslotdialog_p.h>
9#include <metadatabase_p.h>
10
11#include <QtDesigner/abstractformwindow.h>
12#include <QtDesigner/abstractformeditor.h>
13#include <QtDesigner/abstractwidgetdatabase.h>
14#include <QtDesigner/qextensionmanager.h>
15#include <QtDesigner/abstractlanguage.h>
16
17#include <QtWidgets/qpushbutton.h>
18
20
21using namespace Qt::StringLiterals;
22
23static QString realClassName(QDesignerFormEditorInterface *core, QWidget *widget)
24{
25 QString class_name = QLatin1StringView(widget->metaObject()->className());
26 const QDesignerWidgetDataBaseInterface *wdb = core->widgetDataBase();
27 const int idx = wdb->indexOfObject(widget);
28 if (idx != -1)
29 class_name = wdb->item(idx)->name();
30 return class_name;
31}
32
33static QString widgetLabel(QDesignerFormEditorInterface *core, QWidget *widget)
34{
35 return "%1 (%2)"_L1
36 .arg(qdesigner_internal::realObjectName(core, widget),
37 realClassName(core, widget));
38}
39
40namespace qdesigner_internal {
41
42ConnectDialog::ConnectDialog(QDesignerFormWindowInterface *formWindow,
43 QWidget *source, QWidget *destination,
44 QWidget *parent) :
45 QDialog(parent),
46 m_source(source),
47 m_destination(destination),
48 m_sourceMode(widgetMode(m_source, formWindow)),
49 m_destinationMode(widgetMode(m_destination, formWindow)),
50 m_formWindow(formWindow)
51{
52 m_ui.setupUi(this);
53
54 connect(m_ui.signalList, &QListWidget::itemClicked,
55 this, &ConnectDialog::selectSignal);
56 connect(m_ui.slotList, &QListWidget::itemClicked,
57 this, &ConnectDialog::selectSlot);
58 m_ui.slotList->setEnabled(false);
59
60 QPushButton *ok_button = okButton();
61 ok_button->setDefault(true);
62 ok_button->setEnabled(false);
63
64 connect(m_ui.showAllCheckBox, &QCheckBox::toggled, this, &ConnectDialog::populateLists);
65
66 QDesignerFormEditorInterface *core = m_formWindow->core();
67 m_ui.signalGroupBox->setTitle(widgetLabel(core, source));
68 m_ui.slotGroupBox->setTitle(widgetLabel(core, destination));
69
70 m_ui.editSignalsButton->setEnabled(m_sourceMode != NormalWidget);
71 connect(m_ui.editSignalsButton, &QAbstractButton::clicked,
72 this, &ConnectDialog::editSignals);
73
74 m_ui.editSlotsButton->setEnabled(m_destinationMode != NormalWidget);
75 connect(m_ui.editSlotsButton, &QAbstractButton::clicked,
76 this, &ConnectDialog::editSlots);
77
78 populateLists();
79}
80
81ConnectDialog::WidgetMode ConnectDialog::widgetMode(QWidget *w, QDesignerFormWindowInterface *formWindow)
82{
83 QDesignerFormEditorInterface *core = formWindow->core();
84 if (qt_extension<QDesignerLanguageExtension*>(core->extensionManager(), core))
85 return NormalWidget;
86
87 if (w == formWindow || formWindow->mainContainer() == w)
88 return MainContainer;
89
90 if (isPromoted(formWindow->core(), w))
91 return PromotedWidget;
92
93 return NormalWidget;
94}
95
96QPushButton *ConnectDialog::okButton()
97{
98 return m_ui.buttonBox->button(QDialogButtonBox::Ok);
99}
100
101void ConnectDialog::setOkButtonEnabled(bool e)
102{
103 okButton()->setEnabled(e);
104}
105
106void ConnectDialog::populateLists()
107{
108 populateSignalList();
109}
110
111void ConnectDialog::setSignalSlot(const QString &signal, const QString &slot)
112{
113 auto sigItems = m_ui.signalList->findItems(signal, Qt::MatchExactly);
114
115 if (sigItems.isEmpty()) {
116 m_ui.showAllCheckBox->setChecked(true);
117 sigItems = m_ui.signalList->findItems(signal, Qt::MatchExactly);
118 }
119
120 if (!sigItems.isEmpty()) {
121 selectSignal(sigItems.constFirst());
122 auto slotItems = m_ui.slotList->findItems(slot, Qt::MatchExactly);
123 if (slotItems.isEmpty()) {
124 m_ui.showAllCheckBox->setChecked(true);
125 slotItems = m_ui.slotList->findItems(slot, Qt::MatchExactly);
126 }
127 if (!slotItems.isEmpty())
128 selectSlot(slotItems.constFirst());
129 }
130}
131
133{
134 return m_ui.showAllCheckBox->isChecked();
135}
136
138{
139 m_ui.showAllCheckBox->setChecked(showIt);
140}
141
142void ConnectDialog::selectSignal(QListWidgetItem *item)
143{
144 if (item) {
145 m_ui.signalList->setCurrentItem(item);
146 populateSlotList(item->text());
147 m_ui.slotList->setEnabled(true);
148 setOkButtonEnabled(!m_ui.slotList->selectedItems().isEmpty());
149 } else {
150 m_ui.signalList->clearSelection();
151 populateSlotList();
152 m_ui.slotList->setEnabled(false);
153 setOkButtonEnabled(false);
154 }
155}
156
157void ConnectDialog::selectSlot(QListWidgetItem *item)
158{
159 if (item) {
160 m_ui.slotList->setCurrentItem(item);
161 } else {
162 m_ui.slotList->clearSelection();
163 }
164 setOkButtonEnabled(true);
165}
166
168{
169 const auto item_list = m_ui.signalList->selectedItems();
170 if (item_list.size() != 1)
171 return QString();
172 return item_list.at(0)->text();
173}
174
176{
177 const auto item_list = m_ui.slotList->selectedItems();
178 if (item_list.size() != 1)
179 return QString();
180 return item_list.at(0)->text();
181}
182
183void ConnectDialog::populateSlotList(const QString &signal)
184{
185 enum { deprecatedSlot = 0 };
186 QString selectedName;
187 if (const QListWidgetItem * item = m_ui.slotList->currentItem())
188 selectedName = item->text();
189
190 m_ui.slotList->clear();
191
192 QMap<QString, QString> memberToClassName = getMatchingSlots(m_formWindow->core(), m_destination, signal, showAllSignalsSlots());
193
194 QFont font = QApplication::font();
195 font.setItalic(true);
196 QVariant variantFont = QVariant::fromValue(font);
197
198 QListWidgetItem *curr = nullptr;
199 for (auto itMember = memberToClassName.cbegin(), itMemberEnd = memberToClassName.cend(); itMember != itMemberEnd; ++itMember) {
200 const QString member = itMember.key();
201 QListWidgetItem *item = new QListWidgetItem(m_ui.slotList);
202 item->setText(member);
203 if (member == selectedName)
204 curr = item;
205
206 // Mark deprecated slots red. Not currently in use (historically for Qt 3 slots in Qt 4),
207 // but may be used again in the future.
208 if (deprecatedSlot) {
209 item->setData(Qt::FontRole, variantFont);
210 item->setData(Qt::ForegroundRole, QColor(Qt::red));
211 }
212 }
213
214 if (curr)
215 m_ui.slotList->setCurrentItem(curr);
216
217 if (m_ui.slotList->selectedItems().isEmpty())
218 setOkButtonEnabled(false);
219}
220
221void ConnectDialog::populateSignalList()
222{
223 enum { deprecatedSignal = 0 };
224
225 QString selectedName;
226 if (const QListWidgetItem *item = m_ui.signalList->currentItem())
227 selectedName = item->text();
228
229 m_ui.signalList->clear();
230
231 QMap<QString, QString> memberToClassName = getSignals(m_formWindow->core(), m_source, showAllSignalsSlots());
232
233 QFont font = QApplication::font();
234 font.setItalic(true);
235 QVariant variantFont = QVariant::fromValue(font);
236
237 QListWidgetItem *curr = nullptr;
238 for (auto itMember = memberToClassName.cbegin(), itMemberEnd = memberToClassName.cend(); itMember != itMemberEnd; ++itMember) {
239 const QString member = itMember.key();
240
241 QListWidgetItem *item = new QListWidgetItem(m_ui.signalList);
242 item->setText(member);
243 if (!selectedName.isEmpty() && member == selectedName)
244 curr = item;
245
246 // Mark deprecated signals red. Not currently in use (historically for Qt 3 slots in Qt 4),
247 // but may be used again in the future.
248 if (deprecatedSignal) {
249 item->setData(Qt::FontRole, variantFont);
250 item->setData(Qt::ForegroundRole, QColor(Qt::red));
251 }
252 }
253
254 if (curr) {
255 m_ui.signalList->setCurrentItem(curr);
256 } else {
257 selectedName.clear();
258 }
259
260 populateSlotList(selectedName);
261 if (!curr)
262 m_ui.slotList->setEnabled(false);
263}
264
265void ConnectDialog::editSignals()
266{
267 editSignalsSlots(m_source, m_sourceMode, SignalSlotDialog::FocusSignals);
268}
269
270void ConnectDialog::editSlots()
271{
272 editSignalsSlots(m_destination, m_destinationMode, SignalSlotDialog::FocusSlots);
273}
274
275void ConnectDialog::editSignalsSlots(QWidget *w, WidgetMode mode, int signalSlotDialogModeInt)
276{
277 const SignalSlotDialog::FocusMode signalSlotDialogMode = static_cast<SignalSlotDialog::FocusMode>(signalSlotDialogModeInt);
278 switch (mode) {
279 case NormalWidget:
280 break;
281 case MainContainer:
282 if (SignalSlotDialog::editMetaDataBase(m_formWindow, w, this, signalSlotDialogMode))
283 populateLists();
284 break;
285 case PromotedWidget:
286 if (SignalSlotDialog::editPromotedClass(m_formWindow->core(), w, this, signalSlotDialogMode))
287 populateLists();
288 break;
289 }
290}
291
292}
293
294QT_END_NAMESPACE
295
296#include "moc_connectdialog_p.cpp"
void setSignalSlot(const QString &signal, const QString &slot)
static QString widgetLabel(QDesignerFormEditorInterface *core, QWidget *widget)
static QString realClassName(QDesignerFormEditorInterface *core, QWidget *widget)
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.