GUI: Read errors from report XML file.

This commits adds new "Open XML" item to File-menu. Selecting this
menuitem allows user to select report file to open. When the file is
read the error data is printed to debug output. Later patches will
implement adding error data back to the GUI.
This commit is contained in:
Kimmo Varis 2010-07-10 16:37:36 +03:00
parent 81449a823a
commit 5e14abf735
9 changed files with 186 additions and 7 deletions

View File

@ -73,6 +73,7 @@
<property name="title"> <property name="title">
<string>&amp;File</string> <string>&amp;File</string>
</property> </property>
<addaction name="mActionOpenXML"/>
<addaction name="mActionSave"/> <addaction name="mActionSave"/>
<addaction name="mActionQuit"/> <addaction name="mActionQuit"/>
</widget> </widget>
@ -347,6 +348,11 @@
<string>Error categories</string> <string>Error categories</string>
</property> </property>
</action> </action>
<action name="mActionOpenXML">
<property name="text">
<string>&amp;Open XML...</string>
</property>
</action>
</widget> </widget>
<customwidgets> <customwidgets>
<customwidget> <customwidget>

View File

@ -56,6 +56,7 @@ MainWindow::MainWindow() :
connect(mUI.mActionCheckDirectory, SIGNAL(triggered()), this, SLOT(CheckDirectory())); connect(mUI.mActionCheckDirectory, SIGNAL(triggered()), this, SLOT(CheckDirectory()));
connect(mUI.mActionSettings, SIGNAL(triggered()), this, SLOT(ProgramSettings())); connect(mUI.mActionSettings, SIGNAL(triggered()), this, SLOT(ProgramSettings()));
connect(mUI.mActionClearResults, SIGNAL(triggered()), this, SLOT(ClearResults())); connect(mUI.mActionClearResults, SIGNAL(triggered()), this, SLOT(ClearResults()));
connect(mUI.mActionOpenXML, SIGNAL(triggered()), this, SLOT(OpenXML()));
connect(mUI.mActionShowStyle, SIGNAL(toggled(bool)), this, SLOT(ShowStyle(bool))); connect(mUI.mActionShowStyle, SIGNAL(toggled(bool)), this, SLOT(ShowStyle(bool)));
connect(mUI.mActionShowErrors, SIGNAL(toggled(bool)), this, SLOT(ShowErrors(bool))); connect(mUI.mActionShowErrors, SIGNAL(toggled(bool)), this, SLOT(ShowErrors(bool)));
@ -431,6 +432,22 @@ void MainWindow::ClearResults()
mUI.mActionSave->setEnabled(false); mUI.mActionSave->setEnabled(false);
} }
void MainWindow::OpenXML()
{
QString selectedFilter;
QString filter(tr("XML files (*.xml)"));
QString selectedFile = QFileDialog::getOpenFileName(this,
tr("Open the report file"),
QString(),
filter,
&selectedFilter);
if (!selectedFile.isEmpty())
{
mUI.mResults->ReadErrorsXml(selectedFile);
}
}
void MainWindow::EnableCheckButtons(bool enable) void MainWindow::EnableCheckButtons(bool enable)
{ {
mUI.mActionStop->setEnabled(!enable); mUI.mActionStop->setEnabled(!enable);

View File

@ -67,6 +67,12 @@ public slots:
*/ */
void ClearResults(); void ClearResults();
/**
* @brief Slot to open XML report file
*
*/
void OpenXML();
/** /**
* @brief Show errors with type "style" * @brief Show errors with type "style"
* @param checked Should errors be shown (true) or hidden (false) * @param checked Should errors be shown (true) or hidden (false)

View File

@ -41,6 +41,17 @@ bool Report::Create()
return succeed; return succeed;
} }
bool Report::Open()
{
bool succeed = false;
if (!mFile.isOpen())
{
mFile.setFileName(mFilename);
succeed = mFile.open(QIODevice::ReadOnly | QIODevice::Text);
}
return succeed;
}
void Report::Close() void Report::Close()
{ {
if (mFile.isOpen()) if (mFile.isOpen())

View File

@ -50,6 +50,12 @@ public:
*/ */
virtual bool Create(); virtual bool Create();
/**
* @brief Open the existing report (file).
* @return true if succeeded, false if file could not be created.
*/
virtual bool Open();
/** /**
* @brief Close the report (file). * @brief Close the report (file).
*/ */

View File

@ -217,3 +217,29 @@ void ResultsView::DisableProgressbar()
{ {
mUI.mProgress->setEnabled(false); mUI.mProgress->setEnabled(false);
} }
void ResultsView::ReadErrorsXml(const QString &filename)
{
XmlReport *report = new XmlReport(filename, this);
if (report)
{
if (report->Open())
report->Read();
else
{
QMessageBox msgBox;
msgBox.setText(tr("Failed to read the report."));
msgBox.setIcon(QMessageBox::Critical);
msgBox.exec();
}
delete report;
report = NULL;
}
else
{
QMessageBox msgBox;
msgBox.setText(tr("Failed to read the report."));
msgBox.setIcon(QMessageBox::Critical);
msgBox.exec();
}
}

View File

@ -129,6 +129,14 @@ public:
void Translate(); void Translate();
void DisableProgressbar(); void DisableProgressbar();
/**
* @brief Read errors from report XML file.
* @param filename Report file to read.
*
*/
void ReadErrorsXml(const QString &filename);
signals: signals:
/** /**

View File

@ -18,16 +18,27 @@
#include <QFile> #include <QFile>
#include <QXmlStreamWriter> #include <QXmlStreamWriter>
#include <qdebug>
#include "xmlreport.h" #include "xmlreport.h"
static const char ResultElementName[] = "results";
static const char ErrorElementName[] = "error";
static const char FilenameAttribute[] = "file";
static const char LineAttribute[] = "line";
static const char IdAttribute[] = "id";
static const char SeverityAttribute[] = "severity";
static const char MsgAttribute[] = "msg";
XmlReport::XmlReport(const QString &filename, QObject * parent) : XmlReport::XmlReport(const QString &filename, QObject * parent) :
Report(filename, parent), Report(filename, parent),
mXmlReader(NULL),
mXmlWriter(NULL) mXmlWriter(NULL)
{ {
} }
XmlReport::~XmlReport() XmlReport::~XmlReport()
{ {
delete mXmlReader;
delete mXmlWriter; delete mXmlWriter;
Close(); Close();
} }
@ -43,11 +54,22 @@ bool XmlReport::Create()
return success; return success;
} }
bool XmlReport::Open()
{
bool success = false;
if (Report::Open())
{
mXmlReader = new QXmlStreamReader(Report::GetFile());
success = true;
}
return success;
}
void XmlReport::WriteHeader() void XmlReport::WriteHeader()
{ {
mXmlWriter->setAutoFormatting(true); mXmlWriter->setAutoFormatting(true);
mXmlWriter->writeStartDocument(); mXmlWriter->writeStartDocument();
mXmlWriter->writeStartElement("results"); mXmlWriter->writeStartElement(ResultElementName);
} }
void XmlReport::WriteFooter() void XmlReport::WriteFooter()
@ -65,11 +87,66 @@ void XmlReport::WriteError(const QStringList &files, const QStringList &lines,
The callstack seems to be ignored here aswell, instead last item of the stack is used The callstack seems to be ignored here aswell, instead last item of the stack is used
*/ */
mXmlWriter->writeStartElement("error"); mXmlWriter->writeStartElement(ErrorElementName);
mXmlWriter->writeAttribute("file", files[files.size() - 1]); mXmlWriter->writeAttribute(FilenameAttribute, files[files.size() - 1]);
mXmlWriter->writeAttribute("line", lines[lines.size() - 1]); mXmlWriter->writeAttribute(LineAttribute, lines[lines.size() - 1]);
mXmlWriter->writeAttribute("id", id); mXmlWriter->writeAttribute(IdAttribute, id);
mXmlWriter->writeAttribute("severity", severity); mXmlWriter->writeAttribute(SeverityAttribute, severity);
mXmlWriter->writeAttribute("msg", msg); mXmlWriter->writeAttribute(MsgAttribute, msg);
mXmlWriter->writeEndElement(); mXmlWriter->writeEndElement();
} }
void XmlReport::Read()
{
bool insideResults = false;
if (!mXmlReader)
{
qDebug() << "You must Open() the file before reading it!";
return;
}
while (!mXmlReader->atEnd())
{
switch (mXmlReader->readNext())
{
case QXmlStreamReader::StartElement:
if (mXmlReader->name() == ResultElementName)
insideResults = true;
// Read error element from inside result element
if (insideResults && mXmlReader->name() == ErrorElementName)
ReadError(mXmlReader);
break;
case QXmlStreamReader::EndElement:
if (mXmlReader->name() == ResultElementName)
insideResults = false;
break;
// Not handled
case QXmlStreamReader::NoToken:
case QXmlStreamReader::Invalid:
case QXmlStreamReader::StartDocument:
case QXmlStreamReader::EndDocument:
case QXmlStreamReader::Characters:
case QXmlStreamReader::Comment:
case QXmlStreamReader::DTD:
case QXmlStreamReader::EntityReference:
case QXmlStreamReader::ProcessingInstruction:
break;
}
}
}
void XmlReport::ReadError(QXmlStreamReader *reader)
{
if (reader->name().toString() == ErrorElementName)
{
QXmlStreamAttributes attribs = reader->attributes();
QString filename = attribs.value("", FilenameAttribute).toString();
QString line = attribs.value("", LineAttribute).toString();
QString id = attribs.value("", IdAttribute).toString();
QString severity = attribs.value("", SeverityAttribute).toString();
QString msg = attribs.value("", MsgAttribute).toString();
qDebug() << "Error: " << filename << " " << line << " " << id << " " << severity << " " << msg;
}
}

View File

@ -23,6 +23,7 @@
#include <QString> #include <QString>
#include <QStringList> #include <QStringList>
#include <QFile> #include <QFile>
#include <QXmlStreamReader>
#include <QXmlStreamWriter> #include <QXmlStreamWriter>
#include "report.h" #include "report.h"
@ -47,6 +48,11 @@ public:
*/ */
virtual bool Create(); virtual bool Create();
/**
* @brief Open existing report file.
*/
bool Open();
/** /**
* @brief Write report header. * @brief Write report header.
*/ */
@ -63,7 +69,23 @@ public:
virtual void WriteError(const QStringList &files, const QStringList &lines, virtual void WriteError(const QStringList &files, const QStringList &lines,
const QString &id, const QString &severity, const QString &msg); const QString &id, const QString &severity, const QString &msg);
/**
* @brief Read contents of the report file.
*/
void Read();
protected:
/**
* @brief Read and parse error item from XML stream.
* @param reader XML stream reader to use.
*/
void ReadError(QXmlStreamReader *reader);
private: private:
/**
* @brief XML stream reader for reading the report in XML format.
*/
QXmlStreamReader *mXmlReader;
/** /**
* @brief XML stream writer for writing the report in XML format. * @brief XML stream writer for writing the report in XML format.