# HG changeset patch # User Teemu Piippo # Date 1486638331 -7200 # Node ID edddb9b0db9e5a03a11f8ac4285ead44f90c2c74 # Parent ac7db4c076c327b63325db1d6d30cace09e21d0a Moved document editing methods from GLRenderer to Canvas diff -r ac7db4c076c3 -r edddb9b0db9e src/canvas.cpp --- a/src/canvas.cpp Thu Feb 09 11:53:55 2017 +0200 +++ b/src/canvas.cpp Thu Feb 09 13:05:31 2017 +0200 @@ -16,11 +16,156 @@ * along with this program. If not, see . */ +#include #include "canvas.h" +#include "documentmanager.h" #include "ldDocument.h" +#include "mainwindow.h" +#include "messageLog.h" +#include "primitives.h" Canvas::Canvas(LDDocument* document, QWidget* parent) : - GLRenderer {document, parent} + GLRenderer {document, parent}, + m_document {*document}, + m_currentEditMode {AbstractEditMode::createByType (this, EditModeType::Select)} {} + +Canvas::~Canvas() +{ + delete m_currentEditMode; +} + +void Canvas::overpaint(QPainter& painter) +{ + GLRenderer::overpaint(painter); + QFontMetrics metrics {QFont {}}; + +#ifndef RELEASE + { + QString text = format("Rotation: %1\nPanning: (%2, %3), Zoom: %4", rotationMatrix(), panning(X), panning(Y), zoom()); + QRect textSize = metrics.boundingRect(0, 0, width(), height(), Qt::AlignCenter, text); + painter.setPen(textPen()); + painter.drawText((width() - textSize.width()) / 2, height() - textSize.height(), textSize.width(), + textSize.height(), Qt::AlignCenter, text); + } +#endif + + if (camera() != FreeCamera) + { + // Paint the coordinates onto the screen. + QString text = format(tr("X: %1, Y: %2, Z: %3"), m_position3D[X], m_position3D[Y], m_position3D[Z]); + QFontMetrics metrics {font()}; + QRect textSize = metrics.boundingRect (0, 0, width(), height(), Qt::AlignCenter, text); + painter.setPen(textPen()); + painter.drawText(width() - textSize.width(), height() - 16, textSize.width(), textSize.height(), Qt::AlignCenter, text); + } + + // Draw edit mode HUD + m_currentEditMode->render(painter); + + // Message log + if (m_window->messageLog()) + { + int y = 0; + int margin = 2; + QColor penColor = textPen().color(); + + for (const MessageManager::Line& line : m_window->messageLog()->getLines()) + { + penColor.setAlphaF(line.alpha); + painter.setPen(penColor); + painter.drawText(QPoint {margin, y + margin + metrics.ascent()}, line.text); + y += metrics.height(); + } + } +} + +bool Canvas::freeCameraAllowed() const +{ + return m_currentEditMode->allowFreeCamera(); +} + +void Canvas::setEditMode(EditModeType a) { + if (m_currentEditMode and m_currentEditMode->type() == a) + return; + delete m_currentEditMode; + m_currentEditMode = AbstractEditMode::createByType(this, a); + + // If we cannot use the free camera, use the top one instead. + if (camera() == FreeCamera and not m_currentEditMode->allowFreeCamera()) + setCamera(TopCamera); + + m_window->updateEditModeActions(); + update(); } + +EditModeType Canvas::currentEditModeType() const +{ + return m_currentEditMode->type(); +} + +LDDocument* Canvas::document() const +{ + return &m_document; +} + +void Canvas::mouseDoubleClickEvent(QMouseEvent* event) +{ + if (m_currentEditMode->mouseDoubleClicked (event)) + event->accept(); +} + +void Canvas::dropEvent(QDropEvent* event) +{ + if (m_window and event->source() == m_window->getPrimitivesTree()) + { + PrimitiveTreeItem* item = static_cast (m_window->getPrimitivesTree()->currentItem()); + QString primitiveName = item->primitive()->name; + LDSubfileReference* reference = currentDocument()->emplaceAt(m_window->suggestInsertPoint()); + reference->setFileInfo (m_documents->getDocumentByName(primitiveName)); + currentDocument()->addToSelection(reference); + m_window->buildObjectList(); + refresh(); + event->acceptProposedAction(); + } +} + +void Canvas::keyReleaseEvent(QKeyEvent* event) +{ + m_currentEditMode->keyReleased(event); + GLRenderer::keyReleaseEvent(event); +} + +void Canvas::mouseMoveEvent(QMouseEvent* event) +{ + // Calculate 3d position of the cursor + m_position3D = convert2dTo3d(mousePosition(), true); + + if (not m_currentEditMode->mouseMoved(event)) + GLRenderer::mouseMoveEvent(event); +} + +void Canvas::mouseReleaseEvent(QMouseEvent *event) +{ + AbstractEditMode::MouseEventData data; + data.ev = event; + data.mouseMoved = mouseHasMoved(); + data.keymods = keyboardModifiers(); + data.releasedButtons = lastButtons() & ~event->buttons(); + m_currentEditMode->mouseReleased(data); + GLRenderer::mouseReleaseEvent(event); +} + +void Canvas::mousePressEvent(QMouseEvent *event) +{ + if (m_currentEditMode->mousePressed(event)) + event->accept(); + + GLRenderer::mousePressEvent(event); +} + +const Vertex& Canvas::position3D() const +{ + return m_position3D; +} diff -r ac7db4c076c3 -r edddb9b0db9e src/canvas.h --- a/src/canvas.h Thu Feb 09 11:53:55 2017 +0200 +++ b/src/canvas.h Thu Feb 09 13:05:31 2017 +0200 @@ -18,9 +18,31 @@ #pragma once #include "glRenderer.h" +#include "editmodes/abstractEditMode.h" class Canvas : public GLRenderer { public: Canvas(LDDocument* document, QWidget* parent = nullptr); + ~Canvas(); + + EditModeType currentEditModeType() const; + LDDocument* document() const; + const Vertex& position3D() const; + void setEditMode(EditModeType type); + +protected: + void dropEvent(QDropEvent* event) override; + bool freeCameraAllowed() const override; + void keyReleaseEvent(QKeyEvent* event) override; + void mouseDoubleClickEvent(QMouseEvent* event) override; + void mouseMoveEvent(QMouseEvent* event) override; + void mousePressEvent(QMouseEvent *event) override; + void mouseReleaseEvent(QMouseEvent* event) override; + void overpaint(QPainter& painter) override; + +private: + LDDocument& m_document; + AbstractEditMode* m_currentEditMode = nullptr; + Vertex m_position3D; }; diff -r ac7db4c076c3 -r edddb9b0db9e src/editmodes/abstractEditMode.cpp --- a/src/editmodes/abstractEditMode.cpp Thu Feb 09 11:53:55 2017 +0200 +++ b/src/editmodes/abstractEditMode.cpp Thu Feb 09 13:05:31 2017 +0200 @@ -28,7 +28,7 @@ #include "curvemode.h" #include "../mainwindow.h" #include "../ldDocument.h" -#include "../glRenderer.h" +#include "../canvas.h" #include "../miscallenous.h" #include "../grid.h" @@ -38,25 +38,25 @@ /* * Base class constructor of the abstract editing mode. */ -AbstractEditMode::AbstractEditMode(GLRenderer* renderer) : - QObject(renderer), - HierarchyElement(renderer), - m_renderer(renderer) {} +AbstractEditMode::AbstractEditMode(Canvas* canvas) : + QObject(canvas), + HierarchyElement(canvas), + m_canvas(canvas) {} /* * Constructs an edit mode by type. */ -AbstractEditMode* AbstractEditMode::createByType(GLRenderer* renderer, EditModeType type) +AbstractEditMode* AbstractEditMode::createByType(Canvas* canvas, EditModeType 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 (canvas); + case EditModeType::Draw: return new DrawMode (canvas); + case EditModeType::Rectangle: return new RectangleMode (canvas); + case EditModeType::Circle: return new CircleMode (canvas); + case EditModeType::MagicWand: return new MagicWandMode (canvas); + case EditModeType::LinePath: return new LinePathMode (canvas); + case EditModeType::Curve: return new CurveMode (canvas); } throw std::logic_error("bad type given to AbstractEditMode::createByType"); @@ -65,20 +65,20 @@ /* * Returns the edit mode's corresponding renderer pointer. */ -GLRenderer* AbstractEditMode::renderer() const +Canvas* AbstractEditMode::renderer() const { - return m_renderer; + return m_canvas; } /* * Base class constructor of the abstract drwaing mode. */ -AbstractDrawMode::AbstractDrawMode(GLRenderer* renderer) : - AbstractEditMode {renderer}, +AbstractDrawMode::AbstractDrawMode(Canvas* canvas) : + AbstractEditMode {canvas}, 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); + canvas->setContextMenuPolicy(Qt::NoContextMenu); // We need the right mouse button for removing vertices + canvas->setCursor(Qt::CrossCursor); m_window->currentDocument()->clearSelection(); m_window->updateSelection(); m_drawedVerts.clear(); @@ -87,11 +87,11 @@ /* * Base class constructor of the abstract selection mode. */ -AbstractSelectMode::AbstractSelectMode(GLRenderer* renderer) : - AbstractEditMode {renderer} +AbstractSelectMode::AbstractSelectMode(Canvas* canvas) : + AbstractEditMode {canvas} { - renderer->unsetCursor(); - renderer->setContextMenuPolicy (Qt::DefaultContextMenu); + canvas->unsetCursor(); + canvas->setContextMenuPolicy (Qt::DefaultContextMenu); } /* diff -r ac7db4c076c3 -r edddb9b0db9e src/editmodes/abstractEditMode.h --- a/src/editmodes/abstractEditMode.h Thu Feb 09 11:53:55 2017 +0200 +++ b/src/editmodes/abstractEditMode.h Thu Feb 09 13:05:31 2017 +0200 @@ -21,7 +21,7 @@ #include "../model.h" class QPainter; -class GLRenderer; +class Canvas; class QMouseEvent; class QKeyEvent; @@ -52,11 +52,11 @@ Qt::MouseButtons releasedButtons; }; - AbstractEditMode (GLRenderer* renderer); + AbstractEditMode(Canvas* canvas); virtual bool allowFreeCamera() const = 0; virtual void render (QPainter&) const {} - GLRenderer* renderer() const; + Canvas* renderer() const; virtual EditModeType type() const = 0; virtual bool mousePressed (QMouseEvent*) { return false; } virtual bool mouseReleased (MouseEventData const&) { return false; } @@ -64,10 +64,10 @@ virtual bool mouseMoved (QMouseEvent*) { return false; } virtual bool keyReleased (QKeyEvent*) { return false; } - static AbstractEditMode* createByType (GLRenderer* renderer, EditModeType type); + static AbstractEditMode* createByType (Canvas* renderer, EditModeType type); private: - GLRenderer* m_renderer; + Canvas* m_canvas; }; /* @@ -82,7 +82,7 @@ QBrush m_polybrush; public: - AbstractDrawMode (GLRenderer* renderer); + AbstractDrawMode (Canvas* canvas); void addDrawnVertex(const Vertex& pos); bool allowFreeCamera() const override final; @@ -105,7 +105,7 @@ DEFINE_CLASS (AbstractSelectMode, AbstractEditMode) public: - AbstractSelectMode (GLRenderer* renderer); + AbstractSelectMode (Canvas* canvas); virtual bool allowFreeCamera() const override { diff -r ac7db4c076c3 -r edddb9b0db9e src/editmodes/circleMode.cpp --- a/src/editmodes/circleMode.cpp Thu Feb 09 11:53:55 2017 +0200 +++ b/src/editmodes/circleMode.cpp Thu Feb 09 13:05:31 2017 +0200 @@ -23,14 +23,14 @@ #include "../ldDocument.h" #include "../ringFinder.h" #include "../primitives.h" -#include "../glRenderer.h" +#include "../canvas.h" #include "../mainwindow.h" #include "../mathfunctions.h" #include "../miscallenous.h" #include "../grid.h" -CircleMode::CircleMode(GLRenderer* renderer) : - Super {renderer} {} +CircleMode::CircleMode(Canvas* canvas) : + Super {canvas} {} EditModeType CircleMode::type() const diff -r ac7db4c076c3 -r edddb9b0db9e src/editmodes/circleMode.h --- a/src/editmodes/circleMode.h Thu Feb 09 11:53:55 2017 +0200 +++ b/src/editmodes/circleMode.h Thu Feb 09 13:05:31 2017 +0200 @@ -24,7 +24,7 @@ DEFINE_CLASS (CircleMode, AbstractDrawMode) public: - CircleMode (GLRenderer* renderer); + CircleMode (Canvas* canvas); void endDraw() override; double getCircleDrawDist (int position) const; diff -r ac7db4c076c3 -r edddb9b0db9e src/editmodes/curvemode.cpp --- a/src/editmodes/curvemode.cpp Thu Feb 09 11:53:55 2017 +0200 +++ b/src/editmodes/curvemode.cpp Thu Feb 09 13:05:31 2017 +0200 @@ -18,10 +18,10 @@ #include "curvemode.h" #include "../ldObject.h" -#include "../glRenderer.h" +#include "../canvas.h" -CurveMode::CurveMode (GLRenderer* renderer) : - Super (renderer) {} +CurveMode::CurveMode (Canvas* canvas) : + Super (canvas) {} void CurveMode::render (QPainter& painter) const { diff -r ac7db4c076c3 -r edddb9b0db9e src/editmodes/curvemode.h --- a/src/editmodes/curvemode.h Thu Feb 09 11:53:55 2017 +0200 +++ b/src/editmodes/curvemode.h Thu Feb 09 13:05:31 2017 +0200 @@ -24,7 +24,7 @@ DEFINE_CLASS (CurveMode, AbstractDrawMode) public: - CurveMode (GLRenderer* renderer); + CurveMode (Canvas* canvas); void endDraw() override; void render (QPainter& painter) const override; EditModeType type() const override; diff -r ac7db4c076c3 -r edddb9b0db9e src/editmodes/drawMode.cpp --- a/src/editmodes/drawMode.cpp Thu Feb 09 11:53:55 2017 +0200 +++ b/src/editmodes/drawMode.cpp Thu Feb 09 13:05:31 2017 +0200 @@ -22,8 +22,8 @@ #include "../ldObject.h" #include "../glRenderer.h" -DrawMode::DrawMode (GLRenderer* renderer) : - Super (renderer) {} +DrawMode::DrawMode (Canvas* canvas) : + Super (canvas) {} EditModeType DrawMode::type() const { diff -r ac7db4c076c3 -r edddb9b0db9e src/editmodes/drawMode.h --- a/src/editmodes/drawMode.h Thu Feb 09 11:53:55 2017 +0200 +++ b/src/editmodes/drawMode.h Thu Feb 09 13:05:31 2017 +0200 @@ -25,7 +25,7 @@ bool m_rectdraw; public: - DrawMode (GLRenderer* renderer); + DrawMode (Canvas* canvas); void render (QPainter& painter) const override; int maxVertices() const override { return 4; } diff -r ac7db4c076c3 -r edddb9b0db9e src/editmodes/linePathMode.cpp --- a/src/editmodes/linePathMode.cpp Thu Feb 09 11:53:55 2017 +0200 +++ b/src/editmodes/linePathMode.cpp Thu Feb 09 13:05:31 2017 +0200 @@ -18,11 +18,11 @@ #include #include "linePathMode.h" -#include "../glRenderer.h" +#include "../canvas.h" #include "../mainwindow.h" -LinePathMode::LinePathMode (GLRenderer *renderer) : - Super (renderer) {} +LinePathMode::LinePathMode (Canvas* canvas) : + Super (canvas) {} void LinePathMode::render (QPainter& painter) const { diff -r ac7db4c076c3 -r edddb9b0db9e src/editmodes/linePathMode.h --- a/src/editmodes/linePathMode.h Thu Feb 09 11:53:55 2017 +0200 +++ b/src/editmodes/linePathMode.h Thu Feb 09 13:05:31 2017 +0200 @@ -24,7 +24,7 @@ DEFINE_CLASS (LinePathMode, AbstractDrawMode) public: - LinePathMode (GLRenderer* renderer); + LinePathMode (Canvas* canvas); void render (QPainter& painter) const override; EditModeType type() const override { return EditModeType::LinePath; } diff -r ac7db4c076c3 -r edddb9b0db9e src/editmodes/magicWandMode.cpp --- a/src/editmodes/magicWandMode.cpp Thu Feb 09 11:53:55 2017 +0200 +++ b/src/editmodes/magicWandMode.cpp Thu Feb 09 13:05:31 2017 +0200 @@ -20,10 +20,10 @@ #include "magicWandMode.h" #include "../ldDocument.h" #include "../mainwindow.h" -#include "../glRenderer.h" +#include "../canvas.h" -MagicWandMode::MagicWandMode (GLRenderer* renderer) : - Super (renderer) +MagicWandMode::MagicWandMode (Canvas* canvas) : + Super (canvas) { // Get vertex<->object data for (LDObject* obj : currentDocument()->objects()) diff -r ac7db4c076c3 -r edddb9b0db9e src/editmodes/magicWandMode.h --- a/src/editmodes/magicWandMode.h Thu Feb 09 11:53:55 2017 +0200 +++ b/src/editmodes/magicWandMode.h Thu Feb 09 13:05:31 2017 +0200 @@ -39,7 +39,7 @@ InternalRecursion }; - MagicWandMode (GLRenderer* renderer); + MagicWandMode (Canvas* canvas); void doMagic (LDObject* obj, MagicType type); virtual EditModeType type() const override; virtual bool mouseReleased (MouseEventData const& data) override; diff -r ac7db4c076c3 -r edddb9b0db9e src/editmodes/rectangleMode.cpp --- a/src/editmodes/rectangleMode.cpp Thu Feb 09 11:53:55 2017 +0200 +++ b/src/editmodes/rectangleMode.cpp Thu Feb 09 13:05:31 2017 +0200 @@ -20,10 +20,10 @@ #include #include "rectangleMode.h" #include "../ldObject.h" -#include "../glRenderer.h" +#include "../canvas.h" -RectangleMode::RectangleMode (GLRenderer* renderer) : - Super (renderer), +RectangleMode::RectangleMode (Canvas* canvas) : + Super (canvas), m_rectangleVerts (QVector(4)) {} EditModeType RectangleMode::type() const diff -r ac7db4c076c3 -r edddb9b0db9e src/editmodes/rectangleMode.h --- a/src/editmodes/rectangleMode.h Thu Feb 09 11:53:55 2017 +0200 +++ b/src/editmodes/rectangleMode.h Thu Feb 09 13:05:31 2017 +0200 @@ -25,7 +25,7 @@ QVector m_rectangleVerts; public: - RectangleMode (GLRenderer* renderer); + RectangleMode (Canvas* canvas); void render (QPainter& painter) const override; EditModeType type() const override; diff -r ac7db4c076c3 -r edddb9b0db9e src/editmodes/selectMode.cpp --- a/src/editmodes/selectMode.cpp Thu Feb 09 11:53:55 2017 +0200 +++ b/src/editmodes/selectMode.cpp Thu Feb 09 13:05:31 2017 +0200 @@ -18,12 +18,12 @@ #include #include "selectMode.h" -#include "../glRenderer.h" +#include "../canvas.h" #include "../mainwindow.h" #include "../ldDocument.h" -SelectMode::SelectMode (GLRenderer* renderer) : - Super (renderer), +SelectMode::SelectMode (Canvas* canvas) : + Super (canvas), m_rangepick (false) {} EditModeType SelectMode::type() const diff -r ac7db4c076c3 -r edddb9b0db9e src/editmodes/selectMode.h --- a/src/editmodes/selectMode.h Thu Feb 09 11:53:55 2017 +0200 +++ b/src/editmodes/selectMode.h Thu Feb 09 13:05:31 2017 +0200 @@ -28,7 +28,7 @@ DEFINE_CLASS (SelectMode, AbstractSelectMode) public: - SelectMode (GLRenderer* renderer); + SelectMode (Canvas* canvas); void render (QPainter& painter) const override; bool mousePressed (QMouseEvent* ev) override; diff -r ac7db4c076c3 -r edddb9b0db9e src/glRenderer.cpp --- a/src/glRenderer.cpp Thu Feb 09 11:53:55 2017 +0200 +++ b/src/glRenderer.cpp Thu Feb 09 13:05:31 2017 +0200 @@ -31,7 +31,6 @@ #include "miscallenous.h" #include "editHistory.h" #include "dialogs.h" -#include "messageLog.h" #include "glCompiler.h" #include "primitives.h" #include "documentmanager.h" @@ -73,7 +72,6 @@ m_model {model} { m_camera = (Camera) m_config->camera(); - m_currentEditMode = AbstractEditMode::createByType (this, EditModeType::Select); m_compiler = new GLCompiler (this); m_toolTipTimer = new QTimer (this); m_toolTipTimer->setSingleShot (true); @@ -89,9 +87,9 @@ { const char* cameraIconNames[EnumLimits::Count] = { - "camera-top", "camera-front", "camera-left", - "camera-bottom", "camera-back", "camera-right", - "camera-free" + "camera-top", "camera-front", "camera-left", + "camera-bottom", "camera-back", "camera-right", + "camera-free" }; CameraIcon* info = &m_cameraIcons[camera]; @@ -108,7 +106,6 @@ { m_compiler->setRenderer (nullptr); delete m_compiler; - delete m_currentEditMode; glDeleteBuffers (1, &m_axesVbo); glDeleteBuffers (1, &m_axesColorVbo); } @@ -359,6 +356,9 @@ zoomAllToFit(); } + m_virtualWidth = zoom(); + m_virtualHeight = (m_height * m_virtualWidth) / m_width; + if (m_config->drawWireframe() and not m_isDrawingSelectionScene) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); @@ -571,101 +571,63 @@ return linepen; } -// ============================================================================= -// +bool GLRenderer::freeCameraAllowed() const +{ + return true; +} + void GLRenderer::paintEvent(QPaintEvent*) { makeCurrent(); - m_virtualWidth = zoom(); - m_virtualHeight = (m_height * m_virtualWidth) / m_width; initGLData(); drawGLScene(); - QPainter painter {this}; - QFontMetrics metrics {QFont {}}; - painter.setRenderHint(QPainter::Antialiasing); - // If we wish to only draw the brick, stop here - if (isDrawOnly() or m_isDrawingSelectionScene) + if (isDrawingSelectionScene()) return; -#ifndef RELEASE + QPainter painter {this}; + painter.setRenderHint(QPainter::Antialiasing); + overpaint(painter); +} + +void GLRenderer::overpaint(QPainter &painter) +{ + // Draw a background for the selected camera + painter.setPen(m_thinBorderPen); + painter.setBrush(QBrush {QColor {0, 128, 160, 128}}); + painter.drawRect(m_cameraIcons[camera()].hitRect); + + // Draw the camera icons + for (const CameraIcon& info : m_cameraIcons) { - QString text = format("Rotation: %1\nPanning: (%2, %3), Zoom: %4", - m_rotationMatrix, panning(X), panning(Y), zoom()); - QRect textSize = metrics.boundingRect(0, 0, m_width, m_height, Qt::AlignCenter, text); - painter.setPen(textPen()); - painter.drawText((width() - textSize.width()) / 2, height() - textSize.height(), textSize.width(), - textSize.height(), Qt::AlignCenter, text); - } -#endif + // Don't draw the free camera icon when we can't use the free camera + if (&info == &m_cameraIcons[FreeCamera] and not freeCameraAllowed()) + continue; - if (camera() != FreeCamera) - { - // Paint the coordinates onto the screen. - QString text = format(tr("X: %1, Y: %2, Z: %3"), m_position3D[X], m_position3D[Y], m_position3D[Z]); - QFontMetrics metrics {font()}; - QRect textSize = metrics.boundingRect (0, 0, m_width, m_height, Qt::AlignCenter, text); - painter.setPen(textPen()); - painter.drawText(m_width - textSize.width(), m_height - 16, - textSize.width(), textSize.height(), Qt::AlignCenter, text); + painter.drawPixmap(info.targetRect, info.image, info.sourceRect); } + // Tool tips + if (m_drawToolTip) { - // Draw edit mode HUD - m_currentEditMode->render(painter); - - // Draw a background for the selected camera - painter.setPen(m_thinBorderPen); - painter.setBrush(QBrush {QColor {0, 128, 160, 128}}); - painter.drawRect(m_cameraIcons[camera()].hitRect); - - // Draw the camera icons - for (CameraIcon& info : m_cameraIcons) - { - // Don't draw the free camera icon when we can't use the free camera - if (&info == &m_cameraIcons[FreeCamera] and not m_currentEditMode->allowFreeCamera()) - continue; - - painter.drawPixmap(info.targetRect, info.image, info.sourceRect); - } - - // Draw a label for the current camera in the bottom left corner - { - int margin = 4; - painter.setPen(textPen()); - painter.drawText(QPoint {margin, height() - margin - metrics.descent()}, currentCameraName()); - - // Also render triangle count. - if (m_model) - { - QPoint renderPoint = {margin, height() - margin - metrics.height() - metrics.descent()}; - painter.drawText(renderPoint, format("△ %1", m_model->triangleCount())); - } - } - - // Tool tips - if (m_drawToolTip) - { - if (not m_cameraIcons[m_toolTipCamera].targetRect.contains (m_mousePosition)) - m_drawToolTip = false; - else - QToolTip::showText(m_globalpos, currentCameraName()); - } + if (not m_cameraIcons[m_toolTipCamera].targetRect.contains (m_mousePosition)) + m_drawToolTip = false; + else + QToolTip::showText(m_globalpos, currentCameraName()); } - // Message log - if (m_window->messageLog()) + // Draw a label for the current camera in the bottom left corner { - int y = 0; - int margin = 2; - QColor penColor = textPen().color(); + QFontMetrics metrics {QFont {}}; + int margin = 4; + painter.setPen(textPen()); + painter.drawText(QPoint {margin, height() - margin - metrics.descent()}, currentCameraName()); - for (const MessageManager::Line& line : m_window->messageLog()->getLines()) + // Also render triangle count. + if (m_model) { - penColor.setAlphaF(line.alpha); - painter.setPen(penColor); - painter.drawText(QPoint {margin, y + margin + metrics.ascent()}, line.text); - y += metrics.height(); + QPoint renderPoint = {margin, height() - margin - metrics.height() - metrics.descent()}; + painter.drawText(renderPoint, format("△ %1", m_model->triangleCount())); } } } @@ -695,41 +657,24 @@ // ============================================================================= // -void GLRenderer::mouseReleaseEvent(QMouseEvent* ev) +void GLRenderer::mouseReleaseEvent(QMouseEvent* event) { - bool wasLeft = (m_lastButtons & Qt::LeftButton) and not (ev->buttons() & Qt::LeftButton); - Qt::MouseButtons releasedbuttons = m_lastButtons & ~ev->buttons(); + bool wasLeft = (m_lastButtons & Qt::LeftButton) and not (event->buttons() & Qt::LeftButton); m_panning = false; - if (wasLeft) + // Check if we selected a camera icon + if (wasLeft and not mouseHasMoved()) { - // Check if we selected a camera icon - if (not mouseHasMoved()) + for (CameraIcon& info : m_cameraIcons) { - for (CameraIcon& info : m_cameraIcons) + if (info.targetRect.contains (event->pos())) { - if (info.targetRect.contains (ev->pos())) - { - setCamera (info.camera); - goto end; - } + setCamera (info.camera); + break; } } } - if (not isDrawOnly()) - { - AbstractEditMode::MouseEventData data; - data.ev = ev; - data.mouseMoved = mouseHasMoved(); - data.keymods = m_currentKeyboardModifiers; - data.releasedButtons = releasedbuttons; - - if (m_currentEditMode->mouseReleased (data)) - goto end; - } - -end: update(); m_totalMouseMove = 0; } @@ -738,11 +683,8 @@ // void GLRenderer::mousePressEvent(QMouseEvent* event) { + m_lastButtons = event->buttons(); m_totalMouseMove = 0; - m_lastButtons = event->buttons(); - - if (m_currentEditMode->mousePressed(event)) - event->accept(); } // ============================================================================= @@ -754,32 +696,29 @@ m_totalMouseMove += qAbs(xMove) + qAbs(yMove); m_isCameraMoving = false; - if (not m_currentEditMode->mouseMoved (event)) - { - bool left = event->buttons() & Qt::LeftButton; - bool mid = event->buttons() & Qt::MidButton; - bool shift = event->modifiers() & Qt::ShiftModifier; + bool left = event->buttons() & Qt::LeftButton; + bool mid = event->buttons() & Qt::MidButton; + bool shift = event->modifiers() & Qt::ShiftModifier; - if (mid or (left and shift)) - { - panning(X) += 0.03f * xMove * (zoom() / 7.5f); - panning(Y) -= 0.03f * yMove * (zoom() / 7.5f); - m_panning = true; - m_isCameraMoving = true; - } - else if (left and camera() == FreeCamera and (xMove != 0 or yMove != 0)) - { - // Apply current rotation input to the rotation matrix - // ref: https://forums.ldraw.org/thread-22006-post-24426.html#pid24426 - glPushMatrix(); - glLoadIdentity(); - // 0.6 is an arbitrary rotation sensitivity scalar - glRotatef(0.6 * hypot(xMove, yMove), yMove, xMove, 0); - glMultMatrixf(m_rotationMatrix.constData()); - glGetFloatv(GL_MODELVIEW_MATRIX, m_rotationMatrix.data()); - glPopMatrix(); - m_isCameraMoving = true; - } + if (mid or (left and shift)) + { + panning(X) += 0.03f * xMove * (zoom() / 7.5f); + panning(Y) -= 0.03f * yMove * (zoom() / 7.5f); + m_panning = true; + m_isCameraMoving = true; + } + else if (left and camera() == FreeCamera and (xMove != 0 or yMove != 0)) + { + // Apply current rotation input to the rotation matrix + // ref: https://forums.ldraw.org/thread-22006-post-24426.html#pid24426 + glPushMatrix(); + glLoadIdentity(); + // 0.6 is an arbitrary rotation sensitivity scalar + glRotatef(0.6 * hypot(xMove, yMove), yMove, xMove, 0); + glMultMatrixf(m_rotationMatrix.constData()); + glGetFloatv(GL_MODELVIEW_MATRIX, m_rotationMatrix.data()); + glPopMatrix(); + m_isCameraMoving = true; } // Start the tool tip timer @@ -791,9 +730,6 @@ m_globalpos = event->globalPos(); m_mousePositionF = event->localPos(); - // Calculate 3d position of the cursor - m_position3D = (camera() != FreeCamera) ? convert2dTo3d (m_mousePosition, true) : Origin; - highlightCursorObject(); update(); event->accept(); @@ -811,7 +747,6 @@ void GLRenderer::keyReleaseEvent(QKeyEvent* event) { m_currentKeyboardModifiers = event->modifiers(); - m_currentEditMode->keyReleased(event); update(); } @@ -848,7 +783,7 @@ void GLRenderer::setCamera(Camera camera) { // The edit mode may forbid the free camera. - if (m_currentEditMode->allowFreeCamera() or camera != FreeCamera) + if (freeCameraAllowed() or camera != FreeCamera) { m_camera = camera; m_config->setCamera(int {camera}); @@ -969,31 +904,6 @@ // ============================================================================= // -void GLRenderer::setEditMode(EditModeType a) -{ - if (m_currentEditMode and m_currentEditMode->type() == a) - return; - - delete m_currentEditMode; - m_currentEditMode = AbstractEditMode::createByType(this, a); - - // If we cannot use the free camera, use the top one instead. - if (camera() == FreeCamera and not m_currentEditMode->allowFreeCamera()) - setCamera(TopCamera); - - m_window->updateEditModeActions(); - update(); -} - -// ============================================================================= -// -EditModeType GLRenderer::currentEditModeType() const -{ - return m_currentEditMode->type(); -} - -// ============================================================================= -// void GLRenderer::setPicking(bool value) { m_isDrawingSelectionScene = value; @@ -1237,14 +1147,6 @@ // ============================================================================= // -void GLRenderer::mouseDoubleClickEvent (QMouseEvent* ev) -{ - if (m_currentEditMode->mouseDoubleClicked (ev)) - ev->accept(); -} - -// ============================================================================= -// void GLRenderer::highlightCursorObject() { if (not m_config->highlightObjectBelowCursor() and objectAtCursor() == nullptr) @@ -1292,26 +1194,6 @@ ev->acceptProposedAction(); } -void GLRenderer::dropEvent (QDropEvent* ev) -{ - if (m_window and ev->source() == m_window->getPrimitivesTree()) - { - PrimitiveTreeItem* item = static_cast (m_window->getPrimitivesTree()->currentItem()); - QString primitiveName = item->primitive()->name; - LDSubfileReference* ref = currentDocument()->emplaceAt(m_window->suggestInsertPoint()); - ref->setFileInfo (m_documents->getDocumentByName (primitiveName)); - currentDocument()->addToSelection(ref); - m_window->buildObjectList(); - refresh(); - ev->acceptProposedAction(); - } -} - -Vertex const& GLRenderer::position3D() const -{ - return m_position3D; -} - const CameraInfo& GLRenderer::cameraInfo (Camera camera) const { if (valueInEnum(camera)) @@ -1372,3 +1254,18 @@ { return m_zoom[camera()]; } + +const QGenericMatrix<4, 4, GLfloat>& GLRenderer::rotationMatrix() const +{ + return m_rotationMatrix; +} + +bool GLRenderer::isDrawingSelectionScene() const +{ + return m_isDrawingSelectionScene; +} + +Qt::MouseButtons GLRenderer::lastButtons() const +{ + return m_lastButtons; +} diff -r ac7db4c076c3 -r edddb9b0db9e src/glRenderer.h --- a/src/glRenderer.h Thu Feb 09 11:53:55 2017 +0200 +++ b/src/glRenderer.h Thu Feb 09 13:05:31 2017 +0200 @@ -21,7 +21,6 @@ #include "main.h" #include "model.h" #include "glShared.h" -#include "editmodes/abstractEditMode.h" class GLCompiler; class MessageManager; @@ -83,7 +82,6 @@ Vertex convert2dTo3d(const QPoint& pos2d, bool snap) const; QPoint convert3dTo2d(const Vertex& pos3d) const; QString currentCameraName() const; - EditModeType currentEditModeType() const; int depthNegateFactor() const; void drawPoint(QPainter& painter, QPointF pos, QColor color = QColor (64, 192, 0)) const; void drawBlipCoordinates(QPainter& painter, const Vertex& pos3d); @@ -110,7 +108,6 @@ void pick(int mouseX, int mouseY, bool additive); void pick(const QRect& range, bool additive); LDObject* pickOneObject(int mouseX, int mouseY); - Vertex const& position3D() const; void refresh(); void resetAllAngles(); void resetAngles(); @@ -118,7 +115,6 @@ void setCamera(Camera cam); void setDepthValue(double depth); void setDrawOnly(bool value); - void setEditMode(EditModeType type); void setPicking(bool a); QPen textPen() const; void zoomNotch(bool inward); @@ -126,19 +122,32 @@ protected: void contextMenuEvent(QContextMenuEvent* event); void dragEnterEvent(QDragEnterEvent* event); - void dropEvent(QDropEvent* event); void initializeGL(); void keyPressEvent(QKeyEvent* event); void keyReleaseEvent(QKeyEvent* event); void leaveEvent(QEvent* event); - void mouseDoubleClickEvent(QMouseEvent* event); void mousePressEvent(QMouseEvent* event); void mouseMoveEvent(QMouseEvent* ev); void mouseReleaseEvent(QMouseEvent* ev); - void paintEvent(QPaintEvent* ev); + void paintEvent(QPaintEvent*); void resizeGL(int w, int h); void wheelEvent(QWheelEvent* ev); + virtual void overpaint(QPainter& painter); + virtual bool freeCameraAllowed() const; + bool isDrawingSelectionScene() const; + Qt::MouseButtons lastButtons() const; + double panning (Axis ax) const; + const QGenericMatrix<4, 4, GLfloat>& rotationMatrix() const; + double& panning (Axis ax); + double& zoom(); + + template + QString format (QString fmtstr, Args... args) + { + return ::format (fmtstr, args...); + } + private: Model* const m_model; GLCompiler* m_compiler; @@ -147,7 +156,6 @@ QTimer* m_toolTipTimer; Qt::MouseButtons m_lastButtons; Qt::KeyboardModifiers m_currentKeyboardModifiers; - Vertex m_position3D; double m_virtualWidth; double m_virtualHeight; QGenericMatrix<4, 4, GLfloat> m_rotationMatrix; @@ -175,24 +183,14 @@ int m_height = 0; int m_totalMouseMove; QColor m_backgroundColor; - AbstractEditMode* m_currentEditMode = nullptr; GLuint m_axesVbo; GLuint m_axesColorVbo; void calcCameraIcons(); void drawVbos (SurfaceVboType surface, ComplementVboType colors, GLenum type); - double& panning (Axis ax); - double panning (Axis ax) const; - double& zoom(); void zoomToFit(); void zoomAllToFit(); - template - QString format (QString fmtstr, Args... args) - { - return ::format (fmtstr, args...); - } - private slots: void slot_toolTipTimer(); void initializeAxes();