1 /*
2     SPDX-FileCopyrightText: 2011 Rafał Kułaga <rl.kulaga@gmail.com>
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 
FovEditorDialogUI(QWidget * parent)21 FovEditorDialogUI::FovEditorDialogUI(QWidget *parent) : QFrame(parent)
22 {
23     setupUi(this);
24 #ifdef Q_OS_OSX
25     setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
26 #endif
27 
28     setWindowTitle(i18nc("@title:window", "Field of View Snapshot Browser"));
29 }
30 
FovEditorDialog(PrintingWizard * wizard,QWidget * parent)31 FovEditorDialog::FovEditorDialog(PrintingWizard *wizard, QWidget *parent)
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 
40     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);
41     mainLayout->addWidget(buttonBox);
42     connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
43 
44     setupWidgets();
45     setupConnections();
46 }
47 
slotNextFov()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 
slotPreviousFov()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 
slotCaptureAgain()76 void FovEditorDialog::slotCaptureAgain()
77 {
78     hide();
79     m_ParentWizard->recaptureFov(m_CurrentIndex);
80 }
81 
slotDelete()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 
slotSaveDescription()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 
slotSaveImage()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() << "Error: Unable to save image: " << fname;
182         }
183 
184         else
185         {
186             qDebug() << "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 
setupWidgets()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 
setupConnections()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 
updateButtons()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 
updateDescriptions()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 
updateFovImage()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 }
265