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
qApp->setStyle(QStyleFactory::create("Fusion"));
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::Base, QColor(25,25,25));
darkPalette.setColor(QPalette::AlternateBase, QColor(53,53,53));

View File

@ -1,5 +1,6 @@
#include "modelwidget.h"
#include "ds3file.h"
#include "skeletoneditgraphicsview.h"
#include <QMouseEvent>
#include <QOpenGLShaderProgram>
#include <QCoreApplication>
@ -20,7 +21,8 @@ ModelWidget::ModelWidget(QWidget *parent)
m_renderEdgeVertexCount(0),
m_mesh(NULL),
m_meshUpdated(false),
m_moveStarted(false)
m_moveStarted(false),
m_graphicsView(NULL)
{
m_core = QSurfaceFormat::defaultFormat().profile() == QSurfaceFormat::CoreProfile;
// --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()
{
cleanup();
@ -292,6 +299,8 @@ void ModelWidget::resizeGL(int w, int h)
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();
setMouseTracking(true);
if (event->button() == Qt::LeftButton) {
@ -305,6 +314,8 @@ void ModelWidget::mousePressEvent(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) {
m_moveStarted = false;
}
@ -312,6 +323,9 @@ void ModelWidget::mouseReleaseEvent(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 dy = event->y() - m_lastPos.y();
@ -331,6 +345,8 @@ void ModelWidget::mouseMoveEvent(QMouseEvent *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)
return;
qreal delta = event->delta() / 10;

View File

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

View File

@ -30,9 +30,13 @@ void SkeletonEditEdgeItem::updateAppearance()
{
QPen pen;
if (m_firstNode) {
pen.setColor(m_firstNode->sideColor());
QColor color = m_firstNode->sideColor();
color.setAlphaF(Theme::edgeAlpha);
pen.setColor(color);
} 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);
setPen(pen);

View File

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

View File

@ -14,12 +14,13 @@
// White
// 0xf7, 0xd9, 0xc8
QColor Theme::red = QColor(0xfc, 0x66, 0x21);
QColor Theme::green = QColor(0xf7, 0xd9, 0xc8);
QColor Theme::red = QColor(0xff, 0xff, 0xff);
QColor Theme::green = QColor(0xff, 0xff, 0xff);
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::edgeAlpha = Theme::normalAlpha / 2;
float Theme::fillAlpha = 50.0 / 255;
int Theme::skeletonNodeBorderSize = 2;
int Theme::skeletonEdgeWidth = 2;

View File

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