{
Q_OBJECT
typedef QMap<int, int> IdStatsMap;
public:
QVariant headerData
(int section, Qt
::Orientation orientation,
int role
= Qt
::DisplayRole) const;
IdStatsMap idStats;
public slots:
void updateStats(const QVcaCanMsg &canmsg);
public:
MsgStatsModel
(QObject *parent
= 0);
~MsgStatsModel();
};
MsgStatsModel
::MsgStatsModel(QObject *parent
){
// std::cout <<"Model Created!"<<"\n";
}
MsgStatsModel::~MsgStatsModel()
{
// std::cout <<"Model Destroyed!"<<"\n";
}
int MsgStatsModel
::rowCount(const QModelIndex &parent
) const {
Q_UNUSED(parent);
return idStats.size();
}
int MsgStatsModel
::columnCount(const QModelIndex &parent
) const {
Q_UNUSED(parent);
return 3;
}
QVariant MsgStatsModel
::headerData(int section, Qt
::Orientation orientation,
int role
) const {
if(orientation == Qt::Horizontal){
if(role == Qt::DisplayRole){
switch(section){
case 0 :
return tr("ID");
break;
case 1:
return tr("Count");
break;
case 2 :
return tr("Comment");
break;
default :
break;
}
}
}
return ret;
}
{
if(!index.isValid()){
std::cout <<"Invalid Index of data provided!"<< std::endl;
return ret;
}
QList<int> keys = idStats.keys();
int row = index.row();
int col = index.column();
if(row >= 0 && row < rowCount()) {
if(role == Qt::DisplayRole) {
int key = keys[row];
int count = idStats.value(key);
switch(col) {
case 0 :
ret = key;
break;
case 1:
ret = count;
break;
}
}
}
return ret;
}
void MsgStatsModel::updateStats(const QVcaCanMsg &canmsg)
{
qDebug()<< "WE GET UPDATE!";
int key = canmsg.id();
int old_row_count = idStats.size();
if(idStats.contains(key)){
int count = idStats.value(key);
qDebug() <<"KEY IS: " << key<<"COUNT IS: " <<count;
idStats[key] = count+1;
} else {
beginInsertRows
(QModelIndex(), old_row_count, old_row_count
);
idStats.insert(key, 1);
endInsertRows();
}
}
class MsgStatsModel : public QAbstractTableModel
{
Q_OBJECT
typedef QMap<int, int> IdStatsMap;
public:
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
IdStatsMap idStats;
public slots:
void updateStats(const QVcaCanMsg &canmsg);
public:
MsgStatsModel(QObject *parent = 0);
~MsgStatsModel();
};
MsgStatsModel::MsgStatsModel(QObject *parent)
: QAbstractTableModel(parent)
{
// std::cout <<"Model Created!"<<"\n";
}
MsgStatsModel::~MsgStatsModel()
{
// std::cout <<"Model Destroyed!"<<"\n";
}
int MsgStatsModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return idStats.size();
}
int MsgStatsModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 3;
}
QVariant MsgStatsModel::headerData(int section, Qt::Orientation orientation, int role) const
{
QVariant ret = QAbstractTableModel::headerData(section, orientation, role);
if(orientation == Qt::Horizontal){
if(role == Qt::DisplayRole){
switch(section){
case 0 :
return tr("ID");
break;
case 1:
return tr("Count");
break;
case 2 :
return tr("Comment");
break;
default :
break;
}
}
}
return ret;
}
QVariant MsgStatsModel::data(const QModelIndex &index, int role) const
{
QVariant ret;
if(!index.isValid()){
std::cout <<"Invalid Index of data provided!"<< std::endl;
return ret;
}
QList<int> keys = idStats.keys();
int row = index.row();
int col = index.column();
if(row >= 0 && row < rowCount()) {
if(role == Qt::DisplayRole) {
int key = keys[row];
int count = idStats.value(key);
switch(col) {
case 0 :
ret = key;
break;
case 1:
ret = count;
break;
}
}
}
return ret;
}
void MsgStatsModel::updateStats(const QVcaCanMsg &canmsg)
{
qDebug()<< "WE GET UPDATE!";
int key = canmsg.id();
int old_row_count = idStats.size();
if(idStats.contains(key)){
int count = idStats.value(key);
qDebug() <<"KEY IS: " << key<<"COUNT IS: " <<count;
idStats[key] = count+1;
} else {
beginInsertRows(QModelIndex(), old_row_count, old_row_count);
idStats.insert(key, 1);
endInsertRows();
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks