@ -4,6 +4,8 @@
# include <QHeaderView>
# include <QLineEdit>
# include <QVBoxLayout>
# include <QPainter>
# include <QApplication>
# include "tools/cabana/dbcmanager.h"
@ -21,6 +23,7 @@ MessagesWidget::MessagesWidget(QWidget *parent) : QWidget(parent) {
table_widget = new QTableView ( this ) ;
model = new MessageListModel ( this ) ;
table_widget - > setModel ( model ) ;
table_widget - > setItemDelegateForColumn ( 4 , new MessageBytesDelegate ( table_widget ) ) ;
table_widget - > setSelectionBehavior ( QAbstractItemView : : SelectRows ) ;
table_widget - > setSelectionMode ( QAbstractItemView : : SingleSelection ) ;
table_widget - > setSizePolicy ( QSizePolicy : : Preferred , QSizePolicy : : Expanding ) ;
@ -31,6 +34,7 @@ MessagesWidget::MessagesWidget(QWidget *parent) : QWidget(parent) {
table_widget - > setColumnWidth ( 2 , 80 ) ;
table_widget - > horizontalHeader ( ) - > setStretchLastSection ( true ) ;
table_widget - > verticalHeader ( ) - > hide ( ) ;
main_layout - > addWidget ( table_widget ) ;
// signals/slots
@ -65,9 +69,10 @@ QVariant MessageListModel::headerData(int section, Qt::Orientation orientation,
}
QVariant MessageListModel : : data ( const QModelIndex & index , int role ) const {
const auto & id = msgs [ index . row ( ) ] ;
auto & can_data = can - > lastMessage ( id ) ;
if ( role = = Qt : : DisplayRole ) {
const auto & id = msgs [ index . row ( ) ] ;
auto & can_data = can - > lastMessage ( id ) ;
switch ( index . column ( ) ) {
case 0 : return msgName ( id ) ;
case 1 : return id ;
@ -77,6 +82,13 @@ QVariant MessageListModel::data(const QModelIndex &index, int role) const {
}
} else if ( role = = Qt : : FontRole & & index . column ( ) = = columnCount ( ) - 1 ) {
return QFontDatabase : : systemFont ( QFontDatabase : : FixedFont ) ;
} else if ( role = = Qt : : UserRole & & index . column ( ) = = 4 ) {
QList < QVariant > colors ;
for ( int i = 0 ; i < can_data . dat . size ( ) ; i + + ) {
colors . append ( can_data . colors [ i ] ) ;
}
return colors ;
}
return { } ;
}
@ -146,3 +158,40 @@ void MessageListModel::sort(int column, Qt::SortOrder order) {
sortMessages ( ) ;
}
}
MessageBytesDelegate : : MessageBytesDelegate ( QObject * parent ) : QStyledItemDelegate ( parent ) {
}
void MessageBytesDelegate : : paint ( QPainter * painter , const QStyleOptionViewItem & option , const QModelIndex & index ) const {
QList < QVariant > colors = index . data ( Qt : : UserRole ) . toList ( ) ;
QStyleOptionViewItemV4 opt = option ;
initStyleOption ( & opt , index ) ;
const QFont font = index . data ( Qt : : FontRole ) . value < QFont > ( ) ;
painter - > setFont ( font ) ;
QRect rect = opt . rect ;
QString bytes = QString ( opt . text ) ;
QRect pos = rect ;
QRect space = painter - > boundingRect ( pos , opt . displayAlignment , " " ) ;
pos . setX ( pos . x ( ) + space . width ( ) ) ;
if ( ( option . state & QStyle : : State_Selected ) & & ( option . state & QStyle : : State_Active ) ) {
painter - > setPen ( option . palette . color ( QPalette : : HighlightedText ) ) ;
} else {
painter - > setPen ( option . palette . color ( QPalette : : Text ) ) ;
}
int i = 0 ;
for ( auto & byte : bytes . split ( " " ) ) {
QRect sz = painter - > boundingRect ( pos , opt . displayAlignment , byte ) ;
const int m = space . width ( ) / 2 ;
painter - > fillRect ( sz . marginsAdded ( QMargins ( m + 1 , m , m , m ) ) , colors [ i ] . value < QColor > ( ) ) ;
painter - > drawText ( pos , opt . displayAlignment , byte ) ;
pos . setX ( pos . x ( ) + sz . width ( ) + space . width ( ) ) ;
i + + ;
}
}