Skip to content

Commit ecf04c9

Browse files
committed
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
1 parent 8d8fffb commit ecf04c9

8 files changed

Lines changed: 234 additions & 3 deletions

File tree

gui/gui.pro

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ DEPENDPATH += . \
66
INCLUDEPATH += . \
77
../lib
88

9-
# In Qt 5 widgets are in separate module
109
greaterThan(QT_MAJOR_VERSION, 4) {
11-
QT += widgets
10+
QT += widgets # In Qt 5 widgets are in separate module
11+
QT += printsupport # In Qt 5 QPrinter/QPrintDialog are in separate module
1212
}
1313

1414
contains(LINKCORE, [yY][eE][sS]) {
@@ -88,6 +88,7 @@ HEADERS += aboutdialog.h \
8888
logview.h \
8989
mainwindow.h \
9090
platforms.h \
91+
printablereport.h \
9192
project.h \
9293
projectfile.h \
9394
projectfiledialog.h \
@@ -121,6 +122,7 @@ SOURCES += aboutdialog.cpp \
121122
main.cpp \
122123
mainwindow.cpp\
123124
platforms.cpp \
125+
printablereport.cpp \
124126
project.cpp \
125127
projectfile.cpp \
126128
projectfiledialog.cpp \

gui/main.ui

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
<x>0</x>
6363
<y>0</y>
6464
<width>640</width>
65-
<height>25</height>
65+
<height>21</height>
6666
</rect>
6767
</property>
6868
<widget class="QMenu" name="mMenuFile">
@@ -78,6 +78,10 @@
7878
<addaction name="separator"/>
7979
<addaction name="mActionProjectMRU"/>
8080
<addaction name="mActionSave"/>
81+
<addaction name="separator"/>
82+
<addaction name="mActionPrintPreview"/>
83+
<addaction name="mActionPrint"/>
84+
<addaction name="separator"/>
8185
<addaction name="mActionQuit"/>
8286
</widget>
8387
<widget class="QMenu" name="mMenuView">
@@ -682,6 +686,22 @@
682686
<string>C++03</string>
683687
</property>
684688
</action>
689+
<action name="mActionPrint">
690+
<property name="text">
691+
<string>&amp;Print...</string>
692+
</property>
693+
<property name="toolTip">
694+
<string>Print the Current Report</string>
695+
</property>
696+
</action>
697+
<action name="mActionPrintPreview">
698+
<property name="text">
699+
<string>Print Pre&amp;view...</string>
700+
</property>
701+
<property name="toolTip">
702+
<string>Open a Print Preview Dialog for the Current Results</string>
703+
</property>
704+
</action>
685705
</widget>
686706
<customwidgets>
687707
<customwidget>

gui/mainwindow.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ MainWindow::MainWindow(TranslationHandler* th, QSettings* settings) :
7777
connect(mLineEditFilter, SIGNAL(textChanged(const QString&)), mFilterTimer, SLOT(start()));
7878
connect(mLineEditFilter, SIGNAL(returnPressed()), this, SLOT(FilterResults()));
7979

80+
connect(mUI.mActionPrint, SIGNAL(triggered()), this, SLOT(PrintReport()));
81+
connect(mUI.mActionPrintPreview, SIGNAL(triggered()), this, SLOT(PrintPreview()));
8082
connect(mUI.mActionQuit, SIGNAL(triggered()), this, SLOT(close()));
8183
connect(mUI.mActionCheckFiles, SIGNAL(triggered()), this, SLOT(CheckFiles()));
8284
connect(mUI.mActionCheckDirectory, SIGNAL(triggered()), this, SLOT(CheckDirectory()));
@@ -134,6 +136,9 @@ MainWindow::MainWindow(TranslationHandler* th, QSettings* settings) :
134136

135137
EnableCheckButtons(true);
136138

139+
mUI.mActionPrint->setShortcut(QKeySequence::Print);
140+
mUI.mActionPrint->setEnabled(false);
141+
mUI.mActionPrintPreview->setEnabled(false);
137142
mUI.mActionClearResults->setEnabled(false);
138143
mUI.mActionSave->setEnabled(false);
139144
mUI.mActionRecheck->setEnabled(false);
@@ -434,6 +439,16 @@ QStringList MainWindow::SelectFilesToCheck(QFileDialog::FileMode mode)
434439
return selected;
435440
}
436441

442+
void MainWindow::PrintReport()
443+
{
444+
mUI.mResults->Print();
445+
}
446+
447+
void MainWindow::PrintPreview()
448+
{
449+
mUI.mResults->PrintPreview();
450+
}
451+
437452
void MainWindow::CheckFiles()
438453
{
439454
DoCheckFiles(SelectFilesToCheck(QFileDialog::ExistingFiles));
@@ -695,6 +710,8 @@ void MainWindow::CheckDone()
695710
if (mUI.mResults->HasResults()) {
696711
mUI.mActionClearResults->setEnabled(true);
697712
mUI.mActionSave->setEnabled(true);
713+
mUI.mActionPrint->setEnabled(true);
714+
mUI.mActionPrintPreview->setEnabled(true);
698715
}
699716

700717
for (int i = 0; i < MaxRecentProjects + 1; i++) {
@@ -768,6 +785,8 @@ void MainWindow::ClearResults()
768785
mUI.mResults->Clear(true);
769786
mUI.mActionClearResults->setEnabled(false);
770787
mUI.mActionSave->setEnabled(false);
788+
mUI.mActionPrint->setEnabled(false);
789+
mUI.mActionPrintPreview->setEnabled(false);
771790
}
772791

773792
void MainWindow::OpenResults()

gui/mainwindow.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ class MainWindow : public QMainWindow {
7272

7373
public slots:
7474

75+
/**
76+
* @brief Slot opening a print dialog to print the current report
77+
*
78+
*/
79+
void PrintReport();
80+
81+
/**
82+
* @brief Slot opening a print preview dialogPrintPreview
83+
*
84+
*/
85+
void PrintPreview();
86+
7587
/**
7688
* @brief Slot for check files menu item
7789
*

gui/printablereport.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2015 Daniel Marjamäki and Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include "printablereport.h"
20+
#include <QDir>
21+
#include <QTextDocument>
22+
23+
PrintableReport::PrintableReport() :
24+
Report(QString()),
25+
mReportDocument(new QTextDocument(this))
26+
{
27+
}
28+
29+
PrintableReport::~PrintableReport()
30+
{
31+
}
32+
33+
bool PrintableReport::Create()
34+
{
35+
return true;
36+
}
37+
38+
void PrintableReport::WriteHeader()
39+
{
40+
// No header for printable report
41+
}
42+
43+
void PrintableReport::WriteFooter()
44+
{
45+
// No footer for printable report
46+
}
47+
48+
void PrintableReport::WriteError(const ErrorItem &error)
49+
{
50+
const QString file = QDir::toNativeSeparators(error.files[error.files.size() - 1]);
51+
QString line = QString("%1,%2,").arg(file).arg(error.lines[error.lines.size() - 1]);
52+
line += QString("%1,%2").arg(GuiSeverity::toString(error.severity)).arg(error.summary);
53+
54+
mFormattedReport += line;
55+
mFormattedReport += "\n";
56+
}
57+
58+
QTextDocument* PrintableReport::GetReport() const
59+
{
60+
mReportDocument->setPlainText(mFormattedReport);
61+
return mReportDocument;
62+
}
63+

gui/printablereport.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2015 Daniel Marjamäki and Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#ifndef PRINTABLE_REPORT_H
20+
#define PRINTABLE_REPORT_H
21+
22+
#include "report.h"
23+
24+
class QTextDocument;
25+
26+
/// @addtogroup GUI
27+
/// @{
28+
29+
30+
/**
31+
* @brief CSV text file report.
32+
* This report exports results as CSV (comma separated values). CSV files are
33+
* easy to import to many other programs.
34+
* @todo This class should be inherited from TxtReport?
35+
*/
36+
class PrintableReport : public Report {
37+
public:
38+
PrintableReport();
39+
virtual ~PrintableReport();
40+
41+
/**
42+
* @brief Create the report (file).
43+
* @return true if succeeded, false if file could not be created.
44+
*/
45+
virtual bool Create();
46+
47+
/**
48+
* @brief Write report header.
49+
*/
50+
virtual void WriteHeader();
51+
52+
/**
53+
* @brief Write report footer.
54+
*/
55+
virtual void WriteFooter();
56+
57+
/**
58+
* @brief Write error to report.
59+
* @param error Error data.
60+
*/
61+
virtual void WriteError(const ErrorItem &error);
62+
63+
QTextDocument* GetReport() const;
64+
65+
private:
66+
67+
QString mFormattedReport;
68+
QTextDocument* mReportDocument;
69+
};
70+
/// @}
71+
#endif // PRINTABLE_REPORT_H

gui/resultsview.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#include <QVariant>
2323
#include <QString>
2424
#include <QModelIndex>
25+
#include <QPrinter>
26+
#include <QPrintDialog>
27+
#include <QPrintPreviewDialog>
2528
#include <QSettings>
2629
#include "common.h"
2730
#include "erroritem.h"
@@ -32,6 +35,7 @@
3235
#include "xmlreportv1.h"
3336
#include "xmlreportv2.h"
3437
#include "csvreport.h"
38+
#include "printablereport.h"
3539
#include "applicationlist.h"
3640
#include "checkstatistics.h"
3741

@@ -178,6 +182,40 @@ void ResultsView::Save(const QString &filename, Report::Type type) const
178182
}
179183
}
180184

185+
void ResultsView::Print()
186+
{
187+
QPrinter printer;
188+
QPrintDialog dialog(&printer, this);
189+
dialog.setWindowTitle(tr("Print Report"));
190+
if (dialog.exec() != QDialog::Accepted)
191+
return;
192+
193+
Print(&printer);
194+
}
195+
196+
void ResultsView::PrintPreview()
197+
{
198+
QPrinter printer;
199+
QPrintPreviewDialog dialog(&printer, this);
200+
connect(&dialog, SIGNAL(paintRequested(QPrinter*)), SLOT(Print(QPrinter*)));
201+
dialog.exec();
202+
}
203+
204+
void ResultsView::Print(QPrinter* printer)
205+
{
206+
if (!mErrorsFound) {
207+
QMessageBox msgBox;
208+
msgBox.setText(tr("No errors found, nothing to print."));
209+
msgBox.setIcon(QMessageBox::Critical);
210+
msgBox.exec();
211+
return;
212+
}
213+
214+
PrintableReport report;
215+
mUI.mTree->SaveResults(&report);
216+
report.GetReport()->print(printer);
217+
}
218+
181219
void ResultsView::UpdateSettings(bool showFullPath,
182220
bool saveFullPath,
183221
bool saveAllErrors,

gui/resultsview.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
class ErrorItem;
3030
class ApplicationList;
3131
class QModelIndex;
32+
class QPrinter;
3233
class QSettings;
3334
class CheckStatistics;
3435

@@ -75,6 +76,9 @@ class ResultsView : public QWidget {
7576
*/
7677
void Save(const QString &filename, Report::Type type) const;
7778

79+
void Print();
80+
void PrintPreview();
81+
7882
/**
7983
* @brief Update tree settings
8084
*
@@ -222,6 +226,8 @@ public slots:
222226
*/
223227
void UpdateDetails(const QModelIndex &index);
224228

229+
void Print(QPrinter* printer);
230+
225231
protected:
226232
/**
227233
* @brief Have any errors been found

0 commit comments

Comments
 (0)