-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConsoleWindow.cpp
156 lines (137 loc) · 4.52 KB
/
ConsoleWindow.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* ConsoleWindow.cpp
*
* (c) 2016 Dame Diongue -- baydamd(@)gmail(.)com
*
* This program 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
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ConsoleWindow.h"
#include <QtWidgets>
namespace mmp {
ConsoleWindow* ConsoleWindow::instance = NULL;
ConsoleWindow::ConsoleWindow(QWidget *parent) : QMainWindow(parent)
{
// Set Fixed size
resize(CONSOLE_WINDOW_DEFAULT_WIDTH, CONSOLE_WINDOW_DEFAULT_HEIGHT);
setMinimumSize(CONSOLE_WINDOW_DEFAULT_WIDTH, CONSOLE_WINDOW_DEFAULT_HEIGHT);
// Create console
_console = new QPlainTextEdit(this);
// Make read-only but allow copy of text
_console->setReadOnly(true);
// Create and customize font
int id = QFontDatabase::addApplicationFont(":/console-font");
QString family = QFontDatabase::applicationFontFamilies(id).at(0);
QFont font(QFont(family, 10, QFont::Normal));
_console->setFont(font);
// Set color scheme
QPalette scheme = palette();
scheme.setColor(QPalette::Base, Qt::black);
scheme.setColor(QPalette::Text, Qt::white);
_console->setPalette(scheme);
// Create view elements
createActions();
createMenu();
// Set window title
setWindowTitle(tr("Message Log Output - Mapmap"));
// Set window icon
setWindowIcon(QIcon(":/mapmap-logo"));
// Set main widget
setCentralWidget(_console);
}
ConsoleWindow *ConsoleWindow::console()
{
if (!instance)
instance = new ConsoleWindow;
return instance;
}
void ConsoleWindow::createActions()
{
// Quit
quitAction = new QAction(tr("&Close"), this);
quitAction->setShortcut(QKeySequence::Close);
quitAction->setStatusTip(tr("Close the console"));
connect(quitAction, SIGNAL(triggered(bool)), this, SLOT(close()));
}
void ConsoleWindow::createMenu()
{
// File menu
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addSeparator();
fileMenu->addAction(quitAction);
}
void ConsoleWindow::writeLogFile(const QString &message)
{
QString logFilePath = QDir(QDir::tempPath()).filePath("mapmap.log");
QFile logFile(logFilePath);
logFile.open(QIODevice::Append);
QTextStream stream(&logFile);
stream << message << endl;
}
void ConsoleWindow::printMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
// Message
QByteArray message = msg.toLocal8Bit();
// Context
QString contexts(QStringLiteral("%1:%2").arg(context.file).arg(context.line)),
// Date and time
time(QDateTime::currentDateTime().toString(tr("MMM dd yy HH:mm"))),
debug = "<strong style=\"color: #00FF00;\">Debug:</strong>",
info = "<strong style=\"color: #1E90FF;\">Info:</strong>",
warning = "<strong style=\"color: #FFFF00;\">Warning:</strong>",
critical = "<strong style=\"color: #FF0000;\">Critical:</strong>",
fatal = "<strong style=\"background: #FF0000;\">Fatal!</strong>";
// Output
QString output;
switch (type) {
case QtDebugMsg:
output = time + " | " + debug + " " + QString(message.constData()) + " - <strong>" + contexts + "</strong>";
break;
#if QT_VERSION >= 0x050500
case QtInfoMsg:
output = time + " | " + info + " " + QString(message.constData()) + " - <strong>" + contexts + "</strong>";
break;
#endif
case QtWarningMsg:
output = time + " | " + warning + " " + QString(message.constData()) + " - <strong>" + contexts + "</strong>";
break;
case QtCriticalMsg:
output = time + " | " + critical + " " + QString(message.constData()) + " - <strong>" + contexts + "</strong>";
break;
case QtFatalMsg:
output = time + " | " + fatal + " " + QString(message.constData()) + " - <strong>" + contexts + "</strong>";
abort();
}
// Print in console
_console->appendHtml(output);
// Write also on log file
writeLogFile(output.remove(QRegExp("<[^>]*>")));
}
void ConsoleWindow::closeEvent(QCloseEvent *event)
{
// Send signal if the window is closed
emit windowClosed();
event->accept();
}
void ConsoleWindow::kill()
{
if (instance) {
delete instance;
instance = NULL;
}
}
ConsoleWindow::~ConsoleWindow()
{
kill();
}
}