-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinedelegate.cpp
112 lines (90 loc) · 3.6 KB
/
linedelegate.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
/*
Copyright (C) 2020 Denis Gofman - <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/
#include "linedelegate.h"
#include "delegatedlinedata.h"
#include <QDebug>
#include <QEvent>
#include <QMouseEvent>
#include <QPainter>
#define LOG qDebug() << Q_FUNC_INFO
LineDelegate::LineDelegate(QObject *parent)
: QStyledItemDelegate(parent)
, m_linePen(Qt::darkGreen)
{
m_linePen.setWidthF(2.);
}
void LineDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyledItemDelegate::paint(painter, option, index);
const QVariant &lineData = index.data(Line::DataRole);
if (lineData.isValid() && lineData.canConvert<Line>()) {
const Line &line = lineData.value<Line>();
const QLineF &lineF = line.toQLine(option.rect);
// LOG << index.row() << index.column() << option.rect << line.from << line.to << lineF;
painter->save();
painter->setPen(m_linePen);
painter->drawLine(lineF);
painter->restore();
}
}
bool LineDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option,
const QModelIndex &index)
{
switch (event->type()) {
case QEvent::MouseButtonPress:
handleMousePress(static_cast<QMouseEvent *>(event), model, option, index);
break;
case QEvent::MouseMove:
handleMouseMove(static_cast<QMouseEvent *>(event), model, option, index);
break;
case QEvent::MouseButtonRelease:
handleMouseRelease(static_cast<QMouseEvent *>(event), model, option, index);
break;
default:
break;
}
return QStyledItemDelegate::editorEvent(event, model, option, index);
}
void LineDelegate::handleMousePress(QMouseEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option,
const QModelIndex &index)
{
Q_UNUSED(event);
Q_UNUSED(model);
if (index.isValid()) {
m_startIndex = index;
m_startPoint = { event->x() - option.rect.x(), option.rect.center().y() };
}
}
void LineDelegate::handleMouseMove(QMouseEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option,
const QModelIndex &index)
{
const QPoint endPoint = { event->x() - option.rect.x(), option.rect.center().y() };
if (m_startIndex != index) {
m_startIndex = index;
m_startPoint = endPoint;
}
// LOG << startPoint << endPoint;
const Line &line = Line::fromQLine({ m_startPoint, endPoint }, option.rect);
model->setData(index, QVariant::fromValue(line), Line::DataRole);
}
void LineDelegate::handleMouseRelease(QMouseEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option,
const QModelIndex &index)
{
Q_UNUSED(event);
Q_UNUSED(model);
Q_UNUSED(option);
Q_UNUSED(index);
m_startIndex = QModelIndex();
m_startPoint = { -1, -1 };
}