@@ -149,6 +149,25 @@ ScatterplotWidget::ScatterplotWidget() :
149149
150150bool ScatterplotWidget::event (QEvent* event)
151151{
152+ if (!event)
153+ return QOpenGLWidget::event (event);
154+
155+ // Set navigation flag on Alt press/release
156+ if (event->type () == QEvent::KeyRelease)
157+ {
158+ if (const auto * keyEvent = static_cast <QKeyEvent*>(event))
159+ if (keyEvent->key () == Qt::Key_Alt)
160+ _isNavigating = false ;
161+
162+ }
163+ else if (event->type () == QEvent::KeyPress)
164+ {
165+ if (const auto * keyEvent = static_cast <QKeyEvent*>(event))
166+ if (keyEvent->key () == Qt::Key_Alt)
167+ _isNavigating = true ;
168+
169+ }
170+
152171 // Interactions when Alt is pressed
153172 if (isInitialized () && QGuiApplication::keyboardModifiers () == Qt::AltModifier) {
154173
@@ -165,16 +184,14 @@ bool ScatterplotWidget::event(QEvent* event)
165184
166185 case QEvent::MouseButtonPress:
167186 {
168-
169- if (auto * mouseEvent = static_cast <QMouseEvent*>(event))
187+ if (const auto * mouseEvent = static_cast <QMouseEvent*>(event))
170188 {
171189 if (mouseEvent->button () == Qt::MiddleButton)
172190 resetView ();
173191
174192 // Navigation
175193 if (mouseEvent->buttons () == Qt::LeftButton)
176194 {
177- _isNavigating = true ;
178195 _pixelSelectionTool.setEnabled (false );
179196 setCursor (Qt::ClosedHandCursor);
180197 _mousePositions << mouseEvent->pos ();
@@ -187,53 +204,35 @@ bool ScatterplotWidget::event(QEvent* event)
187204
188205 case QEvent::MouseButtonRelease:
189206 {
190- if (_isNavigating)
191- {
192- _isNavigating = false ;
193- _pixelSelectionTool.setEnabled (true );
194- setCursor (Qt::ArrowCursor);
195- _mousePositions.clear ();
196- update ();
197- }
207+ _pixelSelectionTool.setEnabled (true );
208+ setCursor (Qt::ArrowCursor);
209+ _mousePositions.clear ();
210+ update ();
198211
199212 break ;
200213 }
201214
202215 case QEvent::MouseMove:
203216 {
204- if (auto * mouseEvent = static_cast <QMouseEvent*>(event))
217+ if (const auto * mouseEvent = static_cast <QMouseEvent*>(event))
205218 {
206- if (_isNavigating)
207- {
208- _mousePositions << mouseEvent->pos ();
219+ _mousePositions << mouseEvent->pos ();
209220
210- if (mouseEvent->buttons () & Qt::LeftButton && _mousePositions.size () >= 2 ) {
211- const auto & previousMousePosition = _mousePositions[_mousePositions.size () - 2 ];
212- const auto & currentMousePosition = _mousePositions[_mousePositions.size () - 1 ];
213- const auto panVector = currentMousePosition - previousMousePosition;
221+ if (mouseEvent->buttons () == Qt::LeftButton && _mousePositions.size () >= 2 )
222+ {
223+ const auto & previousMousePosition = _mousePositions[_mousePositions.size () - 2 ];
224+ const auto & currentMousePosition = _mousePositions[_mousePositions.size () - 1 ];
225+ const auto panVector = currentMousePosition - previousMousePosition;
214226
215- panBy (panVector);
216- }
227+ panBy (panVector);
217228 }
218229 }
219230
220231 break ;
221232 }
222233
223- case QEvent::KeyRelease:
224- {
225- if (auto * keyEvent = static_cast <QKeyEvent*>(event))
226- {
227- // Reset navigation
228- if (keyEvent && keyEvent->key () == Qt::Key_Alt)
229- {
230- _isNavigating = false ;
231- }
232-
233- }
234- break ;
235- }
236234 }
235+
237236 }
238237
239238 return QOpenGLWidget::event (event);
0 commit comments