Skip to content

Commit

Permalink
Fix mouse cursor hidding
Browse files Browse the repository at this point in the history
Instead of counting the time difference, I use a QTimer that is reset
every time the mouse move.

I enabled mouse tracking in the main view, so the mouseMoveEvent function
is called at each mouse move.  I think it shouldn't be a problem since
all it does is restore the mouse visibility and reset the timer.
  • Loading branch information
guillaumechereau committed Dec 13, 2017
1 parent cbbc89d commit 7c7d35c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 46 deletions.
63 changes: 27 additions & 36 deletions src/StelMainView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,6 @@ class StelRootItem : public QGraphicsObject
app.draw();
painter->endNativePainting();

mainView->handleMouseCursorTimeout(now);
mainView->drawEnded();
}

Expand Down Expand Up @@ -396,10 +395,6 @@ class StelRootItem : public QGraphicsObject

void mouseMoveEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE
{
// GZ TODO: Where to place the mouse cursor wake-up?
//qDebug() << "StelRootItem::mouseMoveEvent()";
if (QGuiApplication::overrideCursor()!=0)
QGuiApplication::restoreOverrideCursor();
QMouseEvent ev = convertMouseEvent(event);
QPointF pos = ev.pos();
event->setAccepted(StelApp::getInstance().handleMove(pos.x(), pos.y(), ev.buttons()));
Expand Down Expand Up @@ -556,12 +551,13 @@ StelMainView::StelMainView(QSettings* settings)
flagOverwriteScreenshots(false),
screenShotPrefix("stellarium-"),
screenShotDir(""),
cursorTimeout(-1.f), flagCursorTimeout(false), maxfps(10000.f)
flagCursorTimeout(false), maxfps(10000.f)
{
setAttribute(Qt::WA_OpaquePaintEvent);
setAttribute(Qt::WA_AcceptTouchEvents);
setAttribute(Qt::WA_TouchPadAcceptSingleTouchEvents);
setAutoFillBackground(false);
setMouseTracking(true);

configuration = settings;
StelApp::initStatic();
Expand All @@ -570,7 +566,11 @@ StelMainView::StelMainView(QSettings* settings)
minFpsTimer->setTimerType(Qt::PreciseTimer);
minFpsTimer->setInterval(1000/minfps);
connect(minFpsTimer,SIGNAL(timeout()),this,SLOT(minFPSUpdate()));


cursorTimeoutTimer = new QTimer(this);
cursorTimeoutTimer->setSingleShot(true);
connect(cursorTimeoutTimer, SIGNAL(timeout()), this, SLOT(hideCursor()));

// Can't create 2 StelMainView instances
Q_ASSERT(!singleton);
singleton = this;
Expand Down Expand Up @@ -659,16 +659,14 @@ void StelMainView::resizeEvent(QResizeEvent* event)
QGraphicsView::resizeEvent(event);
}

// GZ: This should do something to make the cursor visible again when the mouse is moved.
// However, any implementation of this method seems to gobble all mouse moves.
//void StelMainView::mouseMoveEvent(QMouseEvent *event)
//{
//// //Q_UNUSED(event)
////// thereWasAnEvent();
// event->ignore();
//// if (QGuiApplication::overrideCursor()!=0)
//// QGuiApplication::restoreOverrideCursor();
//}
void StelMainView::mouseMoveEvent(QMouseEvent *event)
{
// Show the cursor and reset the timeout if it is active.
if (QGuiApplication::overrideCursor())
QGuiApplication::restoreOverrideCursor();
if (flagCursorTimeout) cursorTimeoutTimer->start();
QGraphicsView::mouseMoveEvent(event);
}


void StelMainView::focusSky() {
Expand Down Expand Up @@ -1294,26 +1292,20 @@ void StelMainView::drawEnded()
}
}

void StelMainView::handleMouseCursorTimeout(const double now)
void StelMainView::setFlagCursorTimeout(bool b)
{
// GZ restore mouse cursor stuff from pre-0.15.2.
// Manage cursor timeout
if (cursorTimeout>0.f && (now-lastEventTimeSec>cursorTimeout) && flagCursorTimeout)
{
if (QGuiApplication::overrideCursor()==0)
{
QGuiApplication::setOverrideCursor(Qt::BlankCursor);
setMouseTracking(true);
}
}
if (b == flagCursorTimeout) return;
flagCursorTimeout = b;
if (b)
cursorTimeoutTimer->start();
else
{
if (QGuiApplication::overrideCursor()!=0)
{
QGuiApplication::restoreOverrideCursor();
setMouseTracking(false);
}
}
cursorTimeoutTimer->stop();
emit flagCursorTimeoutChanged(b);
}

void StelMainView::hideCursor()
{
QGuiApplication::setOverrideCursor(Qt::BlankCursor);
}

void StelMainView::minFPSUpdate()
Expand Down Expand Up @@ -1344,7 +1336,6 @@ void StelMainView::contextDestroyed()

void StelMainView::thereWasAnEvent()
{
//qDebug()<<"event";
lastEventTimeSec = StelApp::getTotalRunTime();
}

Expand Down
21 changes: 11 additions & 10 deletions src/StelMainView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ public slots:

//! Get the state of the mouse cursor timeout flag
bool getFlagCursorTimeout() {return flagCursorTimeout;}
//! Get the mouse cursor timeout in seconds
double getCursorTimeout() const {return cursorTimeout;}
//! Get the state of the mouse cursor timeout flag
void setFlagCursorTimeout(bool b) {flagCursorTimeout=b; emit flagCursorTimeoutChanged(b);}
void setFlagCursorTimeout(bool b);
//! Get the mouse cursor timeout in seconds
double getCursorTimeout() const {return cursorTimeoutTimer->interval() / 1000.0;}
//! Set the mouse cursor timeout in seconds
void setCursorTimeout(double t) {cursorTimeout=t; emit cursorTimeoutChanged(t);}
void setCursorTimeout(double t) {cursorTimeoutTimer->setInterval(t * 1000); emit cursorTimeoutChanged(t);}

//! Set the minimum frames per second. Usually this minimum will be switched to after there are no
//! user events for some seconds to save power. However, if can be useful to set this to a high
Expand Down Expand Up @@ -176,8 +176,8 @@ public slots:
//! Handle window resized events, and change the size of the underlying
//! QGraphicsScene to be the same
virtual void resizeEvent(QResizeEvent* event) Q_DECL_OVERRIDE;
// //! Wake up mouse cursor (if it was hidden)
// virtual void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
//! Wake up mouse cursor (if it was hidden)
virtual void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
signals:
//! emitted when saveScreenShot is requested with saveScreenShot().
//! doScreenshot() does the actual work (it has to do it in the main
Expand All @@ -202,6 +202,8 @@ private slots:
// Do the actual screenshot generation in the main thread with this method.
void doScreenshot(void);
void minFPSUpdate();
void hideCursor();

#ifdef OPENGL_DEBUG_LOGGING
void logGLMessage(const QOpenGLDebugMessage& debugMessage);
void contextDestroyed();
Expand All @@ -213,8 +215,6 @@ private slots:
private:
//! The graphics scene notifies us when a draw finished, so that we can queue the next one
void drawEnded();
//! hide mouse cursor after some time if configured.
void handleMouseCursorTimeout(const double now);
//! Returns the desired OpenGL format settings,
//! on desktop this corresponds to a GL 2.1 context,
//! with 32bit RGBA buffer and 24/8 depth/stencil buffer
Expand Down Expand Up @@ -252,9 +252,9 @@ private slots:
QString screenShotPrefix;
QString screenShotDir;

// Number of seconds before the mouse cursor disappears
double cursorTimeout;
bool flagCursorTimeout;
//! Timer that triggers with the cursor timeout.
QTimer* cursorTimeoutTimer;

bool flagUseButtonsBackground;

Expand All @@ -266,6 +266,7 @@ private slots:
float maxfps;
QTimer* minFpsTimer;


#ifdef OPENGL_DEBUG_LOGGING
QOpenGLDebugLogger* glLogger;
#endif
Expand Down

0 comments on commit 7c7d35c

Please sign in to comment.