18 #include <QDialogButtonBox>
19 #include <QFileDialog>
20 #include <QInputDialog>
21 #include <QMessageBox>
22 #include <QPushButton>
23 #include <QVBoxLayout>
25 class KDReports::PreviewDialogPrivate
30 , m_previewWidget(nullptr)
31 , m_buttonBox(nullptr)
32 , m_quickPrintButton(nullptr)
33 , m_dirBrowsingEnabled(true)
37 void slotTableBreakingDialog()
const;
38 void slotPrintWithDialog()
const;
39 void slotQuickPrint()
const;
44 QDialogButtonBox *m_buttonBox;
45 QPushButton *m_quickPrintButton;
46 QString m_quickPrinterName;
47 QString m_defaultSaveDirectory;
48 QString m_savedFileName;
49 bool m_dirBrowsingEnabled;
54 , d(new PreviewDialogPrivate(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);
66 d->m_buttonBox =
new QDialogButtonBox(Qt::Horizontal,
this);
67 bottomLayout->addWidget(d->m_buttonBox);
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(); });
73 d->m_quickPrintButton =
new QPushButton(
this);
74 d->m_buttonBox->addButton(d->m_quickPrintButton, QDialogButtonBox::ActionRole);
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(); });
80 auto *cancelButton =
new QPushButton(tr(
"Cancel"),
this);
81 d->m_buttonBox->addButton(cancelButton, QDialogButtonBox::RejectRole);
82 connect(cancelButton, &QPushButton::clicked,
this, &QDialog::reject);
84 d->m_quickPrintButton->hide();
93 if (!printerName.isEmpty()) {
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(); });
106 d->m_defaultSaveDirectory = path;
111 d->m_dirBrowsingEnabled = allowed;
117 return dialog.exec();
120 void KDReports::PreviewDialogPrivate::slotTableBreakingDialog()
const
122 if (q->showTableSettingsDialog(m_previewWidget->report())) {
123 m_previewWidget->repaint();
127 void KDReports::PreviewDialogPrivate::slotPrintWithDialog()
const
129 if (m_previewWidget->printWithDialog()) {
134 void KDReports::PreviewDialogPrivate::slotQuickPrint()
const
138 report->setupPrinter(&printer);
139 printer.setPrinterName(m_quickPrinterName);
140 report->
print(&printer, q);
144 void KDReports::PreviewDialogPrivate::slotSave()
148 if (m_dirBrowsingEnabled) {
149 file = QFileDialog::getSaveFileName(q, PreviewDialog::tr(
"Save Report as PDF"),
150 m_defaultSaveDirectory, PreviewDialog::tr(
"PDF Files (*.pdf)"));
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");
160 if (!ok || fileName.isEmpty())
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) {
174 if (!file.isEmpty()) {
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)) {
191 d->m_previewWidget->setPageSizeChangeAllowed(b);
196 d->m_previewWidget->setShowTableSettingsDialog(b);
201 d->m_previewWidget->setWidthForEndlessPrinter(widthMM);
206 return d->m_previewWidget->isSelected(pageNumber);
221 return d->m_previewWidget;
226 return d->m_savedFileName;
QString savedFileName() const
~PreviewDialog() override
void setShowTableSettingsDialog(bool b)
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 setPageSizeChangeAllowed(bool b)
void setDefaultSaveDirectory(const QString &path)
void setQuickPrinterName(const QString &printerName)
bool print(QPrinter *printer, QWidget *parent=nullptr)