-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_button_delegate.cpp
More file actions
88 lines (72 loc) · 2.67 KB
/
delete_button_delegate.cpp
File metadata and controls
88 lines (72 loc) · 2.67 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
#include "delete_button_delegate.h"
#include "cremovepatientbutton.h"
#include <QSortFilterProxyModel>
#include <QApplication>
#include <QtGui>
#include <QRect>
#include <QStyle>
#include <QPainter>
#include <QStyleOptionButton>
#include <QtDebug>
#include "patients_filter_proxy_model.h"
#include "patients_model.h"
DeleteButtonDelegate::DeleteButtonDelegate(QObject *parent)
: QItemDelegate(parent)
{
}
QWidget * DeleteButtonDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
return nullptr;
}
void DeleteButtonDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QRect r = option.rect;//getting the rect of the cell
int x,y,w,h;
x = r.left() + r.width() - 57;//the X coordinate
y = r.top() + 4;//the Y coordinate
w = 56; //button width
h = 30; //button height
QStyleOptionButton button;
// CRemovePatientButton *pB = new CRemovePatientButton;
// pB->setText("Remove");
// auto proxy_model = dynamic_cast<const QSortFilterProxyModel *>(index.model());
// pB->setModelIndex(proxy_model->mapToSource(index));
// pB->setGeometry(0, 0, 30, 30);
// pB->rect = QRect(x,y,w,h);
// pB->text = "Delete";
// pB->state = QStyle::State_Enabled;
button.rect = QRect(x,y,w,h);
button.text = "Delete";
button.state = QStyle::State_Enabled;
auto patients_proxy_model = dynamic_cast<const PatientsFilterProxyModel *>(index.model());
QApplication::style()->drawControl(QStyle::CE_PushButton, &button, painter);
m_sourceIndex = patients_proxy_model->mapToSource(index);
}
bool DeleteButtonDelegate::editorEvent(QEvent *event,
QAbstractItemModel *model,
const QStyleOptionViewItem &option,
const QModelIndex &index)
{
if( event->type() == QEvent::MouseButtonPress )
{
QMouseEvent * e = (QMouseEvent *)event;
int clickX = e->x();
int clickY = e->y();
QRect r = option.rect;//getting the rect of the cell
int x,y,w,h;
x = r.left() + r.width() - 45;//the X coordinate
y = r.top();//the Y coordinate
w = 45;//button width
h = 30;//button height
if( clickX > x && clickX < x + w ) {
if( clickY > y && clickY < y + h )
{
emit dynamic_cast<const PatientsModel *>(m_sourceIndex.model())->itemForRemove(index);
}
}
}
}