qpropertyeditor_items.cpp

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

CPP
1,753
字号
    case Qt::SplitHCursor: return QString::fromUtf8("Split Horizontal");    case Qt::PointingHandCursor: return QString::fromUtf8("Pointing Hand");    case Qt::ForbiddenCursor: return QString::fromUtf8("Forbidden");    case Qt::WhatsThisCursor: return QString::fromUtf8("Whats This");    case Qt::BusyCursor: return QString::fromUtf8("Busy");    default: return QString();    }}QPixmap CursorProperty::cursorPixmap(int shape){    switch (shape) {    case Qt::ArrowCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/arrow.png"));    case Qt::UpArrowCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/uparrow.png"));    case Qt::CrossCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/cross.png"));    case Qt::WaitCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/wait.png"));    case Qt::IBeamCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/ibeam.png"));    case Qt::SizeVerCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/sizev.png"));    case Qt::SizeHorCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/sizeh.png"));    case Qt::SizeBDiagCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/sizef.png"));    case Qt::SizeFDiagCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/sizeb.png"));    case Qt::SizeAllCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/sizeall.png"));    case Qt::BlankCursor:    {        QBitmap cur = QBitmap(25, 25);        cur.clear();        return cur;    }    case Qt::SplitVCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/vsplit.png"));    case Qt::SplitHCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/hsplit.png"));    case Qt::PointingHandCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/hand.png"));    case Qt::ForbiddenCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/no.png"));    case Qt::WhatsThisCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/whatsthis.png"));    case Qt::BusyCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/busy.png"));    default: return QPixmap();    }}void CursorProperty::addCursor(QComboBox *combo, int shape) const{    combo->addItem(cursorPixmap(shape), cursorName(shape), shape);}// -------------------------------------------------------------------------AlignmentProperty::AlignmentProperty(const QMap<QString, QVariant> &items, Qt::Alignment value, const QString &name)    : AbstractPropertyGroup(name){    QStringList horz_keys = QStringList()        << QString::fromUtf8("Qt::AlignLeft") << QString::fromUtf8("Qt::AlignRight")        << QString::fromUtf8("Qt::AlignHCenter") << QString::fromUtf8("Qt::AlignJustify"); // << "Qt::AlignAbsolute"    QMap<QString, QVariant> horz_map;    foreach (QString h, horz_keys) {        horz_map.insert(h, items.value(h));    }    MapProperty *ph = new MapProperty(horz_map, uint(value & Qt::AlignHorizontal_Mask), QLatin1String("horizontal"));    ph->setFake(true);    ph->setParent(this);    m_properties << ph;    QStringList vert_keys = QStringList()        << QString::fromUtf8("Qt::AlignTop") << QString::fromUtf8("Qt::AlignBottom") << QString::fromUtf8("Qt::AlignVCenter");    QMap<QString, QVariant> vert_map;    foreach (QString h, vert_keys) {        vert_map.insert(h, items.value(h));    }    MapProperty *pv = new MapProperty(vert_map, int(value & Qt::AlignVertical_Mask), QLatin1String("vertical"));    pv->setFake(true);    pv->setParent(this);    m_properties << pv;}QVariant AlignmentProperty::value() const{    return uint(propertyAt(0)->value().toUInt() | propertyAt(1)->value().toUInt());}void AlignmentProperty::setValue(const QVariant &value){    QVariant v = value;    if (qVariantCanConvert<FlagType>(value))        v = qvariant_cast<FlagType>(value).value;    else if (qVariantCanConvert<EnumType>(value))        v = qvariant_cast<EnumType>(value).value;    propertyAt(0)->setValue(v.toUInt() & Qt::AlignHorizontal_Mask);    propertyAt(1)->setValue(v.toUInt() & Qt::AlignVertical_Mask);}// -------------------------------------------------------------------------DoubleProperty::DoubleProperty(double value, const QString &name)    : AbstractProperty<double>(value, name){}void DoubleProperty::setValue(const QVariant &value){    m_value = value.toDouble();}QString DoubleProperty::toString() const{    return QString::number(m_value);}QWidget *DoubleProperty::createEditor(QWidget *parent, const QObject *target, const char *receiver) const{    QLineEdit *lineEdit = new QLineEdit(parent);    lineEdit->setFrame(0);    lineEdit->setValidator(new QDoubleValidator(lineEdit));    QObject::connect(lineEdit, SIGNAL(textChanged(QString)), target, receiver);    return lineEdit;}void DoubleProperty::updateEditorContents(QWidget *editor){    if (QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor)) {        double v = lineEdit->text().toDouble();        if (v != m_value)            lineEdit->setText(QString::number(m_value));    }}void DoubleProperty::updateValue(QWidget *editor){    if (QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor)) {        double newValue = lineEdit->text().toDouble();        if (newValue != m_value) {            m_value = newValue;            setChanged(true);        }    }}// -------------------------------------------------------------------------// QDoublePropertySpinBox also emits editingFinished when the spinbox is usedclass QDoublePropertySpinBox: public QDoubleSpinBox{public:    QDoublePropertySpinBox(QWidget *parent = 0)        : QDoubleSpinBox(parent) { }    void stepBy(int steps)    {        QDoubleSpinBox::stepBy(steps);        emit editingFinished();    }};SpinBoxDoubleProperty::SpinBoxDoubleProperty(double value, const QString &name)    : AbstractProperty<double>(value, name), m_low(-HUGE_VAL), m_hi(HUGE_VAL){}void SpinBoxDoubleProperty::setRange(double low, double hi){    m_low = low;    m_hi = hi;}QString SpinBoxDoubleProperty::specialValue() const{    return m_specialValue;}void SpinBoxDoubleProperty::setSpecialValue(const QString &specialValue){    m_specialValue = specialValue;}void SpinBoxDoubleProperty::setValue(const QVariant &value){    m_value = value.toDouble();}QString SpinBoxDoubleProperty::toString() const{    return QString::number(m_value);}QWidget *SpinBoxDoubleProperty::createEditor(QWidget *parent, const QObject *target, const char *receiver) const{    QDoubleSpinBox *spinBox = new QDoublePropertySpinBox(parent);    spinBox->setFrame(0);    spinBox->setSpecialValueText(m_specialValue);    spinBox->setDecimals(6);    spinBox->setRange(m_low, m_hi);    spinBox->setValue(m_value);    spinBox->selectAll();    QObject::connect(spinBox, SIGNAL(editingFinished()), target, receiver);    return spinBox;}void SpinBoxDoubleProperty::updateEditorContents(QWidget *editor){    if (QDoubleSpinBox *spinBox = qobject_cast<QDoubleSpinBox*>(editor)) {        spinBox->setValue(m_value);    }}void SpinBoxDoubleProperty::updateValue(QWidget *editor){    if (QDoubleSpinBox *spinBox = qobject_cast<QDoubleSpinBox*>(editor)) {        double newValue = spinBox->value();        if (newValue != m_value) {            m_value = newValue;            setChanged(true);        }    }}// -------------------------------------------------------------------------CharProperty::CharProperty(QChar value, const QString &name)    : AbstractProperty<QChar>(value, name){}void CharProperty::setValue(const QVariant &value){    m_value = value.toChar();}QString CharProperty::toString() const{    return QString(m_value);}QWidget *CharProperty::createEditor(QWidget *parent, const QObject *target, const char *receiver) const{    QLineEdit *lineEdit = new QLineEdit(parent);    lineEdit->setFrame(0);    lineEdit->setInputMask("X; ");    QObject::connect(lineEdit, SIGNAL(textChanged(QString)), target, receiver);    return lineEdit;}void CharProperty::updateEditorContents(QWidget *editor){    if (QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor)) {        if (lineEdit->text() != QString(m_value)) {            lineEdit->setText(QString(m_value));            lineEdit->setCursorPosition(0);        }    }}void CharProperty::updateValue(QWidget *editor){    if (QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor)) {        lineEdit->setCursorPosition(0);        QChar newValue = QLatin1Char(' ');        if (lineEdit->text().size() > 0)            newValue = lineEdit->text().at(0);        if (newValue != m_value) {            m_value = newValue;            setChanged(true);        }    }}// -------------------------------------------------------------------------LongLongProperty::LongLongProperty(qlonglong value, const QString &name)    : AbstractProperty<qlonglong>(value, name){}void LongLongProperty::setValue(const QVariant &value){    m_value = value.toLongLong();}QString LongLongProperty::toString() const{    return QString::number(m_value);}QWidget *LongLongProperty::createEditor(QWidget *parent, const QObject *target, const char *receiver) const{    QLineEdit *lineEdit = new QLineEdit(parent);    lineEdit->setFrame(0);    lineEdit->setValidator(new QLongLongValidator(lineEdit));    QObject::connect(lineEdit, SIGNAL(textChanged(QString)), target, receiver);    return lineEdit;}void LongLongProperty::updateEditorContents(QWidget *editor){    if (QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor)) {        qlonglong v = lineEdit->text().toLongLong();        if (v != m_value)            lineEdit->setText(QString::number(m_value));    }}void LongLongProperty::updateValue(QWidget *editor){    if (QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor)) {        qlonglong newValue = lineEdit->text().toLongLong();        if (newValue != m_value) {            m_value = newValue;            setChanged(true);        }    }}// -------------------------------------------------------------------------SeparatorProperty::SeparatorProperty(const QString &value, const QString &name)    : StringProperty(value, name){}QWidget *SeparatorProperty::createEditor(QWidget *parent, const QObject *target, const char *receiver) const{    Q_UNUSED(parent);    Q_UNUSED(target);    Q_UNUSED(receiver);    return 0;}bool SeparatorProperty::hasEditor() const{ return false; }void SeparatorProperty::updateEditorContents(QWidget *editor){ Q_UNUSED(editor); }void SeparatorProperty::updateValue(QWidget *editor){ Q_UNUSED(editor); }// -------------------------------------------------------------------------UrlProperty::UrlProperty(const QUrl &value, const QString &name)    : AbstractPropertyGroup(name),      m_value(value){}QVariant UrlProperty::value() const{    return m_value;}void UrlProperty::setValue(const QVariant &value){    m_value = value.toUrl();}QString UrlProperty::toString() const{    return m_value.toString();}QWidget *UrlProperty::createEditor(QWidget *parent, const QObject *target, const char *receiver) const{    QLineEdit *lineEdit = new QLineEdit(parent);    lineEdit->setFrame(0);    QObject::connect(lineEdit, SIGNAL(textChanged(QString)), target, receiver);    return lineEdit;}void UrlProperty::updateEditorContents(QWidget *editor){    if (QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor)) {        if (QUrl(lineEdit->text()) != m_value)            lineEdit->setText(m_value.toString());    }}void UrlProperty::updateValue(QWidget *editor){    if (QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor)) {        QUrl newValue = QUrl(lineEdit->text());        if (newValue != m_value) {            m_value = newValue;            setChanged(true);        }    }}// -------------------------------------------------------------------------StringListProperty::StringListProperty(const QStringList &value, const QString &name)    : AbstractProperty<QStringList>(value, name){}void StringListProperty::setValue(const QVariant &value){    m_value = qvariant_cast<QStringList>(value);}QString StringListProperty::toString() const{    return m_value.join(QLatin1String(", "));}QWidget *StringListProperty::createEditor(QWidget *parent, const QObject *target, const char *receiver) const{    StringListEditorButton *btn = new StringListEditorButton(m_value, parent);    QObject::connect(btn, SIGNAL(changed()), target, receiver);    return btn;}void StringListProperty::updateEditorContents(QWidget *editor){    if (StringListEditorButton *btn = qobject_cast<StringListEditorButton*>(editor)) {        btn->setStringList(m_value);    }}void StringListProperty::updateValue(QWidget *editor){    if (StringListEditorButton *btn = qobject_cast<StringListEditorButton*>(editor)) {        QStringList newValue = btn->stringList();        if (newValue != m_value) {            m_value = newValue;            setChanged(true);        }    }}

⌨️ 快捷键说明

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