Merge pull request #113 from andre-hartmann/improve-switch
Improve switch statements with enums and cleaned up a bit
This commit is contained in:
+27
-33
@@ -215,6 +215,16 @@ void CANFrameModel::recalcOverwrite()
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
enum class Column {
|
||||
TimeStamp = 0, ///< The timestamp when the frame was transmitted or received
|
||||
FrameId = 1, ///< The frames CAN identifier (Standard: 11 or Extended: 29 bit)
|
||||
Extended = 2, ///< True if the frames CAN identifier is 29 bit
|
||||
Direction = 3, ///< Whether the frame was transmitted or received
|
||||
Bus = 4, ///< The bus where the frame was transmitted or received
|
||||
Length = 5, ///< The frames payload data length
|
||||
Data = 6 ///< The frames payload data
|
||||
};
|
||||
|
||||
QVariant CANFrameModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
int dLen;
|
||||
@@ -257,28 +267,22 @@ QVariant CANFrameModel::data(const QModelIndex &index, int role) const
|
||||
}
|
||||
|
||||
if (role == Qt::DisplayRole) {
|
||||
switch (index.column())
|
||||
switch (Column(index.column()))
|
||||
{
|
||||
case 0: //timestamp
|
||||
case Column::TimeStamp:
|
||||
return Utility::formatTimestamp(thisFrame.timestamp);
|
||||
break;
|
||||
case 1: //id
|
||||
case Column::FrameId:
|
||||
return Utility::formatNumber(thisFrame.ID);
|
||||
break;
|
||||
case 2: //ext
|
||||
case Column::Extended:
|
||||
return QString::number(thisFrame.extended);
|
||||
break;
|
||||
case 3: //direction
|
||||
case Column::Direction:
|
||||
if (thisFrame.isReceived) return QString(tr("Rx"));
|
||||
else return QString(tr("Tx"));
|
||||
break;
|
||||
case 4: //bus
|
||||
return QString(tr("Tx"));
|
||||
case Column::Bus:
|
||||
return QString::number(thisFrame.bus);
|
||||
break;
|
||||
case 5: //len
|
||||
case Column::Length:
|
||||
return QString::number(thisFrame.len);
|
||||
break;
|
||||
case 6: //data
|
||||
case Column::Data:
|
||||
dLen = thisFrame.len;
|
||||
if (dLen < 0) dLen = 0;
|
||||
if (dLen > 8) dLen = 8;
|
||||
@@ -307,9 +311,6 @@ QVariant CANFrameModel::data(const QModelIndex &index, int role) const
|
||||
}
|
||||
}
|
||||
return tempString;
|
||||
break;
|
||||
default:
|
||||
return QVariant();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -324,29 +325,22 @@ QVariant CANFrameModel::headerData(int section, Qt::Orientation orientation,
|
||||
|
||||
if (orientation == Qt::Horizontal)
|
||||
{
|
||||
switch (section)
|
||||
switch (Column(section))
|
||||
{
|
||||
case 0:
|
||||
case Column::TimeStamp:
|
||||
return QString(tr("Timestamp"));
|
||||
break;
|
||||
case 1:
|
||||
case Column::FrameId:
|
||||
return QString(tr("ID"));
|
||||
break;
|
||||
case 2:
|
||||
case Column::Extended:
|
||||
return QString(tr("Ext"));
|
||||
break;
|
||||
case 3:
|
||||
case Column::Direction:
|
||||
return QString(tr("Dir"));
|
||||
break;
|
||||
case 4:
|
||||
case Column::Bus:
|
||||
return QString(tr("Bus"));
|
||||
break;
|
||||
case 5:
|
||||
case Column::Length:
|
||||
return QString(tr("Len"));
|
||||
break;
|
||||
case 6:
|
||||
case Column::Data:
|
||||
return QString(tr("Data"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,16 @@ CANConnectionModel::~CANConnectionModel()
|
||||
{
|
||||
}
|
||||
|
||||
enum class Column {
|
||||
Bus = 0, ///< A sequential number describing the bus
|
||||
Type = 1, ///< The CAN driver/backend type, e.g. GVRET, peakcan, or socketcan
|
||||
Port = 2, ///< The CAN hardware port, e.g. can0 for socketcan
|
||||
Speed = 3, ///< The bus speed in bit/second
|
||||
ListenOnly = 4, ///< True if the bus is in listen-only mode
|
||||
SingleWire = 5, ///< True if the bus operates in single-wire mode
|
||||
Active = 6, ///< True if the bus is activated for sending and receiving
|
||||
Status = 7 ///< The bus status as text message
|
||||
};
|
||||
|
||||
QVariant CANConnectionModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
@@ -19,32 +29,24 @@ QVariant CANConnectionModel::headerData(int section, Qt::Orientation orientation
|
||||
|
||||
if (orientation == Qt::Horizontal)
|
||||
{
|
||||
switch (section)
|
||||
switch (Column(section))
|
||||
{
|
||||
case 0:
|
||||
case Column::Bus:
|
||||
return QString(tr("Bus"));
|
||||
break;
|
||||
case 1:
|
||||
case Column::Type:
|
||||
return QString(tr("Type"));
|
||||
break;
|
||||
case 2:
|
||||
case Column::Port:
|
||||
return QString(tr("Port"));
|
||||
break;
|
||||
case 3:
|
||||
case Column::Speed:
|
||||
return QString(tr("Speed"));
|
||||
break;
|
||||
case 4:
|
||||
case Column::ListenOnly:
|
||||
return QString(tr("Listen Only"));
|
||||
break;
|
||||
case 5:
|
||||
case Column::SingleWire:
|
||||
return QString(tr("Single Wire"));
|
||||
break;
|
||||
case 6:
|
||||
case Column::Active:
|
||||
return QString(tr("Active"));
|
||||
break;
|
||||
case 7:
|
||||
case Column::Status:
|
||||
return QString(tr("Status"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,23 +92,19 @@ Qt::ItemFlags CANConnectionModel::flags(const QModelIndex &index) const
|
||||
bool editParams = false;
|
||||
if (conn_p->getType() == CANCon::GVRET_SERIAL) editParams = true;
|
||||
|
||||
switch (index.column())
|
||||
switch (Column(index.column()))
|
||||
{
|
||||
case 3: //speed
|
||||
case Column::Speed:
|
||||
if (editParams) return Qt::ItemFlag::ItemIsEditable | Qt::ItemFlag::ItemIsEnabled;
|
||||
else return Qt::ItemFlag::NoItemFlags;
|
||||
break;
|
||||
case 4: //listen only
|
||||
case 5: //single wire
|
||||
return Qt::ItemFlag::NoItemFlags;
|
||||
case Column::ListenOnly:
|
||||
case Column::SingleWire:
|
||||
if (editParams) return Qt::ItemFlag::ItemIsEditable | Qt::ItemFlag::ItemIsEnabled | Qt::ItemFlag::ItemIsUserCheckable;
|
||||
else return Qt::ItemFlag::NoItemFlags;
|
||||
break;
|
||||
case 6: //enabled
|
||||
return Qt::ItemFlag::NoItemFlags;
|
||||
case Column::Active:
|
||||
return Qt::ItemFlag::ItemIsEditable | Qt::ItemFlag::ItemIsEnabled | Qt::ItemFlag::ItemIsUserCheckable;
|
||||
break;
|
||||
default:
|
||||
return Qt::ItemFlag::ItemIsEnabled;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,20 +120,21 @@ bool CANConnectionModel::setData(const QModelIndex &index, const QVariant &value
|
||||
ret = conn_p->getBusSettings(busId, bus);
|
||||
if (!ret) return false;
|
||||
|
||||
switch (index.column())
|
||||
switch (Column(index.column()))
|
||||
{
|
||||
case 3: //speed
|
||||
case Column::Speed:
|
||||
bus.speed = value.toInt();
|
||||
break;
|
||||
case 4: //listen only
|
||||
case Column::ListenOnly:
|
||||
bus.listenOnly = value.toBool();
|
||||
break;
|
||||
case 5: //single wire
|
||||
case Column::SingleWire:
|
||||
bus.singleWire = value.toBool();
|
||||
break;
|
||||
case 6: //active
|
||||
case Column::Active:
|
||||
bus.active = value.toBool();
|
||||
break;
|
||||
default: {}
|
||||
}
|
||||
conn_p->setBusSettings(busId, bus);
|
||||
return true;
|
||||
@@ -157,15 +156,14 @@ QVariant CANConnectionModel::data(const QModelIndex &index, int role) const
|
||||
|
||||
//qDebug() << "ConnP: " << conn_p << " ret " << ret;
|
||||
|
||||
if (role == Qt::DisplayRole) {
|
||||
if (role == Qt::DisplayRole) {
|
||||
|
||||
switch (index.column())
|
||||
switch (Column(index.column()))
|
||||
{
|
||||
case 0: //bus
|
||||
case Column::Bus:
|
||||
//return QString::number(busId);
|
||||
return QString::number(index.row());
|
||||
break;
|
||||
case 1: //type
|
||||
case Column::Type:
|
||||
if (conn_p)
|
||||
switch (conn_p->getType()) {
|
||||
case CANCon::KVASER: return "KVASER";
|
||||
@@ -175,38 +173,35 @@ QVariant CANConnectionModel::data(const QModelIndex &index, int role) const
|
||||
}
|
||||
else qDebug() << "Tried to show connection type but connection was NULL";
|
||||
break;
|
||||
case 2: //port
|
||||
case Column::Port:
|
||||
if (conn_p) return conn_p->getPort();
|
||||
else qDebug() << "Tried to show connection port but connection was NULL";
|
||||
break;
|
||||
case 3: //speed
|
||||
case Column::Speed:
|
||||
if(!ret) return QVariant();
|
||||
if (!isSocketCAN) return QString::number(bus.speed);
|
||||
else return QString("N/A");
|
||||
case 4: //Listen Only
|
||||
case Column::ListenOnly:
|
||||
return QVariant();
|
||||
case 5: //Single Wire
|
||||
case Column::SingleWire:
|
||||
return QVariant();
|
||||
case 6: //Status
|
||||
case Column::Active:
|
||||
return QVariant();
|
||||
case 7: //Active
|
||||
case Column::Status:
|
||||
return (conn_p->getStatus()==CANCon::CONNECTED) ? "Connected" : "Not Connected";
|
||||
default: {}
|
||||
}
|
||||
}
|
||||
if (role == Qt::CheckStateRole)
|
||||
{
|
||||
switch (index.column())
|
||||
switch (Column(index.column()))
|
||||
{
|
||||
case 4:
|
||||
case Column::ListenOnly:
|
||||
return (bus.listenOnly) ? Qt::Checked : Qt::Unchecked;
|
||||
break;
|
||||
case 5:
|
||||
case Column::SingleWire:
|
||||
return (bus.singleWire) ? Qt::Checked : Qt::Unchecked;
|
||||
break;
|
||||
case 6:
|
||||
case Column::Active:
|
||||
return (bus.active) ? Qt::Checked : Qt::Unchecked;
|
||||
break;
|
||||
default: {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user