Added print and print preview functionality

Added Print... and Print Preview... actions to main window
Added PrintableReport responsible for formatting of ErrorItems and
exposing of a QTextDocument that can be used for printing
This commit is contained in:
Blubbz0r 2015-04-17 16:33:52 +02:00
parent 8d8fffb20f
commit ecf04c90e4
8 changed files with 234 additions and 3 deletions

View File

@ -6,9 +6,9 @@ DEPENDPATH += . \
INCLUDEPATH += . \
../lib
# In Qt 5 widgets are in separate module
greaterThan(QT_MAJOR_VERSION, 4) {
QT += widgets
QT += widgets # In Qt 5 widgets are in separate module
QT += printsupport # In Qt 5 QPrinter/QPrintDialog are in separate module
}
contains(LINKCORE, [yY][eE][sS]) {
@ -88,6 +88,7 @@ HEADERS += aboutdialog.h \
logview.h \
mainwindow.h \
platforms.h \
printablereport.h \
project.h \
projectfile.h \
projectfiledialog.h \
@ -121,6 +122,7 @@ SOURCES += aboutdialog.cpp \
main.cpp \
mainwindow.cpp\
platforms.cpp \
printablereport.cpp \
project.cpp \
projectfile.cpp \
projectfiledialog.cpp \

View File

@ -62,7 +62,7 @@
<x>0</x>
<y>0</y>
<width>640</width>
<height>25</height>
<height>21</height>
</rect>
</property>
<widget class="QMenu" name="mMenuFile">
@ -78,6 +78,10 @@
<addaction name="separator"/>
<addaction name="mActionProjectMRU"/>
<addaction name="mActionSave"/>
<addaction name="separator"/>
<addaction name="mActionPrintPreview"/>
<addaction name="mActionPrint"/>
<addaction name="separator"/>
<addaction name="mActionQuit"/>
</widget>
<widget class="QMenu" name="mMenuView">
@ -682,6 +686,22 @@
<string>C++03</string>
</property>
</action>
<action name="mActionPrint">
<property name="text">
<string>&amp;Print...</string>
</property>
<property name="toolTip">
<string>Print the Current Report</string>
</property>
</action>
<action name="mActionPrintPreview">
<property name="text">
<string>Print Pre&amp;view...</string>
</property>
<property name="toolTip">
<string>Open a Print Preview Dialog for the Current Results</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>

View File

@ -77,6 +77,8 @@ MainWindow::MainWindow(TranslationHandler* th, QSettings* settings) :
connect(mLineEditFilter, SIGNAL(textChanged(const QString&)), mFilterTimer, SLOT(start()));
connect(mLineEditFilter, SIGNAL(returnPressed()), this, SLOT(FilterResults()));
connect(mUI.mActionPrint, SIGNAL(triggered()), this, SLOT(PrintReport()));
connect(mUI.mActionPrintPreview, SIGNAL(triggered()), this, SLOT(PrintPreview()));
connect(mUI.mActionQuit, SIGNAL(triggered()), this, SLOT(close()));
connect(mUI.mActionCheckFiles, SIGNAL(triggered()), this, SLOT(CheckFiles()));
connect(mUI.mActionCheckDirectory, SIGNAL(triggered()), this, SLOT(CheckDirectory()));
@ -134,6 +136,9 @@ MainWindow::MainWindow(TranslationHandler* th, QSettings* settings) :
EnableCheckButtons(true);
mUI.mActionPrint->setShortcut(QKeySequence::Print);
mUI.mActionPrint->setEnabled(false);
mUI.mActionPrintPreview->setEnabled(false);
mUI.mActionClearResults->setEnabled(false);
mUI.mActionSave->setEnabled(false);
mUI.mActionRecheck->setEnabled(false);
@ -434,6 +439,16 @@ QStringList MainWindow::SelectFilesToCheck(QFileDialog::FileMode mode)
return selected;
}
void MainWindow::PrintReport()
{
mUI.mResults->Print();
}
void MainWindow::PrintPreview()
{
mUI.mResults->PrintPreview();
}
void MainWindow::CheckFiles()
{
DoCheckFiles(SelectFilesToCheck(QFileDialog::ExistingFiles));
@ -695,6 +710,8 @@ void MainWindow::CheckDone()
if (mUI.mResults->HasResults()) {
mUI.mActionClearResults->setEnabled(true);
mUI.mActionSave->setEnabled(true);
mUI.mActionPrint->setEnabled(true);
mUI.mActionPrintPreview->setEnabled(true);
}
for (int i = 0; i < MaxRecentProjects + 1; i++) {
@ -768,6 +785,8 @@ void MainWindow::ClearResults()
mUI.mResults->Clear(true);
mUI.mActionClearResults->setEnabled(false);
mUI.mActionSave->setEnabled(false);
mUI.mActionPrint->setEnabled(false);
mUI.mActionPrintPreview->setEnabled(false);
}
void MainWindow::OpenResults()

View File

@ -72,6 +72,18 @@ public:
public slots:
/**
* @brief Slot opening a print dialog to print the current report
*
*/
void PrintReport();
/**
* @brief Slot opening a print preview dialogPrintPreview
*
*/
void PrintPreview();
/**
* @brief Slot for check files menu item
*

63
gui/printablereport.cpp Normal file
View File

@ -0,0 +1,63 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2015 Daniel Marjamäki and Cppcheck team.
*
* 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 "printablereport.h"
#include <QDir>
#include <QTextDocument>
PrintableReport::PrintableReport() :
Report(QString()),
mReportDocument(new QTextDocument(this))
{
}
PrintableReport::~PrintableReport()
{
}
bool PrintableReport::Create()
{
return true;
}
void PrintableReport::WriteHeader()
{
// No header for printable report
}
void PrintableReport::WriteFooter()
{
// No footer for printable report
}
void PrintableReport::WriteError(const ErrorItem &error)
{
const QString file = QDir::toNativeSeparators(error.files[error.files.size() - 1]);
QString line = QString("%1,%2,").arg(file).arg(error.lines[error.lines.size() - 1]);
line += QString("%1,%2").arg(GuiSeverity::toString(error.severity)).arg(error.summary);
mFormattedReport += line;
mFormattedReport += "\n";
}
QTextDocument* PrintableReport::GetReport() const
{
mReportDocument->setPlainText(mFormattedReport);
return mReportDocument;
}

71
gui/printablereport.h Normal file
View File

@ -0,0 +1,71 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2015 Daniel Marjamäki and Cppcheck team.
*
* 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/>.
*/
#ifndef PRINTABLE_REPORT_H
#define PRINTABLE_REPORT_H
#include "report.h"
class QTextDocument;
/// @addtogroup GUI
/// @{
/**
* @brief CSV text file report.
* This report exports results as CSV (comma separated values). CSV files are
* easy to import to many other programs.
* @todo This class should be inherited from TxtReport?
*/
class PrintableReport : public Report {
public:
PrintableReport();
virtual ~PrintableReport();
/**
* @brief Create the report (file).
* @return true if succeeded, false if file could not be created.
*/
virtual bool Create();
/**
* @brief Write report header.
*/
virtual void WriteHeader();
/**
* @brief Write report footer.
*/
virtual void WriteFooter();
/**
* @brief Write error to report.
* @param error Error data.
*/
virtual void WriteError(const ErrorItem &error);
QTextDocument* GetReport() const;
private:
QString mFormattedReport;
QTextDocument* mReportDocument;
};
/// @}
#endif // PRINTABLE_REPORT_H

View File

@ -22,6 +22,9 @@
#include <QVariant>
#include <QString>
#include <QModelIndex>
#include <QPrinter>
#include <QPrintDialog>
#include <QPrintPreviewDialog>
#include <QSettings>
#include "common.h"
#include "erroritem.h"
@ -32,6 +35,7 @@
#include "xmlreportv1.h"
#include "xmlreportv2.h"
#include "csvreport.h"
#include "printablereport.h"
#include "applicationlist.h"
#include "checkstatistics.h"
@ -178,6 +182,40 @@ void ResultsView::Save(const QString &filename, Report::Type type) const
}
}
void ResultsView::Print()
{
QPrinter printer;
QPrintDialog dialog(&printer, this);
dialog.setWindowTitle(tr("Print Report"));
if (dialog.exec() != QDialog::Accepted)
return;
Print(&printer);
}
void ResultsView::PrintPreview()
{
QPrinter printer;
QPrintPreviewDialog dialog(&printer, this);
connect(&dialog, SIGNAL(paintRequested(QPrinter*)), SLOT(Print(QPrinter*)));
dialog.exec();
}
void ResultsView::Print(QPrinter* printer)
{
if (!mErrorsFound) {
QMessageBox msgBox;
msgBox.setText(tr("No errors found, nothing to print."));
msgBox.setIcon(QMessageBox::Critical);
msgBox.exec();
return;
}
PrintableReport report;
mUI.mTree->SaveResults(&report);
report.GetReport()->print(printer);
}
void ResultsView::UpdateSettings(bool showFullPath,
bool saveFullPath,
bool saveAllErrors,

View File

@ -29,6 +29,7 @@
class ErrorItem;
class ApplicationList;
class QModelIndex;
class QPrinter;
class QSettings;
class CheckStatistics;
@ -75,6 +76,9 @@ public:
*/
void Save(const QString &filename, Report::Type type) const;
void Print();
void PrintPreview();
/**
* @brief Update tree settings
*
@ -222,6 +226,8 @@ public slots:
*/
void UpdateDetails(const QModelIndex &index);
void Print(QPrinter* printer);
protected:
/**
* @brief Have any errors been found