-
Notifications
You must be signed in to change notification settings - Fork 0
/
QComboBoxItemDelegate.cpp
170 lines (162 loc) · 6.86 KB
/
QComboBoxItemDelegate.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <QComboBox>
#include <QtGui>
#include <QCheckBox>
#include "QComboBoxItemDelegate.h"
#include "Contstants.h"
#include "TableModel.h"
#include "GroupQCheckBox.h"
QComboBoxItemDelegate::QComboBoxItemDelegate(QObject* parent) : QStyledItemDelegate(parent)
{
}
QWidget* QComboBoxItemDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.column() == COL_VALUE)
{
const TableModel* ptr = qobject_cast<const TableModel*>(index.model());
if(ptr->data1(index, Qt::EditRole).getTypeWidget() == Property::QComboBox)
{
QStringList values = ptr->data1(index,Qt::EditRole).getVariants();
QComboBox* comboBox = new QComboBox(parent);
comboBox->addItems(values);
comboBox->setCurrentIndex(1);
return comboBox;
}
else if(ptr->data1(index, Qt::EditRole).getTypeWidget() == Property::QCheckBox)
{
QCheckBox* checkbox = new QCheckBox(parent);
checkbox->setChecked(ptr->data1(index, Qt::EditRole).isActive());
return checkbox;
}
else if(ptr->data1(index, Qt::EditRole).getTypeWidget() == Property::GroupQCheckBox)
{
GroupQCheckBox* groupBox = new GroupQCheckBox(parent);
QString str = ptr->data1(index, Qt::EditRole).getCurVal();
if(str.indexOf("bluetooth") != -1) groupBox->setValue(GroupQCheckBox::BLUETOOTH, true);
if(str.indexOf("wifi") != -1) groupBox->setValue(GroupQCheckBox::WIFI, true);
if(str.indexOf("wwan") != -1) groupBox->setValue(GroupQCheckBox::WWAN, true);
return groupBox;
}
else
{
QLineEdit* line = new QLineEdit(parent);
line->setText(ptr->data1(index, Qt::EditRole).getDefVal());
if(ptr->data1(index, Qt::EditRole).getValidator().type == "Digit")
{
Property::AboutValidator temp = ptr->data1(index, Qt::EditRole).getValidator();
line->setValidator(new QIntValidator(temp.min, temp.max, parent));
}
else if(ptr->data1(index, Qt::EditRole).getValidator().type == "other")
{
line->setValidator(new QIntValidator(0, (1 << 29), parent));
}
else if(ptr->data1(index, Qt::EditRole).getValidator().type == "string")
{
//line->setValidator();
}
return line;
}
}
else if(index.column() == COL_ACTIVE)
{
const TableModel* ptr = qobject_cast<const TableModel*>(index.model());
QCheckBox* checkbox = new QCheckBox(parent);
checkbox->setChecked(ptr->data1(index, Qt::EditRole).isActive());
return checkbox;
}
else
{
return QStyledItemDelegate::createEditor(parent, option, index);
}
}
void QComboBoxItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
if(index.column() == COL_VALUE)
{
const TableModel* ptr = qobject_cast<const TableModel*>(index.model());
if(ptr->data1(index, Qt::EditRole).getTypeWidget() == Property::QComboBox)
{
QComboBox* combobox = qobject_cast<QComboBox*>(editor);
QString value = combobox->currentText();
model->setData(index, value, Qt::EditRole);
}
else if(ptr->data1(index, Qt::EditRole).getTypeWidget() == Property::QCheckBox)
{
QCheckBox* checkbox = qobject_cast<QCheckBox*>(editor);
model->setData(index, checkbox->isChecked(), Qt::EditRole);
}
else if(ptr->data1(index, Qt::EditRole).getTypeWidget() == Property::GroupQCheckBox)
{
qDebug() << "Group to model\n";
GroupQCheckBox* groupBox = qobject_cast<GroupQCheckBox*>(editor);
QString str;
if(groupBox->getValue(GroupQCheckBox::BLUETOOTH)) str += "bluetooth ";
if(groupBox->getValue(GroupQCheckBox::WIFI)) str += "wifi ";
if(groupBox->getValue(GroupQCheckBox::WWAN)) str += "wwan ";
model->setData(index, str, Qt::EditRole);
}
else
{
QLineEdit* line = qobject_cast<QLineEdit*>(editor);
model->setData(index, line->text(), Qt::EditRole);
}
}
else if(index.column() == COL_ACTIVE)
{
const TableModel* ptr = qobject_cast<const TableModel*>(index.model());
if(ptr->data1(index, Qt::EditRole).getTypeWidget() == Property::QCheckBox)
{
QCheckBox* checkbox = qobject_cast<QCheckBox*>(editor);
model->setData(index, checkbox->isChecked(), Qt::EditRole);
}
}
else
{
QStyledItemDelegate::setModelData(editor, model, index);
}
}
void QComboBoxItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if(index.column() == COL_VALUE)
{
const TableModel* ptr = qobject_cast<const TableModel*>(index.model());
if(ptr->data1(index, Qt::EditRole).getTypeWidget() == Property::QComboBox)
{
QComboBox* combobox = qobject_cast<QComboBox*>(editor);
combobox->setCurrentText(ptr->data(index, Qt::EditRole).toString());
}
else if(ptr->data1(index, Qt::EditRole).getTypeWidget() == Property::QCheckBox)
{
QCheckBox* checkbox = qobject_cast<QCheckBox*>(editor);
checkbox->setChecked(ptr->data1(index, Qt::EditRole).isActive());
}
else if(ptr->data1(index, Qt::EditRole).getTypeWidget() == Property::GroupQCheckBox)
{
GroupQCheckBox* groupBox = qobject_cast<GroupQCheckBox*>(editor);
QString str = ptr->data1(index, Qt::EditRole).getCurVal();
if(str.indexOf("bluetooth") != -1) groupBox->setValue(GroupQCheckBox::BLUETOOTH, true);
if(str.indexOf("wifi") != -1) groupBox->setValue(GroupQCheckBox::WIFI, true);
if(str.indexOf("wwan") != -1) groupBox->setValue(GroupQCheckBox::WWAN, true);
}
else
{
QLineEdit* line = qobject_cast<QLineEdit*>(editor);
line->setText(ptr->data1(index, Qt::EditRole).getCurVal());
}
}
else if(index.column() == COL_ACTIVE)
{
const TableModel* ptr = qobject_cast<const TableModel*>(index.model());
if(ptr->data1(index, Qt::EditRole).getTypeWidget() == Property::QCheckBox)
{
QCheckBox* checkbox = qobject_cast<QCheckBox*>(editor);
checkbox->setChecked(ptr->data1(index, Qt::EditRole).isActive());
}
}
else
{
QStyledItemDelegate::setEditorData(editor, index);
}
}