summaryrefslogtreecommitdiffstats
path: root/examples/serialbus/modbus/custom/registermodel.cpp
blob: 28dfd314dbcd71c72eff0f42f344b865905378ea (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
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include "registermodel.h"

enum { NumColumn = 0, RegistersColumn = 1, ColumnCount = 2, RowCount = 10 };

RegisterModel::RegisterModel(QObject *parent)
    : QAbstractTableModel(parent)
    , m_registers(RowCount, 0u)
{
}

int RegisterModel::rowCount(const QModelIndex &/*parent*/) const
{
    return RowCount;
}

int RegisterModel::columnCount(const QModelIndex &/*parent*/) const
{
    return ColumnCount;
}

QVariant RegisterModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || index.row() >= RowCount || index.column() >= ColumnCount)
        return QVariant();

    Q_ASSERT(m_registers.count() == RowCount);

    if (index.column() == NumColumn && role == Qt::DisplayRole)
        return QString::number(index.row());

    if (index.column() == RegistersColumn && role == Qt::DisplayRole)
        return QString("0x%1").arg(QString::number(m_registers.at(index.row()), 16));

    return QVariant();
}

QVariant RegisterModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();

    if (orientation == Qt::Horizontal) {
        switch (section) {
        case NumColumn:
            return tr("Address");
        case RegistersColumn:
            return tr("Registers");
        default:
            break;
        }
    }
    return QVariant();
}

bool RegisterModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid() ||  index.row() >= RowCount || index.column() >= ColumnCount)
        return false;

    Q_ASSERT(m_registers.count() == RowCount);

    if (index.column() == RegistersColumn && role == Qt::EditRole) {
        bool result = false;
        quint16 newValue = value.toString().toUShort(&result, 16);
        if (result)
            m_registers[index.row()] = newValue;

        emit dataChanged(index, index);
        return result;
    }

    return false;
}

Qt::ItemFlags RegisterModel::flags(const QModelIndex &index) const
{
    if (!index.isValid() || index.row() >= RowCount || index.column() >= ColumnCount)
        return QAbstractTableModel::flags(index);

    Qt::ItemFlags flags = QAbstractTableModel::flags(index);
    if ((index.row() < m_address) || (index.row() >= (m_address + m_number)))
        flags &= ~Qt::ItemIsEnabled;

    if (index.column() == RegistersColumn)
        return flags | Qt::ItemIsEditable;

    return flags;
}

void RegisterModel::setStartAddress(int address)
{
    m_address = address;
    emit updateViewport();
}

void RegisterModel::setNumberOfValues(const QString &number)
{
    m_number = number.toInt();
    emit updateViewport();
}