-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_preview.cpp
More file actions
executable file
·76 lines (65 loc) · 2.66 KB
/
log_preview.cpp
File metadata and controls
executable file
·76 lines (65 loc) · 2.66 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
#include "log_preview.hpp"
LogPreview::LogPreview(QString logPath){
this->logPath = logPath;
QFile file(logPath);
QString text;
if (!file.exists()) {
text = "Warning: There is no log file in current directory.";
} else {
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream stream(&file);
text = stream.readAll();
} else
text = "Warning: Current log file cannot be opened.";
}
content = new QPlainTextEdit();
content->setPlainText(text);
content->setReadOnly(true);
// Move cursor to the end
auto cursor = content->textCursor();
cursor.movePosition(QTextCursor::End);
content->setTextCursor(cursor);
copyAllButton = new QPushButton("Copy All");
QObject::connect(copyAllButton, &QPushButton::clicked, this, &LogPreview::copyAll);
saveButton = new QPushButton("Save as");
QObject::connect(saveButton, &QPushButton::clicked, this, &LogPreview::saveAs);
openButton = new QPushButton("Open in Folder");
QObject::connect(openButton, &QPushButton::clicked, this, &LogPreview::openInFolder);
doneButton = new QPushButton("Done");
QObject::connect(doneButton, &QPushButton::clicked, this, &LogPreview::done);
doneButton->setDefault(true);
auto layout = new QGridLayout();
layout->addWidget(content, 0, 0, 4, 6);
layout->addWidget(copyAllButton, 5, 0);
layout->addWidget(saveButton, 5, 1);
layout->addWidget(openButton, 5, 2);
layout->addWidget(doneButton, 5, 5);
setLayout(layout);
setMinimumSize(700, 400);
}
void LogPreview::copyAll() {
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(content->toPlainText());
}
void LogPreview::saveAs() {
QFileDialog dialog(this);
dialog.setWindowModality(Qt::WindowModal);
dialog.setAcceptMode(QFileDialog::AcceptSave);
if (dialog.exec() == QDialog::Accepted){
QFile file(dialog.selectedFiles().first());
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream stream(&file);
stream << content->toPlainText();
}
}
void LogPreview::openInFolder() {
showFileInFolder(logPath);
}
void showFileInFolder(const QString &path){
#if defined(_WIN32) || defined(_WIN64) //Code for Windows
QProcess::startDetached("explorer.exe", {"/select,", QDir::toNativeSeparators(path)});
#elif defined(__APPLE__) //Code for Mac
QProcess::execute("/usr/bin/osascript", {"-e", "tell application \"Finder\" to reveal POSIX file \"" + path + "\""});
QProcess::execute("/usr/bin/osascript", {"-e", "tell application \"Finder\" to activate"});
#endif
}