# HG changeset patch # User Teemu Piippo # Date 1478864091 -7200 # Node ID b54b78ac41a5272aa3a9d908a31e5de134cde9a5 # Parent 9fc08b73b8c4738f5a89ee54eeac59a5c756d262 Some cleanup diff -r 9fc08b73b8c4 -r b54b78ac41a5 src/editmodes/abstractEditMode.cpp --- a/src/editmodes/abstractEditMode.cpp Sun Oct 16 18:28:56 2016 +0300 +++ b/src/editmodes/abstractEditMode.cpp Fri Nov 11 13:34:51 2016 +0200 @@ -34,14 +34,14 @@ 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) { @@ -54,7 +54,7 @@ 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 @@ -62,9 +62,9 @@ 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); @@ -82,36 +82,36 @@ // ============================================================================= // -void AbstractDrawMode::addDrawnVertex (Vertex const& pos) +void AbstractDrawMode::addDrawnVertex(const Vertex& position) { - if (preAddVertex (pos)) + if (preAddVertex(position)) return; - m_drawedVerts << pos; + m_drawedVerts << position; } -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)) { // 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()); - const Axis relZ = renderer()->getRelativeZ(); - QList vertices = renderer()->document()->inlineVertices().toList(); + double minimumDistance = 1024.0; + const Vertex* closest = nullptr; + Vertex cursorPosition = renderer()->convert2dTo3d (data.ev->pos(), false); + QPoint cursorPosition2D (data.ev->pos()); + const Axis depthAxis = renderer()->getRelativeZ(); + QList vertices = renderer()->document()->inlineVertices().toList(); // Sort the vertices in order of distance to camera std::sort (vertices.begin(), vertices.end(), [&](const Vertex& a, const Vertex& b) -> bool { if (renderer()->cameraInfo (renderer()->camera()).negatedDepth) - return a[relZ] > b[relZ]; - - return a[relZ] < b[relZ]; + return a[depthAxis] > b[depthAxis]; + else + return a[depthAxis] < b[depthAxis]; }); for (const Vertex& vrt : vertices) @@ -146,7 +146,7 @@ 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(); @@ -156,19 +156,16 @@ if (data.releasedButtons & Qt::LeftButton) { if (maxVertices() and m_drawedVerts.size() >= maxVertices()) - { endDraw(); - return true; - } - - addDrawnVertex (getCursorVertex()); + else + addDrawnVertex (getCursorVertex()); return true; } return false; } -void AbstractDrawMode::finishDraw (LDObjectList const& objs) +void AbstractDrawMode::finishDraw(LDObjectList const& objs) { int pos = m_window->suggestInsertPoint(); @@ -187,7 +184,7 @@ m_drawedVerts.clear(); } -void AbstractDrawMode::drawLength (QPainter &painter, const Vertex &v0, const Vertex &v1, +void AbstractDrawMode::drawLineLength(QPainter &painter, const Vertex &v0, const Vertex &v1, const QPointF& v0p, const QPointF& v1p) const { if (not m_config->drawLineLengths()) @@ -198,55 +195,53 @@ painter.drawText (origin, label); } -void AbstractDrawMode::renderPolygon (QPainter& painter, const QVector& poly3d, - bool withlengths, bool withangles) const +void AbstractDrawMode::renderPolygon(QPainter& painter, const QVector& polygon3d, + bool drawLineLengths, bool drawAngles ) const { - QVector poly (poly3d.size()); - QFontMetrics metrics = QFontMetrics (QFont()); + QVector polygon2d (polygon3d.size()); + QFontMetrics metrics = QFontMetrics(QFont()); // Convert to 2D - for (int i = 0; i < poly3d.size(); ++i) - poly[i] = renderer()->convert3dTo2d (poly3d[i]); + for (int i = 0; i < polygon3d.size(); ++i) + polygon2d[i] = renderer()->convert3dTo2d(polygon3d[i]); // Draw the polygon-to-be - painter.setBrush (m_polybrush); - painter.drawPolygon (QPolygonF (poly)); + painter.setBrush(m_polybrush); + painter.drawPolygon(QPolygonF{polygon2d}); // Draw vertex blips - for (int i = 0; i < poly3d.size(); ++i) + for (int i = 0; i < polygon3d.size(); ++i) { - renderer()->drawBlip (painter, poly[i]); - renderer()->drawBlipCoordinates (painter, poly3d[i], poly[i]); + renderer()->drawBlip(painter, polygon2d[i]); + renderer()->drawBlipCoordinates(painter, polygon3d[i], polygon2d[i]); } // Draw line lenghts and angle info if appropriate - if (poly3d.size() >= 2 and (withlengths or withangles)) + if (polygon3d.size() >= 2 and (drawLineLengths or drawAngles)) { painter.setPen (renderer()->textPen()); - for (int i = 0; i < poly3d.size(); ++i) + for (int i = 0; i < polygon3d.size(); ++i) { - const int j = (i + 1) % poly3d.size(); - const int h = (i - 1 >= 0) ? (i - 1) : (poly3d.size() - 1); + int j = (i + 1) % polygon3d.size(); + int prior = (i - 1 >= 0) ? (i - 1) : (polygon3d.size() - 1); - if (withlengths) - drawLength (painter, poly3d[i], poly3d[j], poly[i], poly[j]); + if (drawLineLengths) + drawLineLength(painter, polygon3d[i], polygon3d[j], polygon2d[i], polygon2d[j]); - if (withangles and m_config->drawAngles()) + if (drawAngles and m_config->drawAngles()) { - QLineF l0 (poly[h], poly[i]), - l1 (poly[i], poly[j]); - - double angle = 180 - l0.angleTo (l1); + QLineF line0 = {polygon2d[prior], polygon2d[i]}; + QLineF line1 = {polygon2d[i], polygon2d[j]}; + double angle = 180 - line0.angleTo(line1); if (angle < 0) - angle = 180 - l1.angleTo (l0); + angle = 180 - line1.angleTo(line0); - QString label = QString::number (angle) + QString::fromUtf8 (QByteArray ("\302\260")); - QPoint pos = poly[i]; - pos.setY (pos.y() + metrics.height()); - - painter.drawText (pos, label); + QString label = QString::number(angle) + "°"; + QPoint textPosition = polygon2d[i]; + textPosition.setY(textPosition.y() + metrics.height()); + painter.drawText(textPosition, label); } } } @@ -266,12 +261,17 @@ return false; } +// +// roundToInterval +// +// Rounds 'a' to the nearest multiple of 'interval'. +// template -T intervalClamp (T a, T interval) +T roundToInterval (T a, T interval) { T remainder = a % interval; - if (remainder >= float (interval / 2)) + if (remainder >= interval / 2.0) a += interval; a -= remainder; @@ -282,18 +282,17 @@ { Vertex result = renderer()->position3D(); - if (renderer()->keyboardModifiers() & Qt::ControlModifier - and not m_drawedVerts.isEmpty()) + if ((renderer()->keyboardModifiers() & Qt::ControlModifier) and not m_drawedVerts.isEmpty()) { - Vertex const& v0 = m_drawedVerts.last(); - Vertex const& v1 = result; - Axis relX, relY; + Vertex const& vertex0 = m_drawedVerts.last(); + Vertex const& vertex1 = result; + Axis relativeX, relativeY; - renderer()->getRelativeAxes (relX, relY); - QLineF ln (v0[relX], v0[relY], v1[relX], v1[relY]); - ln.setAngle (intervalClamp (ln.angle(), 45)); - result.setCoordinate (relX, grid()->snap(ln.x2(), Grid::Coordinate)); - result.setCoordinate (relY, grid()->snap(ln.y2(), Grid::Coordinate)); + renderer()->getRelativeAxes (relativeX, relativeY); + QLineF line = {vertex0[relativeX], vertex0[relativeY], vertex1[relativeX], vertex1[relativeY]}; + line.setAngle(roundToInterval(line.angle(), 45)); + result.setCoordinate(relativeX, grid()->snap(line.x2(), Grid::Coordinate)); + result.setCoordinate(relativeY, grid()->snap(line.y2(), Grid::Coordinate)); } return result; diff -r 9fc08b73b8c4 -r b54b78ac41a5 src/editmodes/abstractEditMode.h --- a/src/editmodes/abstractEditMode.h Sun Oct 16 18:28:56 2016 +0300 +++ b/src/editmodes/abstractEditMode.h Fri Nov 11 13:34:51 2016 +0200 @@ -84,7 +84,7 @@ void addDrawnVertex (const Vertex& pos); virtual bool allowFreeCamera() const override final { return false; } virtual void endDraw() {} - void drawLength (QPainter& painter, + void drawLineLength (QPainter& painter, const Vertex& v0, const Vertex& v1, const QPointF& v0p, const QPointF& v1p) const; void finishDraw (const LDObjectList& objs); diff -r 9fc08b73b8c4 -r b54b78ac41a5 src/editmodes/linePathMode.cpp --- a/src/editmodes/linePathMode.cpp Sun Oct 16 18:28:56 2016 +0300 +++ b/src/editmodes/linePathMode.cpp Fri Nov 11 13:34:51 2016 +0200 @@ -40,7 +40,7 @@ for (int i = 0; i < points.size() - 1; ++i) { painter.drawLine (QLineF (points[i], points[i + 1])); - drawLength (painter, points3d[i], points3d[i + 1], points[i], points[i + 1]); + drawLineLength (painter, points3d[i], points3d[i + 1], points[i], points[i + 1]); } for (int i = 0; i < points.size(); ++i)