VPTissue Reference Manual
BitmapGraphicsExporter.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 "BitmapGraphicsExporter.h"
21 
22 #include "bio/Mesh.h"
23 #include "math/MeshGeometry.h"
24 #include "mesh_drawer/MeshDrawer.h"
25 #include "sim/Sim.h"
27 
28 #include <QDir>
29 #include <QGraphicsScene>
30 #include <QImage>
31 #include <QPainter>
32 #include <QPointF>
33 #include <QPrinter>
34 #include <QRectF>
35 #include <QString>
36 
37 #include <iostream>
38 #include <fstream>
39 #include <sstream>
40 
41 using namespace boost::property_tree;
42 using namespace std;
43 using namespace SimPT_Sim;
45 
46 namespace SimPT_Shell {
47 
48 bool BitmapGraphicsExporter::Export(shared_ptr<SimPT_Sim::Sim> sim,
49  const string& file_path,
50  bool overwrite,
51  std::shared_ptr<BitmapGraphicsPreferences> prefs)
52 {
53  if (ifstream(file_path).good()) {
54  if (overwrite) {
55  // Qt doesn't overwrite by default, must delete file first
56  QFile::remove(QString::fromStdString(file_path));
57  } else {
58  return false; // Don't overwrite
59  }
60  }
61 
62  QGraphicsScene canvas;
63  MeshDrawer m(prefs);
64  m.Draw(sim, &canvas);
65 
66  QPointF top_left;
67  QPointF bottom_right;
68  if (prefs->m_window_preset) {
69  top_left = QPointF(prefs->m_window_x_min, prefs->m_window_y_min);
70  bottom_right = QPointF(prefs->m_window_x_max, prefs->m_window_y_max);
71  } else {
72  const auto box = MeshGeometry::BoundingBox(shared_ptr<Mesh>(sim->GetCoreData().m_mesh.get(), [](Mesh*){}));
73  const auto min_p = get<0>(box) - 1.05 * (get<1>(box) - get<0>(box));
74  const auto max_p = get<1>(box) + 1.05 * (get<1>(box) - get<0>(box));
75  top_left = QPointF(get<0>(min_p), get<1>(min_p));
76  bottom_right = QPointF(get<0>(max_p), get<1>(max_p));
77  }
78 
79  int width;
80  int height;
81  if (prefs->m_size_preset) {
82  width = prefs->m_size_x;
83  height = prefs->m_size_y;
84  } else {
85  width = 800;
86  height = 600;
87  }
88 
89  string format;
90  switch (prefs->m_format) {
91  case BitmapGraphicsPreferences::Png:
92  format = "PNG";
93  break;
94  case BitmapGraphicsPreferences::Bmp:
95  format = "BMP";
96  break;
97  case BitmapGraphicsPreferences::Jpeg:
98  format = "JPEG";
99  break;
100  }
101 
102  bool status = false;
103  if (!file_path.empty()) {
104  QImage image(QSize(width, height), QImage::Format_RGB32);
105  image.fill(QColor(QString::fromStdString(prefs->m_background_color)).rgb());
106  QPainter painter(&image);
107  canvas.render(&painter, QRectF(), QRectF(top_left, bottom_right));
108  status = image.save(QDir::toNativeSeparators(QString::fromStdString(file_path)), format.c_str());
109  }
110 
111  return status;
112 }
113 
114 } // namespace
STL namespace.
Class drawing a simulator/mesh onto a QGraphicsScene.
Definition: MeshDrawer.h:34
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.
Sim, the actual simulator.
Interface for MeshDrawer.
Interface for BitmapGraphicsExporter.
Provides wall-clock time stamp using the time call.
Definition: TimeStamp.h:37
Structure of cells; key data structure.
Definition: Mesh.h:62
Interface for Mesh.