From 94cd279ef31f4dc06d3ee0fda957f9585e6d354b Mon Sep 17 00:00:00 2001 From: Jeremy Hu Date: Fri, 30 Mar 2018 07:32:30 +0800 Subject: [PATCH] Render generated model in graphics viewer --- src/modelwidget.cpp | 59 ++++++++++++++++++++++++++++---- src/modelwidget.h | 7 +++- src/skeletoneditgraphicsview.cpp | 15 +++++++- src/skeletoneditgraphicsview.h | 5 +++ src/skeletonwidget.cpp | 13 ++++--- src/theme.cpp | 6 ++-- 6 files changed, 89 insertions(+), 16 deletions(-) diff --git a/src/modelwidget.cpp b/src/modelwidget.cpp index 6635b664..ab1ca272 100644 --- a/src/modelwidget.cpp +++ b/src/modelwidget.cpp @@ -3,11 +3,12 @@ #include #include #include +#include #include // Modifed from http://doc.qt.io/qt-5/qtopengl-hellogl2-glwidget-cpp.html -bool ModelWidget::m_transparent = false; +bool ModelWidget::m_transparent = true; ModelWidget::ModelWidget(QWidget *parent) : QOpenGLWidget(parent), @@ -18,12 +19,15 @@ ModelWidget::ModelWidget(QWidget *parent) m_renderTriangleVertexCount(0), m_renderEdgeVertexCount(0), m_mesh(NULL), - m_meshUpdated(false) + m_meshUpdated(false), + m_moveStarted(false) { m_core = QSurfaceFormat::defaultFormat().profile() == QSurfaceFormat::CoreProfile; // --transparent causes the clear color to be transparent. Therefore, on systems that // support it, the widget will become transparent apart from the logo. if (m_transparent) { + setAttribute(Qt::WA_AlwaysStackOnTop); + setAttribute(Qt::WA_TranslucentBackground); QSurfaceFormat fmt = format(); fmt.setAlphaBufferSize(8); fmt.setSamples(4); @@ -166,8 +170,12 @@ void ModelWidget::initializeGL() connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &ModelWidget::cleanup); initializeOpenGLFunctions(); - QColor bgcolor = QWidget::palette().color(QWidget::backgroundRole()); - glClearColor(bgcolor.redF(), bgcolor.greenF(), bgcolor.blueF(), m_transparent ? 0 : 1); + if (m_transparent) { + glClearColor(0, 0, 0, 0); + } else { + QColor bgcolor = QWidget::palette().color(QWidget::backgroundRole()); + glClearColor(bgcolor.redF(), bgcolor.greenF(), bgcolor.blueF(), 1); + } m_program = new QOpenGLShaderProgram; m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, m_core ? vertexShaderSourceCore : vertexShaderSource); @@ -285,6 +293,21 @@ void ModelWidget::resizeGL(int w, int h) void ModelWidget::mousePressEvent(QMouseEvent *event) { m_lastPos = event->pos(); + setMouseTracking(true); + if (event->button() == Qt::LeftButton) { + if (!m_moveStarted) { + m_moveStartPos = mapToParent(event->pos()); + m_moveStartGeometry = geometry(); + m_moveStarted = true; + } + } +} + +void ModelWidget::mouseReleaseEvent(QMouseEvent *event) +{ + if (m_moveStarted) { + m_moveStarted = false; + } } void ModelWidget::mouseMoveEvent(QMouseEvent *event) @@ -293,15 +316,37 @@ void ModelWidget::mouseMoveEvent(QMouseEvent *event) int dy = event->y() - m_lastPos.y(); if (event->buttons() & Qt::LeftButton) { - setXRotation(m_xRot - 8 * dy); - setYRotation(m_yRot - 8 * dx); + if (m_moveStarted) { + QRect rect = m_moveStartGeometry; + QPoint pos = mapToParent(event->pos()); + rect.translate(pos.x() - m_moveStartPos.x(), pos.y() - m_moveStartPos.y()); + setGeometry(rect); + } } else if (event->buttons() & Qt::RightButton) { setXRotation(m_xRot - 8 * dy); - setZRotation(m_zRot - 8 * dx); + setYRotation(m_yRot - 8 * dx); } m_lastPos = event->pos(); } +void ModelWidget::wheelEvent(QWheelEvent *event) +{ + if (m_moveStarted) + return; + qreal delta = event->delta() / 10; + if (QGuiApplication::queryKeyboardModifiers().testFlag(Qt::ShiftModifier)) { + if (delta > 0) + delta = 1; + else + delta = -1; + } else { + if (fabs(delta) < 1) + delta = delta < 0 ? -1.0 : 1.0; + } + QMargins margins(delta, delta, delta, delta); + setGeometry(geometry().marginsAdded(margins)); +} + void ModelWidget::updateMesh(Mesh *mesh) { QMutexLocker lock(&m_meshMutex); diff --git a/src/modelwidget.h b/src/modelwidget.h index 195eb3b2..f2f9b7a8 100644 --- a/src/modelwidget.h +++ b/src/modelwidget.h @@ -7,6 +7,7 @@ #include #include #include +#include #include "mesh.h" QT_FORWARD_DECLARE_CLASS(QOpenGLShaderProgram) @@ -42,7 +43,8 @@ protected: void resizeGL(int width, int height) override; void mousePressEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override; - + void wheelEvent(QWheelEvent *event) override; + void mouseReleaseEvent(QMouseEvent *event) override; private: bool m_core; int m_xRot; @@ -68,6 +70,9 @@ private: Mesh *m_mesh; QMutex m_meshMutex; bool m_meshUpdated; + bool m_moveStarted; + QPoint m_moveStartPos; + QRect m_moveStartGeometry; }; #endif diff --git a/src/skeletoneditgraphicsview.cpp b/src/skeletoneditgraphicsview.cpp index c02cdeda..2dc075f1 100644 --- a/src/skeletoneditgraphicsview.cpp +++ b/src/skeletoneditgraphicsview.cpp @@ -24,9 +24,17 @@ SkeletonEditGraphicsView::SkeletonEditGraphicsView(QWidget *parent) : m_lastHoverNodeItem(NULL), m_lastMousePos(0, 0), m_isMovingNodeItem(false), - m_backgroundLoaded(false) + m_backgroundLoaded(false), + m_modelWidget(NULL), + m_modelWidgetProxy(NULL) { setScene(new QGraphicsScene()); + + m_modelWidget = new ModelWidget(this); + m_modelWidget->setMinimumSize(128, 128); + m_modelWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); + m_modelWidget->move(100, 100); + m_modelWidgetProxy = scene()->addWidget(m_modelWidget); m_backgroundItem = new QGraphicsPixmapItem(); m_backgroundItem->setOpacity(0.25); @@ -41,6 +49,11 @@ SkeletonEditGraphicsView::SkeletonEditGraphicsView(QWidget *parent) : scene()->addItem(m_pendingEdgeItem); } +ModelWidget *SkeletonEditGraphicsView::modelWidget() +{ + return m_modelWidget; +} + void SkeletonEditGraphicsView::toggleAddNodeMode() { if (!m_backgroundLoaded) diff --git a/src/skeletoneditgraphicsview.h b/src/skeletoneditgraphicsview.h index 3d9942e9..54340f03 100644 --- a/src/skeletoneditgraphicsview.h +++ b/src/skeletoneditgraphicsview.h @@ -5,9 +5,11 @@ #include #include #include +#include #include "skeletoneditnodeitem.h" #include "skeletoneditedgeitem.h" #include "skeletonsnapshot.h" +#include "modelwidget.h" class SkeletonEditGraphicsView : public QGraphicsView { @@ -31,6 +33,7 @@ public: QPixmap backgroundImage(); void saveToSnapshot(SkeletonSnapshot *snapshot); void loadFromSnapshot(SkeletonSnapshot *snapshot); + ModelWidget *modelWidget(); protected: void mouseMoveEvent(QMouseEvent *event); void wheelEvent(QWheelEvent *event); @@ -51,6 +54,8 @@ private: QPointF m_lastMousePos; bool m_isMovingNodeItem; bool m_backgroundLoaded; + ModelWidget *m_modelWidget; + QGraphicsProxyWidget *m_modelWidgetProxy; private: void toggleAddNodeMode(); void applyAddNodeMode(); diff --git a/src/skeletonwidget.cpp b/src/skeletonwidget.cpp index ab78894c..9dd9bc3e 100644 --- a/src/skeletonwidget.cpp +++ b/src/skeletonwidget.cpp @@ -32,11 +32,13 @@ SkeletonWidget::SkeletonWidget(QWidget *parent) : m_graphicsView->setContentsMargins(0, 0, 0, 0); m_graphicsView->setFrameStyle(QFrame::NoFrame); + /* m_modelWidget = new ModelWidget(this); m_modelWidget->setMinimumSize(128, 128); m_modelWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); m_modelWidget->setWindowFlags(Qt::Tool | Qt::Window); m_modelWidget->setWindowTitle("3D Model"); + */ QVBoxLayout *rightLayout = new QVBoxLayout; rightLayout->addSpacing(0); @@ -112,25 +114,28 @@ SkeletonEditGraphicsView *SkeletonWidget::graphicsView() ModelWidget *SkeletonWidget::modelWidget() { - return m_modelWidget; + //return m_modelWidget; + return graphicsView()->modelWidget(); } void SkeletonWidget::showModelingWidgetAtCorner() { + /* if (!m_modelWidget->isVisible()) { QPoint pos = QPoint(QApplication::desktop()->width(), QApplication::desktop()->height()); m_modelWidget->move(pos.x() - m_modelWidget->width(), pos.y() - m_modelWidget->height()); m_modelWidget->show(); - } + }*/ } void SkeletonWidget::meshReady() { Mesh *resultMesh = m_skeletonToMesh->takeResultMesh(); - showModelingWidgetAtCorner(); - m_modelWidget->updateMesh(resultMesh); + //showModelingWidgetAtCorner(); + //m_modelWidget->updateMesh(resultMesh); + modelWidget()->updateMesh(resultMesh); delete m_skeletonToMesh; m_skeletonToMesh = NULL; if (m_skeletonDirty) { diff --git a/src/theme.cpp b/src/theme.cpp index e8c60dce..c7a4a01b 100644 --- a/src/theme.cpp +++ b/src/theme.cpp @@ -15,14 +15,14 @@ // 0xf7, 0xd9, 0xc8 QColor Theme::red = QColor(0xfc, 0x66, 0x21); -QColor Theme::green = QColor(0xaa, 0xeb, 0xc4); +QColor Theme::green = QColor(0xf7, 0xd9, 0xc8); QColor Theme::blue = QColor(0x2a, 0x5a, 0xac); float Theme::normalAlpha = 128.0 / 255; float Theme::branchAlpha = 64.0 / 255; float Theme::checkedAlpha = 1.0; float Theme::fillAlpha = 50.0 / 255; -int Theme::skeletonNodeBorderSize = 1; -int Theme::skeletonEdgeWidth = 1; +int Theme::skeletonNodeBorderSize = 2; +int Theme::skeletonEdgeWidth = 2; std::map createSideColorNameMap() { std::map map;