qdesigner_promotion.cpp

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

CPP
367
字号
/******************************************************************************** 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 "qdesigner_promotion_p.h"#include "widgetdatabase_p.h"#include "metadatabase_p.h"#include "widgetdatabase_p.h"#include <QtDesigner/QDesignerFormEditorInterface>#include <QtDesigner/QDesignerFormWindowInterface>#include <QtDesigner/QDesignerFormWindowManagerInterface>#include <QtDesigner/QDesignerObjectInspectorInterface>#include <QtDesigner/QDesignerWidgetBoxInterface>#include <QtDesigner/QDesignerWidgetDataBaseInterface>#include <QtCore/QMap>#include <QtCore/QCoreApplication>#include <qdebug.h>namespace {    // Return a set of on-promotable classes    const QSet<QString> &nonPromotableClasses() {        static QSet<QString> rc;        if (rc.empty()) {            rc.insert(QLatin1String("Line"));            rc.insert(QLatin1String("QAction"));            rc.insert(QLatin1String("Spacer"));            rc.insert(QLatin1String("QMainWindow"));            rc.insert(QLatin1String("QDialog"));            rc.insert(QLatin1String("QStatusBar"));            rc.insert(QLatin1String("QToolBar"));            rc.insert(QLatin1String("QWorkspace"));            rc.insert(QLatin1String("QMdiArea"));            rc.insert(QLatin1String("QMdiSubWindow"));        }        return rc;    }    // Return widget database index of a promoted class or -1 with error message    int promotedWidgetDataBaseIndex(const QDesignerWidgetDataBaseInterface *widgetDataBase,                                                                 const QString &className,                                                                 QString *errorMessage) {        const int index = widgetDataBase->indexOfClassName(className);        if (index == -1 || !widgetDataBase->item(index)->isPromoted()) {            *errorMessage = QCoreApplication::tr("%1 is not a promoted class.").arg(className);            return -1;        }        return index;    }    // Return widget database item of a promoted class or 0 with error message    QDesignerWidgetDataBaseItemInterface *promotedWidgetDataBaseItem(const QDesignerWidgetDataBaseInterface *widgetDataBase,                                                                     const QString &className,                                                                     QString *errorMessage) {        const int index =  promotedWidgetDataBaseIndex(widgetDataBase, className, errorMessage);        if (index == -1)            return 0;        return widgetDataBase->item(index);    }    // extract class name from xml  "<widget class="QWidget" ...>". Quite a hack.    QString classNameFromXml(QString xml) {        static const QString tag = QLatin1String("class=\"");        const int pos = xml.indexOf(tag);        if (pos == -1)            return QString();        xml.remove(0, pos + tag.size());        const int closingPos = xml.indexOf(QLatin1Char('"'));        if (closingPos == -1)            return QString();        xml.remove(closingPos, xml.size() - closingPos);        return xml;    }    // return a list of class names in the scratch pad    QStringList getScratchPadClasses(const QDesignerWidgetBoxInterface *wb) {        QStringList rc;        const int catCount =  wb->categoryCount();        for (int c = 0; c <  catCount; c++) {            const QDesignerWidgetBoxInterface::Category category = wb->category(c);            if (category.type() == QDesignerWidgetBoxInterface::Category::Scratchpad) {                const int widgetCount = category.widgetCount();                for (int w = 0; w < widgetCount; w++) {                    const QString className = classNameFromXml( category.widget(w).domXml());                    if (!className.isEmpty())                        rc += className;                }            }        }        return rc;    }}namespace qdesigner_internal {    QDesignerPromotion::QDesignerPromotion(QDesignerFormEditorInterface *core) :        m_core(core)  {    }    bool  QDesignerPromotion::addPromotedClass(const QString &baseClass,                                               const QString &className,                                               const QString &includeFile,                                               QString *errorMessage)    {        QDesignerWidgetDataBaseInterface *widgetDataBase = m_core->widgetDataBase();        const int baseClassIndex = widgetDataBase->indexOfClassName(baseClass);        if (baseClassIndex == -1) {            *errorMessage = QCoreApplication::tr("The base class %1 is invalid.").arg(baseClass);            return false;        }        const int existingClassIndex = widgetDataBase->indexOfClassName(className);        if (existingClassIndex != -1) {            *errorMessage = QCoreApplication::tr("The class %1 already exists.").arg(className);            return false;        }        // Clone derived item.        QDesignerWidgetDataBaseItemInterface *promotedItem = WidgetDataBaseItem::clone(widgetDataBase->item(baseClassIndex));        // Also inherit the container flag in case of QWidget-derived classes        // as it is most likely intended for stacked pages.        // set new props        promotedItem->setName(className);        promotedItem->setGroup(QCoreApplication::tr("Promoted Widgets"));        promotedItem->setCustom(true);        promotedItem->setPromoted(true);        promotedItem->setExtends(baseClass);        promotedItem->setIncludeFile(includeFile);        widgetDataBase->append(promotedItem);        return true;    }    QList<QDesignerWidgetDataBaseItemInterface *> QDesignerPromotion::promotionBaseClasses() const    {        typedef QMap<QString, QDesignerWidgetDataBaseItemInterface *> SortedDatabaseItemMap;        SortedDatabaseItemMap sortedDatabaseItemMap;        QDesignerWidgetDataBaseInterface *widgetDataBase = m_core->widgetDataBase();        const int cnt = widgetDataBase->count();        for (int i = 0; i <  cnt; i++) {            QDesignerWidgetDataBaseItemInterface *dbItem = widgetDataBase->item(i);            if (canBePromoted(dbItem)) {                sortedDatabaseItemMap.insert(dbItem->name(), dbItem);            }        }        return sortedDatabaseItemMap.values();    }

⌨️ 快捷键说明

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