qpropertyeditor_items.cpp

来自「QT 开发环境里面一个很重要的文件」· C++ 代码 · 共 1,753 行 · 第 1/4 页

CPP
1,753
字号
   if (qVariantCanConvert<EnumType>(value)) {        EnumType e = qvariant_cast<EnumType>(value);        m_value = e.value;    } else if (qVariantCanConvert<FlagType>(value)) {        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{    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)) {        QString key = combo->currentText();        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());    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)) {        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)) {        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)) {        QTime newValue = lineEdit->time();        if (newValue != m_value) {            m_value = newValue;            setChanged(true);        }    }}// -------------------------------------------------------------------------CursorProperty::CursorProperty(const QCursor &value, const QString &name)    : AbstractProperty<QCursor>(value, name){}void CursorProperty::setValue(const QVariant &value){    m_value = qvariant_cast<QCursor>(value);}QString CursorProperty::toString() const{    return cursorName(m_value.shape());}QVariant CursorProperty::decoration() const{    return qVariantFromValue(cursorPixmap(m_value.shape()));}QWidget *CursorProperty::createEditor(QWidget *parent, const QObject *target, const char *receiver) const{    QComboBox *combo = new QComboBox(parent);    combo->view()->setTextElideMode(Qt::ElideLeft);    combo->setFrame(0);    addCursor(combo, Qt::ArrowCursor);    addCursor(combo, Qt::UpArrowCursor);    addCursor(combo, Qt::CrossCursor);    addCursor(combo, Qt::WaitCursor);    addCursor(combo, Qt::IBeamCursor);    addCursor(combo, Qt::SizeVerCursor);    addCursor(combo, Qt::SizeHorCursor);    addCursor(combo, Qt::SizeBDiagCursor);    addCursor(combo, Qt::SizeFDiagCursor);    addCursor(combo, Qt::SizeAllCursor);    addCursor(combo, Qt::BlankCursor);    addCursor(combo, Qt::SplitVCursor);    addCursor(combo, Qt::SplitHCursor);    addCursor(combo, Qt::PointingHandCursor);    addCursor(combo, Qt::ForbiddenCursor);    QObject::connect(combo, SIGNAL(activated(int)), target, receiver);    return combo;}void CursorProperty::updateEditorContents(QWidget *editor){    if (QComboBox *combo = qobject_cast<QComboBox*>(editor)) {        combo->setCurrentIndex(m_value.shape());    }}void CursorProperty::updateValue(QWidget *editor){    if (QComboBox *combo = qobject_cast<QComboBox*>(editor)) {        QCursor newValue(static_cast<Qt::CursorShape>(combo->currentIndex()));        if (newValue.shape() != m_value.shape()) {            m_value = newValue;            setChanged(true);        }    }}QString CursorProperty::cursorName(int shape){    switch (shape) {    case Qt::ArrowCursor: return QString::fromUtf8("Arrow");    case Qt::UpArrowCursor: return QString::fromUtf8("Up-Arrow");    case Qt::CrossCursor: return QString::fromUtf8("Cross");    case Qt::WaitCursor: return QString::fromUtf8("Waiting");    case Qt::IBeamCursor: return QString::fromUtf8("IBeam");    case Qt::SizeVerCursor: return QString::fromUtf8("Size Vertical");    case Qt::SizeHorCursor: return QString::fromUtf8("Size Horizontal");    case Qt::SizeBDiagCursor: return QString::fromUtf8("Size Slash");    case Qt::SizeFDiagCursor: return QString::fromUtf8("Size Backslash");    case Qt::SizeAllCursor: return QString::fromUtf8("Size All");    case Qt::BlankCursor: return QString::fromUtf8("Blank");    case Qt::SplitVCursor: return QString::fromUtf8("Split Vertical");

⌨️ 快捷键说明

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