Kstars

exportimagedialog.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 "exportimagedialog.h"
8 
9 #include "imageexporter.h"
10 #include "kstars.h"
11 #include "skymap.h"
12 #include "skyqpainter.h"
13 #include "ksnotification.h"
14 #include "printing/legend.h"
15 
16 #include <KMessageBox>
17 
18 #include <QDesktopWidget>
19 #include <QDir>
20 #include <QPushButton>
21 #include <QtSvg/QSvgGenerator>
22 
23 ExportImageDialogUI::ExportImageDialogUI(QWidget *parent) : QFrame(parent)
24 {
25  setupUi(this);
26 }
27 
28 ExportImageDialog::ExportImageDialog(const QString &url, const QSize &size, ImageExporter *imgExporter)
29  : QDialog((QWidget *)KStars::Instance()), m_KStars(KStars::Instance()), m_Url(url), m_Size(size)
30 {
31 #ifdef Q_OS_OSX
33 #endif
34  m_DialogUI = new ExportImageDialogUI(this);
35 
36  QVBoxLayout *mainLayout = new QVBoxLayout;
37  mainLayout->addWidget(m_DialogUI);
38  setLayout(mainLayout);
39 
41  mainLayout->addWidget(buttonBox);
42  connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
43  connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
44 
45  connect(buttonBox, SIGNAL(accepted()), this, SLOT(exportImage()));
46  connect(buttonBox, SIGNAL(rejected()), this, SLOT(close()));
47 
48  QPushButton *previewB = new QPushButton(i18n("Preview image"));
49  buttonBox->addButton(previewB, QDialogButtonBox::ActionRole);
50  connect(previewB, SIGNAL(clicked()), this, SLOT(previewImage()));
51 
52  connect(m_DialogUI->addLegendCheckBox, SIGNAL(toggled(bool)), this, SLOT(switchLegendEnabled(bool)));
53  connect(m_DialogUI->addLegendCheckBox, SIGNAL(toggled(bool)), previewB, SLOT(setEnabled(bool)));
54 
55  m_ImageExporter = ((imgExporter) ? imgExporter : new ImageExporter(this));
56 
57  setWindowTitle(i18nc("@title:window", "Export sky image"));
58 
59  setupWidgets();
60 }
61 
62 void ExportImageDialog::switchLegendEnabled(bool enabled)
63 {
64  m_DialogUI->legendOrientationLabel->setEnabled(enabled);
65  m_DialogUI->legendOrientationComboBox->setEnabled(enabled);
66  m_DialogUI->legendTypeLabel->setEnabled(enabled);
67  m_DialogUI->legendTypeComboBox->setEnabled(enabled);
68  m_DialogUI->legendPositionLabel->setEnabled(enabled);
69  m_DialogUI->legendPositionComboBox->setEnabled(enabled);
70 }
71 
72 void ExportImageDialog::previewImage()
73 {
74  updateLegendSettings();
75  const Legend *legend = m_ImageExporter->getLegend();
76 
77  // Preview current legend settings on sky map
78  m_KStars->map()->setLegend(Legend(*legend));
79  m_KStars->map()->setPreviewLegend(true);
80 
81  // Update sky map
82  m_KStars->map()->forceUpdate(true);
83 
84  // Hide export dialog
85  hide();
86 }
87 
88 void ExportImageDialog::setupWidgets()
89 {
90  m_DialogUI->addLegendCheckBox->setChecked(true);
91 
92  m_DialogUI->legendOrientationComboBox->addItem(i18n("Horizontal"));
93  m_DialogUI->legendOrientationComboBox->addItem(i18n("Vertical"));
94 
96  types << i18n("Full legend") << i18n("Scale with magnitudes chart") << i18n("Only scale") << i18n("Only magnitudes")
97  << i18n("Only symbols");
98  m_DialogUI->legendTypeComboBox->addItems(types);
99 
100  QStringList positions;
101  positions << i18n("Upper left corner") << i18n("Upper right corner") << i18n("Lower left corner")
102  << i18n("Lower right corner");
103  m_DialogUI->legendPositionComboBox->addItems(positions);
104 }
105 
106 void ExportImageDialog::updateLegendSettings()
107 {
108  Legend::LEGEND_ORIENTATION orientation =
109  ((m_DialogUI->legendOrientationComboBox->currentIndex() == 1) ? Legend::LO_VERTICAL : Legend::LO_HORIZONTAL);
110 
111  Legend::LEGEND_TYPE type = static_cast<Legend::LEGEND_TYPE>(m_DialogUI->legendTypeComboBox->currentIndex());
112 
113  Legend::LEGEND_POSITION pos =
114  static_cast<Legend::LEGEND_POSITION>(m_DialogUI->legendPositionComboBox->currentIndex());
115 
116  m_ImageExporter->setLegendProperties(type, orientation, pos);
117 }
118 
119 void ExportImageDialog::exportImage()
120 {
121  qDebug() << Q_FUNC_INFO << "Exporting sky image";
122  updateLegendSettings();
123  m_ImageExporter->includeLegend(m_DialogUI->addLegendCheckBox->isChecked());
124  if (!m_ImageExporter->exportImage(m_Url))
125  {
126  KSNotification::sorry(m_ImageExporter->getLastErrorMessage(), i18n("Could not export image"));
127  }
128 }
virtual void reject()
Type type(const QSqlDatabase &db)
Backends for exporting a sky image, either raster or vector, with a legend.
Definition: imageexporter.h:23
ExportImageDialog(const QString &url, const QSize &size, ImageExporter *imgExporter=nullptr)
short Default constructor.
bool close()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
QStringList types(Mode mode=Writing)
void rejected()
QString i18n(const char *text, const TYPE &arg...)
void setWindowFlags(Qt::WindowFlags type)
void setWindowTitle(const QString &)
virtual void accept()
void addButton(QAbstractButton *button, QDialogButtonBox::ButtonRole role)
This is the main window for KStars. In addition to the GUI elements, the class contains the program c...
Definition: kstars.h:92
void setupUi(QWidget *widget)
void setEnabled(bool)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setLayout(QLayout *layout)
Legend class is used for painting legends on class inheriting QPaintDevice. Its methods enable changi...
Definition: legend.h:32
void accepted()
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 19 2022 03:57:50 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.