BornAgain  1.19.79
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/View/Mask/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 
25 const double min_zoom_value = 1.0;
26 const double max_zoom_value = 5.0;
27 const double zoom_step = 0.05;
28 
29 } // namespace
30 
31 MaskGraphicsView::MaskGraphicsView(QGraphicsScene* scene, QWidget* parent)
32  : QGraphicsView(scene, parent)
33  , m_current_zoom_value(1.0)
34 {
35  setObjectName("MaskGraphicsView");
36  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
37  setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
38  setStyleSheet("QGraphicsView { border-style: none; }");
39  setMouseTracking(true);
40 }
41 
42 //! Reset given view to original zoom state. Also asks graphics scene to do the same with color map.
44 {
45  setZoomValue(1.0);
46 }
47 
48 void MaskGraphicsView::wheelEvent(QWheelEvent* event)
49 {
50  // hold control button
51  if (isControlButtonIsPressed(event)) {
52 #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
53  centerOn(mapToScene(event->position().toPoint()));
54 #else
55  centerOn(mapToScene(event->pos())); // TODO upon Qt5.14: pos() -> position().toPoint()
56 #endif
57  if (event->angleDelta().y() > 0) {
58  // Zoom in
60  } else {
61  // Zooming out
62  if (horizontalScrollBar()->isVisible() || verticalScrollBar()->isVisible())
64  }
65  } else {
66  QGraphicsView::wheelEvent(event);
67  }
68 }
69 
70 //! On resize event changes scene size and MaskGraphicsProxy so they would get the size of viewport
71 void MaskGraphicsView::resizeEvent(QResizeEvent* event)
72 {
73  QWidget::resizeEvent(event);
74  updateSize(event->size());
75  // for(QGraphicsItem *graphicsItem : scene()->items()) {
76  // if(MaskGraphicsProxy *proxy = dynamic_cast<MaskGraphicsProxy *>(graphicsItem)) {
77  // proxy->resize(event->size());
78  // scene()->setSceneRect(0,0,event->size().width(),event->size().height());
79  // proxy->setPos(0,0);
80  // }
81  // }
82 }
83 
84 void MaskGraphicsView::keyPressEvent(QKeyEvent* event)
85 {
86  switch (event->key()) {
87  case Qt::Key_Left:
88  break;
89  case Qt::Key_Space:
90  if (!event->isAutoRepeat())
92  break;
93  case Qt::Key_Escape:
95  break;
96  case Qt::Key_Delete:
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  break;
112  default:
113  QWidget::keyPressEvent(event);
114  }
115 }
116 
118 {
119  return event->modifiers().testFlag(Qt::ControlModifier);
120 }
121 
123 {
124  auto* maskScene = dynamic_cast<MaskGraphicsScene*>(scene());
125  maskScene->cancelCurrentDrawing();
126 }
127 
128 void MaskGraphicsView::setZoomValue(double zoom_value)
129 {
130  if (zoom_value == m_current_zoom_value)
131  return;
132  const QTransform oldMatrix = transform();
133  resetTransform();
134  translate(oldMatrix.dx(), oldMatrix.dy());
135  scale(zoom_value, zoom_value);
136  m_current_zoom_value = zoom_value;
137 }
138 
140 {
141  double zoom_value = m_current_zoom_value - zoom_step;
142  if (zoom_value < min_zoom_value)
143  zoom_value = min_zoom_value;
144  setZoomValue(zoom_value);
145 }
146 
148 {
149  double zoom_value = m_current_zoom_value + zoom_step;
150  if (zoom_value > max_zoom_value)
151  zoom_value = max_zoom_value;
152  setZoomValue(zoom_value);
153 }
154 
155 void MaskGraphicsView::updateSize(const QSize& newSize)
156 {
157  for (QGraphicsItem* graphicsItem : scene()->items()) {
158  if (auto* proxy = dynamic_cast<MaskGraphicsProxy*>(graphicsItem)) {
159  proxy->resize(newSize);
160  scene()->setSceneRect(0, 0, newSize.width(), newSize.height());
161  proxy->setPos(0, 0);
162  }
163  }
164 }
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.
void wheelEvent(QWheelEvent *event) override
void changeActivityRequest(MaskEditorFlags::Activity)
void setZoomValue(double zoom_value)
void updateSize(const QSize &newSize)
void resizeEvent(QResizeEvent *event) override
On resize event changes scene size and MaskGraphicsProxy so they would get the size of viewport.
bool isControlButtonIsPressed(QWheelEvent *event)
MaskGraphicsView(QGraphicsScene *scene, QWidget *parent=nullptr)
void keyReleaseEvent(QKeyEvent *event) override
void deleteSelectedRequest()
void keyPressEvent(QKeyEvent *event) override
void onResetViewRequest()
Reset given view to original zoom state. Also asks graphics scene to do the same with color map.