dust3d/application/sources/log_browser_dialog.cc

122 lines
3.0 KiB
C++
Raw Normal View History

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QTextBrowser>
#include <QPushButton>
#include <QFileDialog>
#include <QDir>
#include <QFile>
#include <QMessageBox>
#include <QTextStream>
#include <QCloseEvent>
#include <QKeyEvent>
#include "version.h"
#include "log_browser_dialog.h"
#include "document_window.h"
LogBrowserDialog::LogBrowserDialog(QWidget *parent) :
QDialog(parent)
{
setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
QVBoxLayout *layout = new QVBoxLayout;
setLayout(layout);
m_browser = new QTextBrowser(this);
layout->addWidget(m_browser);
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->setContentsMargins(0, 0, 0, 0);
layout->addLayout(buttonLayout);
buttonLayout->addStretch(10);
m_clearButton = new QPushButton(this);
m_clearButton->setText("Clear");
buttonLayout->addWidget(m_clearButton);
connect(m_clearButton, SIGNAL(clicked()), m_browser, SLOT(clear()));
m_saveButton = new QPushButton(this);
m_saveButton->setText("Save Output");
buttonLayout->addWidget(m_saveButton);
connect(m_saveButton, SIGNAL(clicked()), this, SLOT(save()));
resize(600, 300);
setWindowTitle(applicationTitle(tr("Debug")));
hide();
}
LogBrowserDialog::~LogBrowserDialog()
{
}
void LogBrowserDialog::outputMessage(QtMsgType type, const QString &msg, const QString &source, int line)
{
switch (type) {
case QtDebugMsg:
m_browser->append(tr("— DEBUG: %1 - %2:%3").arg(msg).arg(source).arg(QString::number(line)));
break;
case QtWarningMsg:
m_browser->append(tr("— WARNING: %1 - %2:%3").arg(msg).arg(source).arg(QString::number(line)));
break;
case QtCriticalMsg:
m_browser->append(tr("— CRITICAL: %1 - %2:%3").arg(msg).arg(source).arg(QString::number(line)));
break;
case QtInfoMsg:
m_browser->append(tr("— INFO: %1 - %2:%3").arg(msg).arg(source).arg(QString::number(line)));
break;
case QtFatalMsg:
m_browser->append(tr("— FATAL: %1 - %2:%3").arg(msg).arg(source).arg(QString::number(line)));
break;
}
}
void LogBrowserDialog::save()
{
QString saveFileName = QFileDialog::getSaveFileName(this,
tr("Save Log Output"),
tr("%1/logfile.txt").arg(QDir::homePath()),
tr("Text Files (*.txt);;All Files (*)"));
if (saveFileName.isEmpty())
return;
QFile file(saveFileName);
if (!file.open(QIODevice::WriteOnly)) {
QMessageBox::warning(this,
tr("Error"),
QString(tr("<nobr>File '%1'<br/>cannot be opened for writing.<br/><br/>"
"The log output could <b>not</b> be saved!</nobr>"))
.arg(saveFileName));
return;
}
QTextStream stream(&file);
stream << m_browser->toPlainText();
file.close();
}
void LogBrowserDialog::closeEvent(QCloseEvent *e)
{
if (0 == DocumentWindow::total()) {
e->accept();
return;
}
e->ignore();
hide();
}
void LogBrowserDialog::keyPressEvent(QKeyEvent *e)
{
e->ignore();
}