KD Reports API Documentation  2.2
KDReportsPreviewDialog.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** This file is part of the KD Reports library.
4 **
5 ** SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6 **
7 ** SPDX-License-Identifier: MIT
8 **
9 ****************************************************************************/
10 
11 #include "KDReportsPreviewDialog.h"
13 #include "KDReportsPreviewWidget.h"
14 #include "KDReportsReport.h"
15 #include "KDReportsReport_p.h"
17 
18 #include <QDialogButtonBox>
19 #include <QFileDialog>
20 #include <QInputDialog>
21 #include <QMessageBox>
22 #include <QPushButton>
23 #include <QVBoxLayout>
24 
25 class KDReports::PreviewDialogPrivate
26 {
27 public:
28  PreviewDialogPrivate(KDReports::PreviewDialog *q)
29  : q(q)
30  , m_previewWidget(nullptr)
31  , m_buttonBox(nullptr)
32  , m_quickPrintButton(nullptr)
33  , m_dirBrowsingEnabled(true)
34  {
35  }
36 
37  void slotTableBreakingDialog() const;
38  void slotPrintWithDialog() const;
39  void slotQuickPrint() const;
40  void slotSave();
41 
43  KDReports::PreviewWidget *m_previewWidget;
44  QDialogButtonBox *m_buttonBox;
45  QPushButton *m_quickPrintButton;
46  QString m_quickPrinterName;
47  QString m_defaultSaveDirectory;
48  QString m_savedFileName;
49  bool m_dirBrowsingEnabled;
50 };
51 
53  : QDialog(parent)
54  , d(new PreviewDialogPrivate(this))
55 {
56  d->m_previewWidget = new KDReports::PreviewWidget(this);
57  d->m_previewWidget->setReport(report);
58  auto *topLayout = new QVBoxLayout(this);
59  topLayout->addWidget(d->m_previewWidget);
60  auto *bottomLayout = new QHBoxLayout();
61  topLayout->addLayout(bottomLayout);
62 
63  connect(d->m_previewWidget, &KDReports::PreviewWidget::tableSettingsClicked, this, [this]() { d->slotTableBreakingDialog(); });
65 
66  d->m_buttonBox = new QDialogButtonBox(Qt::Horizontal, this);
67  bottomLayout->addWidget(d->m_buttonBox);
68 
69  auto *printWithDialogButton = new QPushButton(tr("&Print..."), this);
70  d->m_buttonBox->addButton(printWithDialogButton, QDialogButtonBox::ActionRole);
71  connect(printWithDialogButton, &QPushButton::clicked, this, [this]() { d->slotPrintWithDialog(); });
72 
73  d->m_quickPrintButton = new QPushButton(this); // create it here for the ordering
74  d->m_buttonBox->addButton(d->m_quickPrintButton, QDialogButtonBox::ActionRole);
75 
76  auto *saveButton = new QPushButton(tr("&Save..."), this);
77  d->m_buttonBox->addButton(saveButton, QDialogButtonBox::ActionRole);
78  connect(saveButton, &QPushButton::clicked, this, [this]() { d->slotSave(); });
79 
80  auto *cancelButton = new QPushButton(tr("Cancel"), this);
81  d->m_buttonBox->addButton(cancelButton, QDialogButtonBox::RejectRole);
82  connect(cancelButton, &QPushButton::clicked, this, &QDialog::reject);
83 
84  d->m_quickPrintButton->hide();
85 }
86 
88 {
89 }
90 
91 void KDReports::PreviewDialog::setQuickPrinterName(const QString &printerName)
92 {
93  if (!printerName.isEmpty()) {
94  // QPrinter::setPrinterName has no effect on the QPrintDialog which uses the CUPS default printer anyway...
95  // So we only use this for the separate Print button.
96  // This is why it's not called setDefaultPrinterName.
97  d->m_quickPrinterName = printerName;
98  d->m_quickPrintButton->setText(tr("Print &with %1").arg(printerName));
99  d->m_quickPrintButton->show();
100  connect(d->m_quickPrintButton, &QPushButton::clicked, this, [this]() { d->slotQuickPrint(); });
101  }
102 }
103 
105 {
106  d->m_defaultSaveDirectory = path;
107 }
108 
110 {
111  d->m_dirBrowsingEnabled = allowed;
112 }
113 
115 {
117  return dialog.exec();
118 }
119 
120 void KDReports::PreviewDialogPrivate::slotTableBreakingDialog() const
121 {
122  if (q->showTableSettingsDialog(m_previewWidget->report())) {
123  m_previewWidget->repaint();
124  }
125 }
126 
127 void KDReports::PreviewDialogPrivate::slotPrintWithDialog() const
128 {
129  if (m_previewWidget->printWithDialog()) {
131  }
132 }
133 
134 void KDReports::PreviewDialogPrivate::slotQuickPrint() const
135 {
136  KDReports::Report *report = m_previewWidget->report();
137  QPrinter printer;
138  report->setupPrinter(&printer);
139  printer.setPrinterName(m_quickPrinterName);
140  report->print(&printer, q);
142 }
143 
144 void KDReports::PreviewDialogPrivate::slotSave()
145 {
146  KDReports::Report *report = m_previewWidget->report();
147  QString file;
148  if (m_dirBrowsingEnabled) {
149  file = QFileDialog::getSaveFileName(q, PreviewDialog::tr("Save Report as PDF"),
150  m_defaultSaveDirectory, PreviewDialog::tr("PDF Files (*.pdf)"));
151  } else {
152  bool ok;
153  Q_FOREVER
154  {
155  const QString text = PreviewDialog::tr("Saving as PDF in %1\n\nEnter the file name:").arg(m_defaultSaveDirectory);
156  QString fileName = QInputDialog::getText(q, PreviewDialog::tr("Save Report as PDF"), text, QLineEdit::Normal, report->documentName() + QStringLiteral(".pdf"), &ok);
157  if (!fileName.endsWith(QLatin1String(".pdf"), Qt::CaseInsensitive)) {
158  fileName += QLatin1String(".pdf");
159  }
160  if (!ok || fileName.isEmpty())
161  return;
162  file = m_defaultSaveDirectory + QLatin1Char('/') + fileName;
163  if (QFile::exists(file)) {
164  const QString msg = PreviewDialog::tr("%1 already exists. Do you want to replace it?").arg(fileName);
165  if (QMessageBox::warning(q, PreviewDialog::tr("Overwrite?"), msg, QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
166  break; // overwrite
167  }
168  } else {
169  // All OK
170  break;
171  }
172  }
173  }
174  if (!file.isEmpty()) {
175  QPrinter printer;
176  report->setupPrinter(&printer);
177  printer.setOutputFormat(QPrinter::PdfFormat);
178  printer.setOutputFileName(file);
179  m_savedFileName = file;
180  report->print(&printer, q);
181  if (QFile::exists(file)) {
183  } else {
185  }
186  }
187 }
188 
190 {
191  d->m_previewWidget->setPageSizeChangeAllowed(b);
192 }
193 
195 {
196  d->m_previewWidget->setShowTableSettingsDialog(b);
197 }
198 
200 {
201  d->m_previewWidget->setWidthForEndlessPrinter(widthMM);
202 }
203 
204 bool KDReports::PreviewDialog::isSelected(int pageNumber) const
205 {
206  return d->m_previewWidget->isSelected(pageNumber);
207 }
208 
210 {
211  QDialog::accept();
212 }
213 
215 {
216  QDialog::reject();
217 }
218 
220 {
221  return d->m_previewWidget;
222 }
223 
225 {
226  return d->m_savedFileName;
227 }
virtual bool showTableSettingsDialog(KDReports::Report *report)
KDReports::PreviewWidget * previewWidget()
void setWidthForEndlessPrinter(qreal widthMM)
PreviewDialog(KDReports::Report *report, QWidget *parent=nullptr)
void linkActivated(const QUrl &url)
bool isSelected(int pageNumber) const
void setDirectoryBrowsingEnabled(bool allowed)
void accept() override
Reimplemented for internal purposes. .
void reject() override
Reimplemented for internal purposes. .
void setDefaultSaveDirectory(const QString &path)
void setQuickPrinterName(const QString &printerName)
void tableSettingsClicked()
Emitted when the table settings button has been clicked.
void linkActivated(const QUrl &url)
bool print(QPrinter *printer, QWidget *parent=nullptr)

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
https://www.kdab.com/development-resources/qt-tools/kd-reports/
Generated by doxygen 1.9.1