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