YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
messageWidget.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6#include "messageWidget.h"
7#include <QFileDialog>
8#include <QCoreApplication>
9#include <QDateTime>
10
12 QListWidget(parent),
13 contextMenu(nullptr)
14{
15 //contextMenu = new QMenu(this);
16 clearLogAction = new QAction("Clear Log",this);
17 saveLogAction = new QAction("Save Log",this);
18
19 //clearLogAction->setIcon(QIcon(":/images/clear.png"));
20 //saveLogAction->setIcon(QIcon(":/images/Save-icon.png"));
21
22 this->addAction(clearLogAction);
23 this->addAction(saveLogAction);
24
25 connect(clearLogAction,SIGNAL(triggered()),this,SLOT(onClearLog()));
26 connect(saveLogAction,SIGNAL(triggered()),this,SLOT(onSaveLog()));
27}
28
29void MessageWidget::onClearLog()
30{
31 clear();
32}
33
34void MessageWidget::onSaveLog()
35{
36 QString logFileName = QFileDialog::getSaveFileName(this,"Save the Log",QDir::homePath());
37
38 QFile f(logFileName);
39 f.open(QIODevice::WriteOnly);
40
41 for(int i=0; i<count(); i++){
42 QString line = item(i)->text() + "\n";
43 f.write(line.toLatin1().data());
44 }
45
46 f.flush();
47 f.close();
48
49}
50
51void MessageWidget::addMessage (QString text, int level)
52{
53 QString dateformat = " [dd/MM/yyyy hh:mm:ss] ";
54 QDateTime currDate = QDateTime::currentDateTime();
55 QString date_s = currDate.toString ( dateformat );
56
57 if (level == 0)
58 {
59 QString message_to_add = QString ("[INFO] ") + date_s + text;
60 addItem(message_to_add);
61 setCurrentRow(this->count() - 1);
62 }
63 else if (level == 1)
64 {
65 QString message_to_add = QString ("[WAR] ") + date_s + text;
66 addItem(message_to_add);
67 item(this->count() - 1)->setBackground(QColor("#FFF6C8"));
68 setCurrentRow(this->count() - 1);
69 }
70 else if (level == 2)
71 {
72 QString message_to_add = QString ("[ERR] ") + date_s + text;
73 addItem(message_to_add);
74 item(this->count() - 1)->setBackground(QColor("#F9CCCA"));
75 setCurrentRow(this->count() - 1);
76 }
77}
int SIGNAL(int pid, int signum)
void addMessage(QString text, int level=0)
MessageWidget(QWidget *parent=0)