forked from mpc-qt/mpc-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogwindow.cpp
80 lines (69 loc) · 1.84 KB
/
logwindow.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
#include <QClipboard>
#include <QFileDialog>
#include <QPlainTextEdit>
#include <QTextDocument>
#include "logger.h"
#include "logwindow.h"
#include "ui_logwindow.h"
LogWindow::LogWindow(QWidget *parent) :
QWidget(parent),
ui(new Ui::LogWindow)
{
ui->setupUi(this);
Logger *logger = Logger::singleton();
connect(logger, &Logger::logMessage,
this, &LogWindow::appendMessage,
Qt::QueuedConnection);
connect(logger, &Logger::logMessageBuffer,
this, &LogWindow::appendMessageBlock,
Qt::QueuedConnection);
}
LogWindow::~LogWindow()
{
delete ui;
}
void LogWindow::appendMessage(QString message)
{
ui->messages->appendPlainText(message);
}
void LogWindow::appendMessageBlock(QStringList messages)
{
ui->messages->appendPlainText(messages.join('\n'));
}
void LogWindow::setLogLimit(int lines)
{
ui->messages->setMaximumBlockCount(lines);
}
void LogWindow::closeEvent(QCloseEvent *event)
{
event->accept();
emit windowClosed();
}
void LogWindow::on_copy_clicked()
{
QTextDocument *doc = ui->messages->document();
qApp->clipboard()->setText(doc->toPlainText());
}
void LogWindow::on_save_clicked()
{
static QFileDialog::Options options = QFileDialog::Options();
#ifdef Q_OS_MAC
options = QFileDialog::DontUseNativeDialog;
#endif
QString textToSave = ui->messages->document()->toPlainText();
static QString lastLog;
QString file = QFileDialog::getSaveFileName(this, tr("Save File"), lastLog, "Text files (*.txt)",
nullptr, options);
if (file.isEmpty())
return;
QFile f(file);
if (!f.open(QIODevice::WriteOnly))
return;
QTextStream stream(&f);
stream.setGenerateByteOrderMark(true);
stream << textToSave;
}
void LogWindow::on_clear_clicked()
{
ui->messages->clear();
}