Fix event conflicts between model and graphics view

master
Jeremy Hu 2018-03-30 15:41:29 +08:00
parent 94cd279ef3
commit 01e8abf0cc
8 changed files with 113 additions and 28 deletions

View File

@ -13,7 +13,7 @@ int main(int argc, char ** argv)
// https://gist.github.com/QuantumCD/6245215 // https://gist.github.com/QuantumCD/6245215
qApp->setStyle(QStyleFactory::create("Fusion")); qApp->setStyle(QStyleFactory::create("Fusion"));
QPalette darkPalette; QPalette darkPalette;
darkPalette.setColor(QPalette::Window, QColor(53,53,53)); darkPalette.setColor(QPalette::Window, QColor(0x25,0x25,0x25));
darkPalette.setColor(QPalette::WindowText, QColor(239,239,239)); darkPalette.setColor(QPalette::WindowText, QColor(239,239,239));
darkPalette.setColor(QPalette::Base, QColor(25,25,25)); darkPalette.setColor(QPalette::Base, QColor(25,25,25));
darkPalette.setColor(QPalette::AlternateBase, QColor(53,53,53)); darkPalette.setColor(QPalette::AlternateBase, QColor(53,53,53));

View File

@ -1,5 +1,6 @@
#include "modelwidget.h" #include "modelwidget.h"
#include "ds3file.h" #include "ds3file.h"
#include "skeletoneditgraphicsview.h"
#include <QMouseEvent> #include <QMouseEvent>
#include <QOpenGLShaderProgram> #include <QOpenGLShaderProgram>
#include <QCoreApplication> #include <QCoreApplication>
@ -20,7 +21,8 @@ ModelWidget::ModelWidget(QWidget *parent)
m_renderEdgeVertexCount(0), m_renderEdgeVertexCount(0),
m_mesh(NULL), m_mesh(NULL),
m_meshUpdated(false), m_meshUpdated(false),
m_moveStarted(false) m_moveStarted(false),
m_graphicsView(NULL)
{ {
m_core = QSurfaceFormat::defaultFormat().profile() == QSurfaceFormat::CoreProfile; m_core = QSurfaceFormat::defaultFormat().profile() == QSurfaceFormat::CoreProfile;
// --transparent causes the clear color to be transparent. Therefore, on systems that // --transparent causes the clear color to be transparent. Therefore, on systems that
@ -39,6 +41,11 @@ ModelWidget::ModelWidget(QWidget *parent)
} }
} }
void ModelWidget::setGraphicsView(SkeletonEditGraphicsView *view)
{
m_graphicsView = view;
}
ModelWidget::~ModelWidget() ModelWidget::~ModelWidget()
{ {
cleanup(); cleanup();
@ -292,6 +299,8 @@ void ModelWidget::resizeGL(int w, int h)
void ModelWidget::mousePressEvent(QMouseEvent *event) void ModelWidget::mousePressEvent(QMouseEvent *event)
{ {
if (!m_moveStarted && m_graphicsView && m_graphicsView->mousePress(event, m_graphicsView->mapToScene(m_graphicsView->mapFromGlobal(event->globalPos()))))
return;
m_lastPos = event->pos(); m_lastPos = event->pos();
setMouseTracking(true); setMouseTracking(true);
if (event->button() == Qt::LeftButton) { if (event->button() == Qt::LeftButton) {
@ -305,6 +314,8 @@ void ModelWidget::mousePressEvent(QMouseEvent *event)
void ModelWidget::mouseReleaseEvent(QMouseEvent *event) void ModelWidget::mouseReleaseEvent(QMouseEvent *event)
{ {
if (!m_moveStarted && m_graphicsView && m_graphicsView->mouseRelease(event, m_graphicsView->mapToScene(m_graphicsView->mapFromGlobal(event->globalPos()))))
return;
if (m_moveStarted) { if (m_moveStarted) {
m_moveStarted = false; m_moveStarted = false;
} }
@ -312,6 +323,9 @@ void ModelWidget::mouseReleaseEvent(QMouseEvent *event)
void ModelWidget::mouseMoveEvent(QMouseEvent *event) void ModelWidget::mouseMoveEvent(QMouseEvent *event)
{ {
if (!m_moveStarted && m_graphicsView && m_graphicsView->mouseMove(event, m_graphicsView->mapToScene(m_graphicsView->mapFromGlobal(event->globalPos()))))
return;
int dx = event->x() - m_lastPos.x(); int dx = event->x() - m_lastPos.x();
int dy = event->y() - m_lastPos.y(); int dy = event->y() - m_lastPos.y();
@ -331,6 +345,8 @@ void ModelWidget::mouseMoveEvent(QMouseEvent *event)
void ModelWidget::wheelEvent(QWheelEvent *event) void ModelWidget::wheelEvent(QWheelEvent *event)
{ {
if (!m_moveStarted && m_graphicsView && m_graphicsView->wheel(event, m_graphicsView->mapToScene(m_graphicsView->mapFromGlobal(event->globalPos()))))
return;
if (m_moveStarted) if (m_moveStarted)
return; return;
qreal delta = event->delta() / 10; qreal delta = event->delta() / 10;

View File

@ -12,6 +12,8 @@
QT_FORWARD_DECLARE_CLASS(QOpenGLShaderProgram) QT_FORWARD_DECLARE_CLASS(QOpenGLShaderProgram)
class SkeletonEditGraphicsView;
class ModelWidget : public QOpenGLWidget, protected QOpenGLFunctions class ModelWidget : public QOpenGLWidget, protected QOpenGLFunctions
{ {
Q_OBJECT Q_OBJECT
@ -25,6 +27,7 @@ public:
void updateMesh(Mesh *mesh); void updateMesh(Mesh *mesh);
void exportMeshAsObj(const QString &filename); void exportMeshAsObj(const QString &filename);
void setGraphicsView(SkeletonEditGraphicsView *view);
public slots: public slots:
void setXRotation(int angle); void setXRotation(int angle);
@ -73,6 +76,8 @@ private:
bool m_moveStarted; bool m_moveStarted;
QPoint m_moveStartPos; QPoint m_moveStartPos;
QRect m_moveStartGeometry; QRect m_moveStartGeometry;
SkeletonEditGraphicsView *m_graphicsView;
}; };
#endif #endif

View File

@ -30,9 +30,13 @@ void SkeletonEditEdgeItem::updateAppearance()
{ {
QPen pen; QPen pen;
if (m_firstNode) { if (m_firstNode) {
pen.setColor(m_firstNode->sideColor()); QColor color = m_firstNode->sideColor();
color.setAlphaF(Theme::edgeAlpha);
pen.setColor(color);
} else if (m_secondNode) { } else if (m_secondNode) {
pen.setColor(m_secondNode->sideColor()); QColor color = m_secondNode->sideColor();
color.setAlphaF(Theme::edgeAlpha);
pen.setColor(color);
} }
pen.setWidth(Theme::skeletonEdgeWidth); pen.setWidth(Theme::skeletonEdgeWidth);
setPen(pen); setPen(pen);

View File

@ -34,6 +34,7 @@ SkeletonEditGraphicsView::SkeletonEditGraphicsView(QWidget *parent) :
m_modelWidget->setMinimumSize(128, 128); m_modelWidget->setMinimumSize(128, 128);
m_modelWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); m_modelWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
m_modelWidget->move(100, 100); m_modelWidget->move(100, 100);
m_modelWidget->setGraphicsView(this);
m_modelWidgetProxy = scene()->addWidget(m_modelWidget); m_modelWidgetProxy = scene()->addWidget(m_modelWidget);
m_backgroundItem = new QGraphicsPixmapItem(); m_backgroundItem = new QGraphicsPixmapItem();
@ -133,32 +134,38 @@ SkeletonEditEdgeItem *SkeletonEditGraphicsView::findEdgeItemByNodePair(SkeletonE
return NULL; return NULL;
} }
void SkeletonEditGraphicsView::mousePressEvent(QMouseEvent *event) bool SkeletonEditGraphicsView::mousePress(QMouseEvent *event, const QPointF &scenePos)
{ {
QWidget::mousePressEvent(event); bool processed = false;
if (!m_backgroundLoaded) if (!m_backgroundLoaded)
return; return false;
QPointF pos = mapToScene(event->pos()); QPointF pos = scenePos;
if (event->button() == Qt::LeftButton) { if (event->button() == Qt::LeftButton) {
if (!m_inAddNodeMode) { if (!m_inAddNodeMode) {
if (m_lastHoverNodeItem) { if (m_lastHoverNodeItem) {
setNextStartNodeItem(m_lastHoverNodeItem); setNextStartNodeItem(m_lastHoverNodeItem);
m_lastHoverNodeItem = NULL; m_lastHoverNodeItem = NULL;
processed = true;
} else { } else {
if (m_nextStartNodeItem) { if (m_nextStartNodeItem) {
setNextStartNodeItem(NULL); setNextStartNodeItem(NULL);
processed = true;
} }
} }
} }
} }
m_lastMousePos = pos; m_lastMousePos = pos;
return processed;
} }
void SkeletonEditGraphicsView::mouseDoubleClickEvent(QMouseEvent *event) bool SkeletonEditGraphicsView::mouseDoubleClick(QMouseEvent *event, const QPointF &scenePos)
{ {
QWidget::mouseDoubleClickEvent(event); bool processed = false;
if (QApplication::keyboardModifiers() & Qt::ControlModifier) if (QApplication::keyboardModifiers() & Qt::ControlModifier) {
processed = true;
emit changeTurnaroundTriggered(); emit changeTurnaroundTriggered();
}
return processed;
} }
void SkeletonEditGraphicsView::removeSelectedItems() void SkeletonEditGraphicsView::removeSelectedItems()
@ -203,16 +210,19 @@ void SkeletonEditGraphicsView::removeNodeItemAndSidePairs(SkeletonEditNodeItem *
} }
} }
void SkeletonEditGraphicsView::keyPressEvent(QKeyEvent *event) bool SkeletonEditGraphicsView::keyPress(QKeyEvent *event, const QPointF &scenePos)
{ {
QWidget::keyPressEvent(event); bool processed = false;
if (!m_backgroundLoaded) if (!m_backgroundLoaded)
return; return false;
if (event->key() == Qt::Key_A) if (event->key() == Qt::Key_A) {
toggleAddNodeMode(); toggleAddNodeMode();
else if (event->key() == Qt::Key_Delete || event->key() ==Qt::Key_Backspace) { processed = true;
} else if (event->key() == Qt::Key_Delete || event->key() ==Qt::Key_Backspace) {
removeSelectedItems(); removeSelectedItems();
processed = true;
} }
return processed;
} }
void SkeletonEditGraphicsView::resizeEvent(QResizeEvent *event) void SkeletonEditGraphicsView::resizeEvent(QResizeEvent *event)
@ -267,11 +277,11 @@ void SkeletonEditGraphicsView::addEdgeItem(SkeletonEditNodeItem *first, Skeleton
scene()->addItem(newEdge); scene()->addItem(newEdge);
} }
void SkeletonEditGraphicsView::mouseReleaseEvent(QMouseEvent *event) bool SkeletonEditGraphicsView::mouseRelease(QMouseEvent *event, const QPointF &scenePos)
{ {
QWidget::mouseReleaseEvent(event); bool processed = false;
if (!m_backgroundLoaded) if (!m_backgroundLoaded)
return; return false;
if (event->button() == Qt::LeftButton) { if (event->button() == Qt::LeftButton) {
if (m_inAddNodeMode) { if (m_inAddNodeMode) {
if (m_lastHoverNodeItem && m_nextStartNodeItem && if (m_lastHoverNodeItem && m_nextStartNodeItem &&
@ -280,15 +290,18 @@ void SkeletonEditGraphicsView::mouseReleaseEvent(QMouseEvent *event)
if (!findEdgeItemByNodePair(m_lastHoverNodeItem, m_nextStartNodeItem)) { if (!findEdgeItemByNodePair(m_lastHoverNodeItem, m_nextStartNodeItem)) {
addEdgeItem(m_nextStartNodeItem, m_lastHoverNodeItem); addEdgeItem(m_nextStartNodeItem, m_lastHoverNodeItem);
addEdgeItem(m_nextStartNodeItem->nextSidePair(), m_lastHoverNodeItem->nextSidePair()); addEdgeItem(m_nextStartNodeItem->nextSidePair(), m_lastHoverNodeItem->nextSidePair());
processed = true;
emit nodesChanged(); emit nodesChanged();
} }
} else { } else {
addNodeItemAndSidePairs(m_pendingNodeItem->rect(), m_nextStartNodeItem); addNodeItemAndSidePairs(m_pendingNodeItem->rect(), m_nextStartNodeItem);
processed = true;
emit nodesChanged(); emit nodesChanged();
} }
} }
m_isMovingNodeItem = false; m_isMovingNodeItem = false;
} }
return processed;
} }
bool SkeletonEditGraphicsView::canNodeItemMoveTo(SkeletonEditNodeItem *item, QPointF moveTo) bool SkeletonEditGraphicsView::canNodeItemMoveTo(SkeletonEditNodeItem *item, QPointF moveTo)
@ -304,12 +317,12 @@ bool SkeletonEditGraphicsView::canNodeItemMoveTo(SkeletonEditNodeItem *item, QPo
return true; return true;
} }
void SkeletonEditGraphicsView::mouseMoveEvent(QMouseEvent *event) bool SkeletonEditGraphicsView::mouseMove(QMouseEvent *event, const QPointF &scenePos)
{ {
QWidget::mouseMoveEvent(event); bool processed = false;
if (!m_backgroundLoaded) if (!m_backgroundLoaded)
return; return false;
QPointF pos = mapToScene(event->pos()); QPointF pos = scenePos;
QPointF moveTo = QPointF(pos.x() - m_pendingNodeItem->rect().width() / 2, pos.y() - m_pendingNodeItem->rect().height() / 2); QPointF moveTo = QPointF(pos.x() - m_pendingNodeItem->rect().width() / 2, pos.y() - m_pendingNodeItem->rect().height() / 2);
if (moveTo.x() < 0) if (moveTo.x() < 0)
moveTo.setX(0); moveTo.setX(0);
@ -331,11 +344,13 @@ void SkeletonEditGraphicsView::mouseMoveEvent(QMouseEvent *event)
SkeletonEditNodeItem *hoverNodeItem = findNodeItemByPos(pos); SkeletonEditNodeItem *hoverNodeItem = findNodeItemByPos(pos);
if (hoverNodeItem) { if (hoverNodeItem) {
hoverNodeItem->setHovered(true); hoverNodeItem->setHovered(true);
processed = true;
} }
if (hoverNodeItem != m_lastHoverNodeItem) { if (hoverNodeItem != m_lastHoverNodeItem) {
if (m_lastHoverNodeItem) if (m_lastHoverNodeItem)
m_lastHoverNodeItem->setHovered(false); m_lastHoverNodeItem->setHovered(false);
m_lastHoverNodeItem = hoverNodeItem; m_lastHoverNodeItem = hoverNodeItem;
processed = true;
} }
} }
QPointF curMousePos = pos; QPointF curMousePos = pos;
@ -363,10 +378,12 @@ void SkeletonEditGraphicsView::mouseMoveEvent(QMouseEvent *event)
edgeItem->updatePosition(); edgeItem->updatePosition();
} }
} }
processed = true;
emit nodesChanged(); emit nodesChanged();
} }
} }
m_lastMousePos = curMousePos; m_lastMousePos = curMousePos;
return processed;
} }
void SkeletonEditGraphicsView::AddItemRadius(QGraphicsEllipseItem *item, float delta) void SkeletonEditGraphicsView::AddItemRadius(QGraphicsEllipseItem *item, float delta)
@ -410,11 +427,11 @@ bool SkeletonEditGraphicsView::canAddItemRadius(QGraphicsEllipseItem *item, floa
return true; return true;
} }
void SkeletonEditGraphicsView::wheelEvent(QWheelEvent *event) bool SkeletonEditGraphicsView::wheel(QWheelEvent *event, const QPointF &scenePos)
{ {
QWidget::wheelEvent(event); bool processed = false;
if (!m_backgroundLoaded) if (!m_backgroundLoaded)
return; return false;
qreal delta = event->delta() / 10; qreal delta = event->delta() / 10;
if (QGuiApplication::queryKeyboardModifiers().testFlag(Qt::ShiftModifier)) { if (QGuiApplication::queryKeyboardModifiers().testFlag(Qt::ShiftModifier)) {
if (delta > 0) if (delta > 0)
@ -428,8 +445,10 @@ void SkeletonEditGraphicsView::wheelEvent(QWheelEvent *event)
AddItemRadius(m_pendingNodeItem, delta); AddItemRadius(m_pendingNodeItem, delta);
if (!m_inAddNodeMode && m_lastHoverNodeItem) { if (!m_inAddNodeMode && m_lastHoverNodeItem) {
AddItemRadius(m_lastHoverNodeItem, delta); AddItemRadius(m_lastHoverNodeItem, delta);
processed = true;
emit nodesChanged(); emit nodesChanged();
} }
return processed;
} }
void SkeletonEditGraphicsView::setNextStartNodeItem(SkeletonEditNodeItem *item) void SkeletonEditGraphicsView::setNextStartNodeItem(SkeletonEditNodeItem *item)
@ -625,3 +644,35 @@ void SkeletonEditGraphicsView::markAsChild()
emit nodesChanged(); emit nodesChanged();
} }
} }
void SkeletonEditGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
mouseMove(event, mapToScene(event->pos()));
}
void SkeletonEditGraphicsView::wheelEvent(QWheelEvent *event)
{
wheel(event, mapToScene(event->pos()));
}
void SkeletonEditGraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
mouseRelease(event, mapToScene(event->pos()));
}
void SkeletonEditGraphicsView::mousePressEvent(QMouseEvent *event)
{
mousePress(event, mapToScene(event->pos()));
}
void SkeletonEditGraphicsView::mouseDoubleClickEvent(QMouseEvent *event)
{
mouseDoubleClick(event, mapToScene(event->pos()));
}
void SkeletonEditGraphicsView::keyPressEvent(QKeyEvent *event)
{
keyPress(event, QPointF());
}

View File

@ -2,6 +2,7 @@
#define SKELETON_EDIT_GRAPHICS_VIEW_H #define SKELETON_EDIT_GRAPHICS_VIEW_H
#include <QGraphicsView> #include <QGraphicsView>
#include <QMouseEvent> #include <QMouseEvent>
#include <QWheelEvent>
#include <QKeyEvent> #include <QKeyEvent>
#include <QXmlStreamWriter> #include <QXmlStreamWriter>
#include <QXmlStreamReader> #include <QXmlStreamReader>
@ -26,6 +27,12 @@ public slots:
void markAsTrunk(); void markAsTrunk();
void markAsRoot(); void markAsRoot();
void markAsChild(); void markAsChild();
bool mouseMove(QMouseEvent *event, const QPointF &scenePos);
bool wheel(QWheelEvent *event, const QPointF &scenePos);
bool mouseRelease(QMouseEvent *event, const QPointF &scenePos);
bool mousePress(QMouseEvent *event, const QPointF &scenePos);
bool mouseDoubleClick(QMouseEvent *event, const QPointF &scenePos);
bool keyPress(QKeyEvent *event, const QPointF &scenePos);
public: public:
SkeletonEditGraphicsView(QWidget *parent = 0); SkeletonEditGraphicsView(QWidget *parent = 0);
void updateBackgroundImage(const QImage &image); void updateBackgroundImage(const QImage &image);

View File

@ -14,12 +14,13 @@
// White // White
// 0xf7, 0xd9, 0xc8 // 0xf7, 0xd9, 0xc8
QColor Theme::red = QColor(0xfc, 0x66, 0x21); QColor Theme::red = QColor(0xff, 0xff, 0xff);
QColor Theme::green = QColor(0xf7, 0xd9, 0xc8); QColor Theme::green = QColor(0xff, 0xff, 0xff);
QColor Theme::blue = QColor(0x2a, 0x5a, 0xac); QColor Theme::blue = QColor(0x2a, 0x5a, 0xac);
float Theme::normalAlpha = 128.0 / 255; float Theme::normalAlpha = 128.0 / 255;
float Theme::branchAlpha = 64.0 / 255; float Theme::branchAlpha = 64.0 / 255;
float Theme::checkedAlpha = 1.0; float Theme::checkedAlpha = 1.0;
float Theme::edgeAlpha = Theme::normalAlpha / 2;
float Theme::fillAlpha = 50.0 / 255; float Theme::fillAlpha = 50.0 / 255;
int Theme::skeletonNodeBorderSize = 2; int Theme::skeletonNodeBorderSize = 2;
int Theme::skeletonEdgeWidth = 2; int Theme::skeletonEdgeWidth = 2;

View File

@ -14,6 +14,7 @@ public:
static float checkedAlpha; static float checkedAlpha;
static float branchAlpha; static float branchAlpha;
static float fillAlpha; static float fillAlpha;
static float edgeAlpha;
static int skeletonNodeBorderSize; static int skeletonNodeBorderSize;
static int skeletonEdgeWidth; static int skeletonEdgeWidth;
static QString tabButtonSelectedStylesheet; static QString tabButtonSelectedStylesheet;