File: canconnectionmodel.cpp

package info (click to toggle)
savvycan 220-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,456 kB
  • sloc: cpp: 61,803; sh: 293; javascript: 91; python: 44; makefile: 8
file content (171 lines) | stat: -rw-r--r-- 4,741 bytes parent folder | download
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "canconnectionmodel.h"
#include "connections/canconnection.h"
#include "connections/canconmanager.h"

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

CANConnectionModel::~CANConnectionModel()
{
}

enum class Column {
    Type       = 0, ///< The CAN driver/backend type, e.g. GVRET, peakcan, or socketcan
    Subtype    = 1, ///< Mostly used by SerialBus devices to pick the sub type
    Port       = 2, ///< The CAN hardware port, e.g. can0 for socketcan
    NumBuses   = 3, ///< Number of buses exposed by this device. Usually non-GVRET devices will just have one
    Status     = 4  ///< The bus status as text message
};

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

    if (orientation == Qt::Horizontal)
    {
        switch (Column(section))
        {
        case Column::Type:
            return QString(tr("Type"));
        case Column::Subtype:
            return QString(tr("Subtype"));
        case Column::Port:
            return QString(tr("Port"));
        case Column::NumBuses:
            return QString(tr("Buses"));
        case Column::Status:
            return QString(tr("Status"));
        }
    }

    else
        return QString::number(section + 1);

    return QVariant();
}

int CANConnectionModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return 5;
}


int CANConnectionModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);

    QList<CANConnection*>& conns = CANConManager::getInstance()->getConnections();

    return conns.count();
}

QVariant CANConnectionModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();
    //qDebug() << "Row: " << index.row();

    CANConnection *conn_p = getAtIdx(index.row());

    if (!conn_p) return QVariant();

    //bool isSocketCAN = (conn_p->getType() == CANCon::SERIALBUS) ? true: false;

    if (role == Qt::DisplayRole) {

        switch (Column(index.column()))
        {
            case Column::Type:
                if (conn_p)
                    switch (conn_p->getType()) {
                        case CANCon::KVASER: return "KVASER";
                        case CANCon::SERIALBUS: return "SerialBus";
                        case CANCon::GVRET_SERIAL: return "GVRET";
                        case CANCon::KAYAK: return "socketcand";
                        case CANCon::LAWICEL: return "LAWICEL";
                        case CANCon::CANSERVER: return "CANserver";
                        case CANCon::CANLOGSERVER: return "CanLogServer";
                        default: {}
                    }
                else qDebug() << "Tried to show connection type but connection was nullptr";
                break;
            case Column::Port:
                if (conn_p) return conn_p->getPort();
                else qDebug() << "Tried to show connection port but connection was nullptr";
                break;
            case Column::Subtype:
                return conn_p->getDriver();
                break;
            case Column::NumBuses:
                return conn_p->getNumBuses();
                break;
            case Column::Status:
                 return (conn_p->getStatus()==CANCon::CONNECTED) ? "Connected" : "Not Connected";
        }
    }
    return QVariant();
}


void CANConnectionModel::add(CANConnection* pConn_p)
{
    CANConManager* manager = CANConManager::getInstance();

    beginResetModel();
    manager->add(pConn_p);
    endResetModel();
}


void CANConnectionModel::remove(CANConnection* pConn_p)
{
    CANConManager* manager = CANConManager::getInstance();

    beginResetModel();
    manager->remove(pConn_p);
    endResetModel();
}

void CANConnectionModel::replace(int idx , CANConnection* pConn_p)
{
    CANConManager* manager = CANConManager::getInstance();

    beginResetModel();
    manager->replace(idx, pConn_p);
    endResetModel();
}

CANConnection* CANConnectionModel::getAtIdx(int pIdx) const
{
    if (pIdx < 0)
        return nullptr;

    QList<CANConnection*>& conns = CANConManager::getInstance()->getConnections();

    return conns.at(pIdx);
}

void CANConnectionModel::refresh(int pIndex)
{
    Q_UNUSED(pIndex)

    beginResetModel();
    endResetModel();
    /*
    QModelIndex begin;
    QModelIndex end;

    if(pIndex>=0) {
        begin   = createIndex(pIndex, 0);
        end     = createIndex(pIndex, columnCount()-1);
    }
    else {
        begin   = createIndex(0, 0);
        end     = createIndex(rowCount()-1, columnCount()-1);
    }
    dataChanged(begin, end, QVector<int>(Qt::DisplayRole)); */
}