widgetdatabase.cpp

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

CPP
533
字号
    static_cast<WidgetDataBaseItem *>(item(indexOfClassName(QLatin1String("QDockWidget"))))->setContainer(true);    static_cast<WidgetDataBaseItem *>(item(indexOfClassName(QLatin1String("QDesignerDockWidget"))))->setContainer(true);    static_cast<WidgetDataBaseItem *>(item(indexOfClassName(QLatin1String("QDesignerQ3WidgetStack"))))->setContainer(true);    static_cast<WidgetDataBaseItem *>(item(indexOfClassName(QLatin1String("QWidget"))))->setContainer(true);    static_cast<WidgetDataBaseItem *>(item(indexOfClassName(QLatin1String("QDialog"))))->setContainer(true);}WidgetDataBase::~WidgetDataBase(){}QDesignerFormEditorInterface *WidgetDataBase::core() const{    return m_core;}int WidgetDataBase::indexOfObject(QObject *object, bool /*resolveName*/) const{    QExtensionManager *mgr = m_core->extensionManager();    QDesignerLanguageExtension *lang = qt_extension<QDesignerLanguageExtension*> (mgr, m_core);    QString id;    if (lang)        id = lang->classNameOf(object);    if (id.isEmpty())        id = WidgetFactory::classNameOf(m_core,object);    return QDesignerWidgetDataBaseInterface::indexOfClassName(id);}void WidgetDataBase::loadPlugins(){    typedef QMap<QString, int> NameIndexMap;    typedef QList<QDesignerWidgetDataBaseItemInterface*> ItemList;    typedef QMap<QString, QDesignerWidgetDataBaseItemInterface*> NameItemMap;    typedef QSet<QString> NameSet;    // 1) create a map of existing custom classes    NameIndexMap existingCustomClasses;    NameSet nonCustomClasses;    const int count = m_items.size();    for (int i = 0; i < count; i++)    {        const QDesignerWidgetDataBaseItemInterface* item =  m_items[i];        if (item->isCustom() && !item->isPromoted())            existingCustomClasses.insert(item->name(), i);        else            nonCustomClasses.insert(item->name());    }    // 2) create a list map of plugins and the map for the factory    ItemList pluginList;    QDesignerPluginManager *pluginManager = m_core->pluginManager();    const QStringList plugins = pluginManager->registeredPlugins();    pluginManager->ensureInitialized();    foreach (QString plugin, plugins) {        QObject *o = pluginManager->instance(plugin);        if (QDesignerCustomWidgetInterface *c = qobject_cast<QDesignerCustomWidgetInterface*>(o)) {            pluginList += createCustomWidgetItem(c, plugin);        } else {            if (QDesignerCustomWidgetCollectionInterface *coll = qobject_cast<QDesignerCustomWidgetCollectionInterface*>(o)) {                foreach (QDesignerCustomWidgetInterface *c, coll->customWidgets()) {                    pluginList += createCustomWidgetItem(c, plugin);                }            }        }    }    // 3) replace custom classes or add new ones, remove them from existingCustomClasses,    // leaving behind deleted items    unsigned replacedPlugins = 0;    unsigned addedPlugins = 0;    unsigned removedPlugins = 0;    if (!pluginList.empty()) {        ItemList::const_iterator cend = pluginList.constEnd();        for (ItemList::const_iterator it = pluginList.constBegin();it != cend; ++it )  {            QDesignerWidgetDataBaseItemInterface* pluginItem = *it;            const QString pluginName = pluginItem->name();            NameIndexMap::iterator existingIt = existingCustomClasses.find(pluginName);            if (existingIt == existingCustomClasses.end()) {                // Add new class.                if (nonCustomClasses.contains(pluginName)) {                    designerWarning(QObject::tr("A custom widget plugin whose class name (%1) matches that of an existing class has been found.").arg(pluginName));                } else {                    append(pluginItem);                    addedPlugins++;                }            } else {                // replace existing info                const int existingIndex = existingIt.value();                delete m_items[existingIndex];                m_items[existingIndex] = pluginItem;                existingCustomClasses.erase(existingIt);                replacedPlugins++;            }        }    }    // 4) remove classes that have not been matched. The stored indexes become invalid while deleting.    if (!existingCustomClasses.empty()) {        NameIndexMap::const_iterator cend = existingCustomClasses.constEnd();        for (NameIndexMap::const_iterator it = existingCustomClasses.constBegin();it != cend; ++it )  {            const int index = indexOfClassName(it.key());            if (index != -1) {                remove(index);                removedPlugins++;            }        }    }    if (debugWidgetDataBase)        qDebug() << "WidgetDataBase::loadPlugins(): " << addedPlugins << " added, " << replacedPlugins << " replaced, " << removedPlugins << "deleted.";}WidgetDataBaseItem *WidgetDataBase::createCustomWidgetItem(const QDesignerCustomWidgetInterface *c, const QString &plugin){    WidgetDataBaseItem *item = new WidgetDataBaseItem(c->name(), c->group());    item->setContainer(c->isContainer());    item->setCustom(true);    item->setIcon(c->icon());    item->setIncludeFile(c->includeFile());    item->setToolTip(c->toolTip());    item->setWhatsThis(c->whatsThis());    item->setPluginPath(plugin);    return item;}void WidgetDataBase::remove(int index){    Q_ASSERT(index < m_items.size());    delete m_items.takeAt(index);}QList<QVariant> WidgetDataBase::defaultPropertyValues(const QString &name){    const WidgetFactory factory(m_core);    // Create non-widgets, widgets in order    QObject* object = factory.createObject(name, 0);    if (!object)        object = factory.createWidget(name, 0);    if (!object) {        qDebug() << "** WARNING Factory failed to create " << name;        return QList<QVariant>();    }    // Get properties from sheet.    QList<QVariant> result;    if (const QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(m_core->extensionManager(), object)) {        for (int i = 0; i < sheet->count(); ++i) {            result.append(sheet->property(i));        }    }    delete object;    return result;}void WidgetDataBase::grabDefaultPropertyValues(){    for (int i = 0; i < count(); ++i) {        QDesignerWidgetDataBaseItemInterface *dbItem = item(i);        const QList<QVariant> default_prop_values = defaultPropertyValues(dbItem->name());        dbItem->setDefaultPropertyValues(default_prop_values);    }}QDESIGNER_SHARED_EXPORT IncludeSpecification  includeSpecification(QString includeFile){    const bool global = !includeFile.isEmpty() &&                        includeFile[0] == QLatin1Char('<') &&                        includeFile[includeFile.size() - 1] ==  QLatin1Char('>');    if (global) {        includeFile.remove(includeFile.size() - 1, 1);        includeFile.remove(0, 1);    }    return IncludeSpecification(includeFile, global ? IncludeGlobal : IncludeLocal);}QDESIGNER_SHARED_EXPORT QString buildIncludeFile(QString includeFile, IncludeType includeType) {    if (includeType == IncludeGlobal && !includeFile.isEmpty()) {        includeFile.append(QLatin1Char('>'));        includeFile.insert(0, QLatin1Char('<'));    }    return includeFile;}/* Appends a derived class to the database inheriting the data of the base class. Used   for custom and promoted widgets.   Depending on whether an entry exists, the existing or a newly created entry is   returned. A return value of 0 indicates that the base class could not be found. */QDESIGNER_SHARED_EXPORT QDesignerWidgetDataBaseItemInterface *        appendDerived(QDesignerWidgetDataBaseInterface *db,                      const QString &className, const QString &group,                      const QString &baseClassName,                      const QString &includeFile,                      bool promoted, bool custom)        {    if (debugWidgetDataBase)        qDebug() << "appendDerived " << className << " derived from " << baseClassName;    // Check whether item already exists.    QDesignerWidgetDataBaseItemInterface *derivedItem = 0;    const int existingIndex = db->indexOfClassName(className);    if ( existingIndex != -1)        derivedItem =  db->item(existingIndex);    if (derivedItem) {        // Check the existing item for base class mismatch. This will likely        // happen when loading a file written by an instance with missing plugins.        // In that case, just warn and ignore the file properties.        //        // An empty base class indicates that it is not known (for example, for custom plugins).        // In this case, the widget DB is later updated once the widget is created        // by DOM (by querying the metaobject). Suppress the warning.        const QString existingBaseClass = derivedItem->extends();        if (existingBaseClass.isEmpty() || baseClassName ==  existingBaseClass)            return derivedItem;        // Warn about mismatches        const char *baseWarning = "The file contains a custom widget '%1' whose base class (%2)"          " differs from the current entry in the widget database (%3)."           " The widget database is left unchanged.";        designerWarning(QObject::tr(baseWarning).arg(className).arg(baseClassName).arg(existingBaseClass));        return derivedItem;    }    // Create this item, inheriting its base properties    const int baseIndex = db->indexOfClassName(baseClassName);    if (baseIndex == -1) {        if (debugWidgetDataBase)            qDebug() << "appendDerived failed due to missing base class";        return 0;    }    const QDesignerWidgetDataBaseItemInterface *baseItem = db->item(baseIndex);    derivedItem = WidgetDataBaseItem::clone(baseItem);    // Sort of hack: If base class is QWidget, we most likely    // do not want to inherit the container attribute.    static const QString qWidgetName = QLatin1String("QWidget");    if (baseItem->name() == qWidgetName)        derivedItem->setContainer(false);    // set new props    derivedItem->setName(className);    derivedItem->setGroup(group);    derivedItem->setCustom(custom);    derivedItem->setPromoted(promoted);    derivedItem->setExtends(baseClassName);    derivedItem->setIncludeFile(includeFile);    db->append(derivedItem);    return derivedItem;}/* Return a list of database items to which a class can be promoted to. */QDESIGNER_SHARED_EXPORT WidgetDataBaseItemList        promotionCandidates(const QDesignerWidgetDataBaseInterface *db,                            const QString &baseClassName){    WidgetDataBaseItemList rc;    // find existing promoted widgets deriving from base.    for (int i = 0; i < db->count(); ++i) {        QDesignerWidgetDataBaseItemInterface *item = db->item(i);        if (item->isPromoted() && item->extends() == baseClassName) {            rc.push_back(item);        }    }    return rc;}} // namespace qdesigner_internal

⌨️ 快捷键说明

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