VPTissue Reference Manual
VectorGraphicsExporter.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "VectorGraphicsExporter.h"
21 
22 #include "bio/Mesh.h"
23 #include "math/MeshGeometry.h"
24 #include "mesh_drawer/MeshDrawer.h"
25 #include "sim/CoreData.h"
26 #include "sim/SimInterface.h"
28 
29 #include <QBrush>
30 #include <QDir>
31 #include <QGraphicsScene>
32 #include <QImage>
33 #include <QPainter>
34 #include <QPointF>
35 #include <QPrinter>
36 #include <QRectF>
37 #include <QString>
38 
39 #include <iostream>
40 #include <fstream>
41 #include <sstream>
42 
43 using namespace boost::property_tree;
44 using namespace std;
45 using namespace SimPT_Sim;
47 
48 namespace SimPT_Shell {
49 
50 bool VectorGraphicsExporter::Export(shared_ptr<SimPT_Sim::SimInterface> sim,
51  const string& file_path,
52  bool overwrite,
53  std::shared_ptr<VectorGraphicsPreferences> prefs)
54 {
55  if (ifstream(file_path).good()) {
56  if (overwrite) {
57  // Qt doesn't overwrite by default, must delete file first
58  QFile::remove(QString::fromStdString(file_path));
59  } else {
60  return false; // Don't overwrite
61  }
62  }
63 
64  QGraphicsScene canvas;
65  MeshDrawer m(prefs);
66  m.Draw(sim, &canvas);
67 
68  QPointF top_left;
69  QPointF bottom_right;
70  if (prefs->m_window_preset) {
71  top_left = QPointF(prefs->m_window_x_min, prefs->m_window_y_min);
72  bottom_right = QPointF(prefs->m_window_x_max, prefs->m_window_y_max);
73  } else {
74  const auto box = MeshGeometry::BoundingBox(sim->GetCoreData().m_mesh);
75  const auto min_p = get<0>(box) - 1.05 * (get<1>(box) - get<0>(box));
76  const auto max_p = get<1>(box) + 1.05 * (get<1>(box) - get<0>(box));
77  top_left = QPointF(get<0>(min_p), get<1>(min_p));
78  bottom_right = QPointF(get<0>(max_p), get<1>(max_p));
79  }
80 
81  bool status = false;
82  if (!file_path.empty()) {
83  QPrinter printer(QPrinter::HighResolution);
84  printer.setOutputFileName(QString::fromStdString(file_path));
85  printer.setOutputFormat(QPrinter::PdfFormat);
86  switch (prefs->m_format) {
87  case VectorGraphicsPreferences::Pdf:
88  printer.setOutputFormat(QPrinter::PdfFormat);
89  break;
90  case VectorGraphicsPreferences::Eps:
91  // printer.setOutputFormat(QPrinter::EpsFormat); // EPS not supported by Qt??
92  break;
93  }
94  printer.setColorMode(QPrinter::ColorMode::Color);
95  QPainter painter(&printer);
96  canvas.setBackgroundBrush(QBrush(QColor(QString::fromStdString(prefs->m_background_color)).rgb()));
97  canvas.render(&painter, QRectF(), QRectF(top_left, bottom_right));
98  }
99 
100  return status;
101 }
102 
103 string VectorGraphicsExporter::GetFileExtension()
104 {
105  return "pdf";
106 }
107 
108 } // namespace
Simulator interface.
STL namespace.
Class drawing a simulator/mesh onto a QGraphicsScene.
Definition: MeshDrawer.h:34
Core data used during model execution.
Interface for MeshGeometry.
Namespace for SimPT shell package.
Definition: Client.cpp:50
Namespace for the core simulator.
void Draw(std::shared_ptr< SimPT_Sim::SimInterface > sim, QGraphicsScene *scene)
Draws the mesh to QGraphicsScene.
Definition: MeshDrawer.cpp:64
TimeStamp class.
Interface for VectorGraphicsExporter.
Interface for MeshDrawer.
Provides wall-clock time stamp using the time call.
Definition: TimeStamp.h:37
Interface for Mesh.