source: trunk/tools/designer/src/components/signalsloteditor/connectdialog.cpp@ 561

Last change on this file since 561 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 10.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the Qt Designer of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "connectdialog_p.h"
43#include "signalslot_utils_p.h"
44
45#include <signalslotdialog_p.h>
46#include <metadatabase_p.h>
47
48#include <QtDesigner/QDesignerFormWindowInterface>
49#include <QtDesigner/QDesignerFormEditorInterface>
50#include <QtDesigner/QDesignerWidgetDataBaseInterface>
51#include <QtDesigner/QExtensionManager>
52#include <QtDesigner/QDesignerLanguageExtension>
53
54#include <QtGui/QPushButton>
55
56QT_BEGIN_NAMESPACE
57
58namespace {
59 typedef QList<QListWidgetItem*> ListWidgetItems;
60}
61
62static QString realClassName(QDesignerFormEditorInterface *core, QWidget *widget)
63{
64 QString class_name = QLatin1String(widget->metaObject()->className());
65 const QDesignerWidgetDataBaseInterface *wdb = core->widgetDataBase();
66 const int idx = wdb->indexOfObject(widget);
67 if (idx != -1)
68 class_name = wdb->item(idx)->name();
69 return class_name;
70}
71
72static QString widgetLabel(QDesignerFormEditorInterface *core, QWidget *widget)
73{
74 return QString::fromUtf8("%1 (%2)")
75 .arg(qdesigner_internal::realObjectName(core, widget))
76 .arg(realClassName(core, widget));
77}
78
79namespace qdesigner_internal {
80
81ConnectDialog::ConnectDialog(QDesignerFormWindowInterface *formWindow,
82 QWidget *source, QWidget *destination,
83 QWidget *parent) :
84 QDialog(parent),
85 m_source(source),
86 m_destination(destination),
87 m_sourceMode(widgetMode(m_source, formWindow)),
88 m_destinationMode(widgetMode(m_destination, formWindow)),
89 m_formWindow(formWindow)
90{
91 m_ui.setupUi(this);
92
93 setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
94
95 connect(m_ui.signalList, SIGNAL(itemClicked(QListWidgetItem*)),
96 this, SLOT(selectSignal(QListWidgetItem*)));
97 connect(m_ui.slotList, SIGNAL(itemClicked(QListWidgetItem*)),
98 this, SLOT(selectSlot(QListWidgetItem*)));
99 m_ui.slotList->setEnabled(false);
100
101 QPushButton *ok_button = okButton();
102 ok_button->setDefault(true);
103 ok_button->setEnabled(false);
104
105 connect(m_ui.showAllCheckBox, SIGNAL(toggled(bool)), this, SLOT(populateLists()));
106
107 QDesignerFormEditorInterface *core = m_formWindow->core();
108 m_ui.signalGroupBox->setTitle(widgetLabel(core, source));
109 m_ui.slotGroupBox->setTitle(widgetLabel(core, destination));
110
111 m_ui.editSignalsButton->setEnabled(m_sourceMode != NormalWidget);
112 connect(m_ui.editSignalsButton, SIGNAL(clicked()), this, SLOT(editSignals()));
113
114 m_ui.editSlotsButton->setEnabled(m_destinationMode != NormalWidget);
115 connect(m_ui.editSlotsButton, SIGNAL(clicked()), this, SLOT(editSlots()));
116
117 populateLists();
118}
119
120ConnectDialog::WidgetMode ConnectDialog::widgetMode(QWidget *w, QDesignerFormWindowInterface *formWindow)
121{
122 QDesignerFormEditorInterface *core = formWindow->core();
123 if (qt_extension<QDesignerLanguageExtension*>(core->extensionManager(), core))
124 return NormalWidget;
125
126 if (w == formWindow || formWindow->mainContainer() == w)
127 return MainContainer;
128
129 if (isPromoted(formWindow->core(), w))
130 return PromotedWidget;
131
132 return NormalWidget;
133}
134
135QPushButton *ConnectDialog::okButton()
136{
137 return m_ui.buttonBox->button(QDialogButtonBox::Ok);
138}
139
140void ConnectDialog::setOkButtonEnabled(bool e)
141{
142 okButton()->setEnabled(e);
143}
144
145void ConnectDialog::populateLists()
146{
147 populateSignalList();
148}
149
150void ConnectDialog::setSignalSlot(const QString &signal, const QString &slot)
151{
152 ListWidgetItems sigItems = m_ui.signalList->findItems(signal, Qt::MatchExactly);
153
154 if (sigItems.empty()) {
155 m_ui.showAllCheckBox->setChecked(true);
156 sigItems = m_ui.signalList->findItems(signal, Qt::MatchExactly);
157 }
158
159 if (!sigItems.empty()) {
160 selectSignal(sigItems.front());
161 ListWidgetItems slotItems = m_ui.slotList->findItems(slot, Qt::MatchExactly);
162 if (slotItems.empty()) {
163 m_ui.showAllCheckBox->setChecked(true);
164 slotItems = m_ui.slotList->findItems(slot, Qt::MatchExactly);
165 }
166 if (!slotItems.empty())
167 selectSlot(slotItems.front());
168 }
169}
170
171bool ConnectDialog::showAllSignalsSlots() const
172{
173 return m_ui.showAllCheckBox->isChecked();
174}
175
176void ConnectDialog::setShowAllSignalsSlots(bool showIt)
177{
178 m_ui.showAllCheckBox->setChecked(showIt);
179}
180
181void ConnectDialog::selectSignal(QListWidgetItem *item)
182{
183 if (item) {
184 m_ui.signalList->setCurrentItem(item);
185 populateSlotList(item->text());
186 m_ui.slotList->setEnabled(true);
187 setOkButtonEnabled(!m_ui.slotList->selectedItems().isEmpty());
188 } else {
189 m_ui.signalList->clearSelection();
190 populateSlotList();
191 m_ui.slotList->setEnabled(false);
192 setOkButtonEnabled(false);
193 }
194}
195
196void ConnectDialog::selectSlot(QListWidgetItem *item)
197{
198 if (item) {
199 m_ui.slotList->setCurrentItem(item);
200 } else {
201 m_ui.slotList->clearSelection();
202 }
203 setOkButtonEnabled(true);
204}
205
206QString ConnectDialog::signal() const
207{
208 const ListWidgetItems item_list = m_ui.signalList->selectedItems();
209 if (item_list.size() != 1)
210 return QString();
211 return item_list.at(0)->text();
212}
213
214QString ConnectDialog::slot() const
215{
216 const ListWidgetItems item_list = m_ui.slotList->selectedItems();
217 if (item_list.size() != 1)
218 return QString();
219 return item_list.at(0)->text();
220}
221
222void ConnectDialog::populateSlotList(const QString &signal)
223{
224 QString selectedName;
225 if (const QListWidgetItem * item = m_ui.slotList->currentItem())
226 selectedName = item->text();
227
228 m_ui.slotList->clear();
229
230 QMap<QString, QString> memberToClassName = getMatchingSlots(m_formWindow->core(), m_destination, signal, showAllSignalsSlots());
231
232 QFont font = QApplication::font();
233 font.setItalic(true);
234 QVariant variantFont = qVariantFromValue(font);
235
236 QListWidgetItem *curr = 0;
237 QMap<QString, QString>::ConstIterator itMember = memberToClassName.constBegin();
238 const QMap<QString, QString>::ConstIterator itMemberEnd = memberToClassName.constEnd();
239 while (itMember != itMemberEnd) {
240 const QString member = itMember.key();
241 const bool qt3Slot = isQt3Slot(m_formWindow->core(), m_destination, member);
242
243 QListWidgetItem *item = new QListWidgetItem(m_ui.slotList);
244 item->setText(member);
245 if (member == selectedName)
246 curr = item;
247
248 if (qt3Slot) {
249 item->setData(Qt::FontRole, variantFont);
250 item->setData(Qt::ForegroundRole, Qt::red);
251 }
252 ++itMember;
253 }
254
255 if (curr)
256 m_ui.slotList->setCurrentItem(curr);
257
258 if (m_ui.slotList->selectedItems().isEmpty())
259 setOkButtonEnabled(false);
260}
261
262void ConnectDialog::populateSignalList()
263{
264 QString selectedName;
265 if (const QListWidgetItem *item = m_ui.signalList->currentItem())
266 selectedName = item->text();
267
268 m_ui.signalList->clear();
269
270 QMap<QString, QString> memberToClassName = getSignals(m_formWindow->core(), m_source, showAllSignalsSlots());
271
272 QFont font = QApplication::font();
273 font.setItalic(true);
274 QVariant variantFont = qVariantFromValue(font);
275
276 QListWidgetItem *curr = 0;
277 QMap<QString, QString>::ConstIterator itMember = memberToClassName.constBegin();
278 const QMap<QString, QString>::ConstIterator itMemberEnd = memberToClassName.constEnd();
279 while (itMember != itMemberEnd) {
280 const QString member = itMember.key();
281 const bool qt3Signal = isQt3Signal(m_formWindow->core(), m_source, member);
282
283 QListWidgetItem *item = new QListWidgetItem(m_ui.signalList);
284 item->setText(member);
285 if (!selectedName.isEmpty() && member == selectedName)
286 curr = item;
287
288 if (qt3Signal) {
289 item->setData(Qt::FontRole, variantFont);
290 item->setData(Qt::ForegroundRole, Qt::red);
291 }
292 ++itMember;
293 }
294
295 if (curr) {
296 m_ui.signalList->setCurrentItem(curr);
297 } else {
298 selectedName.clear();
299 }
300
301 populateSlotList(selectedName);
302 if (!curr)
303 m_ui.slotList->setEnabled(false);
304}
305
306void ConnectDialog::editSignals()
307{
308 editSignalsSlots(m_source, m_sourceMode, SignalSlotDialog::FocusSignals);
309}
310
311void ConnectDialog::editSlots()
312{
313 editSignalsSlots(m_destination, m_destinationMode, SignalSlotDialog::FocusSlots);
314}
315
316void ConnectDialog::editSignalsSlots(QWidget *w, WidgetMode mode, int signalSlotDialogModeInt)
317{
318 const SignalSlotDialog::FocusMode signalSlotDialogMode = static_cast<SignalSlotDialog::FocusMode>(signalSlotDialogModeInt);
319 switch (mode) {
320 case NormalWidget:
321 break;
322 case MainContainer:
323 if (SignalSlotDialog::editMetaDataBase(m_formWindow, w, this, signalSlotDialogMode))
324 populateLists();
325 break;
326 case PromotedWidget:
327 if (SignalSlotDialog::editPromotedClass(m_formWindow->core(), w, this, signalSlotDialogMode))
328 populateLists();
329 break;
330 }
331}
332
333}
334
335QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.