-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlineeditdelegate.cpp
More file actions
83 lines (69 loc) · 2.16 KB
/
lineeditdelegate.cpp
File metadata and controls
83 lines (69 loc) · 2.16 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "lineeditdelegate.h"
LineEditDelegate::LineEditDelegate(QObject *parent) {}
QWidget *LineEditDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/*option*/,
const QModelIndex &/*index*/) const
{
QLineEdit *editor = new QLineEdit(parent);
return editor;
}
void LineEditDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
QString value = index.data().toString();
lineEdit->setText(value);
/*
if (!index.parent().isValid()) lineEdit->setEnabled(false);
else lineEdit->setEnabled(true);
*/
}
void LineEditDelegate::setModelData(QWidget *editor,
QAbstractItemModel *model, const QModelIndex &index) const
{
// get editor
QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
// get old val form the model
QString oldVal = model->data(index).toString();
// get new val from editor
QString newVal = lineEdit->text();
// get index of type item
QModelIndex typeIndex = model->index(index.row(), 1, index.parent());
// get type val
QString type = model->data(typeIndex).toString();
// check new value
QString val = (checkInput(type, newVal, index.column())) ? newVal : oldVal;
// set data
//model->setData(index, val, Qt::EditRole);
emit dataChanged(index, val);
}
void LineEditDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
bool LineEditDelegate::checkInput(const QString &type, const QString &val, int col)
{
bool ok;
if (col != 2) ok = true;
else
{
if (type == "integer")
{
val.toInt(&ok);
}
else
{
if(type == "real")
{
val.toFloat(&ok);
}
else
{
// we shouldn`t edit values of a dict or array
ok = ((type == "array" || type == "dict") && col == 2) ? false : true;
}
}
}
return ok;
}