Kstars

foveditordialog.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 "foveditordialog.h"
8 
9 #include "kstars.h"
10 #include "ksnotification.h"
11 #include "printingwizard.h"
12 
13 #include <KJob>
14 #include <KMessageBox>
15 #include <KIO/StoredTransferJob>
16 
17 #include <QDebug>
18 #include <QFileDialog>
19 #include <QTemporaryFile>
20 
22 {
23  setupUi(this);
24 #ifdef Q_OS_OSX
26 #endif
27 
28  setWindowTitle(i18nc("@title:window", "Field of View Snapshot Browser"));
29 }
30 
32  : QDialog(parent), m_ParentWizard(wizard), m_CurrentIndex(0)
33 {
34  m_EditorUi = new FovEditorDialogUI(this);
35 
36  QVBoxLayout *mainLayout = new QVBoxLayout;
37  mainLayout->addWidget(m_EditorUi);
38  setLayout(mainLayout);
39 
41  mainLayout->addWidget(buttonBox);
42  connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
43 
44  setupWidgets();
45  setupConnections();
46 }
47 
48 void FovEditorDialog::slotNextFov()
49 {
50  slotSaveDescription();
51 
52  if (m_CurrentIndex < m_ParentWizard->getFovSnapshotList()->size() - 1)
53  {
54  m_CurrentIndex++;
55 
56  updateFovImage();
57  updateButtons();
58  updateDescriptions();
59  }
60 }
61 
62 void FovEditorDialog::slotPreviousFov()
63 {
64  slotSaveDescription();
65 
66  if (m_CurrentIndex > 0)
67  {
68  m_CurrentIndex--;
69 
70  updateFovImage();
71  updateButtons();
72  updateDescriptions();
73  }
74 }
75 
76 void FovEditorDialog::slotCaptureAgain()
77 {
78  hide();
79  m_ParentWizard->recaptureFov(m_CurrentIndex);
80 }
81 
82 void FovEditorDialog::slotDelete()
83 {
84  if (m_CurrentIndex > m_ParentWizard->getFovSnapshotList()->size() - 1)
85  {
86  return;
87  }
88 
89  delete m_ParentWizard->getFovSnapshotList()->at(m_CurrentIndex);
90  m_ParentWizard->getFovSnapshotList()->removeAt(m_CurrentIndex);
91 
92  if (m_CurrentIndex == m_ParentWizard->getFovSnapshotList()->size())
93  {
94  m_CurrentIndex--;
95  }
96 
97  updateFovImage();
98  updateButtons();
99  updateDescriptions();
100 }
101 
102 void FovEditorDialog::slotSaveDescription()
103 {
104  if (m_CurrentIndex < m_ParentWizard->getFovSnapshotList()->size())
105  {
106  m_ParentWizard->getFovSnapshotList()->at(m_CurrentIndex)->setDescription(m_EditorUi->descriptionEdit->text());
107  }
108 }
109 
110 void FovEditorDialog::slotSaveImage()
111 {
112  if (m_CurrentIndex >= m_ParentWizard->getFovSnapshotList()->size())
113  {
114  return;
115  }
116 
117  //If the filename string contains no "/" separators, assume the
118  //user wanted to place a file in their home directory.
119  QString url = QFileDialog::getSaveFileUrl(KStars::Instance(), i18nc("@title:window", "Save Image"), QUrl(QDir::homePath()),
120  "image/png image/jpeg image/gif image/x-portable-pixmap image/bmp")
121  .url();
122  QUrl fileUrl;
123  if (!url.contains(QDir::separator()))
124  {
125  fileUrl = QUrl::fromLocalFile(QDir::homePath() + '/' + url);
126  }
127 
128  else
129  {
130  fileUrl = QUrl::fromLocalFile(url);
131  }
132 
133  QTemporaryFile tmpfile;
134  tmpfile.open();
135  QString fname;
136 
137  if (fileUrl.isValid())
138  {
139  if (fileUrl.isLocalFile())
140  {
141  fname = fileUrl.toLocalFile();
142  }
143 
144  else
145  {
146  fname = tmpfile.fileName();
147  }
148 
149  //Determine desired image format from filename extension
150  QString ext = fname.mid(fname.lastIndexOf(".") + 1);
151  // export as raster graphics
152  const char *format = "PNG";
153 
154  if (ext.toLower() == "png")
155  {
156  format = "PNG";
157  }
158  else if (ext.toLower() == "jpg" || ext.toLower() == "jpeg")
159  {
160  format = "JPG";
161  }
162  else if (ext.toLower() == "gif")
163  {
164  format = "GIF";
165  }
166  else if (ext.toLower() == "pnm")
167  {
168  format = "PNM";
169  }
170  else if (ext.toLower() == "bmp")
171  {
172  format = "BMP";
173  }
174  else
175  {
176  qWarning() << i18n("Could not parse image format of %1; assuming PNG.", fname);
177  }
178 
179  if (!m_ParentWizard->getFovSnapshotList()->at(m_CurrentIndex)->getPixmap().save(fname, format))
180  {
181  qDebug() << Q_FUNC_INFO << "Error: Unable to save image: " << fname;
182  }
183 
184  else
185  {
186  qDebug() << Q_FUNC_INFO << "Image saved to file: " << fname;
187  }
188  }
189 
190  if (tmpfile.fileName() == fname)
191  {
192  //attempt to upload image to remote location
193  KIO::TransferJob *uploadJob = KIO::storedHttpPost(&tmpfile, fileUrl);
194  //if(!KIO::NetAccess::upload(tmpfile.fileName(), fileUrl, this))
195  if (uploadJob->exec() == false)
196  {
197  QString message = i18n("Could not upload image to remote location: %1", fileUrl.url());
198  KSNotification::sorry(message, i18n("Could not upload file"));
199  }
200  uploadJob->kill();
201  }
202 }
203 
204 void FovEditorDialog::setupWidgets()
205 {
206  if (m_ParentWizard->getFovSnapshotList()->size() > 0)
207  {
208  m_EditorUi->imageLabel->setPixmap(m_ParentWizard->getFovSnapshotList()->first()->getPixmap());
209  }
210 
211  updateButtons();
212  updateDescriptions();
213 }
214 
215 void FovEditorDialog::setupConnections()
216 {
217  connect(m_EditorUi->previousButton, SIGNAL(clicked()), this, SLOT(slotPreviousFov()));
218  connect(m_EditorUi->nextButton, SIGNAL(clicked()), this, SLOT(slotNextFov()));
219  connect(m_EditorUi->recaptureButton, SIGNAL(clicked()), this, SLOT(slotCaptureAgain()));
220  connect(m_EditorUi->deleteButton, SIGNAL(clicked()), this, SLOT(slotDelete()));
221  connect(m_EditorUi->descriptionEdit, SIGNAL(editingFinished()), this, SLOT(slotSaveDescription()));
222  connect(m_EditorUi->saveButton, SIGNAL(clicked()), this, SLOT(slotSaveImage()));
223 }
224 
225 void FovEditorDialog::updateButtons()
226 {
227  m_EditorUi->previousButton->setEnabled(m_CurrentIndex > 0);
228  m_EditorUi->nextButton->setEnabled(m_CurrentIndex < m_ParentWizard->getFovSnapshotList()->size() - 1);
229 }
230 
231 void FovEditorDialog::updateDescriptions()
232 {
233  if (m_ParentWizard->getFovSnapshotList()->size() == 0)
234  {
235  m_EditorUi->imageLabel->setText("No captured field of view images.");
236  m_EditorUi->fovInfoLabel->setText(QString());
237  m_EditorUi->recaptureButton->setEnabled(false);
238  m_EditorUi->deleteButton->setEnabled(false);
239  m_EditorUi->descriptionEdit->setEnabled(false);
240  m_EditorUi->saveButton->setEnabled(false);
241  }
242 
243  else
244  {
245  FOV *fov = m_ParentWizard->getFovSnapshotList()->at(m_CurrentIndex)->getFov();
246 
247  QString fovDescription = i18n("FOV (%1/%2): %3 (%4' x %5')", QString::number(m_CurrentIndex + 1),
248  QString::number(m_ParentWizard->getFovSnapshotList()->size()), fov->name(),
249  QString::number(fov->sizeX()), QString::number(fov->sizeY()));
250 
251  m_EditorUi->fovInfoLabel->setText(fovDescription);
252 
253  m_EditorUi->descriptionEdit->setText(
254  m_ParentWizard->getFovSnapshotList()->at(m_CurrentIndex)->getDescription());
255  }
256 }
257 
258 void FovEditorDialog::updateFovImage()
259 {
260  if (m_CurrentIndex < m_ParentWizard->getFovSnapshotList()->size())
261  {
262  m_EditorUi->imageLabel->setPixmap(m_ParentWizard->getFovSnapshotList()->at(m_CurrentIndex)->getPixmap());
263  }
264 }
T & first()
Definition: fov.h:27
QString number(int n, int base)
virtual void reject()
Class representing Printing Wizard for KStars printed documents (currently only finder charts).
QChar separator()
QString url(QUrl::FormattingOptions options) const const
QString homePath()
bool kill(KillVerbosity verbosity=Quietly)
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void removeAt(int i)
virtual QString fileName() const const override
void hide()
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
static KStars * Instance()
Definition: kstars.h:125
KIOCORE_EXPORT StoredTransferJob * storedHttpPost(const QByteArray &arr, const QUrl &url, JobFlags flags=DefaultFlags)
bool isValid() const const
int size() const const
void rejected()
FovEditorDialog(PrintingWizard *wizard, QWidget *parent=nullptr)
Constructor.
QString i18n(const char *text, const TYPE &arg...)
void setWindowFlags(Qt::WindowFlags type)
QUrl getSaveFileUrl(QWidget *parent, const QString &caption, const QUrl &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options, const QStringList &supportedSchemes)
QUrl fromLocalFile(const QString &localFile)
void setWindowTitle(const QString &)
const T & at(int i) const const
QString toLocalFile() const const
void setupUi(QWidget *widget)
void setEnabled(bool)
QString toLower() const const
User interface for FOV Editor Dialog.
bool isLocalFile() const const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
FovEditorDialogUI(QWidget *parent=nullptr)
Constructor.
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QList< FovSnapshot * > * getFovSnapshotList()
Get FovSnapshot list.
void setLayout(QLayout *layout)
QString mid(int position, int n) const const
QString message
bool exec()
void recaptureFov(int idx)
Recapture FOV snapshot of passed index.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 19 2022 03:57:51 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.