Seamly2D
Code documentation
vpropertydelegate.cpp
Go to the documentation of this file.
1 /************************************************************************
2  **
3  ** @file vpropertydelegate.cpp
4  ** @author hedgeware <internal(at)hedgeware.net>
5  ** @date
6  **
7  ** @brief
8  ** @copyright
9  ** All rights reserved. This program and the accompanying materials
10  ** are made available under the terms of the GNU Lesser General Public License
11  ** (LGPL) version 2.1 which accompanies this distribution, and is available at
12  ** http://www.gnu.org/licenses/lgpl-2.1.html
13  **
14  ** This library is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  ** Lesser General Public License for more details.
18  **
19  *************************************************************************/
20 
21 #include "vpropertydelegate.h"
22 
23 #include <QAbstractItemModel>
24 #include <QApplication>
25 #include <QColor>
26 #include <QModelIndex>
27 #include <QPainter>
28 #include <QPen>
29 #include <QRect>
30 #include <QRgb>
31 #include <QStyle>
32 #include <QVariant>
33 
34 #include "vproperty.h"
35 
37  QStyledItemDelegate(parent), RowHeight(0), AddRowHeight(false)
38 {
39 }
40 
42 {
43  //
44 }
45 
46 QWidget* VPE::VPropertyDelegate::createEditor (QWidget* parent, const QStyleOptionViewItem& option,
47  const QModelIndex& index) const
48 {
49  QWidget* tmpWidget = nullptr;
50  if (index.isValid())
51  {
52  VProperty* tmpProperty = reinterpret_cast<VProperty*>(index.internalPointer());
53  tmpWidget = tmpProperty->createEditor(parent, option, this);
54  }
55 
56  return tmpWidget ? tmpWidget : QStyledItemDelegate::createEditor(parent, option, index);
57 }
58 
59 
60 //! Sets the index data to the editor
61 void VPE::VPropertyDelegate::setEditorData (QWidget * editor, const QModelIndex & index) const
62 {
63  bool done = false;
64  if (index.isValid() && editor)
65  {
66  VProperty* tmpProperty = reinterpret_cast<VProperty*>(index.internalPointer());
67  done = tmpProperty->setEditorData(editor);
68  }
69 
70  if (!done)
71  {
72  QStyledItemDelegate::setEditorData(editor, index);
73  }
74 }
75 
76 //! Updates the index data
77 void VPE::VPropertyDelegate::setModelData (QWidget * editor, QAbstractItemModel * model,
78  const QModelIndex & index) const
79 {
80  QVariant tmpData;
81  if (index.isValid() && editor)
82  {
83  VProperty* tmpProperty = reinterpret_cast<VProperty*>(index.internalPointer());
84  tmpData = tmpProperty->getEditorData(editor);
85  }
86 
87  if (tmpData.isNull())
88  {
89  QStyledItemDelegate::setModelData(editor, model, index);
90  }
91  else
92  model->setData(index, tmpData);
93 }
94 
95 QSize VPE::VPropertyDelegate::sizeHint (const QStyleOptionViewItem& option, const QModelIndex& index) const
96 {
97  QSize tmpStandardSizeHint = QStyledItemDelegate::sizeHint(option, index);
98  tmpStandardSizeHint.setHeight(tmpStandardSizeHint.height() + 1);
99 
100  if (RowHeight > 0)
101  {
102  return QSize(tmpStandardSizeHint.width(), AddRowHeight ? tmpStandardSizeHint.height() + RowHeight : RowHeight);
103  }
104  else
105  return tmpStandardSizeHint;
106 }
107 
108 void VPE::VPropertyDelegate::setRowHeight(int height, bool add_to_standard)
109 {
110  RowHeight = height;
111  AddRowHeight = add_to_standard;
112 }
113 
114 void VPE::VPropertyDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
115  const QModelIndex& index ) const
116 {
117  bool done = false;
118  if (index.isValid() && index.column() == 1)
119  {
120  done = reinterpret_cast<VProperty*>(index.internalPointer())->paint(painter, option, index, this);
121  }
122 
123  if (!done)
124  {
125  QStyledItemDelegate::paint(painter, option, index);
126  }
127 
128  QColor tmpPenColor = static_cast<QRgb>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor, &option));
129 
130  QPen tmpOldPen = painter->pen();
131  painter->setPen(QPen(tmpPenColor));
132  painter->drawLine(option.rect.right(), option.rect.y(), option.rect.right(), option.rect.bottom());
133  painter->drawLine(option.rect.x(), option.rect.bottom(), option.rect.right(), option.rect.bottom());
134  painter->setPen(tmpOldPen);
135 }
virtual ~VPropertyDelegate() Q_DECL_OVERRIDE
void setRowHeight(int height=0, bool add_to_standard=false)
Sets the row height. Set this to 0 and the standard will be taken.
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE
Sets the index data to the editor.
VPropertyDelegate(QObject *parent=nullptr)
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const Q_DECL_OVERRIDE
Updates the index data.
virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE
Returns the size hint.
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE
Renders the delegate using the given painter and style option for the item specified by index.
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE
Creates the editor widget.
virtual bool setEditorData(QWidget *editor)
Sets the property's data to the editor (returns false, if the standard delegate should do that)
Definition: vproperty.cpp:117
virtual QVariant getEditorData(const QWidget *editor) const
Gets the data from the widget.
Definition: vproperty.cpp:138
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &, const QAbstractItemDelegate *)
Returns an editor widget, or NULL if it doesn't supply one.
Definition: vproperty.cpp:105