-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathloggerwidget.cpp
135 lines (108 loc) · 3.67 KB
/
loggerwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright (C) 2012 Jorge Aparicio <jorge.aparicio.r@gmail.com>
*
* This file is part of qSerialTerm.
*
* qSerialTerm is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
* qSerialTerm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with qSerialTerm. If not, see <http://www.gnu.org/licenses/>.
*/
#include "loggerwidget.h"
#include "ui_loggerwidget.h"
#include <QFileDialog>
#include <QMessageBox>
LoggerWidget::LoggerWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::LoggerWidget),
logFile(0),
logFileStream(0),
elapsedTime(0),
secondKeeper(0)
{
ui->setupUi(this);
}
LoggerWidget::~LoggerWidget()
{
if (logFile)
logFile->close();
delete ui;
}
void LoggerWidget::append(QByteArray const& text)
{
if (logFile)
*logFileStream << text;
}
void LoggerWidget::on_browsePushButton_clicked()
{
ui->filePathLineEdit->setText(QFileDialog::getSaveFileName(this,
QLatin1String("Save file")));
}
void LoggerWidget::on_startPushButton_clicked()
{
if (logFile) {
secondKeeper->stop();
logFile->close();
ui->timeLabel->setText(QLatin1String("<font color=red>") +
elapsedTime->toString(QLatin1String("hh:mm:ss")) +
QLatin1String("</font>"));
ui->filePathLineEdit->setEnabled(true);
delete logFile;
logFile = 0;
delete logFileStream;
logFileStream = 0;
delete elapsedTime;
elapsedTime = 0;
delete secondKeeper;
secondKeeper = 0;
ui->appendCheckBox->setEnabled(true);
ui->startPushButton->setText(QLatin1String("Start"));
} else {
bool ok;
logFile = new QFile(ui->filePathLineEdit->text());
if (ui->appendCheckBox->isChecked())
ok = logFile->open(QIODevice::WriteOnly |
QIODevice::Text |
QIODevice::Append);
else
ok = logFile->open(QIODevice::WriteOnly |
QIODevice::Text);
if (ok) {
logFileStream = new QTextStream(logFile);
elapsedTime = new QTime(0, 0, 0);
secondKeeper = new QTimer();
connect(secondKeeper, SIGNAL(timeout()),
this, SLOT(on_secondKeeper_timeout()));
ui->timeLabel->setText(QLatin1String("<font color=green>") +
elapsedTime->toString(QLatin1String("hh:mm:ss")) +
QLatin1String("</font>"));
ui->filePathLineEdit->setDisabled(true);
secondKeeper->start(1000);
ui->appendCheckBox->setDisabled(true);
ui->startPushButton->setText(QLatin1String("Stop"));
} else {
delete logFile;
logFile = 0;
QMessageBox::warning(this, QLatin1String("Couldn't open file"),
QLatin1String("qSerialTerm couldn't open the "
"requested file, check the file path."));
}
}
}
void LoggerWidget::on_secondKeeper_timeout()
{
*elapsedTime = elapsedTime->addSecs(1);
ui->timeLabel->setText(QLatin1String("<font color=green>") +
elapsedTime->toString(QLatin1String("hh:mm:ss")) +
QLatin1String("</font>"));
}
void LoggerWidget::on_filePathLineEdit_textChanged(const QString &str)
{
ui->startPushButton->setEnabled(str.length() != 0);
}