22#include "customfieldsdelegate.h"
24#include "customfieldsmodel.h"
27#include <klocalizedstring.h>
30#include <QDateTimeEdit>
35CustomFieldsDelegate::CustomFieldsDelegate(QObject *parent)
36 : QStyledItemDelegate(parent)
40CustomFieldsDelegate::~CustomFieldsDelegate()
44QWidget *CustomFieldsDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &item,
const QModelIndex &index)
const
46 if (index.column() == 1) {
47 const CustomField::Type type =
static_cast<CustomField::Type
>(index.data(CustomFieldsModel::TypeRole).toInt());
50 case CustomField::TextType:
51 case CustomField::UrlType:
53 return QStyledItemDelegate::createEditor(parent, item, index);
55 case CustomField::NumericType: {
56 QSpinBox *editor =
new QSpinBox(parent);
57 editor->setFrame(
false);
58 editor->setAutoFillBackground(
true);
62 case CustomField::BooleanType: {
63 QCheckBox *editor =
new QCheckBox(parent);
67 case CustomField::DateType: {
68 QDateEdit *editor =
new QDateEdit(parent);
69 editor->setFrame(
false);
70 editor->setAutoFillBackground(
true);
74 case CustomField::TimeType: {
75 QTimeEdit *editor =
new QTimeEdit(parent);
76 editor->setFrame(
false);
77 editor->setAutoFillBackground(
true);
81 case CustomField::DateTimeType: {
82 QDateTimeEdit *editor =
new QDateTimeEdit(parent);
83 editor->setFrame(
false);
84 editor->setAutoFillBackground(
true);
90 return QStyledItemDelegate::createEditor(parent, item, index);
94void CustomFieldsDelegate::setEditorData(QWidget *editor,
const QModelIndex &index)
const
96 if (index.column() == 1) {
97 const CustomField::Type type =
static_cast<CustomField::Type
>(index.data(CustomFieldsModel::TypeRole).toInt());
100 case CustomField::TextType:
101 case CustomField::UrlType:
102 QStyledItemDelegate::setEditorData(editor, index);
104 case CustomField::NumericType: {
105 QSpinBox *widget = qobject_cast<QSpinBox *>(editor);
106 widget->setValue(index.data(Qt::EditRole).toInt());
109 case CustomField::BooleanType: {
110 QCheckBox *widget = qobject_cast<QCheckBox *>(editor);
111 widget->setChecked(index.data(Qt::EditRole).toString() == QLatin1String(
"true"));
114 case CustomField::DateType: {
115 QDateEdit *widget = qobject_cast<QDateEdit *>(editor);
116 widget->setDisplayFormat(QLatin1String(
"dd.MM.yyyy"));
117 widget->setDate(QDate::fromString(index.data(Qt::EditRole).toString(), Qt::ISODate));
120 case CustomField::TimeType: {
121 QTimeEdit *widget = qobject_cast<QTimeEdit *>(editor);
122 widget->setDisplayFormat(QLatin1String(
"hh:mm"));
123 widget->setTime(QTime::fromString(index.data(Qt::EditRole).toString(), Qt::ISODate));
126 case CustomField::DateTimeType: {
127 QDateTimeEdit *widget = qobject_cast<QDateTimeEdit *>(editor);
128 widget->setDisplayFormat(QLatin1String(
"dd.MM.yyyy hh:mm"));
129 widget->setDateTime(QDateTime::fromString(index.data(Qt::EditRole).toString(), Qt::ISODate));
134 QStyledItemDelegate::setEditorData(editor, index);
138void CustomFieldsDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index)
const
140 if (index.column() == 1) {
141 const CustomField::Type type =
static_cast<CustomField::Type
>(index.data(CustomFieldsModel::TypeRole).toInt());
144 case CustomField::TextType:
145 case CustomField::UrlType:
146 QStyledItemDelegate::setModelData(editor, model, index);
148 case CustomField::NumericType: {
149 QSpinBox *widget = qobject_cast<QSpinBox *>(editor);
150 model->setData(index, QString::number(widget->value()));
153 case CustomField::BooleanType: {
154 QCheckBox *widget = qobject_cast<QCheckBox *>(editor);
155 model->setData(index, widget->isChecked() ? QLatin1String(
"true") : QLatin1String(
"false"));
158 case CustomField::DateType: {
159 QDateEdit *widget = qobject_cast<QDateEdit *>(editor);
160 model->setData(index, widget->date().toString(Qt::ISODate));
163 case CustomField::TimeType: {
164 QTimeEdit *widget = qobject_cast<QTimeEdit *>(editor);
165 model->setData(index, widget->time().toString(Qt::ISODate));
168 case CustomField::DateTimeType: {
169 QDateTimeEdit *widget = qobject_cast<QDateTimeEdit *>(editor);
170 model->setData(index, widget->dateTime().toString(Qt::ISODate));
175 QStyledItemDelegate::setModelData(editor, model, index);
179void CustomFieldsDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index)
const
182 QStyledItemDelegate::paint(painter, option, index);