Mantid
Loading...
Searching...
No Matches
InputController.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
9#include <QApplication>
10#include <QCursor>
11#include <QMouseEvent>
12#include <QPainter>
13#include <QPixmap>
14
15#include <cmath>
16
18
19//--------------------------------------------------------------------------------
20
21InputController::InputController(QObject *parent, bool contextAllowed)
22 : QObject(parent), m_canShowContextMenu(contextAllowed) {}
23
24//--------------------------------------------------------------------------------
25
31 : InputController(parent, false), m_isButtonPressed(false) {}
32
38 if (event->buttons() & Qt::MiddleButton) {
39 emit initZoom(event->position().toPoint().x(), event->position().toPoint().y());
40 m_isButtonPressed = true;
41 } else if ((event->buttons() & Qt::LeftButton) && !m_isRotationFrozen) {
42 emit initRotation(event->position().toPoint().x(), event->position().toPoint().y());
43 m_isButtonPressed = true;
44 } else if ((event->buttons() & Qt::RightButton) || ((event->buttons() & Qt::LeftButton) && m_isRotationFrozen)) {
45 emit initTranslation(event->position().toPoint().x(), event->position().toPoint().y());
46 m_isButtonPressed = true;
47 }
48}
49
54void InputController3DMove::mouseMoveEvent(QMouseEvent *event) {
55 if ((event->buttons() & Qt::LeftButton) && !m_isRotationFrozen) {
56 emit rotate(event->position().toPoint().x(), event->position().toPoint().y());
57 } else if ((event->buttons() & Qt::RightButton) || ((event->buttons() & Qt::LeftButton) && m_isRotationFrozen)) {
58 emit translate(event->position().toPoint().x(), event->position().toPoint().y());
59 } else if (event->buttons() & Qt::MiddleButton) {
60 emit zoom(event->position().toPoint().x(), event->position().toPoint().y());
61 }
63}
64
69void InputController3DMove::mouseReleaseEvent(QMouseEvent * /*unused*/) {
70 m_isButtonPressed = false;
71 emit finish();
72}
73
78void InputController3DMove::wheelEvent(QWheelEvent *event) {
79 emit wheelZoom(static_cast<int>(event->position().x()), static_cast<int>(event->position().y()),
80 event->angleDelta().y());
81}
82
84
85//--------------------------------------------------------------------------------
86
91InputControllerPick::InputControllerPick(QObject *parent) : InputController(parent), m_isButtonPressed(false) {}
92
96void InputControllerPick::mousePressEvent(QMouseEvent *event) {
97 if (event->button() == Qt::LeftButton) {
98 m_isButtonPressed = true;
99 m_rect.setRect(event->position().toPoint().x(), event->position().toPoint().y(), 1, 1);
100 emit pickPointAt(event->position().toPoint().x(), event->position().toPoint().y());
101 }
102}
103
107void InputControllerPick::mouseMoveEvent(QMouseEvent *event) {
108 if (m_isButtonPressed) {
109 m_rect.setBottomRight(QPoint(event->position().toPoint().x(), event->position().toPoint().y()));
110 emit setSelection(m_rect);
111 } else {
112 emit touchPointAt(event->position().toPoint().x(), event->position().toPoint().y());
113 }
114}
115
119void InputControllerPick::mouseReleaseEvent(QMouseEvent * /*unused*/) {
120 m_isButtonPressed = false;
121 emit finishSelection();
122}
123
124//--------------------------------------------------------------------------------
125
130 : InputController(parent), m_creating(false), m_x(0), m_y(0), m_shapeType(), m_isButtonPressed(false) {}
131
136 if (event->button() == Qt::LeftButton) {
137 m_isButtonPressed = true;
138 if (m_creating && !m_shapeType.isEmpty()) {
139 emit addShape(m_shapeType, event->position().toPoint().x(), event->position().toPoint().y(), m_borderColor,
141 } else if (event->modifiers() & Qt::ControlModifier) {
142 emit selectCtrlAt(event->position().toPoint().x(), event->position().toPoint().y());
143 } else {
144 emit selectAt(event->position().toPoint().x(), event->position().toPoint().y());
145 }
146 m_x = event->position().toPoint().x();
147 m_y = event->position().toPoint().y();
148 m_rect.setRect(event->position().toPoint().x(), event->position().toPoint().y(), 1, 1);
149 }
150}
151
157 if (m_isButtonPressed) {
158 if (m_creating) {
159 emit moveRightBottomTo(event->position().toPoint().x(), event->position().toPoint().y());
160 } else {
161 emit moveBy(event->position().toPoint().x() - m_x, event->position().toPoint().y() - m_y);
162 m_rect.setBottomRight(QPoint(event->position().toPoint().x(), event->position().toPoint().y()));
163 m_x = event->position().toPoint().x();
164 m_y = event->position().toPoint().y();
165 emit setSelection(m_rect);
166 }
167 } else {
168 emit touchPointAt(event->position().toPoint().x(), event->position().toPoint().y());
169 }
170}
171
175void InputControllerDrawShape::mouseReleaseEvent(QMouseEvent * /*unused*/) {
176 m_isButtonPressed = false;
177 m_creating = false;
178 m_shapeType = "";
180}
181
186 switch (event->key()) {
187 case Qt::Key_Delete:
188 case Qt::Key_Backspace:
190 break;
191 case Qt::Key_C:
192 if (event->modifiers() == Qt::CTRL) {
193 emit copySelectedShapes();
194 }
195 break;
196 case Qt::Key_V:
197 if (event->modifiers() == Qt::CTRL) {
198 emit pasteCopiedShapes();
199 }
200 break;
201 }
202}
203
208
212void InputControllerDrawShape::startCreatingShape2D(const QString &type, const QColor &borderColor,
213 const QColor &fillColor) {
214 m_creating = true;
215 m_shapeType = type;
216 m_borderColor = borderColor;
217 m_fillColor = fillColor;
218}
219
224 m_creating = false;
225 emit disabled();
226}
227//--------------------------------------------------------------------------------
228
233 : InputController(parent, false), m_isButtonPressed(false) {}
234
239 if (event->button() == Qt::LeftButton || event->button() == Qt::RightButton) {
240 m_isButtonPressed = true;
241 m_rect.setTopLeft(QPoint(event->position().toPoint().x(), event->position().toPoint().y()));
242 }
243}
244
249 if (m_isButtonPressed) {
250 m_rect.setBottomRight(QPoint(event->position().toPoint().x(), event->position().toPoint().y()));
252 }
254}
255
260 if (m_isButtonPressed && event->button() == Qt::LeftButton) {
261 emit zoom();
262 } else if (m_isButtonPressed && event->button() == Qt::RightButton) {
263 if (m_rect.width() > 1 && m_rect.height() > 1) {
264 emit unzoom();
265 } else {
266 emit resetZoom();
267 }
268 }
269 m_rect = QRect(); // reset rect
270 m_isButtonPressed = false;
271}
272
273//--------------------------------------------------------------------------------
274
279 : InputController(parent), m_max_size(32), m_size(30), m_isLeftButtonPressed(false), m_isRightButtonPressed(false),
280 m_isActive(false), m_cursor(nullptr) {}
281
283
287void InputControllerDraw::mousePressEvent(QMouseEvent *event) {
288 m_isActive = true;
289 setPosition(QPoint(event->position().toPoint().x(), event->position().toPoint().y()));
290 if (event->button() == Qt::LeftButton) {
293 } else if (event->button() == Qt::RightButton) {
296 }
297}
298
302void InputControllerDraw::mouseMoveEvent(QMouseEvent *event) {
303 m_isActive = true;
304 setPosition(QPoint(event->position().toPoint().x(), event->position().toPoint().y()));
307 } else if (m_isRightButtonPressed) {
309 }
310}
311
316 if (event->button() == Qt::LeftButton) {
317 m_isLeftButtonPressed = false;
318 } else if (event->button() == Qt::RightButton) {
320 }
321}
322
323void InputControllerDraw::wheelEvent(QWheelEvent *event) {
324 int d = m_size + (event->angleDelta().y() > 0 ? 4 : -4);
325 if (d > 2 && d < m_max_size) {
326 m_size = d;
327 resize();
328 redrawCursor();
329 QApplication::restoreOverrideCursor();
330 QApplication::setOverrideCursor(QCursor(*m_cursor, 0, 0));
331 }
332}
333
334void InputControllerDraw::enterEvent(QEvent * /*unused*/) {
335 redrawCursor();
336 QApplication::setOverrideCursor(QCursor(*m_cursor, 0, 0));
337 m_isActive = true;
338}
339
340void InputControllerDraw::leaveEvent(QEvent * /*unused*/) {
341 QApplication::restoreOverrideCursor();
342 m_isActive = false;
343}
344
346 if (!m_cursor) {
347 m_cursor = new QPixmap(m_max_size, m_max_size);
348 }
350}
351
353
354//--------------------------------------------------------------------------------
355
357 : InputControllerDraw(parent), m_rect(0, 0, cursorSize(), cursorSize()) {
358 m_image = icon;
359}
360
362
363void InputControllerSelection::onPaint(QPainter &painter) {
364 if (isActive() && !isLeftButtonPressed()) {
365 painter.drawPixmap(m_rect.bottomRight(), *m_image);
366 }
367}
368
370 cursor->fill(QColor(255, 255, 255, 0));
371 QPainter painter(cursor);
372 auto size = cursorSize();
373
374 auto pen = QPen(Qt::DashLine);
375 QList<qreal> dashPattern;
376 dashPattern << 4 << 4;
377 pen.setDashPattern(dashPattern);
378 pen.setColor(QColor(0, 0, 0));
379 painter.setPen(pen);
380 painter.drawRect(QRect(0, 0, size, size));
381
382 pen.setColor(QColor(255, 255, 255));
383 pen.setDashOffset(4);
384 painter.setPen(pen);
385 painter.drawRect(QRect(0, 0, size, size));
386}
387
388void InputControllerSelection::setPosition(const QPoint &pos) { m_rect.moveTopLeft(pos); }
389
391 auto size = cursorSize();
392 m_rect.setSize(QSize(size, size));
393}
394
396
397//--------------------------------------------------------------------------------
398
400 : InputControllerDraw(parent), m_pos(0, 0), m_rect(8), m_creating(false) {
401 makePolygon();
402}
403
405 auto r = double(cursorSize()) / 2.0;
406 double a = 2.0 * M_PI / double(m_rect.size());
407 for (int i = 0; i < m_rect.size(); ++i) {
408 double ia = double(i) * a;
409 auto x = r + static_cast<int>(r * cos(ia));
410 auto y = r + static_cast<int>(r * sin(ia));
411 m_rect[i] = QPointF(x, y);
412 }
413}
414
416 auto poly = m_rect.translated(m_pos);
417 if (m_creating) {
418 m_creating = false;
420 } else {
421 emit draw(poly);
422 }
423}
424
426 auto poly = m_rect.translated(m_pos);
427 emit erase(poly);
428}
429
431 cursor->fill(QColor(255, 255, 255, 0));
432 QPainter painter(cursor);
433
434 auto bRect = m_rect.boundingRect();
435 auto poly = m_rect.translated(-bRect.topLeft());
436
437 auto pen = QPen(Qt::DashLine);
438 QList<qreal> dashPattern;
439 qreal dashLength = cursorSize() < 10 ? 1 : 2;
440 dashPattern << dashLength << dashLength;
441 pen.setDashPattern(dashPattern);
442 pen.setColor(QColor(0, 0, 0));
443 painter.setPen(pen);
444 painter.drawPolygon(poly);
445
446 pen.setColor(QColor(255, 255, 255));
447 pen.setDashOffset(dashLength);
448 painter.setPen(pen);
449 painter.drawPolygon(poly);
450}
451
452void InputControllerDrawAndErase::setPosition(const QPoint &pos) { m_pos = pos; }
453
455
456void InputControllerDrawAndErase::startCreatingShape2D(const QColor &borderColor, const QColor &fillColor) {
457 m_borderColor = borderColor;
458 m_fillColor = fillColor;
459 m_creating = true;
460}
461} // namespace MantidQt::MantidWidgets
size_t m_size
Maximum size of the store.
void(ComponentInfo::* setPosition)(const size_t, const Mantid::Kernel::V3D &)
void wheelEvent(QWheelEvent *) override
Process the mouse wheel event.
void initRotation(int x, int y)
Init rotation. x and y is the starting point on the screen.
void initZoom(int x, int y)
Init zooming. x and y is the zoom starting point on the screen.
void initTranslation(int x, int y)
Init translation. x and y is the starting point on the screen.
void translate(int x, int y)
Translate.
void mouseReleaseEvent(QMouseEvent *) override
Process the mouse release event.
void mousePressEvent(QMouseEvent *) override
Process the mouse press event.
InputController3DMove(QObject *parent)
Constructor.
void wheelZoom(int x, int y, int d)
Wheel zoom.
void mouseMoveEvent(QMouseEvent *) override
Process the mouse move event.
void startCreatingShape2D(const QColor &borderColor, const QColor &fillColor)
void addShape(const QPolygonF &poly, const QColor &borderColor, const QColor &fillColor)
void onDisabled() override
Action on disabling.
void finishSelection(const QRect &)
Rubber band selection is done.
void restoreOverrideCursor()
Restore the cursor to its default image.
void selectCtrlAt(int, int)
Select a shape with ctrl key pressed at a location on the screen.
void removeSelectedShapes()
Remove the selected shapes.
void selectAt(int, int)
Select a shape or a conrol point at a location on the screen.
void mouseMoveEvent(QMouseEvent *) override
Process the mouse move event.
void mouseReleaseEvent(QMouseEvent *) override
Process the mouse button release event.
void copySelectedShapes()
Copy the selected shapes.
void setSelection(const QRect &)
Update the rubber band selection.
void moveBy(int, int)
Move selected shape or a control point by a displacement vector.
void addShape(const QString &type, int x, int y, const QColor &borderColor, const QColor &fillColor)
Add a new shape.
void moveRightBottomTo(int, int)
Resize the current shape by moving the right-bottom control point to a location on the screen.
void startCreatingShape2D(const QString &type, const QColor &borderColor, const QColor &fillColor)
Slot for defining the shape to draw and initializing drawing.
void mousePressEvent(QMouseEvent *) override
Process the mouse press event.
bool m_creating
a shape is being created with a mouse
void leaveEvent(QEvent *) override
Process event of the mouse leaving the widget.
void pasteCopiedShapes()
Paste previously copied shapes.
void touchPointAt(int, int)
Sent when the mouse is moved to a new position with the buttons up.
void keyPressEvent(QKeyEvent *) override
Process the keyboard key press event.
Controller for free drawing on an unwrapped surface.
void mouseReleaseEvent(QMouseEvent *) override
Process the mouse button release event.
void mouseMoveEvent(QMouseEvent *) override
Process the mouse move event.
virtual void drawCursor(QPixmap *cursor)=0
InputControllerDraw(QObject *parent)
Constructor.
void mousePressEvent(QMouseEvent *) override
Process the mouse press event.
void mousePressEvent(QMouseEvent *) override
Process the mouse press event.
void mouseReleaseEvent(QMouseEvent *) override
Process the mouse button release event.
void mouseMoveEvent(QMouseEvent *) override
Process the mouse move event.
InputControllerPick(QObject *parent)
Constructor.
void mousePressEvent(QMouseEvent *) override
Process the mouse press event.
void mouseReleaseEvent(QMouseEvent *) override
Process the mouse release event.
void mouseMoveEvent(QMouseEvent *) override
Process the mouse move event.
void onPaint(QPainter &) override
To be called after the owner widget has drawn its content.
InputControllerSelection(QObject *parent, QPixmap *icon)
The base class for the mouse and keyboard controllers to work with ProjectionSurfaces.
virtual void mouseMoveEvent(QMouseEvent *event)
InputController(QObject *parent, bool contextAllowed=true)