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->x(), event->y());
40 m_isButtonPressed = true;
41 } else if ((event->buttons() & Qt::LeftButton) && !m_isRotationFrozen) {
42 emit initRotation(event->x(), event->y());
43 m_isButtonPressed = true;
44 } else if ((event->buttons() & Qt::RightButton) || ((event->buttons() & Qt::LeftButton) && m_isRotationFrozen)) {
45 emit initTranslation(event->x(), event->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->x(), event->y());
57 } else if ((event->buttons() & Qt::RightButton) || ((event->buttons() & Qt::LeftButton) && m_isRotationFrozen)) {
58 emit translate(event->x(), event->y());
59 } else if (event->buttons() & Qt::MiddleButton) {
60 emit zoom(event->x(), event->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->x(), event->y(), 1, 1);
100 emit pickPointAt(event->x(), event->y());
101 }
102}
103
107void InputControllerPick::mouseMoveEvent(QMouseEvent *event) {
108 if (m_isButtonPressed) {
109 m_rect.setBottomRight(QPoint(event->x(), event->y()));
110 emit setSelection(m_rect);
111 } else {
112 emit touchPointAt(event->x(), event->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->x(), event->y(), m_borderColor, m_fillColor);
140 } else if (event->modifiers() & Qt::ControlModifier) {
141 emit selectCtrlAt(event->x(), event->y());
142 } else {
143 emit selectAt(event->x(), event->y());
144 }
145 m_x = event->x();
146 m_y = event->y();
147 m_rect.setRect(event->x(), event->y(), 1, 1);
148 }
149}
150
156 if (m_isButtonPressed) {
157 if (m_creating) {
158 emit moveRightBottomTo(event->x(), event->y());
159 } else {
160 emit moveBy(event->x() - m_x, event->y() - m_y);
161 m_rect.setBottomRight(QPoint(event->x(), event->y()));
162 m_x = event->x();
163 m_y = event->y();
164 emit setSelection(m_rect);
165 }
166 } else {
167 emit touchPointAt(event->x(), event->y());
168 }
169}
170
174void InputControllerDrawShape::mouseReleaseEvent(QMouseEvent * /*unused*/) {
175 m_isButtonPressed = false;
176 m_creating = false;
177 m_shapeType = "";
179}
180
185 switch (event->key()) {
186 case Qt::Key_Delete:
187 case Qt::Key_Backspace:
189 break;
190 case Qt::Key_C:
191 if (event->modifiers() == Qt::CTRL) {
192 emit copySelectedShapes();
193 }
194 break;
195 case Qt::Key_V:
196 if (event->modifiers() == Qt::CTRL) {
197 emit pasteCopiedShapes();
198 }
199 break;
200 }
201}
202
207
211void InputControllerDrawShape::startCreatingShape2D(const QString &type, const QColor &borderColor,
212 const QColor &fillColor) {
213 m_creating = true;
214 m_shapeType = type;
215 m_borderColor = borderColor;
216 m_fillColor = fillColor;
217}
218
223 m_creating = false;
224 emit disabled();
225}
226//--------------------------------------------------------------------------------
227
232 : InputController(parent, false), m_isButtonPressed(false) {}
233
238 if (event->button() == Qt::LeftButton || event->button() == Qt::RightButton) {
239 m_isButtonPressed = true;
240 m_rect.setTopLeft(QPoint(event->x(), event->y()));
241 }
242}
243
248 if (m_isButtonPressed) {
249 m_rect.setBottomRight(QPoint(event->x(), event->y()));
251 }
253}
254
259 if (m_isButtonPressed && event->button() == Qt::LeftButton) {
260 emit zoom();
261 } else if (m_isButtonPressed && event->button() == Qt::RightButton) {
262 if (m_rect.width() > 1 && m_rect.height() > 1) {
263 emit unzoom();
264 } else {
265 emit resetZoom();
266 }
267 }
268 m_rect = QRect(); // reset rect
269 m_isButtonPressed = false;
270}
271
272//--------------------------------------------------------------------------------
273
278 : InputController(parent), m_max_size(32), m_size(30), m_isLeftButtonPressed(false), m_isRightButtonPressed(false),
279 m_isActive(false), m_cursor(nullptr) {}
280
282
286void InputControllerDraw::mousePressEvent(QMouseEvent *event) {
287 m_isActive = true;
288 setPosition(QPoint(event->x(), event->y()));
289 if (event->button() == Qt::LeftButton) {
292 } else if (event->button() == Qt::RightButton) {
295 }
296}
297
301void InputControllerDraw::mouseMoveEvent(QMouseEvent *event) {
302 m_isActive = true;
303 setPosition(QPoint(event->x(), event->y()));
306 } else if (m_isRightButtonPressed) {
308 }
309}
310
315 if (event->button() == Qt::LeftButton) {
316 m_isLeftButtonPressed = false;
317 } else if (event->button() == Qt::RightButton) {
319 }
320}
321
322void InputControllerDraw::wheelEvent(QWheelEvent *event) {
323 int d = m_size + (event->angleDelta().y() > 0 ? 4 : -4);
324 if (d > 2 && d < m_max_size) {
325 m_size = d;
326 resize();
327 redrawCursor();
328 QApplication::restoreOverrideCursor();
329 QApplication::setOverrideCursor(QCursor(*m_cursor, 0, 0));
330 }
331}
332
333void InputControllerDraw::enterEvent(QEvent * /*unused*/) {
334 redrawCursor();
335 QApplication::setOverrideCursor(QCursor(*m_cursor, 0, 0));
336 m_isActive = true;
337}
338
339void InputControllerDraw::leaveEvent(QEvent * /*unused*/) {
340 QApplication::restoreOverrideCursor();
341 m_isActive = false;
342}
343
345 if (!m_cursor) {
346 m_cursor = new QPixmap(m_max_size, m_max_size);
347 }
349}
350
352
353//--------------------------------------------------------------------------------
354
356 : InputControllerDraw(parent), m_rect(0, 0, cursorSize(), cursorSize()) {
357 m_image = icon;
358}
359
361
362void InputControllerSelection::onPaint(QPainter &painter) {
363 if (isActive() && !isLeftButtonPressed()) {
364 painter.drawPixmap(m_rect.bottomRight(), *m_image);
365 }
366}
367
369 cursor->fill(QColor(255, 255, 255, 0));
370 QPainter painter(cursor);
371 auto size = cursorSize();
372
373 auto pen = QPen(Qt::DashLine);
374 QVector<qreal> dashPattern;
375 dashPattern << 4 << 4;
376 pen.setDashPattern(dashPattern);
377 pen.setColor(QColor(0, 0, 0));
378 painter.setPen(pen);
379 painter.drawRect(QRect(0, 0, size, size));
380
381 pen.setColor(QColor(255, 255, 255));
382 pen.setDashOffset(4);
383 painter.setPen(pen);
384 painter.drawRect(QRect(0, 0, size, size));
385}
386
387void InputControllerSelection::setPosition(const QPoint &pos) { m_rect.moveTopLeft(pos); }
388
390 auto size = cursorSize();
391 m_rect.setSize(QSize(size, size));
392}
393
395
396//--------------------------------------------------------------------------------
397
399 : InputControllerDraw(parent), m_pos(0, 0), m_rect(8), m_creating(false) {
400 makePolygon();
401}
402
404 auto r = double(cursorSize()) / 2.0;
405 double a = 2.0 * M_PI / double(m_rect.size());
406 for (int i = 0; i < m_rect.size(); ++i) {
407 double ia = double(i) * a;
408 auto x = r + static_cast<int>(r * cos(ia));
409 auto y = r + static_cast<int>(r * sin(ia));
410 m_rect[i] = QPointF(x, y);
411 }
412}
413
415 auto poly = m_rect.translated(m_pos);
416 if (m_creating) {
417 m_creating = false;
419 } else {
420 emit draw(poly);
421 }
422}
423
425 auto poly = m_rect.translated(m_pos);
426 emit erase(poly);
427}
428
430 cursor->fill(QColor(255, 255, 255, 0));
431 QPainter painter(cursor);
432
433 auto bRect = m_rect.boundingRect();
434 auto poly = m_rect.translated(-bRect.topLeft());
435
436 auto pen = QPen(Qt::DashLine);
437 QVector<qreal> dashPattern;
438 qreal dashLength = cursorSize() < 10 ? 1 : 2;
439 dashPattern << dashLength << dashLength;
440 pen.setDashPattern(dashPattern);
441 pen.setColor(QColor(0, 0, 0));
442 painter.setPen(pen);
443 painter.drawPolygon(poly);
444
445 pen.setColor(QColor(255, 255, 255));
446 pen.setDashOffset(dashLength);
447 painter.setPen(pen);
448 painter.drawPolygon(poly);
449}
450
451void InputControllerDrawAndErase::setPosition(const QPoint &pos) { m_pos = pos; }
452
454
455void InputControllerDrawAndErase::startCreatingShape2D(const QColor &borderColor, const QColor &fillColor) {
456 m_borderColor = borderColor;
457 m_fillColor = fillColor;
458 m_creating = true;
459}
460} // 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)