qpropertyeditor_items.cpp
来自「QT 开发环境里面一个很重要的文件」· C++ 代码 · 共 1,753 行 · 第 1/4 页
CPP
1,753 行
pw->setParent(this); pw->setRange(0, INT_MAX); IntProperty *ph = new IntProperty(value.height(), QLatin1String("height")); ph->setFake(true); ph->setParent(this); ph->setRange(0, INT_MAX); if (name == QLatin1String("maximumSize")) { pw->setRange(0, 0xFFFFFF); ph->setRange(0, 0xFFFFFF); } if (name == QLatin1String("minimumSize")) { pw->setRange(0, 0xFFF); ph->setRange(0, 0xFFF); } m_properties << pw << ph;}QVariant SizeProperty::value() const{ return QSize(propertyAt(0)->value().toInt(), propertyAt(1)->value().toInt());}void SizeProperty::setValue(const QVariant &value){ QSize pt = value.toSize(); propertyAt(0)->setValue(pt.width()); propertyAt(1)->setValue(pt.height());}// -------------------------------------------------------------------------SizeFProperty::SizeFProperty(const QSizeF &value, const QString &name) : AbstractPropertyGroup(name){ SpinBoxDoubleProperty *pw = new SpinBoxDoubleProperty(value.width(), QLatin1String("width")); pw->setFake(true); pw->setParent(this); pw->setRange(0.0, HUGE_VAL); SpinBoxDoubleProperty *ph = new SpinBoxDoubleProperty(value.height(), QLatin1String("height")); ph->setFake(true); ph->setParent(this); ph->setRange(0.0, HUGE_VAL); m_properties << pw << ph;}QVariant SizeFProperty::value() const{ return QSizeF(propertyAt(0)->value().toDouble(), propertyAt(1)->value().toDouble());}void SizeFProperty::setValue(const QVariant &value){ QSizeF pt = value.toSizeF(); propertyAt(0)->setValue(pt.width()); propertyAt(1)->setValue(pt.height());}// -------------------------------------------------------------------------// QIntPropertySpinBox also emits editingFinished when the spinbox is usedclass QIntPropertySpinBox: public QSpinBox{public: QIntPropertySpinBox(QWidget *parent = 0) : QSpinBox(parent) { } void stepBy(int steps) { QSpinBox::stepBy(steps); emit editingFinished(); }};IntProperty::IntProperty(int value, const QString &name) : AbstractProperty<int>(value, name), m_low(INT_MIN), m_hi(INT_MAX){}void IntProperty::setRange(int low, int hi){ m_low = low; m_hi = hi;}QString IntProperty::specialValue() const{ return m_specialValue;}void IntProperty::setSpecialValue(const QString &specialValue){ m_specialValue = specialValue;}void IntProperty::setValue(const QVariant &value){ m_value = value.toInt();}QString IntProperty::toString() const{ return QString::number(m_value);}QWidget *IntProperty::createEditor(QWidget *parent, const QObject *target, const char *receiver) const{ QSpinBox *spinBox = new QIntPropertySpinBox(parent); spinBox->setFrame(0); spinBox->setSpecialValueText(m_specialValue); spinBox->setRange(m_low, m_hi); spinBox->setValue(m_value); spinBox->selectAll(); QObject::connect(spinBox, SIGNAL(editingFinished()), target, receiver); return spinBox;}void IntProperty::updateEditorContents(QWidget *editor){ if (QSpinBox *spinBox = qobject_cast<QSpinBox*>(editor)) { spinBox->setValue(m_value); }}void IntProperty::updateValue(QWidget *editor){ if (QSpinBox *spinBox = qobject_cast<QSpinBox*>(editor)) { int newValue = spinBox->value(); if (newValue != m_value) { m_value = newValue; setChanged(true); } }}// -------------------------------------------------------------------------RectProperty::RectProperty(const QRect &value, const QString &name) : AbstractPropertyGroup(name){ IntProperty *px = new IntProperty(value.x(), QLatin1String("x")); px->setFake(true); px->setParent(this); IntProperty *py = new IntProperty(value.y(), QLatin1String("y")); py->setFake(true); py->setParent(this); IntProperty *pw = new IntProperty(value.width(), QLatin1String("width")); pw->setFake(true); pw->setParent(this); pw->setRange(0, INT_MAX); IntProperty *ph = new IntProperty(value.height(), QLatin1String("height")); ph->setFake(true); ph->setParent(this); ph->setRange(0, INT_MAX); if (name == QLatin1String("geometry")) { pw->setRange(0, 0xFFF); ph->setRange(0, 0xFFF); } m_properties << px << py << pw << ph;}QVariant RectProperty::value() const{ return QRect(propertyAt(0)->value().toInt(), propertyAt(1)->value().toInt(), propertyAt(2)->value().toInt(), propertyAt(3)->value().toInt());}void RectProperty::setValue(const QVariant &value){ QRect pt = value.toRect(); propertyAt(0)->setValue(pt.x()); propertyAt(1)->setValue(pt.y()); propertyAt(2)->setValue(pt.width()); propertyAt(3)->setValue(pt.height());}// -------------------------------------------------------------------------RectFProperty::RectFProperty(const QRectF &value, const QString &name) : AbstractPropertyGroup(name){ DoubleProperty *px = new DoubleProperty(value.x(), QLatin1String("x")); px->setFake(true); px->setParent(this); DoubleProperty *py = new DoubleProperty(value.y(), QLatin1String("y")); py->setFake(true); py->setParent(this); SpinBoxDoubleProperty *pw = new SpinBoxDoubleProperty(value.width(), QLatin1String("width")); pw->setFake(true); pw->setParent(this); pw->setRange(0.0, HUGE_VAL); SpinBoxDoubleProperty *ph = new SpinBoxDoubleProperty(value.height(), QLatin1String("height")); ph->setFake(true); ph->setParent(this); ph->setRange(0.0, HUGE_VAL); m_properties << px << py << pw << ph;}QVariant RectFProperty::value() const{ return QRectF(propertyAt(0)->value().toDouble(), propertyAt(1)->value().toDouble(), propertyAt(2)->value().toDouble(), propertyAt(3)->value().toDouble());}void RectFProperty::setValue(const QVariant &value){ QRectF pt = value.toRectF(); propertyAt(0)->setValue(pt.x()); propertyAt(1)->setValue(pt.y()); propertyAt(2)->setValue(pt.width()); propertyAt(3)->setValue(pt.height());}// -------------------------------------------------------------------------ColorProperty::ColorProperty(const QColor &value, const QString &name) : AbstractPropertyGroup(name){ IntProperty *r = new IntProperty(value.red(), QLatin1String("red")); r->setFake(true); r->setRange(0, 255); r->setParent(this); IntProperty *g = new IntProperty(value.green(), QLatin1String("green")); g->setFake(true); g->setRange(0, 255); g->setParent(this); IntProperty *b = new IntProperty(value.blue(), QLatin1String("blue")); b->setFake(true); b->setRange(0, 255); b->setParent(this); m_properties << r << g << b;}QVariant ColorProperty::value() const{ return qVariantFromValue(QColor(propertyAt(0)->value().toInt(), propertyAt(1)->value().toInt(), propertyAt(2)->value().toInt()));}void ColorProperty::setValue(const QVariant &value){ QColor c = qvariant_cast<QColor>(value); propertyAt(0)->setValue(c.red()); propertyAt(1)->setValue(c.green()); propertyAt(2)->setValue(c.blue());}QVariant ColorProperty::decoration() const{ QPixmap pix(16, 16); pix.fill(qvariant_cast<QColor>(value())); return qVariantFromValue(pix);}// -------------------------------------------------------------------------FontProperty::FontProperty(const QFont &value, const QString &name, QWidget *selectedWidget) : AbstractPropertyGroup(name){ m_selectedWidget = selectedWidget; QStringList fonts = fontDatabase()->families(); int index = fonts.indexOf(value.family()); if (index == -1) index = 0; IProperty *i = 0; i = new ListProperty(fonts, index, QLatin1String("Family")); i->setFake(true); i->setHasReset(true); i->setParent(this); m_properties << i; int pointSize = value.pointSize(); if (pointSize < 1) { // try to convert from pixel size and resolved font // see also code in FontProperty::setValue pointSize = QFontInfo(value).pointSize(); } IntProperty *ii = new IntProperty(pointSize, QLatin1String("Point Size")); ii->setFake(true); ii->setHasReset(true); ii->setRange(1, INT_MAX); // ### check ii->setParent(this); m_properties << ii; i = new BoolProperty(value.bold(), QLatin1String("Bold")); i->setFake(true); i->setHasReset(true); i->setParent(this); m_properties << i; i = new BoolProperty(value.italic(), QLatin1String("Italic")); i->setFake(true); i->setHasReset(true); i->setParent(this); m_properties << i; i = new BoolProperty(value.underline(), QLatin1String("Underline")); i->setFake(true); i->setHasReset(true); i->setParent(this); m_properties << i; i = new BoolProperty(value.strikeOut(), QLatin1String("Strikeout")); i->setFake(true); i->setHasReset(true); i->setParent(this); m_properties << i; i = new BoolProperty(value.kerning(), QLatin1String("Kerning")); i->setFake(true); i->setHasReset(true); i->setParent(this); m_properties << i; i = new BoolProperty(value.styleStrategy() == QFont::PreferDefault, QLatin1String("Antialiasing")); i->setFake(true); i->setHasReset(true); i->setParent(this); m_properties << i; m_font = value;}QVariant FontProperty::value() const{ return m_font;}void FontProperty::setValue(const QVariant &value){ m_font = qvariant_cast<QFont>(value); QFont parentFont = QFont(); if (m_selectedWidget) { if (m_selectedWidget->isWindow()) parentFont = QApplication::font(m_selectedWidget); else { if (m_selectedWidget->parentWidget()) parentFont = m_selectedWidget->parentWidget()->font(); } } uint mask = m_font.resolve(); m_font = m_font.resolve(parentFont); m_font.resolve(mask); m_changed = mask != 0; 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() == QFont::PreferDefault);}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{ QString family = propertyAt(0)->toString(); QString pointSize = propertyAt(1)->value().toString(); return QLatin1String(" ") // ### temp hack + QLatin1String("[") + family + QLatin1String(", ") + pointSize + QLatin1String("]");}// -------------------------------------------------------------------------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){
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?