[TD] Rewrite balloon placing method

When clicking on balloon command, cursor is changing to balloon cursor.
It means user is ready to place a balloon (anywhere on the page).
Balloon object is not created until user click somewhere.

Balloon command can be aborted pressing escape or when page view lost
focus (in other words, when user clic somewhere else than the page).
This commit is contained in:
Franck Jullien
2019-05-19 14:19:00 +02:00
committed by WandererFan
parent 977272e4d4
commit 79f8d4a7cd
8 changed files with 125 additions and 118 deletions

View File

@@ -39,6 +39,7 @@
#include <QDomDocument>
#include <QTextStream>
#include <QFile>
#include <QLabel>
#include <cmath>
#endif
@@ -51,6 +52,7 @@
#include <Gui/FileDialog.h>
#include <Gui/Selection.h>
#include <Gui/WaitCursor.h>
#include <Gui/Command.h>
#include <Mod/TechDraw/App/Geometry.h>
#include <Mod/TechDraw/App/DrawPage.h>
@@ -95,6 +97,8 @@
#include "ViewProviderPage.h"
#include "QGVPage.h"
using namespace Gui;
using namespace TechDraw;
using namespace TechDrawGui;
QGVPage::QGVPage(ViewProviderPage *vp, QGraphicsScene* s, QWidget *parent)
@@ -146,7 +150,10 @@ QGVPage::QGVPage(ViewProviderPage *vp, QGraphicsScene* s, QWidget *parent)
bkgBrush = new QBrush(getBackgroundColor());
balloonIndex = 1;
balloonPlacing(false);
balloonCursor = new QLabel(this);
balloonCursor->setPixmap(QPixmap(QString::fromUtf8(":/icons/cursor-balloon.png")));
balloonCursor->hide();
resetCachedContent();
}
@@ -157,6 +164,13 @@ QGVPage::~QGVPage()
}
void QGVPage::cancelBalloonPlacing(void)
{
getDrawPage()->balloonPlacing = false;
balloonCursor->hide();
QApplication::setOverrideCursor(Qt::ArrowCursor);
}
void QGVPage::drawBackground(QPainter *p, const QRectF &)
{
//Note: Background is not part of scene()
@@ -405,28 +419,26 @@ QGIView * QGVPage::addDrawViewImage(TechDraw::DrawViewImage *view)
QGIView * QGVPage::addViewBalloon(TechDraw::DrawViewBalloon *balloon)
{
auto balloonGroup( new QGIViewBalloon );
auto vBalloon( new QGIViewBalloon );
auto ourScene( scene() );
assert(ourScene);
ourScene->addItem(balloonGroup);
ourScene->addItem(vBalloon);
balloonGroup->setViewPartFeature(balloon);
vBalloon->setViewPartFeature(balloon);
// Find if it belongs to a parent
QGIView *parent = 0;
parent = findParent(balloonGroup);
parent = findParent(vBalloon);
if(balloon->OriginIsSet.getValue() == false) {
if(parent) {
balloonPlacing(true);
QApplication::setOverrideCursor(QCursor(QPixmap(QString::fromUtf8(":/icons/cursor-balloon.png")),0,32));
balloonGroup->connect(parent);
addBalloonToParent(balloonGroup,parent);
}
if(parent)
addBalloonToParent(vBalloon,parent);
if (getDrawPage()->balloonPlacing) {
vBalloon->placeBalloon(balloon->origin);
cancelBalloonPlacing();
}
return balloonGroup;
return vBalloon;
}
void QGVPage::addBalloonToParent(QGIViewBalloon* balloon, QGIView* parent)
@@ -896,6 +908,10 @@ void QGVPage::keyPressEvent(QKeyEvent *event)
kbPanScroll(0, -1);
break;
}
case Qt::Key_Escape: {
cancelBalloonPlacing();
break;
}
default: {
break;
}
@@ -904,6 +920,11 @@ void QGVPage::keyPressEvent(QKeyEvent *event)
QGraphicsView::keyPressEvent(event);
}
void QGVPage::focusOutEvent(QFocusEvent *event) {
Q_UNUSED(event);
cancelBalloonPlacing();
}
void QGVPage::kbPanScroll(int xMove, int yMove)
{
if (xMove != 0) {
@@ -925,31 +946,43 @@ void QGVPage::kbPanScroll(int xMove, int yMove)
void QGVPage::enterEvent(QEvent *event)
{
QGraphicsView::enterEvent(event);
setCursor(Qt::ArrowCursor);
viewport()->setCursor(Qt::ArrowCursor);
if(getDrawPage()->balloonPlacing) {
balloonCursor->hide();
QApplication::setOverrideCursor(QCursor(QPixmap(QString::fromUtf8(":/icons/cursor-balloon.png")),0,32));
} else {
setCursor(Qt::ArrowCursor);
QApplication::restoreOverrideCursor();
viewport()->setCursor(Qt::ArrowCursor);
}
}
void QGVPage::leaveEvent(QEvent * event)
{
if(m_balloonPlacing) {
QApplication::setOverrideCursor(Qt::ArrowCursor);
if(getDrawPage()->balloonPlacing) {
// Get the window geometry & cursor position
const QRect &rect = geometry();
QPoint position = this->mapFromGlobal(QCursor::pos());
// Check the bounds
qint32 x = qBound(rect.left(), position.x(), rect.right());
qint32 y = qBound(rect.top(), position.y(), rect.bottom());
int left_x;
if (balloonCursorPos.x() < 32)
left_x = 0;
else if (balloonCursorPos.x() > (this->contentsRect().right() - 32))
left_x = this->contentsRect().right() - 32;
else
left_x = balloonCursorPos.x();
QPoint newPoint(x, y);
int left_y;
if (balloonCursorPos.y() < 32)
left_y = 0;
else if (balloonCursorPos.y() > (this->contentsRect().bottom() - 32))
left_y = this->contentsRect().bottom() - 32;
else
left_y = balloonCursorPos.y();
// Adjust the cursor
if (x != position.x() || y != position.y())
QCursor::setPos(this->mapToGlobal(newPoint));
event->accept();
/* When cursor leave the page, display balloonCursor where it left */
balloonCursor->setGeometry(left_x ,left_y, 32, 32);
balloonCursor->show();
}
QGraphicsView::leaveEvent(event);
}
@@ -961,14 +994,42 @@ void QGVPage::mousePressEvent(QMouseEvent *event)
void QGVPage::mouseMoveEvent(QMouseEvent *event)
{
balloonCursorPos = event->pos();
QGraphicsView::mouseMoveEvent(event);
}
void QGVPage::mouseReleaseEvent(QMouseEvent *event)
{
if(getDrawPage()->balloonPlacing) {
QApplication::setOverrideCursor(Qt::ArrowCursor);
balloonCursor->hide();
std::string FeatName = getDrawPage()->getDocument()->getUniqueObjectName("Balloon");
std::string PageName = getDrawPage()->getNameInDocument();
Gui::Command::openCommand("Create Balloon");
TechDraw::DrawViewBalloon *balloon = 0;
Gui::Command::openCommand("Create Balloon");
Command::doCommand(Command::Doc,"App.activeDocument().addObject('TechDraw::DrawViewBalloon','%s')",FeatName.c_str());
Command::doCommand(Command::Doc,"App.activeDocument().%s.addView(App.activeDocument().%s)",PageName.c_str(),FeatName.c_str());
balloon = dynamic_cast<TechDraw::DrawViewBalloon *>(getDrawPage()->getDocument()->getObject(FeatName.c_str()));
if (!balloon) {
throw Base::TypeError("CmdTechDrawNewBalloon - balloon not found\n");
}
balloon->sourceView.setValue(getDrawPage()->balloonParent);
balloon->origin = mapToScene(event->pos());
Gui::Command::commitCommand();
balloon->recomputeFeature();
//Horrible hack to force Tree update
double x = getDrawPage()->balloonParent->X.getValue();
getDrawPage()->balloonParent->X.setValue(x);
}
QGraphicsView::mouseReleaseEvent(event);
// setCursor(Qt::ArrowCursor);
// viewport()->setCursor(Qt::ArrowCursor);
}
TechDraw::DrawPage* QGVPage::getDrawPage()