-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelegate.cpp
160 lines (140 loc) · 5.59 KB
/
delegate.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
#include "delegate.h"
#include <QApplication>
#include <QSqlQuery>
#include <QEvent>
#include <QPainter>
#include <QStyleOptionButton>
#include <QMouseEvent>
#include <QtDebug>
delegate::delegate(QObject *parent) :
QStyledItemDelegate(parent)
{
}
delegate::~delegate()
{
}
//Creates the editor
QWidget *delegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QCheckBox *chkbox =new QCheckBox(parent);
return chkbox;
}
//Current data that you modify
void delegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QVariant data = index.model()->data(index, Qt::DisplayRole).toInt();
if(data == 1)
{
static_cast<QCheckBox*>(editor)->setCheckState(Qt::Checked);
}
else if (data == 0)
{
static_cast<QCheckBox*>(editor)->setCheckState(Qt::Unchecked);
}
else {
QStyledItemDelegate::setEditorData(editor, index);
}
}
//The data you return to the model
void delegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QVariant value = static_cast<QCheckBox*>(editor)->checkState();
model->setData(index, value, Qt::CheckStateRole);
}
//Changes size based on your needs
void delegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionButton opt;
opt.rect = QApplication::style()->subElementRect( QStyle::SE_CheckBoxIndicator, &opt, NULL );
const int x = option.rect.center().x() - opt.rect.width() / 2;
const int y = (option.rect.center().y() - opt.rect.height() / 2)-7; //Small adjustment to apear more centrally.(paint() checkbox interference?)
opt.rect.moveTo( x, y );
editor->setGeometry(option.rect);
editor->move(x,y);
}
void delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
// Setting parameters
Qt::CheckState state = (Qt::CheckState)index.data( Qt::CheckStateRole ).toInt();
QStyleOptionButton opt;
opt.state = QStyle::State_Enabled; // Enabling checkbox
if ( option.state & QStyle::State_MouseOver )
opt.state |= QStyle::State_MouseOver; // Mouse oversell
QVariant data = index.model()->data(index, Qt::DisplayRole/*CheckStateRole*/).toInt();
if(data == 1)
{
opt.state |= QStyle::State_On;
} else
{
opt.state |= QStyle::State_Off;
}
// Checkbox rect
opt.rect = QApplication::style()->subElementRect( QStyle::SE_CheckBoxIndicator, &opt, NULL );
const int x = option.rect.center().x() - opt.rect.width() / 2;
const int y = option.rect.center().y() - opt.rect.height() / 2;
opt.rect.moveTo( x, y );
// Draws hover focus
if ( option.state & QStyle::State_MouseOver )
painter->fillRect( option.rect, QBrush( QColor( 0xff, 0xff, 0xaa, 0x60 ) ) );
// Drawing checkbox
QApplication::style()->drawControl( QStyle::CE_CheckBox, &opt, painter );
}
bool delegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
switch ( event->type() )
{
case QEvent::MouseButtonPress:
m_lastClickedIndex = index;
break;
case QEvent::MouseButtonRelease:
{
if ( index != m_lastClickedIndex )
break;
QMouseEvent *e = static_cast< QMouseEvent * >( event );
if ( e->button() != Qt::LeftButton )
break;
m_lastClickedIndex = QModelIndex();
//Update database
QVariant id = index.sibling(index.row(),0).data(Qt::DisplayRole).toInt();
QVariant value = index.model()->data(index, Qt::DisplayRole).toInt();
QStyleOptionButton opt;
opt.rect = QApplication::style()->subElementRect( QStyle::SE_CheckBoxIndicator, &opt, NULL );
const int x = option.rect.center().x() - opt.rect.width() / 2;
const int y = option.rect.center().y() - opt.rect.height() / 2;
opt.rect.moveTo( x, y );
if ( opt.rect.contains( e->pos() ) )
{
// Process click on checkbox logic
Qt::CheckState state = (Qt::CheckState)index.data( Qt::CheckStateRole ).toInt();
int xid = id.toInt();
int xvalue = value.toInt();
if (xvalue == 0)
{
xvalue = 1;
state = Qt::Checked;
}
else
{
xvalue = 0;
state = Qt::Unchecked;
}
databaseUpdate(xid,xvalue);
emit clickSignal();
model->setData( index, state, Qt::CheckStateRole );
}
return true;
}
default:
break;
}
return QStyledItemDelegate::editorEvent( event, model, option, index );
}
bool delegate::databaseUpdate(int task_id, int checkbox_value) const
{
QSqlQuery query;
query.prepare("UPDATE task_list SET finished = :finished WHERE id = :id");
query.bindValue(":finished", checkbox_value);
query.bindValue(":id", task_id);
return query.exec();
qDebug() << query.lastQuery();
}