qpropertyeditor_delegate.cpp
来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 398 行 · 第 1/2 页
CPP
398 行
painter->setPen(savedPen);}QSize QPropertyEditorDelegate::sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const{ return QItemDelegate::sizeHint(opt, index) + QSize(4, 4);}bool QPropertyEditorDelegate::isReadOnly() const{ return m_readOnly;}void QPropertyEditorDelegate::setReadOnly(bool readOnly){ // ### close the editor m_readOnly = readOnly;}void QPropertyEditorDelegate::slotDestroyed(QObject *object){ if (m_lastEdited == object) { m_lastEdited = 0; emit editorClosed(); }}QWidget *QPropertyEditorDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{ Q_UNUSED(option); QPropertyEditorModel *model = const_cast<QPropertyEditorModel *>(static_cast<const QPropertyEditorModel *>(index.model())); const IProperty *property = model->privateData(index); if (property == 0) return 0; QWidget *editor = 0; if (!isReadOnly() && property->hasEditor()) { // ### always true if (property->hasReset()) { EditorWithReset *editor_w_reset = new EditorWithReset(property, model, parent); QWidget *child_editor = property->createEditor(editor_w_reset, editor_w_reset, SIGNAL(sync())); editor_w_reset->setChildEditor(child_editor); connect(editor_w_reset, SIGNAL(sync()), this, SLOT(sync())); connect(editor_w_reset, SIGNAL(resetProperty(const IProperty *, QPropertyEditorModel *)), this, SLOT(resetProperty(const IProperty *, QPropertyEditorModel *))); editor = editor_w_reset; if (TextPropertyEditor* edit = qobject_cast<TextPropertyEditor*>(child_editor)) { // in case of TextPropertyEditor install the filter on it's private QLineEdit edit->installEventFilter(const_cast<QPropertyEditorDelegate *>(this)); } else child_editor->installEventFilter(const_cast<QPropertyEditorDelegate *>(this)); } else { editor = property->createEditor(parent, this, SLOT(sync())); editor->installEventFilter(const_cast<QPropertyEditorDelegate *>(this)); } } connect(editor, SIGNAL(destroyed(QObject *)), this, SLOT(slotDestroyed(QObject *))); QPropertyEditorDelegate *that = const_cast<QPropertyEditorDelegate *>(this); if (!m_lastEdited) emit that->editorOpened(); m_lastEdited = editor; return editor;}void QPropertyEditorDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{ if (EditorWithReset *editor_w_reset = qobject_cast<EditorWithReset*>(editor)) editor = editor_w_reset->childEditor(); const QAbstractItemModel *model = index.model(); IProperty *property = static_cast<const QPropertyEditorModel*>(model)->privateData(index); if (property && property->hasEditor() && !m_syncing) { property->updateEditorContents(editor); }}void QPropertyEditorDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{ if (EditorWithReset *editor_w_reset = qobject_cast<EditorWithReset*>(editor)) editor = editor_w_reset->childEditor(); if (IProperty *property = static_cast<const QPropertyEditorModel*>(model)->privateData(index)) { property->updateValue(editor); if (property->propertyName() == QLatin1String("Family") || property->propertyName() == QLatin1String("Point Size") || property->propertyName() == QLatin1String("Bold") || property->propertyName() == QLatin1String("Italic") || property->propertyName() == QLatin1String("Underline") || property->propertyName() == QLatin1String("Strikeout") || property->propertyName() == QLatin1String("Kerning") || property->propertyName() == QLatin1String("Antialiasing")) { const QModelIndex parentIndex = index.parent(); if (IProperty *fontProperty = static_cast<const QPropertyEditorModel*>(model)->privateData(parentIndex)) { QFont f = qvariant_cast<QFont>(fontProperty->value()); if (property->propertyName() == QLatin1String("Family")) f.setFamily(property->toString()); else if (property->propertyName() == QLatin1String("Point Size")) f.setPointSize(property->value().toInt()); else if (property->propertyName() == QLatin1String("Bold")) f.setBold(property->value().toBool()); else if (property->propertyName() == QLatin1String("Italic")) f.setItalic(property->value().toBool()); else if (property->propertyName() == QLatin1String("Underline")) f.setUnderline(property->value().toBool()); else if (property->propertyName() == QLatin1String("Strikeout")) f.setStrikeOut(property->value().toBool()); else if (property->propertyName() == QLatin1String("Kerning")) f.setKerning(property->value().toBool()); else if (property->propertyName() == QLatin1String("Antialiasing")) f.setStyleStrategy((QFont::StyleStrategy)property->value().toInt()); fontProperty->setValue(f); model->setData(parentIndex, f, Qt::EditRole); return; } } model->setData(index, property->value(), Qt::EditRole); }}void QPropertyEditorDelegate::drawDecoration(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QPixmap &pixmap) const{ QItemDelegate::drawDecoration(painter, option, rect, pixmap);}void QPropertyEditorDelegate::sync(){ m_syncing = true; QWidget *w = qobject_cast<QWidget*>(sender()); if (w == 0) return; emit commitData(w); m_syncing = false;}void QPropertyEditorDelegate::resetProperty(const IProperty *property, QPropertyEditorModel *model){ QString propName = property->propertyName(); if (propName == QLatin1String("Family") || propName == QLatin1String("Point Size") || propName == QLatin1String("Bold") || propName == QLatin1String("Italic") || propName == QLatin1String("Underline") || propName == QLatin1String("Strikeout") || propName == QLatin1String("Kerning") || propName == QLatin1String("Antialiasing")) { IProperty *fontProperty = property->parent(); if (fontProperty) { QFont f = qvariant_cast<QFont>(fontProperty->value()); uint mask = f.resolve(); if (property->propertyName() == QLatin1String("Family")) mask &= ~QFontPrivate::Family; else if (property->propertyName() == QLatin1String("Point Size")) mask &= ~QFontPrivate::Size; else if (property->propertyName() == QLatin1String("Bold")) mask &= ~QFontPrivate::Weight; else if (property->propertyName() == QLatin1String("Italic")) mask &= ~QFontPrivate::Style; else if (property->propertyName() == QLatin1String("Underline")) mask &= ~QFontPrivate::Underline; else if (property->propertyName() == QLatin1String("Strikeout")) mask &= ~QFontPrivate::StrikeOut; else if (property->propertyName() == QLatin1String("Kerning")) mask &= ~QFontPrivate::Kerning; else if (property->propertyName() == QLatin1String("Antialiasing")) mask &= ~QFontPrivate::StyleStrategy; f.resolve(mask); if (mask) { const QModelIndex fontIndex = model->indexOf(fontProperty); fontProperty->setDirty(true); model->setData(fontIndex, f, Qt::EditRole); return; } propName = fontProperty->propertyName(); } } emit resetProperty(propName);}void QPropertyEditorDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const{ QItemDelegate::updateEditorGeometry(editor, option, index); editor->setGeometry(editor->geometry().adjusted(0, 0, -1, -1));}}#include "qpropertyeditor_delegate.moc"
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?