-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelegatedlinedata.h
59 lines (46 loc) · 1.86 KB
/
delegatedlinedata.h
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
/*
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 <QDebug>
#include <QLineF>
#include <QObject>
#include <QRect>
#pragma once
/*!
* \brief The Line struct is a wrapper for a line to be drawn by delegate
* \a from & \a to are the X coordinates of start and end point in cell in logical range [0.0, 1.0].
*/
struct Line {
static constexpr int DataRole = Qt::UserRole + 1;
qreal from;
qreal to;
bool isEmpty() const { return qFuzzyCompare(from, to); }
bool isFull() const { return !isEmpty() && qFuzzyIsNull(from) && qFuzzyCompare(1., to); }
QLineF toQLine(const QRect &viewport) const
{
const QPoint ¢er = viewport.center();
QPointF p1(viewport.left(), center.y());
QPointF p2(viewport.right(), center.y());
if (!isFull()) {
p1.rx() = viewport.left() + viewport.width() * qreal(from);
p2.rx() = viewport.left() + viewport.width() * qreal(to);
}
return { p1, p2 };
}
static Line fromQLine(const QLine &line, const QRect &viewport)
{
const qreal w(viewport.width());
return { qreal(line.x1()) / w, qreal(line.x2()) / w };
}
};
Q_DECLARE_METATYPE(Line);