Kstars

pwizprint.cpp
1 /*
2  SPDX-FileCopyrightText: 2011 Rafał Kułaga <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "pwizprint.h"
8 
9 #include "finderchart.h"
10 #include "kstars.h"
11 #include "ksnotification.h"
12 #include "Options.h"
13 #include "printingwizard.h"
14 
15 #include <KMessageBox>
16 #include <KIO/StoredTransferJob>
17 
18 #include <QFileDialog>
19 #include <QTemporaryFile>
20 #include <QPrintPreviewDialog>
21 #include <QPrinter>
22 #include <QPrintDialog>
23 #include <QPointer>
24 
25 PWizPrintUI::PWizPrintUI(PrintingWizard *wizard, QWidget *parent) : QFrame(parent), m_ParentWizard(wizard)
26 {
27  setupUi(this);
28 
29  connect(previewButton, SIGNAL(clicked()), this, SLOT(slotPreview()));
30  connect(printButton, SIGNAL(clicked()), this, SLOT(slotPrint()));
31  connect(exportButton, SIGNAL(clicked()), this, SLOT(slotExport()));
32 }
33 
34 void PWizPrintUI::slotPreview()
35 {
37  connect(previewDlg, SIGNAL(paintRequested(QPrinter*)), SLOT(slotPrintPreview(QPrinter*)));
38  previewDlg->exec();
39  delete previewDlg;
40 }
41 
42 void PWizPrintUI::slotPrintPreview(QPrinter *printer)
43 {
44  printDocument(printer);
45 }
46 
47 void PWizPrintUI::slotPrint()
48 {
49  QPointer<QPrintDialog> dialog(new QPrintDialog(m_ParentWizard->getPrinter(), KStars::Instance()));
50  if (dialog->exec() == QDialog::Accepted)
51  {
52  printDocument(m_ParentWizard->getPrinter());
53  }
54  delete dialog;
55 }
56 
57 void PWizPrintUI::printDocument(QPrinter *printer)
58 {
59  m_ParentWizard->getFinderChart()->print(printer);
60 }
61 
62 void PWizPrintUI::slotExport()
63 {
64  QUrl url =
65  QFileDialog::getSaveFileUrl(KStars::Instance(), i18nc("@title:window", "Export"), QUrl(QDir::homePath()),
66  "application/pdf application/postscript application/vnd.oasis.opendocument.text");
67  //User cancelled file selection dialog - abort image export
68  if (url.isEmpty())
69  {
70  return;
71  }
72 
73  //Warn user if file exists!
74  if (QFile::exists(url.toLocalFile()))
75  {
77  parentWidget(), i18n("A file named \"%1\" already exists. Overwrite it?", url.fileName()),
78  i18n("Overwrite File?"), KStandardGuiItem::overwrite());
79  if (r == KMessageBox::Cancel)
80  return;
81  }
82 
83  QString urlStr = url.url();
84  if (!urlStr.contains(QDir::separator()))
85  {
86  urlStr = QDir::homePath() + '/' + urlStr;
87  }
88 
89  QTemporaryFile tmpfile;
90  tmpfile.open();
91  QString fname;
92 
93  if (url.isValid())
94  {
95  if (url.isLocalFile())
96  {
97  fname = url.toLocalFile();
98  }
99 
100  else
101  {
102  fname = tmpfile.fileName();
103  }
104 
105  //Determine desired image format from filename extension
106  QString ext = fname.mid(fname.lastIndexOf(".") + 1);
107  if (ext == "pdf" || ext == "ps")
108  {
109  m_ParentWizard->getFinderChart()->writePsPdf(fname);
110  }
111  else if (ext == "odt")
112  {
113  m_ParentWizard->getFinderChart()->writeOdt(fname);
114  }
115  else
116  {
117  return;
118  }
119 
120  if (tmpfile.fileName() == fname)
121  {
122  //attempt to upload image to remote location
123  if (KIO::storedHttpPost(&tmpfile, url)->exec() == false)
124  //if(!KIO::NetAccess::upload(tmpfile.fileName(), url, this))
125  {
126  QString message = i18n("Could not upload file to remote location: %1", url.url());
127  KSNotification::sorry(message, i18n("Could not upload file"));
128  }
129  }
130  }
131 }
bool writeOdt(const QString &fname)
Write contents of the document to Open Document Text file.
Class representing Printing Wizard for KStars printed documents (currently only finder charts).
QPrinter * getPrinter()
Get printer used by Printing Wizard.
ButtonCode warningContinueCancel(QWidget *parent, const QString &text, const QString &title=QString(), const KGuiItem &buttonContinue=KStandardGuiItem::cont(), const KGuiItem &buttonCancel=KStandardGuiItem::cancel(), const QString &dontAskAgainName=QString(), Options options=Notify)
QChar separator()
QString url(QUrl::FormattingOptions options) const const
QString homePath()
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
bool exists() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
virtual QString fileName() const const override
static KStars * Instance()
Definition: kstars.h:125
KIOCORE_EXPORT StoredTransferJob * storedHttpPost(const QByteArray &arr, const QUrl &url, JobFlags flags=DefaultFlags)
bool isValid() const const
QString i18n(const char *text, const TYPE &arg...)
bool isEmpty() const const
PWizPrintUI(PrintingWizard *wizard, QWidget *parent=nullptr)
Constructor.
Definition: pwizprint.cpp:25
QUrl getSaveFileUrl(QWidget *parent, const QString &caption, const QUrl &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options, const QStringList &supportedSchemes)
QString fileName(QUrl::ComponentFormattingOptions options) const const
QString toLocalFile() const const
void setupUi(QWidget *widget)
FinderChart * getFinderChart()
Get used FinderChart document.
void print(QPrinter *printer)
Print contents of the document.
bool isLocalFile() const const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
void writePsPdf(const QString &fname)
Write contents of the document to the Postscript/PDF file.
QString mid(int position, int n) const const
KGuiItem overwrite()
QWidget * parentWidget() const const
QString message
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Aug 14 2022 04:13:59 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.