summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2016-09-05 18:24:01 +0200
committerStanislaw Halik <sthalik@misaki.pl>2016-09-05 18:24:01 +0200
commit9e63b4ecafcb66c275760cd28d2cfb0d258a474e (patch)
treebde33bb3e4d3c576637709f056ca994372d9a588
parentb77a3539a46f9ac8fc06ee701e00512aac4c7550 (diff)
spline-widget: stop dragging when alt-tabbing away
Unfortunately now spline widgets grab keyboard focus. Qt doesn't seem to have a better way to detect whether some other application gained focus.
-rw-r--r--spline-widget/spline-widget.cpp39
-rw-r--r--spline-widget/spline-widget.hpp4
2 files changed, 23 insertions, 20 deletions
diff --git a/spline-widget/spline-widget.cpp b/spline-widget/spline-widget.cpp
index d4e01189..ed07419e 100644
--- a/spline-widget/spline-widget.cpp
+++ b/spline-widget/spline-widget.cpp
@@ -33,6 +33,8 @@ spline_widget::spline_widget(QWidget *parent) :
{
update_range();
setMouseTracking(true);
+ setFocusPolicy(Qt::ClickFocus);
+ setCursor(Qt::ArrowCursor);
}
spline_widget::~spline_widget()
@@ -280,7 +282,6 @@ void spline_widget::mousePressEvent(QMouseEvent *e)
points_t points = _config->getPoints();
if (e->button() == Qt::LeftButton)
{
- setFocus();
bool bTouchingPoint = false;
moving_control_point_idx = -1;
if (_config)
@@ -314,7 +315,6 @@ void spline_widget::mousePressEvent(QMouseEvent *e)
{
_config->addPoint(pixel_coord_to_point(e->pos()));
show_tooltip(e->pos());
- setFocus();
}
}
}
@@ -322,7 +322,6 @@ void spline_widget::mousePressEvent(QMouseEvent *e)
if (e->button() == Qt::RightButton)
{
- clearFocus();
if (_config)
{
int found_pt = -1;
@@ -348,22 +347,15 @@ void spline_widget::mousePressEvent(QMouseEvent *e)
void spline_widget::mouseMoveEvent(QMouseEvent *e)
{
- if (!_config || !isEnabled())
- return;
-
- const bool has_focus = hasFocus();
-
- if (!has_focus)
+ if (!_config || !isEnabled() || !hasFocus() || !isActiveWindow())
{
- moving_control_point_idx = -1;
- setCursor(Qt::ArrowCursor);
+ clearFocus();
+ return;
}
points_t points = _config->getPoints();
- if (hasFocus() &&
- moving_control_point_idx >= 0 &&
- moving_control_point_idx < points.size())
+ if (moving_control_point_idx >= 0 && moving_control_point_idx < points.size())
{
const int idx = moving_control_point_idx;
@@ -419,8 +411,6 @@ void spline_widget::mouseMoveEvent(QMouseEvent *e)
}
else
{
- moving_control_point_idx = -1;
-
int i;
bool is_on_point = is_on_pt(e->pos(), &i);
@@ -442,10 +432,11 @@ void spline_widget::mouseMoveEvent(QMouseEvent *e)
void spline_widget::mouseReleaseEvent(QMouseEvent *e)
{
- if (!_config || !isEnabled())
+ if (!_config || !isEnabled() || !isActiveWindow() || !hasFocus())
+ {
+ clearFocus();
return;
-
- clearFocus();
+ }
if (e->button() == Qt::LeftButton)
{
@@ -542,6 +533,16 @@ bool spline_widget::point_within_pixel(const QPointF& pt, const QPoint &pixel)
return sqrt(QPointF::dotProduct(tmp, tmp)) < point_size;
}
+void spline_widget::focusOutEvent(QFocusEvent* e)
+{
+ if (moving_control_point_idx != -1)
+ QToolTip::hideText();
+ moving_control_point_idx = -1;
+ lower();
+ setCursor(Qt::ArrowCursor);
+ e->accept();
+}
+
QPointF spline_widget::pixel_coord_to_point(const QPoint& point)
{
if (!_config)
diff --git a/spline-widget/spline-widget.hpp b/spline-widget/spline-widget.hpp
index 53560d6a..477e3ba6 100644
--- a/spline-widget/spline-widget.hpp
+++ b/spline-widget/spline-widget.hpp
@@ -22,6 +22,7 @@ using namespace options;
#include <QPointF>
#include <QToolTip>
#include <QShowEvent>
+#include <QFocusEvent>
#include <QMetaObject>
#include <QDebug>
@@ -74,7 +75,8 @@ private:
void drawPoint(QPainter& painter, const QPointF& pt, const QColor& colBG, const QColor& border = QColor(50, 100, 120, 200));
void drawLine(QPainter& painter, const QPoint& start, const QPoint& end, const QPen& pen);
bool point_within_pixel(const QPointF& pt, const QPoint& pixel);
-protected:
+
+ void focusOutEvent(QFocusEvent*e) override;
void resizeEvent(QResizeEvent *) override;
private:
bool is_on_pt(const QPoint& pos, int* pt = nullptr);