2022-10-01 17:10:44 +02:00
# include "devicelog.h"
# include "ui_devicelog.h"
2024-11-03 12:54:16 +01:00
# include "preferences.h"
2022-10-01 17:10:44 +02:00
# include <QScrollBar>
# include <QFileDialog>
# include <fstream>
using namespace std ;
DeviceLog : : DeviceLog ( QWidget * parent ) :
QWidget ( parent ) ,
ui ( new Ui : : DeviceLog )
{
ui - > setupUi ( this ) ;
connect ( ui - > bClear , & QPushButton : : clicked , this , & DeviceLog : : clear ) ;
connect ( ui - > limitLines , & QCheckBox : : toggled , [ = ] ( bool enabled ) {
if ( enabled ) {
ui - > text - > setMaximumBlockCount ( ui - > numLines - > value ( ) ) ;
ui - > numLines - > setEnabled ( true ) ;
} else {
ui - > text - > setMaximumBlockCount ( 0 ) ;
ui - > numLines - > setEnabled ( false ) ;
}
} ) ;
ui - > text - > setMaximumBlockCount ( ui - > numLines - > value ( ) ) ;
connect ( ui - > numLines , qOverload < int > ( & QSpinBox : : valueChanged ) , [ = ] ( int lines ) {
ui - > text - > setMaximumBlockCount ( lines ) ;
} ) ;
}
DeviceLog : : ~ DeviceLog ( )
{
delete ui ;
}
void DeviceLog : : addLine ( QString line )
{
2025-03-13 11:27:04 +01:00
// Set color depending on log level.
// Use theme dependent color for info messages
QColor color = QApplication : : palette ( ) . text ( ) . color ( ) ;
// Use fixed colors for other log levels
2022-10-01 17:10:44 +02:00
if ( line . contains ( " ,CRT] " ) ) {
color = Qt : : red ;
} else if ( line . contains ( " ,ERR] " ) ) {
color = QColor ( 255 , 94 , 0 ) ;
} else if ( line . contains ( " ,WRN] " ) ) {
color = QColor ( 255 , 174 , 26 ) ;
} else if ( line . contains ( " ,DBG " ) ) {
color = Qt : : gray ;
}
QTextCharFormat tf ;
tf = ui - > text - > currentCharFormat ( ) ;
tf . setForeground ( QBrush ( color ) ) ;
ui - > text - > setCurrentCharFormat ( tf ) ;
ui - > text - > appendPlainText ( line ) ;
if ( ui - > cbAutoscroll - > isChecked ( ) ) {
QScrollBar * sb = ui - > text - > verticalScrollBar ( ) ;
sb - > setValue ( sb - > maximum ( ) ) ;
}
}
void DeviceLog : : clear ( )
{
ui - > text - > clear ( ) ;
}
void DeviceLog : : on_bToFile_clicked ( )
{
2025-06-23 16:39:18 +02:00
auto filename = QFileDialog : : getSaveFileName ( this , " Select file for device log " , Preferences : : getInstance ( ) . UISettings . Paths . packetlog , " " , nullptr , Preferences : : QFileDialogOptions ( ) ) ;
2022-10-01 17:10:44 +02:00
if ( filename . length ( ) > 0 ) {
2025-06-23 16:39:18 +02:00
Preferences : : getInstance ( ) . UISettings . Paths . packetlog = QFileInfo ( filename ) . path ( ) ;
2022-10-01 17:10:44 +02:00
// create file
ofstream file ;
file . open ( filename . toStdString ( ) ) ;
file < < ui - > text - > toPlainText ( ) . toStdString ( ) ;
file . close ( ) ;
}
}