• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kstars

  • sources
  • kde-4.12
  • kdeedu
  • kstars
  • kstars
imageexporter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  imageexporter.cpp - K Desktop Planetarium
3  -------------------
4  begin : Sun 13 Jan 2013 00:53:50 CST
5  copyright : (c) 2013 by Akarsh Simha
6  email : akarsh.simha@kdemail.net
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 
19 /* Project Includes */
20 #include "imageexporter.h"
21 #include "kstars.h"
22 #include "skyqpainter.h"
23 #include "skymap.h"
24 
25 /* KDE Includes */
26 #include <kio/netaccess.h>
27 #include <ktemporaryfile.h>
28 
29 /* Qt Includes */
30 #include <QSvgGenerator>
31 
32 ImageExporter::ImageExporter( QObject *parent ) : QObject( parent ), m_KStars( KStars::Instance() ), m_includeLegend( false ), m_Size( 0 )
33 {
34  Q_ASSERT( m_KStars );
35  m_Legend = new Legend;
36 
37  // set font for legend labels
38  m_Legend->setFont(QFont("Courier New", 8));
39 
40  // set up the default alpha
41  setLegendAlpha( 160 );
42 }
43 
44 void ImageExporter::exportSvg(const QString &fileName)
45 {
46  SkyMap *map = m_KStars->map();
47 
48  // export as SVG
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()));
56 
57  SkyQPainter painter(m_KStars, &svgGenerator);
58  painter.begin();
59 
60  map->exportSkyImage(&painter);
61 
62  if( m_includeLegend )
63  {
64  addLegend(&painter);
65  }
66 
67  painter.end();
68 }
69 
70 bool ImageExporter::exportRasterGraphics(const QString &fileName)
71 {
72  //Determine desired image format from filename extension
73  QString ext = fileName.mid(fileName.lastIndexOf(".") + 1);
74 
75  // export as raster graphics
76  const char* format = "PNG";
77 
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"; }
83  else
84  {
85  kWarning() << i18n("Could not parse image format of %1; assuming PNG.", fileName);
86  }
87 
88  SkyMap *map = m_KStars->map();
89 
90  int width, height;
91  if ( m_Size ) {
92  width = m_Size->width();
93  height = m_Size->height();
94  }
95  else {
96  width = map->width();
97  height = map->height();
98  }
99 
100  QPixmap skyimage(map->width(), map->height());
101  QPixmap outimage(width, height);
102  outimage.fill();
103 
104  map->exportSkyImage(&skyimage);
105  qApp->processEvents();
106 
107  //skyImage is the size of the sky map. The requested image size is w x h.
108  //If w x h is smaller than the skymap, then we simply crop the image.
109  //If w x h is larger than the skymap, pad the skymap image with a white border.
110  if(width == map->width() && height == map->height())
111  {
112  outimage = skyimage.copy();
113  }
114 
115  else
116  {
117  int dx(0), dy(0), sx(0), sy(0);
118  int sw(map->width()), sh(map->height());
119 
120  if(width > map->width())
121  {
122  dx = (width - map->width())/2;
123  }
124 
125  else
126  {
127  sx = (map->width() - width)/2;
128  sw = width;
129  }
130 
131  if(height > map->height())
132  {
133  dy = (height - map->height())/2;
134  }
135 
136  else
137  {
138  sy = (map->height() - height)/2;
139  sh = height;
140  }
141 
142  QPainter p;
143  p.begin(&outimage);
144  p.fillRect(outimage.rect(), QBrush( Qt::white));
145  p.drawImage(dx, dy, skyimage.toImage(), sx, sy, sw, sh);
146  p.end();
147  }
148 
149  if( m_includeLegend )
150  {
151  addLegend(&outimage);
152  }
153 
154  if(!outimage.save(fileName, format))
155  {
156  m_lastErrorMessage = i18n("Error: Unable to save image: %1 ", fileName);
157  kDebug() << m_lastErrorMessage;
158  return false;
159  }
160 
161  else
162  {
163  kDebug() << i18n("Image saved to file: %1", fileName);
164  return true;
165  }
166 }
167 void ImageExporter::addLegend(SkyQPainter *painter)
168 {
169  m_Legend->paintLegend(painter);
170 }
171 
172 void ImageExporter::addLegend(QPaintDevice *pd)
173 {
174  SkyQPainter painter(m_KStars, pd);
175  painter.begin();
176 
177  addLegend(&painter);
178 
179  painter.end();
180 }
181 
182 bool ImageExporter::exportImage( QString url )
183 {
184  //If the filename string contains no "/" separators, assume the
185  //user wanted to place a file in their home directory.
186  KUrl fileURL;
187  if(!url.contains("/"))
188  {
189  fileURL = QDir::homePath() + '/' + url;
190  }
191 
192  else
193  {
194  fileURL = url;
195  }
196 
197  m_lastErrorMessage = QString();
198  if(fileURL.isValid())
199  {
200  KTemporaryFile tmpfile;
201  QString fname;
202  bool isLocalFile = fileURL.isLocalFile();
203 
204  if(isLocalFile)
205  {
206  fname = fileURL.toLocalFile();
207  }
208 
209  else
210  {
211  tmpfile.open();
212  fname = tmpfile.fileName();
213  }
214 
215  //Determine desired image format from filename extension
216  QString ext = fname.mid(fname.lastIndexOf(".") + 1);
217  if(ext.toLower() == "svg")
218  {
219  exportSvg(fname);
220  }
221 
222  else
223  {
224  return exportRasterGraphics(fname);
225  }
226 
227  if(!isLocalFile)
228  {
229  //attempt to upload image to remote location
230  if(!KIO::NetAccess::upload(tmpfile.fileName(), fileURL, m_KStars))
231  {
232  m_lastErrorMessage = i18n( "Could not upload image to remote location: %1", fileURL.prettyUrl() );
233 // KMessageBox::sorry( 0, message, i18n( "Could not upload file" ) );
234  kWarning() << m_lastErrorMessage;
235  return false;
236  }
237  }
238  return true;
239  }
240  m_lastErrorMessage = i18n( "Could not export image: URL %1 invalid", fileURL.prettyUrl() );
241  kWarning() << m_lastErrorMessage;
242  return false;
243 }
244 
245 
246 void ImageExporter::setLegendProperties( Legend::LEGEND_TYPE type, Legend::LEGEND_ORIENTATION orientation, Legend::LEGEND_POSITION position, int alpha, bool include)
247 {
248  // set background color (alpha)
249  setLegendAlpha( alpha );
250  // set legend orientation
251  m_Legend->setOrientation(orientation);
252 
253  // set legend type
254  m_Legend->setType(type);
255 
256  // set legend position
257  m_Legend->setPosition(position);
258 
259  m_includeLegend = include;
260 }
261 
262 ImageExporter::~ImageExporter()
263 {
264  delete m_Legend;
265 }
266 
267 void ImageExporter::setRasterOutputSize( const QSize *size )
268 {
269  if ( size )
270  m_Size = new QSize( *size ); // make a copy, so it's safe if the original gets deleted
271  else
272  m_Size = 0;
273 }
274 
275 void ImageExporter::setLegendAlpha( int alpha )
276 {
277  Q_ASSERT( alpha >= 0 && alpha <= 255 );
278  Q_ASSERT( m_Legend );
279  QColor bgColor = m_Legend->getBgColor();
280  bgColor.setAlpha(alpha);
281  m_Legend->setBgColor(bgColor);
282 }
283 
ImageExporter::setLegendAlpha
void setLegendAlpha(int alpha)
Set legend transparency.
Definition: imageexporter.cpp:275
ImageExporter::exportImage
bool exportImage(QString url)
Exports an image with the defined settings.
Definition: imageexporter.cpp:182
ImageExporter::setLegendProperties
void setLegendProperties(Legend::LEGEND_TYPE type, Legend::LEGEND_ORIENTATION orientation, Legend::LEGEND_POSITION position, int alpha=160, bool include=true)
Set the legend properties.
Definition: imageexporter.cpp:246
ImageExporter::ImageExporter
ImageExporter(QObject *parent)
Constructor.
Definition: imageexporter.cpp:32
KStars::map
SkyMap * map() const
Definition: kstars.h:134
QObject
SkyQPainter::begin
virtual void begin()
Begin painting.
Definition: skyqpainter.cpp:110
KStars
This is the main window for KStars.
Definition: kstars.h:94
SkyQPainter
The QPainter-based painting backend.
Definition: skyqpainter.h:32
Legend::setBgColor
void setBgColor(const QColor &color)
Set background color.
Definition: legend.h:260
Legend::LEGEND_POSITION
LEGEND_POSITION
Legend position enumeration.
Definition: legend.h:73
skymap.h
Legend::LEGEND_TYPE
LEGEND_TYPE
Legend type enumeration.
Definition: legend.h:52
Legend::setType
void setType(LEGEND_TYPE type)
Set legend type.
Definition: legend.h:188
Legend::getBgColor
QColor getBgColor()
Get background color.
Definition: legend.h:176
Legend::LEGEND_ORIENTATION
LEGEND_ORIENTATION
Legend orientation enumeration.
Definition: legend.h:64
imageexporter.h
Legend
Legend class is used for painting legends on class inheriting QPaintDevice.
Definition: legend.h:45
SkyMap::exportSkyImage
void exportSkyImage(QPaintDevice *pd, bool scale=false)
Proxy method for SkyMapDrawAbstract::exportSkyImage()
Definition: skymap.h:270
skyqpainter.h
SkyMap
This is the canvas on which the sky is painted.
Definition: skymap.h:72
Legend::setFont
void setFont(const QFont &font)
Set font.
Definition: legend.h:254
Legend::setPosition
void setPosition(LEGEND_POSITION pos)
Set legend position.
Definition: legend.h:200
Legend::setOrientation
void setOrientation(LEGEND_ORIENTATION orientation)
Set legend orientation.
Definition: legend.h:194
Legend::paintLegend
void paintLegend(QPaintDevice *pd)
Paint legend on passed QPaintDevice at selected position.
Definition: legend.cpp:162
ImageExporter::~ImageExporter
~ImageExporter()
Destructor.
Definition: imageexporter.cpp:262
kstars.h
SkyQPainter::end
virtual void end()
End and finalize painting.
Definition: skyqpainter.cpp:119
ImageExporter::setRasterOutputSize
void setRasterOutputSize(const QSize *size)
Set the size of output raster images.
Definition: imageexporter.cpp:267
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:36:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kstars

Skip menu "kstars"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal