src/editmodes/abstractEditMode.cpp

changeset 1217
314e12e23c3a
parent 1215
77a0270352a3
child 1222
34def2630300
--- a/src/editmodes/abstractEditMode.cpp	Thu Jan 04 19:40:52 2018 +0200
+++ b/src/editmodes/abstractEditMode.cpp	Thu Jan 04 19:44:26 2018 +0200
@@ -30,30 +30,30 @@
 #include "../glRenderer.h"
 #include "../miscallenous.h"
 
-ConfigOption (bool DrawLineLengths = true)
-ConfigOption (bool DrawAngles = false)
+ConfigOption(bool DrawLineLengths = true)
+ConfigOption(bool DrawAngles = false)
 
-AbstractEditMode::AbstractEditMode (GLRenderer* renderer) :
-	QObject (renderer),
-	HierarchyElement (renderer),
-	m_renderer (renderer) {}
+AbstractEditMode::AbstractEditMode(GLRenderer* renderer) :
+	QObject(renderer),
+	HierarchyElement(renderer),
+	m_renderer(renderer) {}
 
 AbstractEditMode::~AbstractEditMode() {}
 
-AbstractEditMode* AbstractEditMode::createByType (GLRenderer* renderer, EditModeType type)
+AbstractEditMode* AbstractEditMode::createByType(GLRenderer* renderer, EditModeType type)
 {
-	switch (type)
+	switch(type)
 	{
-	case EditModeType::Select: return new SelectMode (renderer);
-	case EditModeType::Draw: return new DrawMode (renderer);
-	case EditModeType::Rectangle: return new RectangleMode (renderer);
-	case EditModeType::Circle: return new CircleMode (renderer);
-	case EditModeType::MagicWand: return new MagicWandMode (renderer);
-	case EditModeType::LinePath: return new LinePathMode (renderer);
-	case EditModeType::Curve: return new CurveMode (renderer);
+	case EditModeType::Select: return new SelectMode(renderer);
+	case EditModeType::Draw: return new DrawMode(renderer);
+	case EditModeType::Rectangle: return new RectangleMode(renderer);
+	case EditModeType::Circle: return new CircleMode(renderer);
+	case EditModeType::MagicWand: return new MagicWandMode(renderer);
+	case EditModeType::LinePath: return new LinePathMode(renderer);
+	case EditModeType::Curve: return new CurveMode(renderer);
 	}
 
-	throw std::logic_error ("bad type given to AbstractEditMode::createByType");
+	throw std::logic_error("bad type given to AbstractEditMode::createByType");
 }
 
 GLRenderer* AbstractEditMode::renderer() const
@@ -61,53 +61,53 @@
 	return m_renderer;
 }
 
-AbstractDrawMode::AbstractDrawMode (GLRenderer* renderer) :
-	AbstractEditMode (renderer),
-	m_polybrush (QBrush (QColor (64, 192, 0, 128)))
+AbstractDrawMode::AbstractDrawMode(GLRenderer* renderer) :
+	AbstractEditMode(renderer),
+	m_polybrush(QBrush(QColor(64, 192, 0, 128)))
 {
-	renderer->setContextMenuPolicy (Qt::NoContextMenu); // We need the right mouse button for removing vertices
-	renderer->setCursor (Qt::CrossCursor);
+	renderer->setContextMenuPolicy(Qt::NoContextMenu); // We need the right mouse button for removing vertices
+	renderer->setCursor(Qt::CrossCursor);
 	m_window->currentDocument()->clearSelection();
 	m_window->updateSelection();
 	m_drawedVerts.clear();
 }
 
-AbstractSelectMode::AbstractSelectMode (GLRenderer* renderer) :
-	AbstractEditMode (renderer)
+AbstractSelectMode::AbstractSelectMode(GLRenderer* renderer) :
+	AbstractEditMode(renderer)
 {
 	renderer->unsetCursor();
-	renderer->setContextMenuPolicy (Qt::DefaultContextMenu);
+	renderer->setContextMenuPolicy(Qt::DefaultContextMenu);
 }
 
 // =============================================================================
 //
-void AbstractDrawMode::addDrawnVertex (Vertex const& pos)
+void AbstractDrawMode::addDrawnVertex(Vertex const& pos)
 {
-	if (preAddVertex (pos))
+	if (preAddVertex(pos))
 		return;
 
 	m_drawedVerts << pos;
 }
 
-bool AbstractDrawMode::mouseReleased (MouseEventData const& data)
+bool AbstractDrawMode::mouseReleased(MouseEventData const& data)
 {
-	if (Super::mouseReleased (data))
+	if (Super::mouseReleased(data))
 		return true;
 
-	if ((data.releasedButtons & Qt::MidButton) and (m_drawedVerts.size() < 4) and (not data.mouseMoved))
+	if ((data.releasedButtons & Qt::MidButton) and(m_drawedVerts.size() < 4) and(not data.mouseMoved))
 	{
 		// Find the closest vertex to our cursor
 		double			minimumDistance = 1024.0;
 		const Vertex*	closest = nullptr;
-		Vertex			cursorPosition = renderer()->convert2dTo3d (data.ev->pos(), false);
-		QPoint			cursorPosition2D (data.ev->pos());
+		Vertex			cursorPosition = renderer()->convert2dTo3d(data.ev->pos(), false);
+		QPoint			cursorPosition2D(data.ev->pos());
 		const Axis		relZ = renderer()->getRelativeZ();
 		QVector<Vertex>	vertices = renderer()->document()->inlineVertices();
 
 		// Sort the vertices in order of distance to camera
-		std::sort (vertices.begin(), vertices.end(), [&](const Vertex& a, const Vertex& b) -> bool
+		std::sort(vertices.begin(), vertices.end(), [&](const Vertex& a, const Vertex& b) -> bool
 		{
-			if (renderer()->getFixedCamera (renderer()->camera()).negatedDepth)
+			if (renderer()->getFixedCamera(renderer()->camera()).negatedDepth)
 				return a[relZ] > b[relZ];
 
 			return a[relZ] < b[relZ];
@@ -116,8 +116,8 @@
 		for (const Vertex& vrt : vertices)
 		{
 			// If the vertex in 2d space is very close to the cursor then we use it regardless of depth.
-			QPoint vect2d = renderer()->convert3dTo2d (vrt) - cursorPosition2D;
-			const double distance2DSquared = std::pow (vect2d.x(), 2) + std::pow (vect2d.y(), 2);
+			QPoint vect2d = renderer()->convert3dTo2d(vrt) - cursorPosition2D;
+			const double distance2DSquared = std::pow(vect2d.x(), 2) + std::pow(vect2d.y(), 2);
 			if (distance2DSquared < 16.0 * 16.0)
 			{
 				closest = &vrt;
@@ -140,12 +140,12 @@
 		}
 
 		if (closest)
-			addDrawnVertex (*closest);
+			addDrawnVertex(*closest);
 
 		return true;
 	}
 
-	if ((data.releasedButtons & Qt::RightButton) and (not m_drawedVerts.isEmpty()))
+	if ((data.releasedButtons & Qt::RightButton) and(not m_drawedVerts.isEmpty()))
 	{
 		// Remove the last vertex
 		m_drawedVerts.removeLast();
@@ -160,14 +160,14 @@
 			return true;
 		}
 
-		addDrawnVertex (getCursorVertex());
+		addDrawnVertex(getCursorVertex());
 		return true;
 	}
 
 	return false;
 }
 
-void AbstractDrawMode::finishDraw (LDObjectList const& objs)
+void AbstractDrawMode::finishDraw(LDObjectList const& objs)
 {
 	int pos = m_window->suggestInsertPoint();
 
@@ -175,8 +175,8 @@
 	{
 		for (LDObject* obj : objs)
 		{
-			renderer()->document()->insertObj (pos++, obj);
-			renderer()->compileObject (obj);
+			renderer()->document()->insertObj(pos++, obj);
+			renderer()->compileObject(obj);
 		}
 
 		m_window->refresh();
@@ -186,74 +186,74 @@
 	m_drawedVerts.clear();
 }
 
-void AbstractDrawMode::drawLength (QPainter &painter, const Vertex &v0, const Vertex &v1,
+void AbstractDrawMode::drawLength(QPainter &painter, const Vertex &v0, const Vertex &v1,
 	const QPointF& v0p, const QPointF& v1p) const
 {
 	if (not config.drawLineLengths())
 		return;
 
-	const QString label = QString::number ((v1 - v0).length());
-	QPoint origin = QLineF (v0p, v1p).pointAt (0.5).toPoint();
-	painter.drawText (origin, label);
+	const QString label = QString::number((v1 - v0).length());
+	QPoint origin = QLineF(v0p, v1p).pointAt(0.5).toPoint();
+	painter.drawText(origin, label);
 }
 
-void AbstractDrawMode::renderPolygon (QPainter& painter, const QVector<Vertex>& poly3d,
+void AbstractDrawMode::renderPolygon(QPainter& painter, const QVector<Vertex>& poly3d,
 	bool withlengths, bool withangles) const
 {
-	QVector<QPoint> poly (poly3d.size());
-	QFontMetrics metrics = QFontMetrics (QFont());
+	QVector<QPoint> poly(poly3d.size());
+	QFontMetrics metrics = QFontMetrics(QFont());
 
 	// Convert to 2D
 	for (int i = 0; i < poly3d.size(); ++i)
-		poly[i] = renderer()->convert3dTo2d (poly3d[i]);
+		poly[i] = renderer()->convert3dTo2d(poly3d[i]);
 
 	// Draw the polygon-to-be
-	painter.setBrush (m_polybrush);
-	painter.drawPolygon (QPolygonF (poly));
+	painter.setBrush(m_polybrush);
+	painter.drawPolygon(QPolygonF(poly));
 
 	// Draw vertex blips
 	for (int i = 0; i < poly3d.size(); ++i)
 	{
-		renderer()->drawBlip (painter, poly[i]);
-		renderer()->drawBlipCoordinates (painter, poly3d[i], poly[i]);
+		renderer()->drawBlip(painter, poly[i]);
+		renderer()->drawBlipCoordinates(painter, poly3d[i], poly[i]);
 	}
 
 	// Draw line lenghts and angle info if appropriate
-	if (poly3d.size() >= 2 and (withlengths or withangles))
+	if (poly3d.size() >= 2 and(withlengths or withangles))
 	{
-		painter.setPen (renderer()->textPen());
+		painter.setPen(renderer()->textPen());
 
 		for (int i = 0; i < poly3d.size(); ++i)
 		{
 			const int j = (i + 1) % poly3d.size();
-			const int h = (i - 1 >= 0) ? (i - 1) : (poly3d.size() - 1);
+			const int h = (i - 1 >= 0) ?(i - 1) :(poly3d.size() - 1);
 
 			if (withlengths)
-				drawLength (painter, poly3d[i], poly3d[j], poly[i], poly[j]);
+				drawLength(painter, poly3d[i], poly3d[j], poly[i], poly[j]);
 
 			if (withangles and config.drawAngles())
 			{
-				QLineF l0 (poly[h], poly[i]),
-					l1 (poly[i], poly[j]);
+				QLineF l0(poly[h], poly[i]),
+					l1(poly[i], poly[j]);
 
-				double angle = 180 - l0.angleTo (l1);
+				double angle = 180 - l0.angleTo(l1);
 
 				if (angle < 0)
-					angle = 180 - l1.angleTo (l0);
+					angle = 180 - l1.angleTo(l0);
 
-				QString label = QString::number (angle) + QString::fromUtf8 (QByteArray ("\302\260"));
+				QString label = QString::number(angle) + QString::fromUtf8(QByteArray("\302\260"));
 				QPoint pos = poly[i];
-				pos.setY (pos.y() + metrics.height());
+				pos.setY(pos.y() + metrics.height());
 
-				painter.drawText (pos, label);
+				painter.drawText(pos, label);
 			}
 		}
 	}
 }
 
-bool AbstractDrawMode::keyReleased (QKeyEvent *ev)
+bool AbstractDrawMode::keyReleased(QKeyEvent *ev)
 {
-	if (Super::keyReleased (ev))
+	if (Super::keyReleased(ev))
 		return true;
 
 	if (not m_drawedVerts.isEmpty() and ev->key() == Qt::Key_Backspace)
@@ -266,11 +266,11 @@
 }
 
 template<typename T>
-T intervalClamp (T a, T interval)
+T intervalClamp(T a, T interval)
 {
 	T remainder = a % interval;
 
-	if (remainder >= float (interval / 2))
+	if (remainder >= float(interval / 2))
 		a += interval;
 
 	a -= remainder;
@@ -288,11 +288,11 @@
 		Vertex const& v1 = result;
 		Axis relX, relY;
 
-		renderer()->getRelativeAxes (relX, relY);
-		QLineF ln (v0[relX], v0[relY], v1[relX], v1[relY]);
-		ln.setAngle (intervalClamp<int> (ln.angle(), 45));
-		result.setCoordinate (relX, Grid::Snap (ln.x2(), Grid::Coordinate));
-		result.setCoordinate (relY, Grid::Snap (ln.y2(), Grid::Coordinate));
+		renderer()->getRelativeAxes(relX, relY);
+		QLineF ln(v0[relX], v0[relY], v1[relX], v1[relY]);
+		ln.setAngle(intervalClamp<int>(ln.angle(), 45));
+		result.setCoordinate(relX, Grid::Snap(ln.x2(), Grid::Coordinate));
+		result.setCoordinate(relY, Grid::Snap(ln.y2(), Grid::Coordinate));
 	}
 
 	return result;

mercurial