qdesigner_propertycommand.cpp

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

CPP
1,210
字号
    // try to reset sheet, else try to find default    if (m_propertySheet->reset(m_index)) {        defaultValue.first = m_propertySheet->property(m_index);    } else {        defaultValue.first = findDefaultValue(fw);        m_propertySheet->setProperty(m_index, defaultValue.first);    }    m_propertySheet->setChanged(m_index, defaultValue.second);    if (m_objectType == OT_Widget) {        checkApplyWidgetValue(fw, qobject_cast<QWidget *>(m_object), m_specialProperty, defaultValue.first);    }    if (m_specialProperty == SP_ObjectName) {        fw->ensureUniqueObjectName(m_object);        defaultValue.first = m_propertySheet->property(m_index);    }    updateObject(fw, currentValue, defaultValue.first);    return defaultValue;}// ---- PropertyListCommand::PropertyDescription(PropertyListCommand::PropertyDescription::PropertyDescription(const QString &propertyName,                                                              QDesignerPropertySheetExtension *propertySheet,                                                              int index) :    m_propertyName(propertyName),    m_propertyGroup(propertySheet->propertyGroup(index)),    m_propertyType(propertySheet->property(index).type()),    m_specialProperty(getSpecialProperty(propertyName)){}PropertyListCommand::PropertyDescription::PropertyDescription() :    m_propertyType(QVariant::Invalid),    m_specialProperty(SP_None){}void PropertyListCommand::PropertyDescription::debug() const{    qDebug() << m_propertyName << m_propertyGroup << m_propertyType << m_specialProperty;}bool PropertyListCommand::PropertyDescription::equals(const PropertyDescription &p) const{    return m_propertyType == p.m_propertyType && m_specialProperty == p.m_specialProperty &&           m_propertyName == p.m_propertyName && m_propertyGroup   == p.m_propertyGroup;}// ---- PropertyListCommandPropertyListCommand::PropertyListCommand(QDesignerFormWindowInterface *formWindow) :    QDesignerFormWindowCommand(QString(), formWindow){}const QString PropertyListCommand::propertyName() const{    return m_propertyDescription.m_propertyName;}SpecialProperty PropertyListCommand::specialProperty() const{    return m_propertyDescription.m_specialProperty;}// add an objectbool PropertyListCommand::add(QObject *object, const QString &propertyName){    QDesignerPropertySheetExtension* sheet = propertySheet(object);    Q_ASSERT(sheet);    const int index = sheet->indexOf(propertyName);    if (index == -1)        return false;    const PropertyDescription description(propertyName, sheet, index);    if (m_propertyHelperList.empty()) {        // first entry        m_propertyDescription = description;    } else {        // checks: mismatch or only one object in case of name        const bool match = m_propertyDescription.equals(description);        if (!match || m_propertyDescription.m_specialProperty == SP_ObjectName)            return false;    }    m_propertyHelperList.push_back(PropertyHelper(object, m_propertyDescription.m_specialProperty, sheet, index));    return true;}// Init from a list and make sure referenceObject is added first to obtain the right property groupbool PropertyListCommand::initList(const ObjectList &list, const QString &apropertyName, QObject *referenceObject){    propertyHelperList().clear();    // Ensure the referenceObject (property editor) is first, so the right property group is chosen.    if (referenceObject) {        if (!add(referenceObject, apropertyName))            return false;    }    foreach (QObject *o, list) {        if (o != referenceObject)            add(o, apropertyName);    }    return !propertyHelperList().empty();}QObject* PropertyListCommand::object(int index) const{    Q_ASSERT(index < m_propertyHelperList.size());    return m_propertyHelperList[index].object();}QVariant PropertyListCommand::oldValue(int index) const{    Q_ASSERT(index < m_propertyHelperList.size());    return m_propertyHelperList[index].oldValue();}void PropertyListCommand::setOldValue(const QVariant &oldValue, int index){    Q_ASSERT(index < m_propertyHelperList.size());    m_propertyHelperList[index].setOldValue(oldValue);}// ----- SetValueFunction: Set a new value when applied to a PropertyHelper.class SetValueFunction {public:    SetValueFunction(QDesignerFormWindowInterface *formWindow, const PropertyHelper::Value &newValue, unsigned subPropertyMask);    PropertyHelper::Value operator()(PropertyHelper&);private:    QDesignerFormWindowInterface *m_formWindow;    const PropertyHelper::Value m_newValue;    const unsigned m_subPropertyMask;};SetValueFunction::SetValueFunction(QDesignerFormWindowInterface *formWindow, const PropertyHelper::Value &newValue, unsigned subPropertyMask) :    m_formWindow(formWindow),    m_newValue(newValue),    m_subPropertyMask(subPropertyMask){}PropertyHelper::Value SetValueFunction::operator()(PropertyHelper &ph) {        return ph.setValue(m_formWindow, m_newValue.first, m_newValue.second, m_subPropertyMask);}// ----- UndoSetValueFunction: Restore old value when applied to a PropertyHelper.class UndoSetValueFunction {public:    UndoSetValueFunction(QDesignerFormWindowInterface *formWindow) : m_formWindow(formWindow) {}    PropertyHelper::Value operator()(PropertyHelper& ph) { return ph.restoreOldValue(m_formWindow); }private:    QDesignerFormWindowInterface *m_formWindow;};// ----- RestoreDefaultFunction: Restore default value when applied to a PropertyHelper.class RestoreDefaultFunction {public:    RestoreDefaultFunction(QDesignerFormWindowInterface *formWindow) : m_formWindow(formWindow) {}    PropertyHelper::Value operator()(PropertyHelper& ph) { return ph.restoreDefaultValue(m_formWindow); }private:    QDesignerFormWindowInterface *m_formWindow;};// ----- changePropertyList: Iterates over a sequence of PropertyHelpers and// applies a function to them.// The function returns the  corrected value which is then set in  the property editor.// Returns a combination of update flags.template <class PropertyListIterator, class Function>        unsigned changePropertyList(QDesignerFormEditorInterface *core,                                    const QString &propertyName,                                    PropertyListIterator begin,                                    PropertyListIterator end,                                    Function function){    unsigned updateMask = 0;    QDesignerPropertyEditorInterface *propertyEditor = core->propertyEditor();    bool updatedPropertyEditor = false;    for (PropertyListIterator it = begin; it != end; ++it) {        if (QObject* object = it->object()) { // Might have been deleted in the meantime            const PropertyHelper::Value newValue = function(*it);            updateMask |= it->updateMask();            // Update property editor if it is the current object            if (!updatedPropertyEditor && propertyEditor && object == propertyEditor->object()) {                propertyEditor->setPropertyValue(propertyName, newValue.first,  newValue.second);                updatedPropertyEditor = true;            }        }    }    if (!updatedPropertyEditor) updateMask |=  PropertyHelper::UpdatePropertyEditor;    return updateMask;}// set a new value, return update maskunsigned PropertyListCommand::setValue(QVariant value, bool changed, unsigned subPropertyMask){    if(debugPropertyCommands)        qDebug() << "PropertyListCommand::setValue(" << value <<  changed << subPropertyMask << ')';    return changePropertyList(formWindow()->core(),                              m_propertyDescription.m_propertyName, m_propertyHelperList.begin(), m_propertyHelperList.end(),                              SetValueFunction(formWindow(), PropertyHelper::Value(value, changed), subPropertyMask));}// restore old value,  return update maskunsigned PropertyListCommand::restoreOldValue(){    if(debugPropertyCommands)        qDebug() << "PropertyListCommand::restoreOldValue()";    return changePropertyList(formWindow()->core(),                              m_propertyDescription.m_propertyName, m_propertyHelperList.begin(), m_propertyHelperList.end(),                              UndoSetValueFunction(formWindow()));}// set default value,  return update maskunsigned PropertyListCommand::restoreDefaultValue(){    if(debugPropertyCommands)        qDebug() << "PropertyListCommand::restoreDefaultValue()";    return changePropertyList(formWindow()->core(),                              m_propertyDescription.m_propertyName, m_propertyHelperList.begin(), m_propertyHelperList.end(),                              RestoreDefaultFunction(formWindow()));}// updatevoid PropertyListCommand::update(unsigned updateMask){    if(debugPropertyCommands)        qDebug() << "PropertyListCommand::update(" << updateMask << ')';    if (updateMask & PropertyHelper::UpdateObjectInspector) {        if (QDesignerObjectInspectorInterface *oi = formWindow()->core()->objectInspector())            oi->setFormWindow(formWindow());    }    if (updateMask & PropertyHelper::UpdatePropertyEditor) {        // this is needed when f.ex. undo, changes parent's palette, but        // the child is the active widget,        // TODO: current object?        if (QDesignerPropertyEditorInterface *propertyEditor = formWindow()->core()->propertyEditor()) {            propertyEditor->setObject(propertyEditor->object());        }    }}void PropertyListCommand::undo(){    update(restoreOldValue());    QDesignerPropertyEditor *designerPropertyEditor = qobject_cast<QDesignerPropertyEditor *>(core()->propertyEditor());    if (designerPropertyEditor)        designerPropertyEditor->updatePropertySheet();}// check if lists are aequivalent for command merging (same widgets and props)bool PropertyListCommand::canMergeLists(const PropertyHelperList& other) const{    if (m_propertyHelperList.size() !=  other.size())        return false;    for (int i = 0; i < m_propertyHelperList.size(); i++) {        if (!m_propertyHelperList[i].canMerge(other[i]))            return false;    }    return true;}// ---- SetPropertyCommand ----SetPropertyCommand::SetPropertyCommand(QDesignerFormWindowInterface *formWindow)    :  PropertyListCommand(formWindow),       m_subPropertyMask(SubPropertyAll){}bool SetPropertyCommand::init(QObject *object, const QString &apropertyName, const QVariant &newValue){    Q_ASSERT(object);    m_newValue = newValue;    propertyHelperList().clear();    if (!add(object, apropertyName))        return false;    setDescription();    return true;}bool SetPropertyCommand::init(const ObjectList &list, const QString &apropertyName, const QVariant &newValue,                              QObject *referenceObject){    if (!initList(list, apropertyName, referenceObject))        return false;

⌨️ 快捷键说明

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