VPTissue Reference Manual
RegularGeneratorDialog.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  */
21 
22 #include "editor/PolygonUtils.h"
29 #include "gui/PanAndZoomView.h"
31 
32 #include <QBoxLayout>
33 #include <QBrush>
34 #include <QComboBox>
35 #include <QDialogButtonBox>
36 #include <QGraphicsScene>
37 #include <QGraphicsPolygonItem>
38 #include <QPen>
39 
40 #include <cassert>
41 
42 namespace SimPT_Editor {
43 
44 using namespace SimShell;
45 using namespace SimShell::Gui;
46 
50 class RegularGeneratorDialog::ITileFactory
51 {
52 public:
53  virtual ~ITileFactory() {}
54  virtual Tile *Create() const = 0;
55 };
56 
60 template <class TileType>
61 class RegularGeneratorDialog::TileFactory : public RegularGeneratorDialog::ITileFactory
62 {
63 public:
64  virtual ~TileFactory() {}
65  virtual Tile *Create() const { return new TileType(); }
66 };
67 
68 const double RegularGeneratorDialog::g_scene_margin = 2.0;
69 const std::vector<std::pair<std::shared_ptr<const RegularGeneratorDialog::ITileFactory>, std::string>> RegularGeneratorDialog::g_tile_factories{
70  std::make_pair(std::make_shared<TileFactory<RectangularTile>>(), "Rectangles"),
71  std::make_pair(std::make_shared<TileFactory<TriangularTile>>(), "Triangles"),
72  std::make_pair(std::make_shared<TileFactory<DiamondTile>>(), "Diamonds"),
73  std::make_pair(std::make_shared<TileFactory<HexagonalTile>>(), "Hexagons")};
74 
75 RegularGeneratorDialog::RegularGeneratorDialog(const QPolygonF &boundaryPolygon, double initialScale, QWidget *parent)
76  : QDialog(parent)
77 {
78  SetupSceneItems(boundaryPolygon);
79  SetupGui(initialScale);
80 }
81 
83 {
84 }
85 
87 {
88  std::list<QPolygonF> polygons;
89  for (const QPolygonF& polygon : m_tiling->GetPolygons()) {
90  auto openPolygon = PolygonUtils::OpenPolygon(polygon);
91  for (const QPolygonF& subpolygon : PolygonUtils::ClipPolygon(openPolygon, m_boundary->polygon())) {
92  polygons.push_back(PolygonUtils::Counterclockwise(subpolygon));
93  }
94  }
95  return polygons;
96 }
97 
98 void RegularGeneratorDialog::UpdateTiling(int index)
99 {
100  delete m_tiling;
101  m_tiling = new RegularTiling(g_tile_factories[index].first->Create(), m_boundary->boundingRect(), m_boundary);
102  UpdateTransformation();
103 }
104 
105 void RegularGeneratorDialog::UpdateTransformation()
106 {
107  QTransform transformation;
108 
109  QPointF polygonCenter = m_boundary->boundingRect().center();
110  transformation.translate(m_transformation->GetTranslationX(), m_transformation->GetTranslationY());
111 
112  transformation.translate(polygonCenter.x(), polygonCenter.y());
113  transformation.rotate(m_transformation->GetRotation());
114  transformation.scale(m_transformation->GetScalingX(), m_transformation->GetScalingY());
115  transformation.translate(-polygonCenter.x(), -polygonCenter.y());
116 
117  m_tiling->setTransform(transformation);
118 }
119 
120 void RegularGeneratorDialog::SetupSceneItems(const QPolygonF &boundaryPolygon)
121 {
122  m_scene = new QGraphicsScene();
123 
124  m_boundary = new QGraphicsPolygonItem(boundaryPolygon);
125  m_boundary->setPen(QPen(QBrush(QColor(51, 102, 0, 255)), 0));
126  m_scene->addItem(m_boundary);
127 
128  m_boundary->setFlag(QGraphicsItem::ItemClipsChildrenToShape, true);
129  m_tiling = new RegularTiling(g_tile_factories[0].first->Create(), m_boundary->boundingRect(), m_boundary);
130 
131  m_scene->setSceneRect(m_boundary->boundingRect().adjusted(-g_scene_margin, -g_scene_margin, g_scene_margin, g_scene_margin));
132 }
133 
134 void RegularGeneratorDialog::SetupGui(double scale)
135 {
136  setWindowTitle("Regular cell pattern generator");
137  setMinimumSize(800, 500);
138 
139  // > GUI layout
140  QHBoxLayout *layout = new QHBoxLayout();
141 
142  // > Generator graphics view
143  PanAndZoomView *view = new PanAndZoomView();
144  view->setParent(this);
145  view->ScaleView(scale);
146  view->setScene(m_scene);
147  view->setRenderHints(QPainter::Antialiasing);
148 
149  layout->addWidget(view, 3);
150  // < Generator graphics view
151 
152  // > Tiling selection, sliders and dialog button box layout
153  QVBoxLayout *controlsLayout = new QVBoxLayout();
154 
155  // > Tiling selection
156  QComboBox *tilingSelection = new QComboBox();
157  for (auto pair : g_tile_factories) {
158  tilingSelection->addItem(QString::fromStdString(pair.second));
159  }
160  tilingSelection->setCurrentIndex(0);
161  connect(tilingSelection, SIGNAL(currentIndexChanged(int)), this, SLOT(UpdateTiling(int)));
162  controlsLayout->addWidget(tilingSelection);
163  // < Tiling selection
164 
165  QFrame *line = new QFrame();
166  line->setFrameShape(QFrame::HLine);
167  line->setFrameShadow(QFrame::Sunken);
168  controlsLayout->addWidget(line);
169 
170  // > Transformations
171  m_transformation = new TransformationWidget(10, m_boundary->boundingRect().width() / 2, m_boundary->boundingRect().height() / 2);
172  connect(m_transformation, SIGNAL(TransformationChanged()), this, SLOT(UpdateTransformation()));
173  controlsLayout->addWidget(m_transformation);
174  // < Transformations
175 
176  controlsLayout->addStretch();
177 
178  // > Dialog button box
179  QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
180  connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
181  connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
182  controlsLayout->addWidget(buttonBox);
183  // < Dialog button box
184 
185  layout->addLayout(controlsLayout, 1);
186  // < Tiling selection, sliders and dialog button box layout
187 
188  setLayout(layout);
189  // < GUI layout
190 }
191 
192 } // namespace
QGraphicsView with the ability to pan and zoom.
static QPolygonF OpenPolygon(const QPolygonF &polygon)
Opens the polygon, ie.
Namespace for SimPT tissue editor package.
Definition: Cell.h:32
Combo header for circular iterator.
Interface for RegularGeneratorDialog.
static std::list< QPolygonF > ClipPolygon(const QPolygonF &polygon1, const QPolygonF &polygon2)
Clip a polygon with another polygon and return the intersecting subpolygons.
Interface for TriangularTile.
see the online Qt documentation
Interface for DiamondTile.
static QPolygonF Counterclockwise(const QPolygonF &polygon)
Checks if the order of the polygon's points is clockwise and if so, reverts it, else the original pol...
Namespace for graphical interface classes.
double GetTranslationY()
Gets the y translation of the transformation.
Interface for TransformationWidget.
int GetRotation()
Gets the rotation of the transformation.
Interface for RegularTiling.
Interface for PanAndZoomView.
std::list< QPolygonF > GetPolygons() const
Get the polygons associated with this tiling.
Interface for HexagonalTile.
double GetScalingX()
Gets the x scaling of the transformation.
double GetTranslationX()
Gets the x translation of the transformation.
Class of a cell tiling with regular polygons.
Definition: RegularTiling.h:34
double GetScalingY()
Gets the y scaling of the transformation.
see the online Qt documentation
std::list< QPolygonF > GetGeneratedPolygons() const
Retrieves the generated polygons after the dialog has successfully executed.
void ScaleView(double factor)
Scales the view, bounded by the minimum and maximum zooming factor.
Interface for PolygonUtils.
Namespace for generic graphical shell for simulators.
Definition: SimSession.h:32
see the online Qt documentation
RegularGeneratorDialog(const QPolygonF &boundaryPolygon, double initialScale=1.0, QWidget *parent=nullptr)
Constructor.
Interface for RectangularTile.