PropertyItemDelegate.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "PreCompiled.h"
00025
00026 #ifndef _PreComp_
00027 # include <QApplication>
00028 # include <QModelIndex>
00029 # include <QPainter>
00030 #endif
00031
00032 #include "PropertyItemDelegate.h"
00033 #include "PropertyItem.h"
00034
00035 using namespace Gui::PropertyEditor;
00036
00037
00038 PropertyItemDelegate::PropertyItemDelegate(QObject* parent)
00039 : QItemDelegate(parent)
00040 {
00041 }
00042
00043 PropertyItemDelegate::~PropertyItemDelegate()
00044 {
00045 }
00046
00047 QSize PropertyItemDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
00048 {
00049 QSize size = QItemDelegate::sizeHint(option, index);
00050 size.setHeight(20);
00051 return size;
00052 }
00053
00054 void PropertyItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opt, const QModelIndex &index) const
00055 {
00056 QStyleOptionViewItem option = opt;
00057
00058 PropertyItem *property = static_cast<PropertyItem*>(index.internalPointer());
00059
00060 if (property && property->isSeparator()) {
00061 option.palette.setColor(QPalette::Text, option.palette.color(QPalette::BrightText));
00062 option.font.setBold(true);
00063 option.state &= ~QStyle::State_Selected;
00064 }
00065
00066 if (index.column() == 1) {
00067 option.state &= ~QStyle::State_Selected;
00068 }
00069
00070 option.state &= ~QStyle::State_HasFocus;
00071
00072 if (property && property->isSeparator()) {
00073 QBrush bg = option.palette.dark();
00074 painter->fillRect(option.rect, bg);
00075 }
00076
00077 QPen savedPen = painter->pen();
00078
00079 QItemDelegate::paint(painter, option, index);
00080
00081 QColor color = static_cast<QRgb>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor, &option));
00082 painter->setPen(QPen(color));
00083 if (index.column() == 1 || !(property && property->isSeparator())) {
00084 int right = (option.direction == Qt::LeftToRight) ? option.rect.right() : option.rect.left();
00085 painter->drawLine(right, option.rect.y(), right, option.rect.bottom());
00086 }
00087 painter->drawLine(option.rect.x(), option.rect.bottom(),
00088 option.rect.right(), option.rect.bottom());
00089 painter->setPen(savedPen);
00090 }
00091
00092 QWidget * PropertyItemDelegate::createEditor (QWidget * parent, const QStyleOptionViewItem & ,
00093 const QModelIndex & index ) const
00094 {
00095 if (!index.isValid())
00096 return 0;
00097
00098 PropertyItem *childItem = static_cast<PropertyItem*>(index.internalPointer());
00099 if (!childItem)
00100 return 0;
00101 QWidget* editor = childItem->createEditor(parent, this, SLOT(valueChanged()));
00102 if (editor && childItem->isReadOnly())
00103 editor->setDisabled(true);
00104 return editor;
00105 }
00106
00107 void PropertyItemDelegate::valueChanged()
00108 {
00109 QWidget* editor = qobject_cast<QWidget*>(sender());
00110 if (editor)
00111 commitData(editor);
00112 }
00113
00114 void PropertyItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
00115 {
00116 if (!index.isValid())
00117 return;
00118 QVariant data = index.data(Qt::EditRole);
00119 PropertyItem *childItem = static_cast<PropertyItem*>(index.internalPointer());
00120 editor->blockSignals(true);
00121 childItem->setEditorData(editor, data);
00122 editor->blockSignals(false);
00123 return;
00124 }
00125
00126 void PropertyItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
00127 {
00128 if (!index.isValid())
00129 return;
00130 PropertyItem *childItem = static_cast<PropertyItem*>(index.internalPointer());
00131 QVariant data = childItem->editorData(editor);
00132 model->setData(index, data, Qt::EditRole);
00133 }
00134
00135 #include "moc_PropertyItemDelegate.cpp"