-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogController.cpp
More file actions
112 lines (96 loc) · 2.89 KB
/
Copy pathLogController.cpp
File metadata and controls
112 lines (96 loc) · 2.89 KB
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
#include "LogController.h"
#include <AppSettings.h>
#include <qapplication.h>
#include <qchar.h>
#include <qclipboard.h>
#include <qdatetime.h>
#include <qfont.h>
#include <qglobal.h>
#include <qnamespace.h>
#include <qobject.h>
#include <qobjectdefs.h>
#include <qplaintextedit.h>
#include <qscrollbar.h>
#include <qstring.h>
#include <qtextcursor.h>
#include <qtextformat.h>
#include <qtimer.h>
#include "iec104/iec104_log.h"
LogController::LogController(iec104_log* logQueue, QPlainTextEdit* logUI, QObject* parent) : QObject(parent),
m_logQueue(logQueue),
m_logUI(logUI)
{
QFont font = QFont("Consolas");
font.setStyleHint(QFont::Monospace);
font.setPointSize(8);
font.setFixedPitch(true);
m_logUI->setFont(font);
connect(&m_tmLogMsg, &QTimer::timeout, this, &LogController::slot_timerLogmsg);
m_tmLogMsg.start(350);
setLogState(true);
}
LogController::~LogController() = default;
void LogController::copyToClipboard()
{
QApplication::clipboard()->setText(m_logUI->toPlainText());
}
void LogController::setLogState(bool state)
{
if (state) {
m_logQueue->activateLog();
auto d = QDate::currentDate();
QString str = d.toString("yyyy.MM.dd") + QString(" Версия программы - ") + AppSettings::instance().VERSION;
m_logQueue->pushMsg(str.toStdString());
}
else
m_logQueue->deactivateLog();
}
void LogController::setLogLevel(bool state)
{
m_logQueue->setLogLevel(state ? 0 : 2);
}
void LogController::setAutoScrollState(bool state)
{
m_autoScroll = state;
}
void LogController::clear()
{
m_logUI->clear();
}
void LogController::slot_timerLogmsg()
{
static const int maxLogMsgsPerTick = 250;
if (m_logQueue->isNotEmpty()) {
int logMsgsProcessed = 0;
QTextCursor cur(m_logUI->document());
cur.movePosition(QTextCursor::End);
cur.beginEditBlock();
while (m_logQueue->isNotEmpty() && logMsgsProcessed < maxLogMsgsPerTick) {
const QString msg = QString::fromStdString(m_logQueue->pullMsg());
QTextCharFormat fmt;
if (msg.contains(QLatin1String("I104M"))) {
fmt.setForeground(Qt::darkGray);
}
else if (msg.contains(QLatin1String("COMMAND"))) {
fmt.setForeground(Qt::darkGray);
fmt.setBackground(Qt::lightGray);
}
else if (msg.contains(QLatin1Char('['))) {
fmt.setForeground(Qt::red);
}
else {
fmt.setForeground(Qt::darkGray);
}
if (!cur.atStart())
cur.insertBlock();
cur.insertText(msg, fmt);
logMsgsProcessed++;
}
cur.endEditBlock();
if (m_autoScroll) {
QScrollBar* vbar = m_logUI->verticalScrollBar();
vbar->setValue(vbar->maximum());
}
emit signal_logUpdated();
}
}