24 #include <QDialogButtonBox>
25 #include <QFileDialog>
26 #include <QInputDialog>
27 #include <QMessageBox>
28 #include <QPushButton>
29 #include <QVBoxLayout>
31 class KDReports::PreviewDialogPrivate
36 , m_previewWidget(nullptr)
37 , m_buttonBox(nullptr)
38 , m_quickPrintButton(nullptr)
39 , m_dirBrowsingEnabled(true)
43 void slotTableBreakingDialog()
const;
44 void slotPrintWithDialog()
const;
45 void slotQuickPrint()
const;
50 QDialogButtonBox *m_buttonBox;
51 QPushButton *m_quickPrintButton;
52 QString m_quickPrinterName;
53 QString m_defaultSaveDirectory;
54 QString m_savedFileName;
55 bool m_dirBrowsingEnabled;
60 , d(new PreviewDialogPrivate(this))
63 d->m_previewWidget->setReport(report);
64 auto *topLayout =
new QVBoxLayout(
this);
65 topLayout->addWidget(d->m_previewWidget);
66 auto *bottomLayout =
new QHBoxLayout();
67 topLayout->addLayout(bottomLayout);
72 d->m_buttonBox =
new QDialogButtonBox(Qt::Horizontal,
this);
73 bottomLayout->addWidget(d->m_buttonBox);
75 auto *printWithDialogButton =
new QPushButton(tr(
"&Print..."),
this);
76 d->m_buttonBox->addButton(printWithDialogButton, QDialogButtonBox::ActionRole);
77 connect(printWithDialogButton, &QPushButton::clicked,
this, [
this]() { d->slotPrintWithDialog(); } );
79 d->m_quickPrintButton =
new QPushButton(
this);
80 d->m_buttonBox->addButton(d->m_quickPrintButton, QDialogButtonBox::ActionRole);
82 auto *saveButton =
new QPushButton(tr(
"&Save..."),
this);
83 d->m_buttonBox->addButton(saveButton, QDialogButtonBox::ActionRole);
84 connect(saveButton, &QPushButton::clicked,
this, [
this]() { d->slotSave(); } );
86 auto *cancelButton =
new QPushButton(tr(
"Cancel"),
this);
87 d->m_buttonBox->addButton(cancelButton, QDialogButtonBox::RejectRole);
88 connect(cancelButton, &QPushButton::clicked,
this, &QDialog::reject);
90 d->m_quickPrintButton->hide();
100 if (!printerName.isEmpty()) {
104 d->m_quickPrinterName = printerName;
105 d->m_quickPrintButton->setText(tr(
"Print &with %1").arg(printerName));
106 d->m_quickPrintButton->show();
107 connect(d->m_quickPrintButton, &QPushButton::clicked,
this, [
this]() { d->slotQuickPrint(); } );
113 d->m_defaultSaveDirectory = path;
118 d->m_dirBrowsingEnabled = allowed;
124 return dialog.exec();
127 void KDReports::PreviewDialogPrivate::slotTableBreakingDialog()
const
129 if (q->showTableSettingsDialog(m_previewWidget->report())) {
130 m_previewWidget->repaint();
134 void KDReports::PreviewDialogPrivate::slotPrintWithDialog()
const
136 if (m_previewWidget->printWithDialog()) {
142 void KDReports::PreviewDialogPrivate::slotQuickPrint()
const
146 report->setupPrinter(&printer);
147 printer.setPrinterName(m_quickPrinterName);
148 report->
print(&printer, q);
153 void KDReports::PreviewDialogPrivate::slotSave()
157 if (m_dirBrowsingEnabled) {
158 file = QFileDialog::getSaveFileName(q, PreviewDialog::tr(
"Save Report as PDF"),
159 m_defaultSaveDirectory, PreviewDialog::tr(
"PDF Files (*.pdf)"));
163 const QString text = PreviewDialog::tr(
"Saving as PDF in %1\n\nEnter the file name:").arg(m_defaultSaveDirectory);
164 QString fileName = QInputDialog::getText(q, PreviewDialog::tr(
"Save Report as PDF"), text, QLineEdit::Normal, report->
documentName() + QStringLiteral(
".pdf"), &ok);
165 if (!fileName.endsWith(QLatin1String(
".pdf"), Qt::CaseInsensitive)) {
166 fileName += QLatin1String(
".pdf");
168 if (!ok || fileName.isEmpty())
170 file = m_defaultSaveDirectory + QLatin1Char(
'/') + fileName;
171 if (QFile::exists(file)) {
172 const QString msg = PreviewDialog::tr(
"%1 already exists. Do you want to replace it?").arg(fileName);
173 if (QMessageBox::warning(q, PreviewDialog::tr(
"Overwrite?"), msg, QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
182 if (!file.isEmpty()) {
184 report->setupPrinter(&printer);
185 printer.setOutputFormat(QPrinter::PdfFormat);
186 printer.setOutputFileName(file);
187 m_savedFileName = file;
188 report->
print(&printer, q);
189 if (QFile::exists(file)) {
200 d->m_previewWidget->setPageSizeChangeAllowed(b);
205 d->m_previewWidget->setShowTableSettingsDialog(b);
210 d->m_previewWidget->setWidthForEndlessPrinter(widthMM);
215 return d->m_previewWidget->isSelected(pageNumber);
230 return d->m_previewWidget;
235 return d->m_savedFileName;