source: trunk/tools/designer/src/lib/shared/qdesigner_membersheet.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: 13.1 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 "qdesigner_membersheet_p.h"
43
44#include <QtDesigner/QDesignerFormEditorInterface>
45#include <abstractintrospection_p.h>
46
47#include <QtGui/QWidget>
48
49namespace {
50
51class Qt3Members
52 {
53 public:
54 static Qt3Members *instance();
55 QMap<QString, QStringList> getSignals() const { return m_classNameToSignals; }
56 QMap<QString, QStringList> getSlots() const { return m_classNameToSlots; }
57 private:
58 Qt3Members();
59 static Qt3Members *m_instance;
60 QMap<QString, QStringList> m_classNameToSignals;
61 QMap<QString, QStringList> m_classNameToSlots;
62 };
63
64Qt3Members *Qt3Members::m_instance = 0;
65
66Qt3Members::Qt3Members()
67{
68 m_classNameToSignals[QLatin1String("QTextEdit")].append(QLatin1String("currentFontChanged(QFont)"));
69 m_classNameToSignals[QLatin1String("QTextEdit")].append(QLatin1String("currentColorChanged(QColor)"));
70 m_classNameToSignals[QLatin1String("QTabWidget")].append(QLatin1String("currentChanged(QWidget*)"));
71 m_classNameToSignals[QLatin1String("QTabWidget")].append(QLatin1String("selected(QString)"));
72 m_classNameToSignals[QLatin1String("QTabBar")].append(QLatin1String("selected(int)"));
73 m_classNameToSignals[QLatin1String("QMenuBar")].append(QLatin1String("activated(int)"));
74 m_classNameToSignals[QLatin1String("QMenuBar")].append(QLatin1String("highlighted(int)"));
75 m_classNameToSignals[QLatin1String("QMenu")].append(QLatin1String("activated(int)"));
76 m_classNameToSignals[QLatin1String("QMenu")].append(QLatin1String("highlighted(int)"));
77 m_classNameToSignals[QLatin1String("QLineEdit")].append(QLatin1String("lostFocus()"));
78 m_classNameToSignals[QLatin1String("QDial")].append(QLatin1String("dialPressed()"));
79 m_classNameToSignals[QLatin1String("QDial")].append(QLatin1String("dialMoved(int)"));
80 m_classNameToSignals[QLatin1String("QDial")].append(QLatin1String("dialReleased()"));
81 m_classNameToSignals[QLatin1String("QComboBox")].append(QLatin1String("textChanged(QString)"));
82 m_classNameToSignals[QLatin1String("QActionGroup")].append(QLatin1String("selected(QAction*)"));
83 m_classNameToSignals[QLatin1String("QAction")].append(QLatin1String("activated(int)"));
84 m_classNameToSignals[QLatin1String("QAbstractSocket")].append(QLatin1String("connectionClosed()"));
85 m_classNameToSignals[QLatin1String("QAbstractSocket")].append(QLatin1String("delayedCloseFinished()"));
86
87 m_classNameToSlots[QLatin1String("QWidget")].append(QLatin1String("setShown(bool)"));
88 m_classNameToSlots[QLatin1String("QToolButton")].append(QLatin1String("setTextPosition(QToolButton::TextPosition)"));
89 m_classNameToSlots[QLatin1String("QToolButton")].append(QLatin1String("setUsesBigPixmap(bool)"));
90 m_classNameToSlots[QLatin1String("QToolButton")].append(QLatin1String("setUsesTextLabel(bool)"));
91 m_classNameToSlots[QLatin1String("QTextEdit")].append(QLatin1String("setModified(bool)"));
92 m_classNameToSlots[QLatin1String("QTextEdit")].append(QLatin1String("setColor(QColor)"));
93 m_classNameToSlots[QLatin1String("QTabWidget")].append(QLatin1String("setCurrentPage(int)"));
94 m_classNameToSlots[QLatin1String("QTabWidget")].append(QLatin1String("showPage(QWidget*)"));
95 m_classNameToSlots[QLatin1String("QTabWidget")].append(QLatin1String("removePage(QWidget*)"));
96 m_classNameToSlots[QLatin1String("QTabBar")].append(QLatin1String("setCurrentTab(int)"));
97 m_classNameToSlots[QLatin1String("QStatusBar")].append(QLatin1String("message(QString,int)"));
98 m_classNameToSlots[QLatin1String("QStatusBar")].append(QLatin1String("clear()"));
99 m_classNameToSlots[QLatin1String("QSplashScreen")].append(QLatin1String("message(QString,int)"));
100 m_classNameToSlots[QLatin1String("QSplashScreen")].append(QLatin1String("clear()"));
101 m_classNameToSlots[QLatin1String("QSlider")].append(QLatin1String("addStep()"));
102 m_classNameToSlots[QLatin1String("QSlider")].append(QLatin1String("subtractStep()"));
103 m_classNameToSlots[QLatin1String("QAbstractButton")].append(QLatin1String("setOn(bool)"));
104 m_classNameToSlots[QLatin1String("QAction")].append(QLatin1String("setOn(bool)"));
105 m_classNameToSlots[QLatin1String("QErrorMessage")].append(QLatin1String("message(QString)"));
106 m_classNameToSlots[QLatin1String("QTimer")].append(QLatin1String("changeInterval(int)"));
107 m_classNameToSlots[QLatin1String("QTimer")].append(QLatin1String("start(int,bool)"));
108}
109
110Qt3Members *Qt3Members::instance()
111{
112 if (!m_instance)
113 m_instance = new Qt3Members();
114 return m_instance;
115}
116}
117
118QT_BEGIN_NAMESPACE
119
120static QList<QByteArray> stringListToByteArray(const QStringList &l)
121{
122 if (l.empty())
123 return QList<QByteArray>();
124 QList<QByteArray> rc;
125 const QStringList::const_iterator cend = l.constEnd();
126 for (QStringList::const_iterator it = l.constBegin(); it != cend; ++it)
127 rc += it->toUtf8();
128 return rc;
129}
130
131// Find the form editor in the hierarchy.
132// We know that the parent of the sheet is the extension manager
133// whose parent is the core.
134
135static QDesignerFormEditorInterface *formEditorForObject(QObject *o) {
136 do {
137 if (QDesignerFormEditorInterface* core = qobject_cast<QDesignerFormEditorInterface*>(o))
138 return core;
139 o = o->parent();
140 } while(o);
141 Q_ASSERT(o);
142 return 0;
143}
144
145// ------------ QDesignerMemberSheetPrivate
146class QDesignerMemberSheetPrivate {
147public:
148 explicit QDesignerMemberSheetPrivate(QObject *object, QObject *sheetParent);
149
150 QDesignerFormEditorInterface *m_core;
151 const QDesignerMetaObjectInterface *m_meta;
152
153 class Info {
154 public:
155 inline Info() : visible(true) {}
156
157 QString group;
158 bool visible;
159 };
160
161 typedef QHash<int, Info> InfoHash;
162
163 Info &ensureInfo(int index);
164
165 InfoHash m_info;
166};
167
168QDesignerMemberSheetPrivate::QDesignerMemberSheetPrivate(QObject *object, QObject *sheetParent) :
169 m_core(formEditorForObject(sheetParent)),
170 m_meta(m_core->introspection()->metaObject(object))
171{
172}
173
174QDesignerMemberSheetPrivate::Info &QDesignerMemberSheetPrivate::ensureInfo(int index)
175{
176 InfoHash::iterator it = m_info.find(index);
177 if (it == m_info.end()) {
178 it = m_info.insert(index, Info());
179 }
180 return it.value();
181}
182
183// --------- QDesignerMemberSheet
184
185QDesignerMemberSheet::QDesignerMemberSheet(QObject *object, QObject *parent) :
186 QObject(parent),
187 d(new QDesignerMemberSheetPrivate(object, parent))
188{
189}
190
191QDesignerMemberSheet::~QDesignerMemberSheet()
192{
193 delete d;
194}
195
196int QDesignerMemberSheet::count() const
197{
198 return d->m_meta->methodCount();
199}
200
201int QDesignerMemberSheet::indexOf(const QString &name) const
202{
203 return d->m_meta->indexOfMethod(name);
204}
205
206QString QDesignerMemberSheet::memberName(int index) const
207{
208 return d->m_meta->method(index)->tag();
209}
210
211QString QDesignerMemberSheet::declaredInClass(int index) const
212{
213 const QString member = d->m_meta->method(index)->signature();
214
215 // Find class whose superclass does not contain the method.
216 const QDesignerMetaObjectInterface *meta_obj = d->m_meta;
217
218 for (;;) {
219 const QDesignerMetaObjectInterface *tmp = meta_obj->superClass();
220 if (tmp == 0)
221 break;
222 if (tmp->indexOfMethod(member) == -1)
223 break;
224 meta_obj = tmp;
225 }
226 return meta_obj->className();
227}
228
229QString QDesignerMemberSheet::memberGroup(int index) const
230{
231 return d->m_info.value(index).group;
232}
233
234void QDesignerMemberSheet::setMemberGroup(int index, const QString &group)
235{
236 d->ensureInfo(index).group = group;
237}
238
239QString QDesignerMemberSheet::signature(int index) const
240{
241 return d->m_meta->method(index)->normalizedSignature();
242}
243
244bool QDesignerMemberSheet::isVisible(int index) const
245{
246 typedef QDesignerMemberSheetPrivate::InfoHash InfoHash;
247 const InfoHash::const_iterator it = d->m_info.constFind(index);
248 if (it != d->m_info.constEnd())
249 return it.value().visible;
250
251 return d->m_meta->method(index)->methodType() == QDesignerMetaMethodInterface::Signal
252 || d->m_meta->method(index)->access() == QDesignerMetaMethodInterface::Public;
253}
254
255void QDesignerMemberSheet::setVisible(int index, bool visible)
256{
257 d->ensureInfo(index).visible = visible;
258}
259
260bool QDesignerMemberSheet::isSignal(int index) const
261{
262 return d->m_meta->method(index)->methodType() == QDesignerMetaMethodInterface::Signal;
263}
264
265bool QDesignerMemberSheet::isSlot(int index) const
266{
267 return d->m_meta->method(index)->methodType() == QDesignerMetaMethodInterface::Slot;
268}
269
270bool QDesignerMemberSheet::inheritedFromWidget(int index) const
271{
272 const QString name = d->m_meta->method(index)->signature();
273 return declaredInClass(index) == QLatin1String("QWidget") || declaredInClass(index) == QLatin1String("QObject");
274}
275
276
277QList<QByteArray> QDesignerMemberSheet::parameterTypes(int index) const
278{
279 return stringListToByteArray(d->m_meta->method(index)->parameterTypes());
280}
281
282QList<QByteArray> QDesignerMemberSheet::parameterNames(int index) const
283{
284 return stringListToByteArray(d->m_meta->method(index)->parameterNames());
285}
286
287bool QDesignerMemberSheet::signalMatchesSlot(const QString &signal, const QString &slot)
288{
289 bool result = true;
290
291 do {
292 int signal_idx = signal.indexOf(QLatin1Char('('));
293 int slot_idx = slot.indexOf(QLatin1Char('('));
294 if (signal_idx == -1 || slot_idx == -1)
295 break;
296
297 ++signal_idx; ++slot_idx;
298
299 if (slot.at(slot_idx) == QLatin1Char(')'))
300 break;
301
302 while (signal_idx < signal.size() && slot_idx < slot.size()) {
303 const QChar signal_c = signal.at(signal_idx);
304 const QChar slot_c = slot.at(slot_idx);
305
306 if (signal_c == QLatin1Char(',') && slot_c == QLatin1Char(')'))
307 break;
308
309 if (signal_c == QLatin1Char(')') && slot_c == QLatin1Char(')'))
310 break;
311
312 if (signal_c != slot_c) {
313 result = false;
314 break;
315 }
316
317 ++signal_idx; ++slot_idx;
318 }
319 } while (false);
320
321 return result;
322}
323
324bool QDesignerMemberSheet::isQt3Signal(int index) const
325{
326 if (!isSignal(index))
327 return false;
328
329 const QString className = declaredInClass(index);
330 const QString signalSignature = signature(index);
331
332 QMap<QString, QStringList> qt3signals = Qt3Members::instance()->getSignals();
333 QMap<QString, QStringList>::const_iterator it = qt3signals.constFind(className);
334 if (it != qt3signals.constEnd() && (*it).contains(signalSignature))
335 return true;
336
337 return false;
338}
339
340bool QDesignerMemberSheet::isQt3Slot(int index) const
341{
342 if (!isSlot(index))
343 return false;
344
345 const QString className = declaredInClass(index);
346 const QString slotSignature = signature(index);
347
348 QMap<QString, QStringList> qt3slots = Qt3Members::instance()->getSlots();
349 QMap<QString, QStringList>::const_iterator it = qt3slots.constFind(className);
350 if (it != qt3slots.constEnd() && (*it).contains(slotSignature))
351 return true;
352 return false;
353}
354
355// ------------ QDesignerMemberSheetFactory
356
357QDesignerMemberSheetFactory::QDesignerMemberSheetFactory(QExtensionManager *parent)
358 : QExtensionFactory(parent)
359{
360}
361
362QObject *QDesignerMemberSheetFactory::createExtension(QObject *object, const QString &iid, QObject *parent) const
363{
364 if (iid == Q_TYPEID(QDesignerMemberSheetExtension)) {
365 return new QDesignerMemberSheet(object, parent);
366 }
367
368 return 0;
369}
370
371QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.