File indexing completed on 2024-09-15 04:57:33
0001 #include <common_widgets/logpanel.h> 0002 0003 #include "ui_logpanel.h" 0004 0005 #include <QFileDialog> 0006 #include <QTextStream> 0007 0008 ///////////////////////////////////// 0009 // 0010 // LogPanel code 0011 // 0012 ///////////////////////////////////// 0013 LogPanel::LogPanel(QWidget* parent) : QWidget(parent), ui(new Ui::LogPanel) 0014 { 0015 ui->setupUi(this); 0016 ui->m_eraseBtn->setDefaultAction(ui->m_eraseAllAct); 0017 ui->m_saveBtn->setDefaultAction(ui->m_saveAct); 0018 0019 ui->m_eraseAllAct->setIcon(style()->standardIcon(QStyle::SP_TrashIcon)); 0020 ui->m_saveAct->setIcon(QIcon::fromTheme("document-save", QIcon::fromTheme("save"))); 0021 } 0022 0023 LogPanel::~LogPanel() 0024 { 0025 delete ui; 0026 } 0027 0028 void LogPanel::setController(LogController* controller) 0029 { 0030 m_controller= controller; 0031 0032 connect(ui->m_logLevel, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, [this]() { 0033 auto logLevel= static_cast<LogController::LogLevel>(ui->m_logLevel->currentIndex()); 0034 m_controller->setLogLevel(logLevel); 0035 }); 0036 0037 connect(m_controller, &LogController::showMessage, this, &LogPanel::showMessage); 0038 connect(m_controller, &LogController::logLevelChanged, ui->m_logLevel, 0039 [this]() { ui->m_logLevel->setCurrentIndex(m_controller->logLevel()); }); 0040 0041 if(m_controller) 0042 ui->m_logLevel->setCurrentIndex(m_controller->logLevel()); 0043 } 0044 0045 void LogPanel::showMessage(QString msg, LogController::LogLevel level) 0046 { 0047 static bool alternance= false; 0048 0049 if(level > m_controller->logLevel()) 0050 return; 0051 0052 QColor color; 0053 alternance= !alternance; 0054 0055 if(alternance) 0056 color= Qt::darkBlue; 0057 else 0058 color= Qt::darkRed; 0059 0060 if(level == LogController::Error) 0061 { 0062 color= Qt::red; 0063 } 0064 if(level == LogController::Warning) 0065 { 0066 color= Qt::darkRed; 0067 } 0068 ui->m_logview->setTextColor(color); 0069 ui->m_logview->append(msg); 0070 } 0071 0072 void LogPanel::initSetting() 0073 { 0074 if(m_controller == nullptr) 0075 return; 0076 0077 // ui->m_logLevel->setCurrentIndex(m_prefManager->value("LogController_LogLevel", 0).toInt()); 0078 auto logLevel= static_cast<LogController::LogLevel>(ui->m_logLevel->currentIndex()); 0079 m_controller->setLogLevel(logLevel); 0080 } 0081 0082 void LogPanel::saveLog() 0083 { 0084 auto filePath 0085 = QFileDialog::getSaveFileName(this, tr("Saving logs"), QDir::homePath(), tr("Log files %1").arg("(*.rlog)")); 0086 QFile file(filePath); 0087 if(file.open(QFile::WriteOnly)) 0088 { 0089 QTextStream in(&file); 0090 in << ui->m_logview->toPlainText(); 0091 } 0092 }