@@ -92,6 +92,90 @@ getExportImageFormatFromString(const std::wstring& extension)
9292 return ERROR_EXPORT;
9393}
9494// =============================================================================
95+ static bool exportToPDF (GOWindow* f, const std::wstring& filename, GOFigure* hf)
96+ {
97+ QPrinter printer (QPrinter::ScreenResolution);
98+ printer.setOutputFormat (QPrinter::PdfFormat);
99+ printer.setOutputFileName (wstringToQString (filename));
100+ QPainter painter;
101+ painter.begin (&printer);
102+ const auto pageLayout = printer.pageLayout ();
103+ const auto pageRect = pageLayout.paintRectPixels (printer.resolution ());
104+ const auto paperRect = pageLayout.fullRectPixels (printer.resolution ());
105+ double xscale = pageRect.width () / double (f->width ());
106+ double yscale = pageRect.height () / double (f->height ());
107+ double scale = qMin (xscale, yscale);
108+ painter.translate (
109+ pageRect.x () + paperRect.width () / 2 ., pageRect.y () + paperRect.height () / 2 .);
110+ painter.scale (scale, scale);
111+ painter.translate (-f->width () / 2 ., -f->height () / 2 .);
112+ RenderQt gc (&painter, 0 , 0 , f->width (), f->height (), L" PDF" );
113+ hf->paintMe (gc);
114+ return true ;
115+ }
116+ // =============================================================================
117+ static bool exportToImage (GOWindow* f, const std::wstring& filename, IMAGE_FORMAT exportFormat)
118+ {
119+ QPixmap pxmap (f->getMainQWigdet ()->grab ());
120+ QImage img (pxmap.toImage ());
121+ return img.save (wstringToQString (filename),
122+ wstring_to_utf8 (getExportImageFormatAsString (exportFormat)).c_str ());
123+ }
124+ // =============================================================================
125+ static bool exportToSVG (GOWindow* f, const std::wstring& filename, GOFigure* hf)
126+ {
127+ QSvgGenerator gen;
128+ int width = f->width ();
129+ int height = f->height ();
130+ gen.setDescription (QString (" " ));
131+ gen.setTitle (QString (" " ));
132+ gen.setFileName (wstringToQString (filename));
133+ gen.setSize (QSize (width, height));
134+ qreal dpi = 96.0 ;
135+ QScreen* screen = nullptr ;
136+ QWidget* mainWidget = f->getMainQWigdet ();
137+ if (mainWidget) {
138+ #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
139+ screen = mainWidget->screen ();
140+ #else
141+ screen = QGuiApplication::screenAt (mainWidget->mapToGlobal (QPoint (0 , 0 )));
142+ #endif
143+ }
144+ if (!screen) {
145+ screen = QGuiApplication::primaryScreen ();
146+ }
147+ if (screen) {
148+ dpi = screen->logicalDotsPerInch ();
149+ }
150+ gen.setResolution (dpi);
151+ gen.setViewBox (QRect (0 , 0 , width, height));
152+ QPainter pnt (&gen);
153+ RenderQt gc (&pnt, 0 , 0 , width, height, L" SVG" );
154+ hf->paintMe (gc);
155+ return true ;
156+ }
157+ // =============================================================================
158+ #if WITH_TIFF
159+ static bool exportToTIFF (GOWindow* f, const std::wstring& filename)
160+ {
161+ QPixmap pxmap (f->getMainQWigdet ()->grab ());
162+ QImage img (pxmap.toImage ());
163+ QString errorMessage;
164+ return TiffFileHandler::writeTiff (wstringToQString (filename), img, errorMessage);
165+ }
166+ #endif
167+ // =============================================================================
168+ #if WITH_GIF
169+ static bool exportToGIF (GOWindow* f, const std::wstring& filename)
170+ {
171+ QPixmap pxmap (f->getMainQWigdet ()->grab ());
172+ QImage img (pxmap.toImage ());
173+ QGifImage gif (QSize (img.width (), img.height ()));
174+ gif.addFrame (img);
175+ return gif.save (wstringToQString (filename));
176+ }
177+ #endif
178+ // =============================================================================
95179bool
96180ExportGraphics (GOWindow* f, const std::wstring& filename, IMAGE_FORMAT exportFormat)
97181{
@@ -107,87 +191,34 @@ ExportGraphics(GOWindow* f, const std::wstring& filename, IMAGE_FORMAT exportFor
107191 double cb = color->at (2 );
108192 hf->setThreeVectorDefault (GO_COLOR_PROPERTY_NAME_STR, 1 , 1 , 1 );
109193 hf->updateState ();
194+
110195 switch (exportFormat) {
111- case PDF_EXPORT: {
112- QPrinter printer (QPrinter::ScreenResolution);
113- printer.setOutputFormat (QPrinter::PdfFormat);
114- printer.setOutputFileName (wstringToQString (filename));
115- QPainter painter;
116- painter.begin (&printer);
117- const auto pageLayout = printer.pageLayout ();
118- const auto pageRect = pageLayout.paintRectPixels (printer.resolution ());
119- const auto paperRect = pageLayout.fullRectPixels (printer.resolution ());
120- double xscale = pageRect.width () / double (f->width ());
121- double yscale = pageRect.height () / double (f->height ());
122- double scale = qMin (xscale, yscale);
123- painter.translate (
124- pageRect.x () + paperRect.width () / 2 ., pageRect.y () + paperRect.height () / 2 .);
125- painter.scale (scale, scale);
126- painter.translate (-f->width () / 2 ., -f->height () / 2 .);
127- RenderQt gc (&painter, 0 , 0 , f->width (), f->height (), L" PDF" );
128- hf->paintMe (gc);
129- result = true ;
130- } break ;
196+ case PDF_EXPORT:
197+ result = exportToPDF (f, filename, hf);
198+ break ;
131199 case PNG_EXPORT:
132- case JPG_EXPORT: {
133- QPixmap pxmap (f->getMainQWigdet ()->grab ());
134- QImage img (pxmap.toImage ());
135- result = img.save (wstringToQString (filename),
136- wstring_to_utf8 (getExportImageFormatAsString (exportFormat)).c_str ());
137- } break ;
138- case SVG_EXPORT: {
139- QSvgGenerator gen;
140- int width = f->width ();
141- int height = f->height ();
142- gen.setDescription (QString (" " ));
143- gen.setTitle (QString (" " ));
144- gen.setFileName (wstringToQString (filename));
145- gen.setSize (QSize (width, height));
146- qreal dpi = 96.0 ;
147- QScreen* screen = nullptr ;
148- QWidget* mainWidget = f->getMainQWigdet ();
149- if (mainWidget) {
150- #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
151- screen = mainWidget->screen ();
152- #else
153- screen = QGuiApplication::screenAt (mainWidget->mapToGlobal (QPoint (0 , 0 )));
154- #endif
155- }
156- if (!screen) {
157- screen = QGuiApplication::primaryScreen ();
158- }
159- if (screen) {
160- dpi = screen->logicalDotsPerInch ();
161- }
162- gen.setResolution (dpi);
163- gen.setViewBox (QRect (0 , 0 , width, height));
164- QPainter pnt (&gen);
165- RenderQt gc (&pnt, 0 , 0 , width, height, L" SVG" );
166- hf->paintMe (gc);
167- result = true ;
168- } break ;
200+ case JPG_EXPORT:
201+ result = exportToImage (f, filename, exportFormat);
202+ break ;
203+ case SVG_EXPORT:
204+ result = exportToSVG (f, filename, hf);
205+ break ;
169206#if WITH_TIFF
170- case TIF_EXPORT: {
171- QPixmap pxmap (f->getMainQWigdet ()->grab ());
172- QImage img (pxmap.toImage ());
173- QString errorMessage;
174- result = TiffFileHandler::writeTiff (wstringToQString (filename), img, errorMessage);
175- } break ;
207+ case TIF_EXPORT:
208+ result = exportToTIFF (f, filename);
209+ break ;
176210#endif
177211#if WITH_GIF
178- case GIF_EXPORT: {
179- QPixmap pxmap (f->getMainQWigdet ()->grab ());
180- QImage img (pxmap.toImage ());
181- QGifImage gif (QSize (img.width (), img.height ()));
182- gif.addFrame (img);
183- result = gif.save (wstringToQString (filename));
184- } break ;
212+ case GIF_EXPORT:
213+ result = exportToGIF (f, filename);
214+ break ;
185215#endif
186216 case ERROR_EXPORT:
187- default : {
217+ default :
188218 result = false ;
189- } break ;
219+ break ;
190220 }
221+
191222 hf->setThreeVectorDefault (GO_COLOR_PROPERTY_NAME_STR, cr, cg, cb);
192223 hf->updateState ();
193224 return result;
0 commit comments