summaryrefslogtreecommitdiff
path: root/seriesmodel.cpp
blob: 628cb60034cc8046f18764672b18c41057220315 (plain)
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
#include "seriesmodel.h"

// cpp
// tbb
#ifdef __linux__
#ifndef Q_MOC_RUN
#if defined(emit)
#undef emit
#include <execution>
#define emit  // restore the macro definition of "emit", as it was defined in
              // gtmetamacros.h
#else
#include <execution>
#endif  // defined(emit)
#endif  // Q_MOC_RUN
#endif

SeriesModel::SeriesModel(QObject* parent)
    : QAbstractTableModel(parent)
{
}

SeriesModel::SeriesModel(const SeriesModel& other)
    : QAbstractTableModel()
{
    append(other.m_points);
}

int SeriesModel::rowCount(const QModelIndex& parent) const
{
    Q_UNUSED(parent);
    return m_points.count();
}

int SeriesModel::columnCount(const QModelIndex& parent) const
{
    Q_UNUSED(parent);
    return 2;
}

QVariant SeriesModel::data(const QModelIndex& modelIndex, int role) const
{
    if (!modelIndex.isValid())
        return QVariant();

    if (modelIndex.row() < 0 || modelIndex.row() >= m_points.count())
        return QVariant();

    if (role == Qt::DisplayRole)
    {
        if (modelIndex.column() == 0)
            return m_points.at(modelIndex.row()).x();
        else if (modelIndex.column() == 1)
            return m_points.at(modelIndex.row()).y();
    }

    return QVariant();
}

void SeriesModel::append(const QVector<QVector2D> points)
{
    QMutexLocker l(&m_mtx);
    beginInsertRows(
        QModelIndex(),
        m_points.count(),
        m_points.count() + points.count() - 1
    );

    m_points << points;
    endInsertRows();
    recalcLimits();
}

void SeriesModel::clear()
{
    QMutexLocker l(&m_mtx);

    if (m_points.isEmpty())
        return;

    beginRemoveRows(QModelIndex(), 0, m_points.count() - 1);
    m_points.clear();
    endRemoveRows();
}

void SeriesModel::recalcLimits()
{
    // WARNING: not thread safe
    m_minX = std::numeric_limits<float>::max();
    m_maxX = std::numeric_limits<float>::min();
    m_minY = std::numeric_limits<float>::max();
    m_maxY = std::numeric_limits<float>::min();

#ifdef __linux__
    const auto [minX, maxX] = std::minmax_element(
        std::execution::par_unseq,
        m_points.constBegin(),
        m_points.constEnd(),
        [](const auto& a, const auto& b) { return a.x() < b.x(); }
    );

    const auto [minY, maxY] = std::minmax_element(
        std::execution::par_unseq,
        m_points.constBegin(),
        m_points.constEnd(),
        [](const auto& a, const auto& b) { return a.y() < b.y(); }
    );

    float min = std::min(minX->x(), minY->y());
    float max = std::max(maxX->x(), maxY->y());
#else
    for (const auto& p : m_points)
    {
        if (p.x() < m_minX)
            m_minX = p.x();

        if (p.x() > m_maxX)
            m_maxX = p.x();

        if (p.y() < m_minY)
            m_minY = p.y();

        if (p.y() > m_maxY)
            m_maxY = p.y();
    }

    float min = std::min(m_minX, m_minY);
    float max = std::max(m_maxX, m_maxY);
#endif

    m_minX = min;
    m_maxX = max;
    m_minY = min;
    m_maxY = max;

    emit minXChanged();
    emit maxXChanged();
    emit minYChanged();
    emit maxYChanged();
}

void SeriesModel::replace(const QVector<QVector2D> points)
{
    clear();
    append(points);
}