From fc92e0b4a4502540b5f3235672012f29756b10fb Mon Sep 17 00:00:00 2001 From: dismine Date: Mon, 8 Sep 2014 14:02:14 +0300 Subject: [PATCH] Set (restore) cursor only if not active (active). --HG-- branch : develop --- src/app/tools/drawTools/vtoolsinglepoint.cpp | 18 ++------ src/app/visualization/vcontrolpointspline.cpp | 18 ++------ .../visualization/vgraphicssimpletextitem.cpp | 20 +++------ src/app/widgets/vapplication.cpp | 42 +++++++++++++++++++ src/app/widgets/vapplication.h | 5 ++- 5 files changed, 60 insertions(+), 43 deletions(-) diff --git a/src/app/tools/drawTools/vtoolsinglepoint.cpp b/src/app/tools/drawTools/vtoolsinglepoint.cpp index 45bd66058..2eaf41d5f 100644 --- a/src/app/tools/drawTools/vtoolsinglepoint.cpp +++ b/src/app/tools/drawTools/vtoolsinglepoint.cpp @@ -202,10 +202,7 @@ void VToolSinglePoint::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { VToolPoint::hoverEnterEvent(event); -#ifndef QT_NO_CURSOR - QPixmap pixmap(QLatin1String("://cursor/cursor-arrow-openhand.png")); - QApplication::setOverrideCursor(QCursor(pixmap, 1, 1)); -#endif + VApplication::setOverrideCursor(QStringLiteral("://cursor/cursor-arrow-openhand.png"), 1, 1); } //--------------------------------------------------------------------------------------------------------------------- @@ -214,9 +211,7 @@ void VToolSinglePoint::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) VToolPoint::hoverLeaveEvent(event); //Disable cursor-arrow-openhand -#ifndef QT_NO_CURSOR - QApplication::restoreOverrideCursor(); -#endif + VApplication::restoreOverrideCursor(QStringLiteral("://cursor/cursor-arrow-openhand.png"), 1, 1); } //--------------------------------------------------------------------------------------------------------------------- @@ -224,10 +219,7 @@ void VToolSinglePoint::mousePressEvent(QGraphicsSceneMouseEvent *event) { if (event->button() == Qt::LeftButton && event->type() != QEvent::GraphicsSceneMouseDoubleClick) { - #ifndef QT_NO_CURSOR - QPixmap pixmap(QLatin1String("://cursor/cursor-arrow-closehand.png")); - QApplication::setOverrideCursor(QCursor(pixmap, 1, 1)); - #endif + VApplication::setOverrideCursor(QStringLiteral("://cursor/cursor-arrow-closehand.png"), 1, 1); } VToolPoint::mousePressEvent(event); } @@ -238,9 +230,7 @@ void VToolSinglePoint::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) if (event->button() == Qt::LeftButton && event->type() != QEvent::GraphicsSceneMouseDoubleClick) { //Disable cursor-arrow-closehand - #ifndef QT_NO_CURSOR - QApplication::restoreOverrideCursor(); - #endif + VApplication::restoreOverrideCursor(QStringLiteral("://cursor/cursor-arrow-closehand.png"), 1, 1); } VToolPoint::mouseReleaseEvent(event); } diff --git a/src/app/visualization/vcontrolpointspline.cpp b/src/app/visualization/vcontrolpointspline.cpp index 70b73bda3..d2b0a5bf0 100644 --- a/src/app/visualization/vcontrolpointspline.cpp +++ b/src/app/visualization/vcontrolpointspline.cpp @@ -88,10 +88,7 @@ void VControlPointSpline::paint(QPainter *painter, const QStyleOptionGraphicsIte void VControlPointSpline::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { this->setPen(QPen(Qt::black, qApp->toPixel(qApp->widthMainLine()))); -#ifndef QT_NO_CURSOR - QPixmap pixmap(QLatin1String("://cursor/cursor-arrow-openhand.png")); - QApplication::setOverrideCursor(QCursor(pixmap, 1, 1)); -#endif + VApplication::setOverrideCursor(QStringLiteral("://cursor/cursor-arrow-openhand.png"), 1, 1); QGraphicsEllipseItem::hoverEnterEvent(event); } @@ -100,9 +97,7 @@ void VControlPointSpline::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) { this->setPen(QPen(Qt::black, qApp->toPixel(qApp->widthHairLine()))); //Disable cursor-arrow-openhand -#ifndef QT_NO_CURSOR - QApplication::restoreOverrideCursor(); -#endif + VApplication::restoreOverrideCursor(QStringLiteral("://cursor/cursor-arrow-openhand.png"), 1, 1); QGraphicsEllipseItem::hoverLeaveEvent(event); } @@ -129,10 +124,7 @@ void VControlPointSpline::mousePressEvent(QGraphicsSceneMouseEvent *event) { if (event->button() == Qt::LeftButton && event->type() != QEvent::GraphicsSceneMouseDoubleClick) { - #ifndef QT_NO_CURSOR - QPixmap pixmap(QLatin1String("://cursor/cursor-arrow-closehand.png")); - QApplication::setOverrideCursor(QCursor(pixmap, 1, 1)); - #endif + VApplication::setOverrideCursor(QStringLiteral("://cursor/cursor-arrow-closehand.png"), 1, 1); } QGraphicsEllipseItem::mousePressEvent(event); } @@ -143,9 +135,7 @@ void VControlPointSpline::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) if (event->button() == Qt::LeftButton && event->type() != QEvent::GraphicsSceneMouseDoubleClick) { //Disable cursor-arrow-closehand - #ifndef QT_NO_CURSOR - QApplication::restoreOverrideCursor(); - #endif + VApplication::restoreOverrideCursor(QStringLiteral("://cursor/cursor-arrow-closehand.png"), 1, 1); } QGraphicsEllipseItem::mouseReleaseEvent(event); } diff --git a/src/app/visualization/vgraphicssimpletextitem.cpp b/src/app/visualization/vgraphicssimpletextitem.cpp index 825362721..538adb85a 100644 --- a/src/app/visualization/vgraphicssimpletextitem.cpp +++ b/src/app/visualization/vgraphicssimpletextitem.cpp @@ -33,6 +33,8 @@ #include #include +#include "../widgets/vapplication.h" + //--------------------------------------------------------------------------------------------------------------------- /** * @brief VGraphicsSimpleTextItem default constructor. @@ -105,10 +107,7 @@ void VGraphicsSimpleTextItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { this->setBrush(Qt::green); -#ifndef QT_NO_CURSOR - QPixmap pixmap(QLatin1String("://cursor/cursor-arrow-openhand.png")); - QApplication::setOverrideCursor(QCursor(pixmap, 1, 1)); -#endif + VApplication::setOverrideCursor(QStringLiteral("://cursor/cursor-arrow-openhand.png"), 1, 1); QGraphicsSimpleTextItem::hoverEnterEvent(event); } @@ -123,9 +122,7 @@ void VGraphicsSimpleTextItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) this->setBrush(Qt::black); //Disable cursor-arrow-openhand -#ifndef QT_NO_CURSOR - QApplication::restoreOverrideCursor(); -#endif + VApplication::restoreOverrideCursor(QStringLiteral("://cursor/cursor-arrow-openhand.png"), 1, 1); QGraphicsSimpleTextItem::hoverLeaveEvent(event); } @@ -144,10 +141,7 @@ void VGraphicsSimpleTextItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { if (event->button() == Qt::LeftButton && event->type() != QEvent::GraphicsSceneMouseDoubleClick) { - #ifndef QT_NO_CURSOR - QPixmap pixmap(QLatin1String("://cursor/cursor-arrow-closehand.png")); - QApplication::setOverrideCursor(QCursor(pixmap, 1, 1)); - #endif + VApplication::setOverrideCursor(QStringLiteral("://cursor/cursor-arrow-closehand.png"), 1, 1); } QGraphicsSimpleTextItem::mousePressEvent(event); } @@ -158,9 +152,7 @@ void VGraphicsSimpleTextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) if (event->button() == Qt::LeftButton && event->type() != QEvent::GraphicsSceneMouseDoubleClick) { //Disable cursor-arrow-closehand - #ifndef QT_NO_CURSOR - QApplication::restoreOverrideCursor(); - #endif + VApplication::restoreOverrideCursor(QStringLiteral("://cursor/cursor-arrow-closehand.png"), 1, 1); } QGraphicsSimpleTextItem::mouseReleaseEvent(event); } diff --git a/src/app/widgets/vapplication.cpp b/src/app/widgets/vapplication.cpp index f7399e2d8..110389dd2 100644 --- a/src/app/widgets/vapplication.cpp +++ b/src/app/widgets/vapplication.cpp @@ -2029,3 +2029,45 @@ void VApplication::setCurrentScene(VMainGraphicsScene *value) { currentScene = value; } + +//--------------------------------------------------------------------------------------------------------------------- +void VApplication::setOverrideCursor(const QString &pixmapPath, int hotX, int hotY) +{ +#ifndef QT_NO_CURSOR + QPixmap oldPixmap; + if (QCursor *oldCursor = QGuiApplication::overrideCursor()) + { + oldPixmap = oldCursor->pixmap(); + } + QPixmap newPixmap(pixmapPath); + + QImage oldImage = oldPixmap.toImage(); + QImage newImage = newPixmap.toImage(); + + if (oldImage != newImage ) + { + QApplication::setOverrideCursor(QCursor(newPixmap, hotX, hotY)); + } +#endif +} + +//--------------------------------------------------------------------------------------------------------------------- +void VApplication::restoreOverrideCursor(const QString &pixmapPath, int hotX, int hotY) +{ +#ifndef QT_NO_CURSOR + QPixmap oldPixmap; + if (QCursor *oldCursor = QGuiApplication::overrideCursor()) + { + oldPixmap = oldCursor->pixmap(); + } + QPixmap newPixmap(pixmapPath); + + QImage oldImage = oldPixmap.toImage(); + QImage newImage = newPixmap.toImage(); + + if (oldImage == newImage ) + { + QApplication::restoreOverrideCursor(); + } +#endif +} diff --git a/src/app/widgets/vapplication.h b/src/app/widgets/vapplication.h index a80b72fe5..0f7738f2a 100644 --- a/src/app/widgets/vapplication.h +++ b/src/app/widgets/vapplication.h @@ -95,6 +95,9 @@ public: void setCurrentDocument(VPattern *doc); VPattern *getCurrentDocument()const; + + static void setOverrideCursor(const QString & pixmapPath, int hotX = -1, int hotY = -1); + static void restoreOverrideCursor(const QString & pixmapPath, int hotX = -1, int hotY = -1); private: Q_DISABLE_COPY(VApplication) Unit _patternUnit; @@ -213,7 +216,7 @@ inline void VApplication::setCurrentDocument(VPattern *doc) inline VPattern *VApplication::getCurrentDocument() const { SCASSERT(doc != nullptr) - return doc; + return doc; } #endif // VAPPLICATION_H