Changeset View
Changeset View
Standalone View
Standalone View
testclient/widgets/logwidget.cpp
- This file was added.
1 | /* AtCore Test Client | ||||
---|---|---|---|---|---|
2 | Copyright (C) <2018> | ||||
3 | Author: Chris Rizzitello - rizzitello@kde.org | ||||
4 | Patrick José Pereira <patrickjp@kde.org> | ||||
5 | | ||||
6 | This program is free software: you can redistribute it and/or modify | ||||
7 | it under the terms of the GNU General Public License as published by | ||||
8 | the Free Software Foundation, either version 3 of the License, or | ||||
9 | (at your option) any later version. | ||||
10 | | ||||
11 | This program is distributed in the hope that it will be useful, | ||||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
14 | GNU General Public License for more details. | ||||
15 | | ||||
16 | You should have received a copy of the GNU General Public License | ||||
17 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
18 | */ | ||||
19 | #include "logwidget.h" | ||||
20 | #include <QFileDialog> | ||||
21 | #include <QPushButton> | ||||
22 | #include <QStyle> | ||||
23 | #include <QTime> | ||||
24 | #include <QVBoxLayout> | ||||
25 | | ||||
26 | LogWidget::LogWidget(QTemporaryFile *tempFile, QWidget *parent) : | ||||
27 | QWidget(parent), | ||||
28 | logFile(tempFile) | ||||
29 | { | ||||
30 | textLog = new QPlainTextEdit; | ||||
31 | textLog->setReadOnly(true); | ||||
32 | textLog->setMaximumBlockCount(1000); | ||||
33 | | ||||
34 | auto newButton = new QPushButton(style()->standardIcon(QStyle::SP_DialogSaveButton), tr("Save Session Log")); | ||||
35 | connect(newButton, &QPushButton::clicked, this, &LogWidget::savePressed); | ||||
36 | | ||||
37 | auto mainLayout = new QVBoxLayout; | ||||
38 | mainLayout->addWidget(textLog); | ||||
39 | mainLayout->addWidget(newButton); | ||||
40 | | ||||
41 | setLayout(mainLayout); | ||||
42 | } | ||||
43 | | ||||
44 | QString LogWidget::getTime() | ||||
45 | { | ||||
46 | return QTime::currentTime().toString(QStringLiteral("hh:mm:ss:zzz")); | ||||
47 | } | ||||
48 | | ||||
49 | void LogWidget::appendLog(const QString &msg) | ||||
50 | { | ||||
51 | QString message(QStringLiteral("[%1] %2").arg(getTime(), msg)); | ||||
52 | textLog->appendPlainText(message); | ||||
53 | writeTempFile(message); | ||||
54 | } | ||||
55 | | ||||
56 | void LogWidget::appendRLog(const QByteArray &bmsg) | ||||
57 | { | ||||
58 | QString message(QStringLiteral("[%1]< %2").arg(getTime(), QString::fromUtf8(bmsg))); | ||||
59 | textLog->appendPlainText(message); | ||||
60 | writeTempFile(message); | ||||
61 | } | ||||
62 | | ||||
63 | void LogWidget::appendSLog(const QByteArray &bmsg) | ||||
64 | { | ||||
65 | QString msg = QString::fromUtf8(bmsg); | ||||
66 | QRegExp _newLine(QChar::fromLatin1('\n')); | ||||
67 | QRegExp _return(QChar::fromLatin1('\r')); | ||||
68 | msg.replace(_newLine, QStringLiteral("\\n")); | ||||
69 | msg.replace(_return, QStringLiteral("\\r")); | ||||
70 | QString message(QStringLiteral("[%1]> %2").arg(getTime(), msg)); | ||||
71 | textLog->appendPlainText(message); | ||||
72 | writeTempFile(message); | ||||
73 | } | ||||
74 | | ||||
75 | void LogWidget::writeTempFile(QString text) | ||||
76 | { | ||||
77 | /* | ||||
78 | A QTemporaryFile will always be opened in QIODevice::ReadWrite mode, | ||||
79 | this allows easy access to the data in the file. This function will | ||||
80 | return true upon success and will set the fileName() to the unique | ||||
81 | filename used. | ||||
82 | */ | ||||
83 | logFile->open(); | ||||
84 | logFile->seek(logFile->size()); | ||||
85 | logFile->write(text.toLatin1()); | ||||
86 | logFile->putChar('\n'); | ||||
87 | logFile->close(); | ||||
88 | } | ||||
89 | | ||||
90 | void LogWidget::savePressed() | ||||
91 | { | ||||
92 | // Note that if a file with the name newName already exists, copy() returns false (i.e. QFile will not overwrite it). | ||||
93 | QString fileName = QDir::homePath() + QChar::fromLatin1('/') + QFileInfo(logFile->fileName()).fileName() + QStringLiteral(".txt"); | ||||
94 | QString saveFileName = QFileDialog::getSaveFileName(this, tr("Save Log to file"), fileName); | ||||
95 | QFile::copy(logFile->fileName(), saveFileName); | ||||
96 | logFile->close(); | ||||
97 | } | ||||
98 | | ||||
99 | bool LogWidget::endsWith(const QString &string) | ||||
100 | { | ||||
101 | return textLog->toPlainText().endsWith(string); | ||||
102 | } |