-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryDelegate.cpp
More file actions
140 lines (134 loc) · 4.4 KB
/
Copy pathMemoryDelegate.cpp
File metadata and controls
140 lines (134 loc) · 4.4 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
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
#include "CheckBox.h"
#include "LineEdit.h"
#include "MemoryDelegate.h"
#include "MemoryTableModel.h"
#include <QApplication>
#include <QCheckBox>
#include <QComboBox>
#include <QDebug>
#include <QDoubleSpinBox>
#include <QLineEdit>
MemoryDelegate::MemoryDelegate() : QItemDelegate()
{
m_header.setColor(QColor(159, 158, 95));
m_header.setStyle(Qt::SolidPattern);
}
void MemoryDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QStyleOptionViewItem resultOption(option);
int column = index.column();
QVariant data = index.data();
if(index.row() == 0 || column == 0)
{
resultOption.backgroundBrush = m_header;
QApplication::style()->drawControl(QStyle::CE_ItemViewItem,
&resultOption, painter);
}
else if(column > 11 || column == 10 || column == 9)
{
// QItemDelegate::drawCheck(painter, option, option.rect,
// data.toBool() ? Qt::Checked
// : Qt::Unchecked);
// return;
QStyleOptionButton style;
style.rect = option.rect;
style.state = index.data().toBool() ? QStyle::State_On
: QStyle::State_Off;
style.state |= index.flags() & Qt::ItemIsEnabled ? QStyle::State_Enabled
: QStyle::State_None;
QApplication::style()->drawControl(QStyle::CE_CheckBox,
&style, painter);
return;
}
QItemDelegate::paint(painter, resultOption, index);
return;
resultOption.text = index.data().toString();
QApplication::style()->drawControl(QStyle::CE_ItemViewItem,
&resultOption, painter);
}
QWidget *MemoryDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if(!index.isValid() || index.column() == 0 || index.row() == 0)
return new QWidget(parent);
#warning not full released
QVariant variant = index.data();
int column = index.column();
switch(column)
{
case 2:
case 1:
{
QDoubleSpinBox *widget = new QDoubleSpinBox(parent);
widget->setMinimum(33.0);
widget->setMaximum(48.5);
widget->setSingleStep(0.00025);
widget->setDecimals(5);
return widget;
}
case 4:
{
LineEdit *widget = new LineEdit(parent);
static QRegExpValidator validator(QRegExp("[A-Z0-9 ]{,7}"));
widget->setValidator(&validator);
// widget->setInputMask(">nnnnnnn");
return widget;
}
case 9:
case 10:
case 12:
case 13:
case 14:
case 15:
case 16:
{
QCheckBox *widget = new QCheckBox(parent);
return widget;
}
default:
{
QComboBox *widget = new QComboBox(parent);
MemoryTableModel *model = qobject_cast<MemoryTableModel*>(
const_cast<QAbstractItemModel*>(index.model()));
widget->addItems(model->variants(index.column(), index.row() - 1));
return widget;
}
}
}
void MemoryDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
if(comboBox != nullptr)
comboBox->setCurrentText(index.data(Qt::EditRole).toString());
QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor);
if(lineEdit != nullptr)
lineEdit->setText(index.data(Qt::EditRole).toString());
QDoubleSpinBox *spinBox = qobject_cast<QDoubleSpinBox*>(editor);
if(spinBox != nullptr)
spinBox->setValue(index.data(Qt::EditRole).toDouble());
QCheckBox *check = qobject_cast<QCheckBox*>(editor);
if(check != nullptr)
check->setChecked(!index.data(Qt::EditRole).toBool());
}
void MemoryDelegate::setModelData(QWidget *editor,
QAbstractItemModel *model,
const QModelIndex &index) const
{
QVariant value;
QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
if(comboBox != nullptr)
value = comboBox->currentText();
QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor);
if(lineEdit != nullptr)
value = lineEdit->text();
QDoubleSpinBox *spinBox = qobject_cast<QDoubleSpinBox*>(editor);
if(spinBox != nullptr)
value = spinBox->value();
QCheckBox *check = qobject_cast<QCheckBox*>(editor);
if(check != nullptr)
value = check->isChecked();
model->setData(index, value);
}