File: plotwidget.cpp

package info (click to toggle)
aoflagger 3.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,960 kB
  • sloc: cpp: 83,076; python: 10,187; sh: 260; makefile: 178
file content (153 lines) | stat: -rw-r--r-- 4,308 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "plotwidget.h"

PlotWidget::PlotWidget()
    : _plot(nullptr),
      _mouseIsIn(false),
      _mouseX(0),
      _mouseY(0),
      _isButtonPressed(false),
      _isZooming(false),
      _isPanning(false),
      _bpressStartX(0),
      _bpressStartY(0) {
  add_events(Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_RELEASE_MASK |
             Gdk::BUTTON_PRESS_MASK | Gdk::LEAVE_NOTIFY_MASK |
             Gdk::SCROLL_MASK);
  signal_motion_notify_event().connect(
      sigc::mem_fun(*this, &PlotWidget::onMotion));
  signal_leave_notify_event().connect(
      sigc::mem_fun(*this, &PlotWidget::onLeave));
  signal_button_press_event().connect(
      sigc::mem_fun(*this, &PlotWidget::onButtonPress));
  signal_button_release_event().connect(
      sigc::mem_fun(*this, &PlotWidget::onButtonRelease));
  signal_scroll_event().connect(sigc::mem_fun(*this, &PlotWidget::onScroll));
  signal_draw().connect(sigc::mem_fun(*this, &PlotWidget::onDraw));
  set_size_request(300, 200);
}

PlotWidget::~PlotWidget() { _linkedRedrawConnection.disconnect(); }

bool PlotWidget::onDraw(const Cairo::RefPtr<Cairo::Context>& cr) {
  _beforeDrawSignal();
  const Glib::RefPtr<Gdk::Window> window = get_window();
  if (window && get_width() > 0 && get_height() > 0) {
    if (_plot != nullptr) {
      _plot->Draw(cr, get_width(), get_height());
      if (_isZooming) {
        cr->set_line_width(1.0);
        cr->rectangle(_bpressStartX, _bpressStartY, _mouseX - _bpressStartX,
                      _mouseY - _bpressStartY);
        cr->set_source_rgba(0.35, 0.35, 1.0, 0.4);
        cr->fill_preserve();
        cr->set_source_rgb(0.0, 0.0, 0.0);
        cr->stroke();
      }
    } else {
      cr->set_source_rgba(1, 1, 1, 1);
      cr->paint();
      cr->fill();
    }
  }
  return true;
}

void PlotWidget::Update() { queue_draw(); }

bool PlotWidget::onMotion(GdkEventMotion* event) {
  if (_plot) {
    double posX, posY;
    const bool isInside = _plot->ConvertToPlot(event->x, event->y, posX, posY);
    if (_isZooming) {
      _mouseX = event->x;
      _mouseY = event->y;
      Update();
    } else if (_isPanning) {
      _plot->Pan(event->x - _mouseX, event->y - _mouseY);
      _mouseX = event->x;
      _mouseY = event->y;
      Update();
    } else {
      if (isInside) {
        _mouseX = event->x;
        _mouseY = event->y;
        _mouseIsIn = true;
        _onMouseMoved(posX, posY);
      } else if (_mouseIsIn) {
        _onMouseLeft();
        _mouseIsIn = false;
      }
    }
  }
  return true;
}

bool PlotWidget::onLeave(GdkEventCrossing* /*event*/) {
  if (_mouseIsIn) {
    _onMouseLeft();
    _mouseIsIn = false;
  }
  return true;
}

bool PlotWidget::onButtonPress(GdkEventButton* event) {
  _isButtonPressed = true;
  if (_plot) {
    double posX, posY;
    if (_plot->ConvertToPlot(event->x, event->y, posX, posY)) {
      _mouseX = event->x;
      _mouseY = event->y;
      _bpressStartX = _mouseX;
      _bpressStartY = _mouseY;
      if (event->button == 1)  // left
        _isZooming = true;
      else if (event->button == 3)  // right
        _isPanning = true;
    }
  }
  return true;
}

bool PlotWidget::onButtonRelease(GdkEventButton* event) {
  _isButtonPressed = false;
  if (_plot) {
    const double oldMouseX = _mouseX;
    const double oldMouseY = _mouseY;
    double posX, posY;
    if (_plot->ConvertToPlot(event->x, event->y, posX, posY)) {
      _mouseX = event->x;
      _mouseY = event->y;
      _onButtonReleased(posX, posY);
    }
    if (_isZooming) {
      _isZooming = false;
      if (_bpressStartX != _mouseX || _bpressStartY != _mouseY) {
        double startX, startY;
        _plot->ConvertToPlot(_bpressStartX, _bpressStartY, startX, startY);
        _plot->ZoomTo(startX, startY, posX, posY);
      }
    }
    if (_isPanning) {
      _isPanning = false;
      _plot->Pan(oldMouseX - event->x, oldMouseY - event->y);
    }

    Update();
  }
  return true;
}

bool PlotWidget::onScroll(GdkEventScroll* event) {
  if (_plot) {
    double posX, posY;
    if (_plot->ConvertToPlot(event->x, event->y, posX, posY)) {
      int direction = 0;
      if (event->direction == GDK_SCROLL_UP)
        direction = -1;
      else if (event->direction == GDK_SCROLL_DOWN)
        direction = 1;
      _onScroll(posX, posY, direction);
    }
  }
  return true;
}