aboutsummaryrefslogtreecommitdiffstats
path: root/examples/demos/graphs_csv/datamodel.cpp
blob: 5e3b903c9823d30b0f6d4a8820fad863b95b8b53 (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
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include "datamodel.h"
#include <QFileInfo>
#include <QQmlFile>
#include <QQmlContext>
#include <charconv>
#include <csv.hpp>
#include <qlogging.h>
#include <qqml.h>
#include <qtpreprocessorsupport.h>
#include <sstream>
#include <string_view>

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

DataModel::~DataModel() { }

int DataModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_csvData.count() ? m_csvData.at(0).count() : 0;
}


QVariant DataModel::data(const QModelIndex &index, int role) const
{
    if (role == Qt::DisplayRole) {
        if (index.row() < rowCount() && index.column() < columnCount())
            return m_csvData.at(index.row()).at(index.column());
    }
    return QVariant();
}

QVariant DataModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::DisplayRole) {
        if (orientation == Qt::Horizontal) {
            if (section < columnCount())
                return m_csvData.at(0).at(section);
        }
        else if (orientation == Qt::Vertical) {
            if (section < rowCount() && columnCount() > 0)
                return m_csvData.at(section).at(0);
        }
    }
    return QVariant();
}

QHash<int, QByteArray> DataModel::roleNames() const
{
    QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
    roles[CustomRoles::Background] = "background";
    return roles;
}

static int tryConvertToInt(std::string_view team, std::string_view fieldName, std::string field)
{
    int value = -1;
    auto [ptr, ec] = std::from_chars(field.data(), field.data() + field.size(), value);
    Q_UNUSED(ec);
    if (value == -1)
        qWarning("%s: error in %s field", team.data(), fieldName.data());

    return value;
};

void DataModel::readCsv(const QUrl &csvFile)
{
    QAbstractItemModel::beginResetModel();

    const auto context = qmlContext(this);
    const auto resolvedUrl = context ? context->resolvedUrl(csvFile) : csvFile;

    QFile file(QQmlFile::urlToLocalFileOrQrc(resolvedUrl));

    if (!file.open(QIODeviceBase::ReadOnly)) {
        qWarning("Could not open %s for reading", qUtf8Printable(csvFile.toString()));
        return;
    }

    m_csvData.clear();
    std::stringstream ss(file.readAll().toStdString());

    csv::CSVReader reader(ss);
    auto headers = reader.get_col_names();
    QList<QVariant> headersList;
    headersList.resize(headers.size());
    auto hIt = headersList.begin();
    auto it = headers.begin();
    while (it != headers.end()) {
        *hIt = QString::fromStdString(*it);
        ++hIt;
        ++it;
    }

    if (headersList.count() > 0)
        m_csvData.push_back(headersList);

    for (const csv::CSVRow &csvRow : reader) {
        csv::CSVField teamField = csvRow[headers.at(0)];
        csv::CSVField goldField = csvRow[headers.at(1)];
        csv::CSVField silverField = csvRow[headers.at(2)];
        csv::CSVField bronzeField = csvRow[headers.at(3)];
        csv::CSVField totalField = csvRow[headers.at(4)];

        QList<QVariant> row;
        row.resize(5);
        auto team = teamField.get();
        row[0] = QString::fromStdString(teamField.get());
        row[1] = tryConvertToInt(team, headers.at(1), goldField.get());
        row[2] = tryConvertToInt(team, headers.at(2), silverField.get());
        row[3] = tryConvertToInt(team, headers.at(3), bronzeField.get());
        row[4] = tryConvertToInt(team, headers.at(4), totalField.get());
        m_csvData.push_back(row);
    }

    QAbstractItemModel::endResetModel();
}