Wed, 03 Sep 2014 01:28:58 +0300
- finished line loop mode
- enter and backspace keys may now be used with the draw modes
--- a/src/editmodes/abstractEditMode.cc Tue Sep 02 13:34:01 2014 +0300 +++ b/src/editmodes/abstractEditMode.cc Wed Sep 03 01:28:58 2014 +0300 @@ -161,11 +161,13 @@ void AbstractDrawMode::finishDraw (LDObjectList const& objs) { + int pos = g_win->getInsertionPoint(); + if (objs.size() > 0) { for (LDObjectPtr obj : objs) { - renderer()->document()->addObject (obj); + renderer()->document()->insertObj (pos++, obj); renderer()->compileObject (obj); } @@ -176,6 +178,17 @@ m_drawedVerts.clear(); } +void AbstractDrawMode::drawLength (QPainter &painter, const Vertex &v0, const Vertex &v1, + const QPointF& v0p, const QPointF& v1p) const +{ + if (not cfg::DrawLineLengths) + return; + + 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, bool withlengths, bool withangles) const { @@ -212,12 +225,8 @@ const int j = (i + 1) % poly3d.size(); const int h = (i - 1 >= 0) ? (i - 1) : (poly3d.size() - 1); - if (withlengths and cfg::DrawLineLengths) - { - const QString label = QString::number ((poly3d[j] - poly3d[i]).length()); - QPoint origin = QLineF (poly[i], poly[j]).pointAt (0.5).toPoint(); - painter.drawText (origin, label); - } + if (withlengths) + drawLength (painter, poly3d[i], poly3d[j], poly[i], poly[j]); if (withangles and cfg::DrawAngles) { @@ -238,3 +247,17 @@ } } } + +bool AbstractDrawMode::keyReleased (QKeyEvent *ev) +{ + if (Super::keyReleased (ev)) + return true; + + if (not m_drawedVerts.isEmpty() and ev->key() == Qt::Key_Backspace) + { + m_drawedVerts.removeLast(); + return true; + } + + return false; +}
--- a/src/editmodes/abstractEditMode.h Tue Sep 02 13:34:01 2014 +0300 +++ b/src/editmodes/abstractEditMode.h Wed Sep 03 01:28:58 2014 +0300 @@ -22,6 +22,7 @@ class QPainter; class GLRenderer; class QMouseEvent; +class QKeyEvent; enum class EditModeType { @@ -57,6 +58,7 @@ virtual bool mouseReleased (MouseEventData const&) { return false; } virtual bool mouseDoubleClicked (QMouseEvent*) { return false; } virtual bool mouseMoved (QMouseEvent*) { return false; } + virtual bool keyReleased (QKeyEvent*) { return false; } static AbstractEditMode* createByType (GLRenderer* renderer, EditModeType type); }; @@ -85,6 +87,9 @@ void finishDraw (const LDObjectList& objs); void renderPolygon (QPainter& painter, const QVector<Vertex>& poly3d, bool withlengths, bool withangles) const; + void drawLength (QPainter& painter, Vertex const& v0, Vertex const& v1, + const QPointF& v0p, const QPointF& v1p) const; + bool keyReleased (QKeyEvent* ev) override; virtual bool preAddVertex (Vertex const&) {
--- a/src/editmodes/drawMode.h Tue Sep 02 13:34:01 2014 +0300 +++ b/src/editmodes/drawMode.h Wed Sep 03 01:28:58 2014 +0300 @@ -27,11 +27,9 @@ public: DrawMode (GLRenderer* renderer); - virtual void render (QPainter& painter) const override; - virtual EditModeType type() const override; - virtual bool preAddVertex (Vertex const& pos) override; - virtual bool mouseReleased (MouseEventData const& data) override; - -private: + void render (QPainter& painter) const override; + EditModeType type() const override; + bool mouseReleased (MouseEventData const& data) override; void endDraw(); + bool preAddVertex (Vertex const&); };
--- a/src/editmodes/lineLoopMode.cpp Tue Sep 02 13:34:01 2014 +0300 +++ b/src/editmodes/lineLoopMode.cpp Wed Sep 03 01:28:58 2014 +0300 @@ -1,21 +1,28 @@ #include "lineLoopMode.h" #include "../glRenderer.h" +#include "../mainWindow.h" +#include <QKeyEvent> LineLoopMode::LineLoopMode (GLRenderer *renderer) : Super (renderer) {} void LineLoopMode::render (QPainter& painter) const { - renderer()->drawBlip (painter, renderer()->coordconv3_2 (renderer()->position3D())); QVector<QPointF> points; + QList<Vertex> points3d (m_drawedVerts); + points3d << renderer()->position3D(); - for (Vertex const& vrt : m_drawedVerts) + for (Vertex const& vrt : points3d) points << renderer()->coordconv3_2 (vrt); painter.setPen (renderer()->textPen()); + assert (points.size() == points3d.size()); 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]); + } for (QPointF const& point : points) renderer()->drawBlip (painter, point); @@ -34,3 +41,44 @@ return false; } + +bool LineLoopMode::preAddVertex (Vertex const& pos) +{ + // If we picked an the last vertex, stop drawing + if (not m_drawedVerts.isEmpty() and pos == m_drawedVerts.last()) + { + endDraw(); + return true; + } + + return false; +} + +void LineLoopMode::endDraw() +{ + LDObjectList objs; + + for (int i = 0; i < m_drawedVerts.size() - 1; ++i) + { + LDLinePtr line = LDSpawn<LDLine>(); + line->setVertex (0, m_drawedVerts[i]); + line->setVertex (1, m_drawedVerts[i + 1]); + objs << line; + } + + finishDraw (objs); +} + +bool LineLoopMode::keyReleased (QKeyEvent* ev) +{ + if (Super::keyReleased (ev)) + return true; + + if (ev->key() == Qt::Key_Enter or ev->key() == Qt::Key_Return) + { + endDraw(); + return true; + } + + return false; +}
--- a/src/editmodes/lineLoopMode.h Tue Sep 02 13:34:01 2014 +0300 +++ b/src/editmodes/lineLoopMode.h Wed Sep 03 01:28:58 2014 +0300 @@ -11,5 +11,8 @@ void render (QPainter& painter) const override; EditModeType type() const override { return EditModeType::LineLoop; } bool mouseReleased (MouseEventData const& data) override; + bool preAddVertex (Vertex const& pos) override; + bool keyReleased (QKeyEvent*) override; + void endDraw(); };
--- a/src/glRenderer.cc Tue Sep 02 13:34:01 2014 +0300 +++ b/src/glRenderer.cc Wed Sep 03 01:28:58 2014 +0300 @@ -850,6 +850,8 @@ void GLRenderer::keyReleaseEvent (QKeyEvent* ev) { m_keymods = ev->modifiers(); + m_editmode->keyReleased (ev); + update(); } // =============================================================================