FreeCAD/src/Gui/propertyeditor/PropertyItemDelegate.cpp

138 lines
5.1 KiB
C++

/***************************************************************************
* Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <QApplication>
# include <QModelIndex>
# include <QPainter>
#endif
#include "PropertyItemDelegate.h"
#include "PropertyItem.h"
using namespace Gui::PropertyEditor;
PropertyItemDelegate::PropertyItemDelegate(QObject* parent)
: QItemDelegate(parent)
{
}
PropertyItemDelegate::~PropertyItemDelegate()
{
}
QSize PropertyItemDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
{
QSize size = QItemDelegate::sizeHint(option, index);
size.setHeight(20);
return size;
}
void PropertyItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opt, const QModelIndex &index) const
{
QStyleOptionViewItem option = opt;
PropertyItem *property = static_cast<PropertyItem*>(index.internalPointer());
if (property && property->isSeparator()) {
option.palette.setColor(QPalette::Text, option.palette.color(QPalette::BrightText));
option.font.setBold(true);
option.state &= ~QStyle::State_Selected;
}
if (index.column() == 1) {
option.state &= ~QStyle::State_Selected;
}
option.state &= ~QStyle::State_HasFocus;
if (property && property->isSeparator()) {
QBrush bg = option.palette.dark();
painter->fillRect(option.rect, bg);
}
QPen savedPen = painter->pen();
QItemDelegate::paint(painter, option, index);
QColor color = static_cast<QRgb>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor, &option));
painter->setPen(QPen(color));
if (index.column() == 1 || !(property && property->isSeparator())) {
int right = (option.direction == Qt::LeftToRight) ? option.rect.right() : option.rect.left();
painter->drawLine(right, option.rect.y(), right, option.rect.bottom());
}
painter->drawLine(option.rect.x(), option.rect.bottom(),
option.rect.right(), option.rect.bottom());
painter->setPen(savedPen);
}
QWidget * PropertyItemDelegate::createEditor (QWidget * parent, const QStyleOptionViewItem & /*option*/,
const QModelIndex & index ) const
{
if (!index.isValid())
return 0;
PropertyItem *childItem = static_cast<PropertyItem*>(index.internalPointer());
if (!childItem)
return 0;
QWidget* editor = childItem->createEditor(parent, this, SLOT(valueChanged()));
if (editor && childItem->isReadOnly())
editor->setDisabled(true);
else if (editor)
editor->setFocus();
return editor;
}
void PropertyItemDelegate::valueChanged()
{
QWidget* editor = qobject_cast<QWidget*>(sender());
if (editor)
commitData(editor);
}
void PropertyItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if (!index.isValid())
return;
QVariant data = index.data(Qt::EditRole);
PropertyItem *childItem = static_cast<PropertyItem*>(index.internalPointer());
editor->blockSignals(true);
childItem->setEditorData(editor, data);
editor->blockSignals(false);
return;
}
void PropertyItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{
if (!index.isValid())
return;
PropertyItem *childItem = static_cast<PropertyItem*>(index.internalPointer());
QVariant data = childItem->editorData(editor);
model->setData(index, data, Qt::EditRole);
}
#include "moc_PropertyItemDelegate.cpp"