File indexing completed on 2024-04-14 14:11:15

0001 /*
0002     SPDX-FileCopyrightText: 2011 Rafał Kułaga <rl.kulaga@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "foveditordialog.h"
0008 
0009 #include "kstars.h"
0010 #include "ksnotification.h"
0011 #include "printingwizard.h"
0012 
0013 #include <KJob>
0014 #include <KMessageBox>
0015 #include <KIO/StoredTransferJob>
0016 
0017 #include <QDebug>
0018 #include <QFileDialog>
0019 #include <QTemporaryFile>
0020 
0021 FovEditorDialogUI::FovEditorDialogUI(QWidget *parent) : QFrame(parent)
0022 {
0023     setupUi(this);
0024 #ifdef Q_OS_OSX
0025     setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
0026 #endif
0027 
0028     setWindowTitle(i18nc("@title:window", "Field of View Snapshot Browser"));
0029 }
0030 
0031 FovEditorDialog::FovEditorDialog(PrintingWizard *wizard, QWidget *parent)
0032     : QDialog(parent), m_ParentWizard(wizard), m_CurrentIndex(0)
0033 {
0034     m_EditorUi = new FovEditorDialogUI(this);
0035 
0036     QVBoxLayout *mainLayout = new QVBoxLayout;
0037     mainLayout->addWidget(m_EditorUi);
0038     setLayout(mainLayout);
0039 
0040     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);
0041     mainLayout->addWidget(buttonBox);
0042     connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
0043 
0044     setupWidgets();
0045     setupConnections();
0046 }
0047 
0048 void FovEditorDialog::slotNextFov()
0049 {
0050     slotSaveDescription();
0051 
0052     if (m_CurrentIndex < m_ParentWizard->getFovSnapshotList()->size() - 1)
0053     {
0054         m_CurrentIndex++;
0055 
0056         updateFovImage();
0057         updateButtons();
0058         updateDescriptions();
0059     }
0060 }
0061 
0062 void FovEditorDialog::slotPreviousFov()
0063 {
0064     slotSaveDescription();
0065 
0066     if (m_CurrentIndex > 0)
0067     {
0068         m_CurrentIndex--;
0069 
0070         updateFovImage();
0071         updateButtons();
0072         updateDescriptions();
0073     }
0074 }
0075 
0076 void FovEditorDialog::slotCaptureAgain()
0077 {
0078     hide();
0079     m_ParentWizard->recaptureFov(m_CurrentIndex);
0080 }
0081 
0082 void FovEditorDialog::slotDelete()
0083 {
0084     if (m_CurrentIndex > m_ParentWizard->getFovSnapshotList()->size() - 1)
0085     {
0086         return;
0087     }
0088 
0089     delete m_ParentWizard->getFovSnapshotList()->at(m_CurrentIndex);
0090     m_ParentWizard->getFovSnapshotList()->removeAt(m_CurrentIndex);
0091 
0092     if (m_CurrentIndex == m_ParentWizard->getFovSnapshotList()->size())
0093     {
0094         m_CurrentIndex--;
0095     }
0096 
0097     updateFovImage();
0098     updateButtons();
0099     updateDescriptions();
0100 }
0101 
0102 void FovEditorDialog::slotSaveDescription()
0103 {
0104     if (m_CurrentIndex < m_ParentWizard->getFovSnapshotList()->size())
0105     {
0106         m_ParentWizard->getFovSnapshotList()->at(m_CurrentIndex)->setDescription(m_EditorUi->descriptionEdit->text());
0107     }
0108 }
0109 
0110 void FovEditorDialog::slotSaveImage()
0111 {
0112     if (m_CurrentIndex >= m_ParentWizard->getFovSnapshotList()->size())
0113     {
0114         return;
0115     }
0116 
0117     //If the filename string contains no "/" separators, assume the
0118     //user wanted to place a file in their home directory.
0119     QString url = QFileDialog::getSaveFileUrl(KStars::Instance(), i18nc("@title:window", "Save Image"), QUrl(QDir::homePath()),
0120                   "image/png image/jpeg image/gif image/x-portable-pixmap image/bmp")
0121                   .url();
0122     QUrl fileUrl;
0123     if (!url.contains(QDir::separator()))
0124     {
0125         fileUrl = QUrl::fromLocalFile(QDir::homePath() + '/' + url);
0126     }
0127 
0128     else
0129     {
0130         fileUrl = QUrl::fromLocalFile(url);
0131     }
0132 
0133     QTemporaryFile tmpfile;
0134     tmpfile.open();
0135     QString fname;
0136 
0137     if (fileUrl.isValid())
0138     {
0139         if (fileUrl.isLocalFile())
0140         {
0141             fname = fileUrl.toLocalFile();
0142         }
0143 
0144         else
0145         {
0146             fname = tmpfile.fileName();
0147         }
0148 
0149         //Determine desired image format from filename extension
0150         QString ext = fname.mid(fname.lastIndexOf(".") + 1);
0151         // export as raster graphics
0152         const char *format = "PNG";
0153 
0154         if (ext.toLower() == "png")
0155         {
0156             format = "PNG";
0157         }
0158         else if (ext.toLower() == "jpg" || ext.toLower() == "jpeg")
0159         {
0160             format = "JPG";
0161         }
0162         else if (ext.toLower() == "gif")
0163         {
0164             format = "GIF";
0165         }
0166         else if (ext.toLower() == "pnm")
0167         {
0168             format = "PNM";
0169         }
0170         else if (ext.toLower() == "bmp")
0171         {
0172             format = "BMP";
0173         }
0174         else
0175         {
0176             qWarning() << i18n("Could not parse image format of %1; assuming PNG.", fname);
0177         }
0178 
0179         if (!m_ParentWizard->getFovSnapshotList()->at(m_CurrentIndex)->getPixmap().save(fname, format))
0180         {
0181             qDebug() << Q_FUNC_INFO << "Error: Unable to save image: " << fname;
0182         }
0183 
0184         else
0185         {
0186             qDebug() << Q_FUNC_INFO << "Image saved to file: " << fname;
0187         }
0188     }
0189 
0190     if (tmpfile.fileName() == fname)
0191     {
0192         //attempt to upload image to remote location
0193         KIO::TransferJob *uploadJob = KIO::storedHttpPost(&tmpfile, fileUrl);
0194         //if(!KIO::NetAccess::upload(tmpfile.fileName(), fileUrl, this))
0195         if (uploadJob->exec() == false)
0196         {
0197             QString message = i18n("Could not upload image to remote location: %1", fileUrl.url());
0198             KSNotification::sorry(message, i18n("Could not upload file"));
0199         }
0200         uploadJob->kill();
0201     }
0202 }
0203 
0204 void FovEditorDialog::setupWidgets()
0205 {
0206     if (m_ParentWizard->getFovSnapshotList()->size() > 0)
0207     {
0208         m_EditorUi->imageLabel->setPixmap(m_ParentWizard->getFovSnapshotList()->first()->getPixmap());
0209     }
0210 
0211     updateButtons();
0212     updateDescriptions();
0213 }
0214 
0215 void FovEditorDialog::setupConnections()
0216 {
0217     connect(m_EditorUi->previousButton, SIGNAL(clicked()), this, SLOT(slotPreviousFov()));
0218     connect(m_EditorUi->nextButton, SIGNAL(clicked()), this, SLOT(slotNextFov()));
0219     connect(m_EditorUi->recaptureButton, SIGNAL(clicked()), this, SLOT(slotCaptureAgain()));
0220     connect(m_EditorUi->deleteButton, SIGNAL(clicked()), this, SLOT(slotDelete()));
0221     connect(m_EditorUi->descriptionEdit, SIGNAL(editingFinished()), this, SLOT(slotSaveDescription()));
0222     connect(m_EditorUi->saveButton, SIGNAL(clicked()), this, SLOT(slotSaveImage()));
0223 }
0224 
0225 void FovEditorDialog::updateButtons()
0226 {
0227     m_EditorUi->previousButton->setEnabled(m_CurrentIndex > 0);
0228     m_EditorUi->nextButton->setEnabled(m_CurrentIndex < m_ParentWizard->getFovSnapshotList()->size() - 1);
0229 }
0230 
0231 void FovEditorDialog::updateDescriptions()
0232 {
0233     if (m_ParentWizard->getFovSnapshotList()->size() == 0)
0234     {
0235         m_EditorUi->imageLabel->setText("No captured field of view images.");
0236         m_EditorUi->fovInfoLabel->setText(QString());
0237         m_EditorUi->recaptureButton->setEnabled(false);
0238         m_EditorUi->deleteButton->setEnabled(false);
0239         m_EditorUi->descriptionEdit->setEnabled(false);
0240         m_EditorUi->saveButton->setEnabled(false);
0241     }
0242 
0243     else
0244     {
0245         FOV *fov = m_ParentWizard->getFovSnapshotList()->at(m_CurrentIndex)->getFov();
0246 
0247         QString fovDescription = i18n("FOV (%1/%2): %3 (%4' x %5')", QString::number(m_CurrentIndex + 1),
0248                                       QString::number(m_ParentWizard->getFovSnapshotList()->size()), fov->name(),
0249                                       QString::number(fov->sizeX()), QString::number(fov->sizeY()));
0250 
0251         m_EditorUi->fovInfoLabel->setText(fovDescription);
0252 
0253         m_EditorUi->descriptionEdit->setText(
0254             m_ParentWizard->getFovSnapshotList()->at(m_CurrentIndex)->getDescription());
0255     }
0256 }
0257 
0258 void FovEditorDialog::updateFovImage()
0259 {
0260     if (m_CurrentIndex < m_ParentWizard->getFovSnapshotList()->size())
0261     {
0262         m_EditorUi->imageLabel->setPixmap(m_ParentWizard->getFovSnapshotList()->at(m_CurrentIndex)->getPixmap());
0263     }
0264 }