-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparametereditdelegate.cpp
70 lines (62 loc) · 2.67 KB
/
parametereditdelegate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "parametereditdelegate.h"
#include "parametermodel.h"
#include <QDateEdit>
#include <QDebug>
ParameterEditDelegate::ParameterEditDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
QWidget *ParameterEditDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
const ParameterModel *parmodel = static_cast<const ParameterModel*>(index.model());
const Parameter *par = parmodel->getParameterAtRow(index.row());
if(par->type == parametertype_ptime)
{
QDateEdit *dateEdit = new QDateEdit(parent);
dateEdit->setLocale(QLocale()); //NOTE: To make sure that it has the default locale (which we set in mainwindow constructor).
dateEdit->setDisplayFormat("d. MMMM yyyy");
dateEdit->setMinimumDate(Parameter::valueAsQDate(par->min));
dateEdit->setMaximumDate(Parameter::valueAsQDate(par->max));
return dateEdit;
}
else if(par->type == parametertype_bool)
return 0; //NOTE: We don't use a delegate for editing bools, instead we just do "click means toggle" in the parametermodel. See ParameterModel::handleClick
return new MyLineEdit(parent);
}
void ParameterEditDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
const ParameterModel *parmodel = static_cast<const ParameterModel*>(index.model());
const Parameter *par = parmodel->getParameterAtRow(index.row());
if(par->type == parametertype_ptime)
{
QDateEdit *dateEdit = static_cast<QDateEdit*>(editor);
dateEdit->setDate(Parameter::valueAsQDate(par->value));
}
else
{
QString value = index.model()->data(index, Qt::EditRole).toString();
MyLineEdit *lineEdit = static_cast<MyLineEdit*>(editor);
lineEdit->setText(value);
}
}
void ParameterEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
ParameterModel *parmodel = static_cast<ParameterModel*>(model);
const Parameter *par = parmodel->getParameterAtRow(index.row());
if(par->type == parametertype_ptime)
{
QDateEdit *dateEdit = static_cast<QDateEdit*>(editor);
int64_t intVal = QDateTime(dateEdit->date(), QTime(), Qt::OffsetFromUTC, 0).toSecsSinceEpoch();
model->setData(index, QVariant((qlonglong)intVal), Qt::EditRole);
}
else
{
MyLineEdit *lineEdit = static_cast<MyLineEdit*>(editor);
QString value = lineEdit->text();
model->setData(index, value, Qt::EditRole);
}
}
void ParameterEditDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}