UltraScan III
us_gui_util.cpp
Go to the documentation of this file.
1 #include <QtSvg>
3 
4 #include "us_gui_util.h"
5 #include "us_gzip.h"
6 #include "us_settings.h"
7 
8 // Save SVG+PNG or PNG file
9 int US_GuiUtil::save_plot( const QString& filename, const QwtPlot* plot )
10 {
11  int status = 0;
12 
13  if ( filename.contains( ".svg" ) )
14  { // Save the file as SVG, then save a PNG version
15  save_svg( filename, plot );
16 
17  QString fnamepng = QString( filename ).section( ".", 0, -2 ) + ".png";
18  save_png( fnamepng, plot );
19  }
20 
21  else if ( filename.endsWith( ".png" ) )
22  { // Save the file as PNG
23  save_png( filename, plot );
24  }
25 
26  else
27  { // Flag an error: file name does not end in ".svg" or ".png"
28  status = 1;
29  }
30 
31  return status;
32 }
33 
34 // Save SVG file
35 int US_GuiUtil::save_svg( const QString& filename, const QwtPlot* plot )
36 {
37  int status = 0;
38 
39  if ( filename.contains( ".svg" ) )
40  { // Save the file as SVG
41  QSvgGenerator generator;
42  QString fnsvg = QString( filename ).section( ".", 0, -2 ) + ".svg";
43 
44  // Set resolution to screen resolution
45  double px = (double)qApp->desktop()->width();
46  double in = (double)qApp->desktop()->widthMM() / 25.4;
47  int res = qRound( px / in );
48  int pw = plot->width() + res;
49  int ph = plot->height() + res;
50 
51  // Generate the SVG file
52  generator.setResolution( res );
53  generator.setFileName ( fnsvg );
54  generator.setSize ( plot->size() );
55  generator.setViewBox ( QRect( QPoint( 0, 0 ), QPoint( pw, ph ) ) );
56 
57  plot->print( generator );
58 
59  // Compress it and save SVGZ file
60  US_Gzip gz;
61  gz.gzip( fnsvg );
62  }
63 
64  else
65  {
66  status = 1;
67  }
68 
69  return status;
70 }
71 
72 // Save PNG file
73 int US_GuiUtil::save_png( const QString& filename, const QwtPlot* plot )
74 {
75  int status = 0;
76 
77 
78  if ( filename.endsWith( ".png" ) )
79  { // Save the file as a PNG version
80  int pw = plot->width();
81  int ph = plot->height();
82 
83  QPixmap pixmap = QPixmap::grabWidget( (QWidget*)plot, 0, 0, pw, ph );
84  if ( ! pixmap.save( filename ) )
85  status = 2;
86  }
87 
88  else
89  { // Mark error: filename does not end with ".png"
90  status = 1;
91  }
92 
93  return status;
94 }
95