BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
MaskGraphicsView.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Views/MaskWidgets/MaskGraphicsView.cpp
6 //! @brief Implements class MaskGraphicsView
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
18 #include <QGraphicsScene>
19 #include <QScrollBar>
20 #include <QTransform>
21 #include <QWheelEvent>
22 
23 namespace {
24 const double min_zoom_value = 1.0;
25 const double max_zoom_value = 5.0;
26 const double zoom_step = 0.05;
27 } // namespace
28 
29 MaskGraphicsView::MaskGraphicsView(QGraphicsScene* scene, QWidget* parent)
30  : QGraphicsView(scene, parent), m_current_zoom_value(1.0)
31 {
32  setObjectName("MaskGraphicsView");
33  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
34  setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
35  setStyleSheet("QGraphicsView { border-style: none; }");
36  setMouseTracking(true);
37 }
38 
39 //! Reset given view to original zoom state. Also asks graphics scene to do the same with color map.
41 {
42  setZoomValue(1.0);
43 }
44 
45 void MaskGraphicsView::wheelEvent(QWheelEvent* event)
46 {
47  // hold control button
48  if (isControlButtonIsPressed(event)) {
49 #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
50  centerOn(mapToScene(event->position().toPoint()));
51 #else
52  centerOn(mapToScene(event->pos())); // TODO upon Qt5.14: pos() -> position().toPoint()
53 #endif
54  if (event->angleDelta().y() > 0) {
55  // Zoom in
57  } else {
58  // Zooming out
59  if (horizontalScrollBar()->isVisible() || verticalScrollBar()->isVisible())
61  }
62  } else {
63  QGraphicsView::wheelEvent(event);
64  }
65 }
66 
67 //! On resize event changes scene size and MaskGraphicsProxy so they would get the size of viewport
68 void MaskGraphicsView::resizeEvent(QResizeEvent* event)
69 {
70  QWidget::resizeEvent(event);
71  updateSize(event->size());
72  // for(QGraphicsItem *graphicsItem : scene()->items()) {
73  // if(MaskGraphicsProxy *proxy = dynamic_cast<MaskGraphicsProxy *>(graphicsItem)) {
74  // proxy->resize(event->size());
75  // scene()->setSceneRect(0,0,event->size().width(),event->size().height());
76  // proxy->setPos(0,0);
77  // }
78  // }
79 }
80 
81 void MaskGraphicsView::keyPressEvent(QKeyEvent* event)
82 {
83  switch (event->key()) {
84  case Qt::Key_Left:
85  break;
86  case Qt::Key_Space:
87  if (!event->isAutoRepeat()) {
89  }
90  break;
91  case Qt::Key_Escape:
93  break;
94  case Qt::Key_Delete:
95  emit deleteSelectedRequest();
96  break;
97  case Qt::Key_Backspace:
98  emit deleteSelectedRequest();
99  break;
100  default:
101  QWidget::keyPressEvent(event);
102  }
103 }
104 
105 void MaskGraphicsView::keyReleaseEvent(QKeyEvent* event)
106 {
107  switch (event->key()) {
108  case Qt::Key_Space:
109  if (!event->isAutoRepeat()) {
111  }
112  break;
113  default:
114  QWidget::keyPressEvent(event);
115  }
116 }
117 
119 {
120  if (event->modifiers().testFlag(Qt::ControlModifier)) {
121  return true;
122  }
123  return false;
124 }
125 
127 {
128  MaskGraphicsScene* maskScene = dynamic_cast<MaskGraphicsScene*>(scene());
129  maskScene->cancelCurrentDrawing();
130 }
131 
132 void MaskGraphicsView::setZoomValue(double zoom_value)
133 {
134  if (zoom_value == m_current_zoom_value)
135  return;
136  const QTransform oldMatrix = transform();
137  resetTransform();
138  translate(oldMatrix.dx(), oldMatrix.dy());
139  scale(zoom_value, zoom_value);
140  m_current_zoom_value = zoom_value;
141 }
142 
144 {
145  double zoom_value = m_current_zoom_value - zoom_step;
146  if (zoom_value < min_zoom_value)
147  zoom_value = min_zoom_value;
148  setZoomValue(zoom_value);
149 }
150 
152 {
153  double zoom_value = m_current_zoom_value + zoom_step;
154  if (zoom_value > max_zoom_value)
155  zoom_value = max_zoom_value;
156  setZoomValue(zoom_value);
157 }
158 
159 void MaskGraphicsView::updateSize(const QSize& newSize)
160 {
161  for (QGraphicsItem* graphicsItem : scene()->items()) {
162  if (MaskGraphicsProxy* proxy = dynamic_cast<MaskGraphicsProxy*>(graphicsItem)) {
163  proxy->resize(newSize);
164  scene()->setSceneRect(0, 0, newSize.width(), newSize.height());
165  proxy->setPos(0, 0);
166  }
167  }
168 }
Defines class MaskGraphicsProxy.
Defines class MaskGraphicsScene.
Defines class MaskGraphicsView.
Graphics proxy to place QWidget inside QGraphicsScene, used by MaskEditorCanvas.
Graphics scene for MaskEditorCanvas to draw masks on top of intensity data widgets.
MaskGraphicsView(QGraphicsScene *scene, QWidget *parent=0)
void changeActivityRequest(MaskEditorFlags::Activity)
void wheelEvent(QWheelEvent *event)
void setZoomValue(double zoom_value)
void updateSize(const QSize &newSize)
bool isControlButtonIsPressed(QWheelEvent *event)
void keyPressEvent(QKeyEvent *event)
void resizeEvent(QResizeEvent *event)
On resize event changes scene size and MaskGraphicsProxy so they would get the size of viewport.
void deleteSelectedRequest()
void keyReleaseEvent(QKeyEvent *event)
void onResetViewRequest()
Reset given view to original zoom state. Also asks graphics scene to do the same with color map.