qpropertyeditor_items.cpp

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

CPP
2,138
字号
    const int family = fontDatabase()->families().indexOf(m_font.family());    int pointSize = m_font.pointSize();    if (pointSize < 1) {        // try to convert from pixel size and resolved font        // see also code in FontProperty constructor        pointSize = QFontInfo(m_font).pointSize();    }    propertyAt(0)->setValue(family);    propertyAt(1)->setValue(pointSize);    propertyAt(2)->setValue(m_font.bold());    propertyAt(3)->setValue(m_font.italic());    propertyAt(4)->setValue(m_font.underline());    propertyAt(5)->setValue(m_font.strikeOut());    propertyAt(6)->setValue(m_font.kerning());    propertyAt(7)->setValue(m_font.styleStrategy());}QVariant FontProperty::decoration() const{    QPixmap pix(16, 16);    pix.fill(Qt::white);    QPainter p(&pix);    QFont fnt = qvariant_cast<QFont>(value());    fnt.setPointSize(10); // ### always 10pt!!    p.drawRect(0, 0, 16, 16);    p.setFont(fnt);    p.drawText(0, 16 - 2, QLatin1String("Aa")); // ### 2px for the border!!    return qVariantFromValue(pix);}QString FontProperty::toString() const{    const QString family = propertyAt(0)->toString();    const QString pointSize = propertyAt(1)->value().toString();    QString rc(QLatin1String("  "));  // ### temp hack    rc += QLatin1Char('[');    rc += family;    rc += QLatin1String(", ");    rc += pointSize;    rc += QLatin1Char(']');    return rc;}// -------------------------------------------------------------------------MapProperty::MapProperty(const QMap<QString, QVariant> &items,                         const QVariant &value,                         const QString &name,                         const QStringList &okeys)    : AbstractProperty<QVariant>(value, name),      m_items(items),      m_keys(items.keys()),      comboKeys(okeys.isEmpty() ? m_keys : okeys){}QStringList MapProperty::keys() const{    return m_keys;}QMap<QString, QVariant> MapProperty::items() const{    return m_items;}QVariant MapProperty::value() const{    return m_value;}void MapProperty::setValue(const QVariant &value){   if (qVariantCanConvert<EnumType>(value)) {        const EnumType e = qvariant_cast<EnumType>(value);        m_value = e.value;    } else if (qVariantCanConvert<FlagType>(value)) {        const FlagType e = qvariant_cast<FlagType>(value);        m_value = e.value;    } else {        m_value = value;    }}QString MapProperty::toString() const{    return m_items.key(m_value);}int MapProperty::indexOf(const QVariant &value) const{    const QString key = m_items.key(value);    return comboKeys.indexOf(key);}QWidget *MapProperty::createEditor(QWidget *parent, const QObject *target, const char *receiver) const{    QComboBox *combo = new QComboBox(parent);    combo->view()->setTextElideMode(Qt::ElideLeft);    combo->setFrame(0);    combo->addItems(comboKeys);    QObject::connect(combo, SIGNAL(activated(int)), target, receiver);    return combo;}void MapProperty::updateEditorContents(QWidget *editor){    if (QComboBox *combo = qobject_cast<QComboBox*>(editor)) {        combo->setCurrentIndex(indexOf(m_value));    }}void MapProperty::updateValue(QWidget *editor){    if (QComboBox *combo = qobject_cast<QComboBox*>(editor)) {        const QString key = combo->currentText();        const QVariant newValue = m_items.value(key);        if (newValue != m_value) {            m_value = newValue;            setChanged(true);        }    }}// -------------------------------------------------------------------------FlagsProperty::FlagsProperty(const QMap<QString, QVariant> &items, unsigned int value,                             const QString &name)    : MapProperty(items, QVariant(value), name){}QWidget *FlagsProperty::createEditor(QWidget *parent, const QObject *target, const char *receiver) const{    QList<FlagBoxModelItem> l;    QMapIterator<QString, QVariant> it(items());    const unsigned int v = m_value.toUInt();    int initialIndex = -1;    int i = 0;    while (it.hasNext()) {        it.next();        unsigned int value = it.value().toUInt();        bool checked = (value == 0) ? (v == 0) : ((value & v) == value);        l.append(FlagBoxModelItem(it.key(), value, checked));        if ((value & v) == value) {            if (initialIndex == -1)                initialIndex = i;            else if (FlagBoxModel::bitcount(value) > FlagBoxModel::bitcount(l.at(initialIndex).value()))                initialIndex = i;        }        ++i;    }    FlagBox *editor = new FlagBox(parent);    editor->setItems(l);    editor->setCurrentIndex(initialIndex);    QObject::connect(editor, SIGNAL(activated(int)), target, receiver);    return editor;}void FlagsProperty::updateEditorContents(QWidget *editor){    FlagBox *box = qobject_cast<FlagBox*>(editor);    if (box == 0)        return;    box->view()->reset();}void FlagsProperty::updateValue(QWidget *editor){    FlagBox *box = qobject_cast<FlagBox*>(editor);    if ((box == 0) || (box->currentIndex() < 0))        return;    unsigned int newValue = 0;    FlagBoxModelItem &thisItem = box->item(box->currentIndex());    if (thisItem.value() == 0) {        // Uncheck all items except 0-mask        for (int i=0; i<box->count(); ++i)            box->item(i).setChecked(i == box->currentIndex());    } else {        // Compute new value, without including (additional) supermasks        if (thisItem.isChecked())            newValue = thisItem.value();        for (int i=0; i<box->count(); ++i) {            FlagBoxModelItem &item = box->item(i);            if (item.isChecked() && (FlagBoxModel::bitcount(item.value()) == 1))                newValue |= item.value();        }        if (newValue == 0) {            // Uncheck all items except 0-mask            for (int i=0; i<box->count(); ++i) {                FlagBoxModelItem &item = box->item(i);                item.setChecked(item.value() == 0);            }        } else if (newValue == m_value) {            if (!thisItem.isChecked() && (FlagBoxModel::bitcount(thisItem.value()) > 1)) {                // We unchecked something, but the original value still holds                thisItem.setChecked(true);            }        } else {            // Make sure 0-mask is not selected            for (int i=0; i<box->count(); ++i) {                FlagBoxModelItem &item = box->item(i);                if (item.value() == 0)                        item.setChecked(false);            }            // Check/uncheck proper masks            if (thisItem.isChecked()) {                // Make sure submasks and supermasks are selected                for (int i=0; i<box->count(); ++i) {                    FlagBoxModelItem &item = box->item(i);                    if ((item.value() != 0) && ((item.value() & newValue) == item.value()) && !item.isChecked())                        item.setChecked(true);                }            } else {                // Make sure supermasks are not selected if they're no longer valid                for (int i=0; i<box->count(); ++i) {                    FlagBoxModelItem &item = box->item(i);                    if (item.isChecked() && ((item.value() == thisItem.value()) || ((item.value() & newValue) != item.value())))                        item.setChecked(false);                }            }        }    }    if (newValue != m_value) {        m_value = newValue;        setChanged(true);    }}// -------------------------------------------------------------------------SizePolicyProperty::SizePolicyProperty(const QSizePolicy &value, const QString &name)    : AbstractPropertyGroup(name){    QStringList lst;    lst << QString::fromUtf8("Fixed")        << QString::fromUtf8("Minimum")        << QString::fromUtf8("Maximum")        << QString::fromUtf8("Preferred")        << QString::fromUtf8("MinimumExpanding")        << QString::fromUtf8("Expanding")        << QString::fromUtf8("Ignored");    IProperty *i = 0;    i = new ListProperty(lst, size_type_to_int(value.horizontalPolicy()), QLatin1String("hSizeType"));    i->setFake(true);    i->setParent(this);    m_properties << i;    i = new ListProperty(lst, size_type_to_int(value.verticalPolicy()), QLatin1String("vSizeType"));    i->setFake(true);    i->setParent(this);    m_properties << i;    i = new IntProperty(value.horizontalStretch(), QLatin1String("horizontalStretch"));    i->setFake(true);    i->setParent(this);    m_properties << i;    i = new IntProperty(value.verticalStretch(), QLatin1String("verticalStretch"));    i->setFake(true);    i->setParent(this);    m_properties << i;}QVariant SizePolicyProperty::value() const{    QSizePolicy sizePolicy;    sizePolicy.setHorizontalPolicy(int_to_size_type(propertyAt(0)->value().toInt()));    sizePolicy.setVerticalPolicy(int_to_size_type(propertyAt(1)->value().toInt()));    sizePolicy.setHorizontalStretch(propertyAt(2)->value().toInt());    sizePolicy.setVerticalStretch(propertyAt(3)->value().toInt());    return qVariantFromValue(sizePolicy);}void SizePolicyProperty::setValue(const QVariant &value){    QSizePolicy sizePolicy = qvariant_cast<QSizePolicy>(value);    propertyAt(0)->setValue(size_type_to_int(sizePolicy.horizontalPolicy()));    propertyAt(1)->setValue(size_type_to_int(sizePolicy.verticalPolicy()));    propertyAt(2)->setValue(sizePolicy.horizontalStretch());    propertyAt(3)->setValue(sizePolicy.verticalStretch());}QVariant SizePolicyProperty::decoration() const{    return QVariant();}QString SizePolicyProperty::toString() const{    return AbstractPropertyGroup::toString();}// -------------------------------------------------------------------------DateTimeProperty::DateTimeProperty(const QDateTime &value, const QString &name)    : AbstractProperty<QDateTime>(value, name){}void DateTimeProperty::setValue(const QVariant &value){    m_value = value.toDateTime();}QString DateTimeProperty::toString() const{    return m_value.toString();}QWidget *DateTimeProperty::createEditor(QWidget *parent, const QObject *target, const char *receiver) const{    QDateTimeEdit *lineEdit = new QDateTimeEdit(parent);    QObject::connect(lineEdit, SIGNAL(dateTimeChanged(QDateTime)), target, receiver);    return lineEdit;}void DateTimeProperty::updateEditorContents(QWidget *editor){    if (QDateTimeEdit *lineEdit = qobject_cast<QDateTimeEdit*>(editor)) {        lineEdit->setDateTime(m_value);    }}void DateTimeProperty::updateValue(QWidget *editor){    if (QDateTimeEdit *lineEdit = qobject_cast<QDateTimeEdit*>(editor)) {        const QDateTime newValue = lineEdit->dateTime();        if (newValue != m_value) {            m_value = newValue;            setChanged(true);        }    }}// -------------------------------------------------------------------------DateProperty::DateProperty(const QDate &value, const QString &name)    : AbstractProperty<QDate>(value, name){}void DateProperty::setValue(const QVariant &value){    m_value = value.toDate();}QString DateProperty::toString() const{    return m_value.toString();}QWidget *DateProperty::createEditor(QWidget *parent, const QObject *target, const char *receiver) const{    QDateEdit *lineEdit = new QDateEdit(parent);    QObject::connect(lineEdit, SIGNAL(dateChanged(QDate)), target, receiver);    return lineEdit;}void DateProperty::updateEditorContents(QWidget *editor){    if (QDateEdit *lineEdit = qobject_cast<QDateEdit*>(editor)) {        lineEdit->setDate(m_value);    }}void DateProperty::updateValue(QWidget *editor){    if (QDateEdit *lineEdit = qobject_cast<QDateEdit*>(editor)) {        const QDate newValue = lineEdit->date();        if (newValue != m_value) {            m_value = newValue;            setChanged(true);        }    }}// -------------------------------------------------------------------------TimeProperty::TimeProperty(const QTime &value, const QString &name)    : AbstractProperty<QTime>(value, name){}void TimeProperty::setValue(const QVariant &value){    m_value = value.toTime();}QString TimeProperty::toString() const{    return m_value.toString();}QWidget *TimeProperty::createEditor(QWidget *parent, const QObject *target, const char *receiver) const{    QTimeEdit *lineEdit = new QTimeEdit(parent);    QObject::connect(lineEdit, SIGNAL(timeChanged(QTime)), target, receiver);    return lineEdit;}void TimeProperty::updateEditorContents(QWidget *editor){    if (QTimeEdit *lineEdit = qobject_cast<QTimeEdit*>(editor)) {        lineEdit->setTime(m_value);    }}void TimeProperty::updateValue(QWidget *editor){    if (QTimeEdit *lineEdit = qobject_cast<QTimeEdit*>(editor)) {        const QTime newValue = lineEdit->time();        if (newValue != m_value) {            m_value = newValue;            setChanged(true);        }

⌨️ 快捷键说明

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