signalsloteditor.cpp

来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 925 行 · 第 1/2 页

CPP
925
字号
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt Designer of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "signalsloteditor.h"#include "signalsloteditor_p.h"#include <qdesigner_membersheet_p.h>#include <ui4_p.h>#include <QtDesigner/QDesignerFormWindowInterface>#include <QtDesigner/QDesignerFormEditorInterface>#include <QtDesigner/QExtensionManager>#include <QtDesigner/QDesignerMetaDataBaseInterface>#include <QtDesigner/QDesignerContainerExtension>#include <QtDesigner/QDesignerLanguageExtension>#include <QtDesigner/QDesignerFormWindowCursorInterface>#include <QtDesigner/QDesignerWidgetDataBaseInterface>#include <QtGui/QAction>#include <QtGui/QDialog>#include <QtGui/QDialogButtonBox>#include <QtGui/QListWidget>#include <QtGui/QHBoxLayout>#include <QtGui/QVBoxLayout>#include <QtGui/QPushButton>#include <QtGui/QLabel>#include <QtGui/QCheckBox>#include <QtGui/QApplication>#include <QtGui/QUndoCommand>#include <QtGui/QMenu>#include <QtCore/qdebug.h>/********************************************************************************* Tools*/template <typename T>static void merge(QDesignerFormWindowInterface *form, QStringList *lst, const QList<T> &elts){    QDesignerMetaDataBaseInterface *db = form->core()->metaDataBase();    foreach (T e, elts) {        QAction *action = qobject_cast<QAction*>(e);        if (action && db->item(action->menu())) {            // good        } else if (!db->item(e)) {            // hmm, nothing to do            continue;        }        QString name = e->objectName();        if (action && action->menu())            name = action->menu()->objectName();        if (name.isEmpty())            continue;        lst->append(name);    }}static bool signalMatchesSlot(QDesignerFormEditorInterface *core,                              const QString &signal,                              const QString &slot){        QExtensionManager *em = core->extensionManager();    const QDesignerLanguageExtension *lang = qt_extension<QDesignerLanguageExtension*> (em, core);    if (lang)        return lang->signalMatchesSlot(signal, slot);    return QDesignerMemberSheet::signalMatchesSlot(signal, slot);}namespace qdesigner_internal {QStringList objectNameList(QDesignerFormWindowInterface *form){    QStringList result;    if (form->mainContainer()) {        QDesignerContainerExtension *c = qt_extension<QDesignerContainerExtension *>(                    form->core()->extensionManager(), form->mainContainer());        if (c) {            for (int i = 0 ; i < c->count(); i++)                result.append(c->widget(i)->objectName().trimmed());        }    }    QDesignerFormWindowCursorInterface *cursor = form->cursor();    for (int i = 0; i < cursor->widgetCount(); ++i) {        const QString name = cursor->widget(i)->objectName().trimmed();        if (!name.isEmpty())            result.append(name);    }    if (form->mainContainer()) {        merge(form, &result, qFindChildren<QAction*>(form->mainContainer()));    }    result.sort();    return result;}QStringList memberList(QDesignerFormWindowInterface *form, QObject *object, MemberType member_type){    QStringList result;    if (object == 0)        return result;    QDesignerMemberSheetExtension *members        = qt_extension<QDesignerMemberSheetExtension*>                (form->core()->extensionManager(), object);    Q_ASSERT(members != 0);    for (int i = 0; i < members->count(); ++i) {        if (!members->isVisible(i))            continue;        if (member_type == SignalMember && !members->isSignal(i))            continue;        if (member_type == SlotMember && !members->isSlot(i))            continue;        result.append(members->signature(i));    }    return result;}ClassList classList(const QString &obj_name, MemberType member_type,                            const QString &peer, QDesignerFormWindowInterface *form){    ClassList result;    QObject *object = qFindChild<QObject*>(form, obj_name);    if (object == 0)        return result;    QDesignerFormEditorInterface *core = form->core();    QDesignerMemberSheetExtension *members = qt_extension<QDesignerMemberSheetExtension*>(core->extensionManager(), object);    Q_ASSERT(members != 0);    QString class_name;    QStringList member_list;    for (int i = members->count() -  1; i >= 0; --i) {        if (!members->isVisible(i))            continue;        if (member_type == SignalMember && !members->isSignal(i))            continue;        if (member_type == SlotMember && !members->isSlot(i))            continue;        const QString signal = member_type == SignalMember ? members->signature(i) : peer;        const QString slot = member_type == SignalMember ? peer : members->signature(i);        if (!signalMatchesSlot(core, signal, slot))            continue;       const QString s = members->declaredInClass(i);        if (s != class_name) {            if (!member_list.isEmpty())                result.append(ClassInfo(class_name, member_list));            class_name = s;            member_list.clear();        }        member_list.append(members->signature(i));    }    if (!member_list.isEmpty())        result.append(ClassInfo(class_name, member_list));    return result;}}namespace {/********************************************************************************* OldSignalSlotDialog*/class OldSignalSlotDialog : public QDialog{    Q_OBJECTpublic:    OldSignalSlotDialog(QDesignerFormEditorInterface *core, QWidget *sender, QWidget *receiver, QWidget *parent = 0);    QString signal() const;    QString slot() const;    void setSignalSlot(const QString &signal, const QString &slot);    bool showAllSignalsSlots() const;    void setShowAllSignalsSlots(bool showIt);private slots:    void selectSignal(QListWidgetItem *item);    void selectSlot(QListWidgetItem *item);    void populateSignalList();    void populateSlotList(const QString &signal = QString());private:    QListWidget *m_signal_list, *m_slot_list;    QDialogButtonBox *m_buttonBox;    QPushButton *m_ok_button;    QWidget *m_source, *m_destination;    QDesignerFormEditorInterface *m_core;    QCheckBox *m_show_all_checkbox;};// ### deprecatedQString realObjectName(QDesignerFormEditorInterface *core, QObject *object){    if (object == 0)        return QString();    const QDesignerMetaDataBaseInterface *mdb = core->metaDataBase();    if (const QDesignerMetaDataBaseItemInterface *item = mdb->item(object))        return item->name();    return object->objectName();}QString realClassName(QDesignerFormEditorInterface *core, QWidget *widget){    QString class_name = QLatin1String(widget->metaObject()->className());    const QDesignerWidgetDataBaseInterface *wdb = core->widgetDataBase();    const int idx = wdb->indexOfObject(widget);    if (idx != -1)        class_name = wdb->item(idx)->name();    return class_name;}QString widgetLabel(QDesignerFormEditorInterface *core, QWidget *widget){    return QString::fromUtf8("%1 (%2)")            .arg(realObjectName(core, widget))            .arg(realClassName(core, widget));}void OldSignalSlotDialog::populateSlotList(const QString &signal){    QString selectedName;    if (QListWidgetItem * item = m_slot_list->currentItem()) {        selectedName = item->text();    }    m_slot_list->clear();    const bool show_all = m_show_all_checkbox->isChecked();    QStringList signatures;    if (QDesignerMemberSheetExtension *members            = qt_extension<QDesignerMemberSheetExtension*>                    (m_core->extensionManager(), m_destination)) {        for (int i=0; i<members->count(); ++i) {            if (!members->isVisible(i))                continue;            if (!show_all && members->inheritedFromWidget(i))                continue;            if (members->isSlot(i)) {                if (!signalMatchesSlot(m_core, signal, members->signature(i)))                    continue;                signatures.append(members->signature(i));            }        }    }    signatures.sort();    QListWidgetItem *curr = 0;    foreach (QString sig, signatures) {        QListWidgetItem *item = new QListWidgetItem(m_slot_list);        item->setText(sig);        if (sig == selectedName)            curr = item;    }    if (curr)        m_slot_list->setCurrentItem(curr);    if (m_slot_list->selectedItems().isEmpty())        m_ok_button->setEnabled(false);}void OldSignalSlotDialog::populateSignalList(){    QString selectedName;    if (QListWidgetItem * item = m_signal_list->currentItem()) {        selectedName = item->text();    }    m_signal_list->clear();    const bool show_all = m_show_all_checkbox->isChecked();    QStringList signatures;    if (QDesignerMemberSheetExtension *members = qt_extension<QDesignerMemberSheetExtension*>(m_core->extensionManager(), m_source)) {        for (int i=0; i<members->count(); ++i) {            if (!members->isVisible(i))                continue;            if (!show_all && members->inheritedFromWidget(i))                continue;            if (members->isSignal(i)) {                signatures.append(members->signature(i));            }        }    }    signatures.sort();    QListWidgetItem *curr = 0;    foreach (QString sig, signatures) {        QListWidgetItem *item = new QListWidgetItem(m_signal_list);        item->setText(sig);        if (!selectedName.isEmpty() && sig == selectedName) {            curr = item;        }    }    if (curr) {        m_signal_list->setCurrentItem(curr);    } else {        selectedName.clear();    }    populateSlotList(selectedName);    if (!curr) {        m_slot_list->setEnabled(false);    }}// ### use designerOldSignalSlotDialog::OldSignalSlotDialog(QDesignerFormEditorInterface *core, QWidget *source, QWidget *destination,                                         QWidget *parent) :    QDialog(parent),    m_signal_list(new QListWidget(this)),    m_slot_list(new QListWidget(this)),    m_buttonBox(new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,Qt::Horizontal, this)),    m_ok_button(m_buttonBox->button(QDialogButtonBox::Ok)),    m_source(source),    m_destination(destination),    m_core(core),    m_show_all_checkbox(new QCheckBox(QObject::tr("Show all signals and slots"))){    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);    m_signal_list->setTextElideMode (Qt::ElideMiddle);    m_slot_list->setTextElideMode (Qt::ElideMiddle);    connect(m_signal_list,                SIGNAL(itemClicked(QListWidgetItem*)),            this,                SLOT(selectSignal(QListWidgetItem*)));    connect(m_slot_list,                SIGNAL(itemClicked(QListWidgetItem*)),            this,                SLOT(selectSlot(QListWidgetItem*)));    m_slot_list->setEnabled(false);    m_ok_button->setDefault(true);    m_ok_button->setEnabled(false);    connect(m_buttonBox, SIGNAL(accepted()), this, SLOT(accept()));    connect(m_buttonBox, SIGNAL(rejected()), this, SLOT(reject()));    connect(m_show_all_checkbox, SIGNAL(toggled(bool)), this, SLOT(populateSignalList()));    QLabel *source_label = new QLabel(this);    source_label->setText(widgetLabel(core, source));    QLabel *destination_label = new QLabel(this);    destination_label->setText(widgetLabel(core, destination));    QVBoxLayout *l1 = new QVBoxLayout(this);    QHBoxLayout *l2 = new QHBoxLayout();    l1->addLayout(l2);    QVBoxLayout *l3 = new QVBoxLayout();    l2->addLayout(l3);    l3->addWidget(source_label);    l3->addWidget(m_signal_list);    QVBoxLayout *l4 = new QVBoxLayout();    l2->addLayout(l4);    l4->addWidget(destination_label);    l4->addWidget(m_slot_list);    l1->addWidget(m_show_all_checkbox);    l1->addWidget(m_buttonBox);    setWindowTitle(QObject::tr("Configure Connection"));    populateSignalList();}QListWidgetItem *findItem(const QListWidget &list_widget, const QString &text){    QListWidgetItem *result = 0;    for (int i = 0; i < list_widget.count(); ++i) {        QListWidgetItem *item = list_widget.item(i);        if (item->text() == text) {            result = item;            break;        }    }    return result;}void OldSignalSlotDialog::setSignalSlot(const QString &signal, const QString &slot){    QListWidgetItem *sig_item = findItem(*m_signal_list, signal);    if (sig_item == 0) {        m_show_all_checkbox->setChecked(true);        sig_item = findItem(*m_signal_list, signal);    }    if (sig_item != 0) {        selectSignal(sig_item);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?