File indexing completed on 2024-03-24 15:15:33

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 "exportimagedialog.h"
0008 
0009 #include "imageexporter.h"
0010 #include "kstars.h"
0011 #include "skymap.h"
0012 #include "skyqpainter.h"
0013 #include "ksnotification.h"
0014 #include "printing/legend.h"
0015 
0016 #include <KMessageBox>
0017 
0018 #include <QDesktopWidget>
0019 #include <QDir>
0020 #include <QPushButton>
0021 #include <QtSvg/QSvgGenerator>
0022 
0023 ExportImageDialogUI::ExportImageDialogUI(QWidget *parent) : QFrame(parent)
0024 {
0025     setupUi(this);
0026 }
0027 
0028 ExportImageDialog::ExportImageDialog(const QString &url, const QSize &size, ImageExporter *imgExporter)
0029     : QDialog((QWidget *)KStars::Instance()), m_KStars(KStars::Instance()), m_Url(url), m_Size(size)
0030 {
0031 #ifdef Q_OS_OSX
0032     setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
0033 #endif
0034     m_DialogUI = new ExportImageDialogUI(this);
0035 
0036     QVBoxLayout *mainLayout = new QVBoxLayout;
0037     mainLayout->addWidget(m_DialogUI);
0038     setLayout(mainLayout);
0039 
0040     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0041     mainLayout->addWidget(buttonBox);
0042     connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
0043     connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
0044 
0045     connect(buttonBox, SIGNAL(accepted()), this, SLOT(exportImage()));
0046     connect(buttonBox, SIGNAL(rejected()), this, SLOT(close()));
0047 
0048     QPushButton *previewB = new QPushButton(i18n("Preview image"));
0049     buttonBox->addButton(previewB, QDialogButtonBox::ActionRole);
0050     connect(previewB, SIGNAL(clicked()), this, SLOT(previewImage()));
0051 
0052     connect(m_DialogUI->addLegendCheckBox, SIGNAL(toggled(bool)), this, SLOT(switchLegendEnabled(bool)));
0053     connect(m_DialogUI->addLegendCheckBox, SIGNAL(toggled(bool)), previewB, SLOT(setEnabled(bool)));
0054 
0055     m_ImageExporter = ((imgExporter) ? imgExporter : new ImageExporter(this));
0056 
0057     setWindowTitle(i18nc("@title:window", "Export sky image"));
0058 
0059     setupWidgets();
0060 }
0061 
0062 void ExportImageDialog::switchLegendEnabled(bool enabled)
0063 {
0064     m_DialogUI->legendOrientationLabel->setEnabled(enabled);
0065     m_DialogUI->legendOrientationComboBox->setEnabled(enabled);
0066     m_DialogUI->legendTypeLabel->setEnabled(enabled);
0067     m_DialogUI->legendTypeComboBox->setEnabled(enabled);
0068     m_DialogUI->legendPositionLabel->setEnabled(enabled);
0069     m_DialogUI->legendPositionComboBox->setEnabled(enabled);
0070 }
0071 
0072 void ExportImageDialog::previewImage()
0073 {
0074     updateLegendSettings();
0075     const Legend *legend = m_ImageExporter->getLegend();
0076 
0077     // Preview current legend settings on sky map
0078     m_KStars->map()->setLegend(Legend(*legend));
0079     m_KStars->map()->setPreviewLegend(true);
0080 
0081     // Update sky map
0082     m_KStars->map()->forceUpdate(true);
0083 
0084     // Hide export dialog
0085     hide();
0086 }
0087 
0088 void ExportImageDialog::setupWidgets()
0089 {
0090     m_DialogUI->addLegendCheckBox->setChecked(true);
0091 
0092     m_DialogUI->legendOrientationComboBox->addItem(i18n("Horizontal"));
0093     m_DialogUI->legendOrientationComboBox->addItem(i18n("Vertical"));
0094 
0095     QStringList types;
0096     types << i18n("Full legend") << i18n("Scale with magnitudes chart") << i18n("Only scale") << i18n("Only magnitudes")
0097           << i18n("Only symbols");
0098     m_DialogUI->legendTypeComboBox->addItems(types);
0099 
0100     QStringList positions;
0101     positions << i18n("Upper left corner") << i18n("Upper right corner") << i18n("Lower left corner")
0102               << i18n("Lower right corner");
0103     m_DialogUI->legendPositionComboBox->addItems(positions);
0104 }
0105 
0106 void ExportImageDialog::updateLegendSettings()
0107 {
0108     Legend::LEGEND_ORIENTATION orientation =
0109         ((m_DialogUI->legendOrientationComboBox->currentIndex() == 1) ? Legend::LO_VERTICAL : Legend::LO_HORIZONTAL);
0110 
0111     Legend::LEGEND_TYPE type = static_cast<Legend::LEGEND_TYPE>(m_DialogUI->legendTypeComboBox->currentIndex());
0112 
0113     Legend::LEGEND_POSITION pos =
0114         static_cast<Legend::LEGEND_POSITION>(m_DialogUI->legendPositionComboBox->currentIndex());
0115 
0116     m_ImageExporter->setLegendProperties(type, orientation, pos);
0117 }
0118 
0119 void ExportImageDialog::exportImage()
0120 {
0121     qDebug() << Q_FUNC_INFO << "Exporting sky image";
0122     updateLegendSettings();
0123     m_ImageExporter->includeLegend(m_DialogUI->addLegendCheckBox->isChecked());
0124     if (!m_ImageExporter->exportImage(m_Url))
0125     {
0126         KSNotification::sorry(m_ImageExporter->getLastErrorMessage(), i18n("Could not export image"));
0127     }
0128 }