22 #include "customfieldsdelegate.h"
24 #include "customfieldsmodel.h"
27 #include <klocalizedstring.h>
30 #include <QDateTimeEdit>
35 CustomFieldsDelegate::CustomFieldsDelegate(QObject *parent)
36 : QStyledItemDelegate(parent)
40 CustomFieldsDelegate::~CustomFieldsDelegate()
44 QWidget *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:
57 QSpinBox *editor =
new QSpinBox(parent);
58 editor->setFrame(
false);
59 editor->setAutoFillBackground(
true);
63 case CustomField::BooleanType:
65 QCheckBox *editor =
new QCheckBox(parent);
69 case CustomField::DateType:
71 QDateEdit *editor =
new QDateEdit(parent);
72 editor->setFrame(
false);
73 editor->setAutoFillBackground(
true);
77 case CustomField::TimeType:
79 QTimeEdit *editor =
new QTimeEdit(parent);
80 editor->setFrame(
false);
81 editor->setAutoFillBackground(
true);
85 case CustomField::DateTimeType:
87 QDateTimeEdit *editor =
new QDateTimeEdit(parent);
88 editor->setFrame(
false);
89 editor->setAutoFillBackground(
true);
95 return QStyledItemDelegate::createEditor(parent, item, index);
99 void CustomFieldsDelegate::setEditorData(QWidget *editor,
const QModelIndex &index)
const
101 if (index.column() == 1) {
102 const CustomField::Type type =
static_cast<CustomField::Type
>(index.data(CustomFieldsModel::TypeRole).toInt());
105 case CustomField::TextType:
106 case CustomField::UrlType:
107 QStyledItemDelegate::setEditorData(editor, index);
109 case CustomField::NumericType:
111 QSpinBox *widget = qobject_cast<QSpinBox *>(editor);
112 widget->setValue(index.data(Qt::EditRole).toInt());
115 case CustomField::BooleanType:
117 QCheckBox *widget = qobject_cast<QCheckBox *>(editor);
118 widget->setChecked(index.data(Qt::EditRole).toString() == QLatin1String(
"true"));
121 case CustomField::DateType:
123 QDateEdit *widget = qobject_cast<QDateEdit *>(editor);
124 widget->setDisplayFormat(QLatin1String(
"dd.MM.yyyy"));
125 widget->setDate(QDate::fromString(index.data(Qt::EditRole).toString(), Qt::ISODate));
128 case CustomField::TimeType:
130 QTimeEdit *widget = qobject_cast<QTimeEdit *>(editor);
131 widget->setDisplayFormat(QLatin1String(
"hh:mm"));
132 widget->setTime(QTime::fromString(index.data(Qt::EditRole).toString(), Qt::ISODate));
135 case CustomField::DateTimeType:
137 QDateTimeEdit *widget = qobject_cast<QDateTimeEdit *>(editor);
138 widget->setDisplayFormat(QLatin1String(
"dd.MM.yyyy hh:mm"));
139 widget->setDateTime(QDateTime::fromString(index.data(Qt::EditRole).toString(), Qt::ISODate));
144 QStyledItemDelegate::setEditorData(editor, index);
148 void CustomFieldsDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index)
const
150 if (index.column() == 1) {
151 const CustomField::Type type =
static_cast<CustomField::Type
>(index.data(CustomFieldsModel::TypeRole).toInt());
154 case CustomField::TextType:
155 case CustomField::UrlType:
156 QStyledItemDelegate::setModelData(editor, model, index);
158 case CustomField::NumericType:
160 QSpinBox *widget = qobject_cast<QSpinBox *>(editor);
161 model->setData(index, QString::number(widget->value()));
164 case CustomField::BooleanType:
166 QCheckBox *widget = qobject_cast<QCheckBox *>(editor);
167 model->setData(index, widget->isChecked() ? QLatin1String(
"true") : QLatin1String(
"false"));
170 case CustomField::DateType:
172 QDateEdit *widget = qobject_cast<QDateEdit *>(editor);
173 model->setData(index, widget->date().toString(Qt::ISODate));
176 case CustomField::TimeType:
178 QTimeEdit *widget = qobject_cast<QTimeEdit *>(editor);
179 model->setData(index, widget->time().toString(Qt::ISODate));
182 case CustomField::DateTimeType:
184 QDateTimeEdit *widget = qobject_cast<QDateTimeEdit *>(editor);
185 model->setData(index, widget->dateTime().toString(Qt::ISODate));
190 QStyledItemDelegate::setModelData(editor, model, index);
194 void CustomFieldsDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index)
const
197 QStyledItemDelegate::paint(painter, option, index);