26 #include <kio/netaccess.h>
27 #include <ktemporaryfile.h>
30 #include <QSvgGenerator>
38 m_Legend->
setFont(QFont(
"Courier New", 8));
44 void ImageExporter::exportSvg(
const QString &fileName)
49 QSvgGenerator svgGenerator;
50 svgGenerator.setFileName(fileName);
51 svgGenerator.setTitle(i18n(
"KStars Exported Sky Image"));
52 svgGenerator.setDescription(i18n(
"KStars Exported Sky Image"));
53 svgGenerator.setSize(QSize(map->width(), map->height()));
54 svgGenerator.setResolution(qMax(map->logicalDpiX(), map->logicalDpiY()));
55 svgGenerator.setViewBox(QRect(0, 0, map->width(), map->height()));
70 bool ImageExporter::exportRasterGraphics(
const QString &fileName)
73 QString ext = fileName.mid(fileName.lastIndexOf(
".") + 1);
76 const char* format =
"PNG";
78 if(ext.toLower() ==
"png") {format =
"PNG";}
79 else if(ext.toLower() ==
"jpg" || ext.toLower() ==
"jpeg" ) { format =
"JPG"; }
80 else if(ext.toLower() ==
"gif") { format =
"GIF"; }
81 else if(ext.toLower() ==
"pnm") { format =
"PNM"; }
82 else if(ext.toLower() ==
"bmp") { format =
"BMP"; }
85 kWarning() << i18n(
"Could not parse image format of %1; assuming PNG.", fileName);
92 width = m_Size->width();
93 height = m_Size->height();
97 height = map->height();
100 QPixmap skyimage(map->width(), map->height());
101 QPixmap outimage(width, height);
105 qApp->processEvents();
110 if(width == map->width() && height == map->height())
112 outimage = skyimage.copy();
117 int dx(0), dy(0), sx(0), sy(0);
118 int sw(map->width()), sh(map->height());
120 if(width > map->width())
122 dx = (width - map->width())/2;
127 sx = (map->width() - width)/2;
131 if(height > map->height())
133 dy = (height - map->height())/2;
138 sy = (map->height() - height)/2;
144 p.fillRect(outimage.rect(), QBrush( Qt::white));
145 p.drawImage(dx, dy, skyimage.toImage(), sx, sy, sw, sh);
149 if( m_includeLegend )
151 addLegend(&outimage);
154 if(!outimage.save(fileName, format))
156 m_lastErrorMessage = i18n(
"Error: Unable to save image: %1 ", fileName);
157 kDebug() << m_lastErrorMessage;
163 kDebug() << i18n(
"Image saved to file: %1", fileName);
167 void ImageExporter::addLegend(
SkyQPainter *painter)
172 void ImageExporter::addLegend(QPaintDevice *pd)
187 if(!url.contains(
"/"))
189 fileURL = QDir::homePath() +
'/' + url;
197 m_lastErrorMessage = QString();
198 if(fileURL.isValid())
200 KTemporaryFile tmpfile;
202 bool isLocalFile = fileURL.isLocalFile();
206 fname = fileURL.toLocalFile();
212 fname = tmpfile.fileName();
216 QString ext = fname.mid(fname.lastIndexOf(
".") + 1);
217 if(ext.toLower() ==
"svg")
224 return exportRasterGraphics(fname);
230 if(!KIO::NetAccess::upload(tmpfile.fileName(), fileURL, m_KStars))
232 m_lastErrorMessage = i18n(
"Could not upload image to remote location: %1", fileURL.prettyUrl() );
234 kWarning() << m_lastErrorMessage;
240 m_lastErrorMessage = i18n(
"Could not export image: URL %1 invalid", fileURL.prettyUrl() );
241 kWarning() << m_lastErrorMessage;
259 m_includeLegend = include;
270 m_Size =
new QSize( *size );
277 Q_ASSERT( alpha >= 0 && alpha <= 255 );
278 Q_ASSERT( m_Legend );
280 bgColor.setAlpha(alpha);
void setLegendAlpha(int alpha)
Set legend transparency.
bool exportImage(QString url)
Exports an image with the defined settings.
void setLegendProperties(Legend::LEGEND_TYPE type, Legend::LEGEND_ORIENTATION orientation, Legend::LEGEND_POSITION position, int alpha=160, bool include=true)
Set the legend properties.
ImageExporter(QObject *parent)
Constructor.
virtual void begin()
Begin painting.
This is the main window for KStars.
The QPainter-based painting backend.
void setBgColor(const QColor &color)
Set background color.
LEGEND_POSITION
Legend position enumeration.
LEGEND_TYPE
Legend type enumeration.
void setType(LEGEND_TYPE type)
Set legend type.
QColor getBgColor()
Get background color.
LEGEND_ORIENTATION
Legend orientation enumeration.
Legend class is used for painting legends on class inheriting QPaintDevice.
void exportSkyImage(QPaintDevice *pd, bool scale=false)
Proxy method for SkyMapDrawAbstract::exportSkyImage()
This is the canvas on which the sky is painted.
void setFont(const QFont &font)
Set font.
void setPosition(LEGEND_POSITION pos)
Set legend position.
void setOrientation(LEGEND_ORIENTATION orientation)
Set legend orientation.
void paintLegend(QPaintDevice *pd)
Paint legend on passed QPaintDevice at selected position.
~ImageExporter()
Destructor.
virtual void end()
End and finalize painting.
void setRasterOutputSize(const QSize *size)
Set the size of output raster images.