Class: Cosmos::OverviewGraph

Inherits:
LineGraph show all
Defined in:
lib/cosmos/gui/line_graph/overview_graph.rb

Overview

The overview graph provides a graph which contains the entire graph history. It provides a window with draggable bars that expands the viewport on graph. Expanding this viewport results in a callback that can be used to adjust the view on a different graph.

Constant Summary collapse

DEFAULT_ZOOM_FACTOR =

Default zoom when pressing the up or down arrow keys

0.1
DEFAULT_PAN_FACTOR =

Default pan when pressing the left or right arrow keys

0.1

Constants inherited from LineGraph

LineGraph::DOUBLE_CLICK_SECONDS, LineGraph::FRAME_OFFSET, LineGraph::GRAPH_SPACER, LineGraph::LABEL_TICK_SIZE, LineGraph::LEFT_X_LABEL_WIDTH_ADJUST

Instance Attribute Summary collapse

Attributes inherited from LineGraph

#draw_cursor_line_callback, #horizontal_lines, #left_y_max, #left_y_min, #mouse_leave_callback, #mouse_left_button_press_callback, #post_error_callback, #pre_error_callback, #right_y_max, #right_y_min, #x_max, #x_max_label, #x_min, #x_min_label

Instance Method Summary collapse

Methods inherited from LineGraph

#add_horizontal_line, #add_line, #add_popups_for_lines, #adjust_popup_positions, attr_accessor_with_redraw, #auto_scale_x, #auto_scale_y, #auto_scale_y_axis, #build_popups_from_x_value, #build_x_grid_lines, #build_y_grid_lines, #calculate_base, #calculate_scaling_factors, #calculate_x_grid_lines, #calculate_y_grid_lines, #calculate_y_labels, #clear_horizontal_lines, #clear_lines, #convert_x_value_to_text, #convert_y_value_to_text, #draw_cursor_line_and_popups, #draw_cursor_line_and_popups_at_x, #draw_error_icon, #draw_frame, #draw_graph_background, #draw_graph_to_screen, #draw_horizontal_lines, #draw_legend, #draw_legend_text, #draw_origin_lines, #draw_popups, #draw_title, #draw_x_axis_grid_lines, #draw_x_axis_title, #draw_x_label, #draw_y_axis_grid_lines, #draw_y_axis_title, #draw_y_label, #get_legend_position, #manual_scale_x, #manual_scale_y, #paintEvent, #remote_draw_cursor_line_at_x, #resizeEvent, #scale_graph, #scale_graph_to_value_x, #scale_left_to_right_y, #update_graph_size

Constructor Details

#initialize(parent) ⇒ OverviewGraph

Returns a new instance of OverviewGraph.



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
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 38

def initialize(parent)
  super(parent)
  setMaximumHeight(50)
  setFocusPolicy(Qt::StrongFocus)

  @qt_back_color = Cosmos::getColor(@back_color)

  @point_size = 0

  # Adjust minimum height for overview
  @minimum_height = 50
  @size_hint = Qt::Size.new(0, @minimum_height)
  # Size of the window in value space
  @window_size = 100.0
  # Minimum size of the window
  @window_min_size = 0.0
  # Maximum size of the window
  @window_max_size = nil
  # Minimum window x value
  @window_min_x = 0
  # Maximum window x value
  @window_max_x = 0
  # Flag to indicate if we are currently dragging the window
  @drag_window = false
  # Cursor position at start of drag
  @drag_start_x = nil
  # Variable to hold callback function that indicates the window position
  @callback = nil
  # window_min_x in graph coordinates
  @window_min_start = 0
  # window_max_x in graph coordinates
  @window_max_start = 0
  # Flag to indicate if we are currently dragging the bar
  @drag_bar = false
  # Flag to indicate if the cursor is in the selection window
  @cursor_in_window = false
  # The true maximum x value
  @true_max_x = 0
  @show_cursor_and_popups = false
  @window_left_x = 0
  @window_right_x = 0
end

Instance Attribute Details

#callbackObject

Callback called when the window changes



30
31
32
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 30

def callback
  @callback
end

#window_max_sizeObject

Maximum allowed size of the window in seconds plotted



28
29
30
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 28

def window_max_size
  @window_max_size
end

#window_max_xObject (readonly)

Maximum x value in window



36
37
38
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 36

def window_max_x
  @window_max_x
end

#window_min_sizeObject

Minimum allowed size of the window in seconds plotted



26
27
28
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 26

def window_min_size
  @window_min_size
end

#window_min_xObject (readonly)

Minimum x value in window



34
35
36
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 34

def window_min_x
  @window_min_x
end

#window_sizeObject

Window size in seconds plotted



32
33
34
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 32

def window_size
  @window_size
end

Instance Method Details

#back_color=(value) ⇒ Object



310
311
312
313
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 310

def back_color=(value)
  super(value)
  @qt_back_color = Cosmos::getColor(@back_color)
end

#drag_bar(x) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 138

def drag_bar(x)
  if @cursor_on_bar == :LEFT
    temp_min = (@window_min_start + (x - @drag_start_x))
    temp_max = @window_max_start
  else
    temp_min = @window_min_start
    temp_max = (@window_max_start + (x - @drag_start_x))
  end

  value_temp_min = scale_graph_to_value_x(temp_min)
  value_temp_max = scale_graph_to_value_x(temp_max)

  # Verify the left bar hasn't moved past the minimum size
  if value_temp_min > (@window_max_x - @window_min_size)
    @window_min_x = @window_max_x - @window_min_size
  # Verify the left bar hasn't moved past the maximum size
  elsif @window_max_size and value_temp_min < (@window_max_x - @window_max_size)
    @window_min_x = @window_max_x - @window_max_size
  else
    @window_min_x = value_temp_min
  end

  # Verify the left bar hasn't been moved past the end
  if scale_value_to_graph_x(@window_min_x) < @graph_left_x
    @window_min_x = @x_min
  end

  # Verify the right bar hasn't moved past the minimum size
  if value_temp_max < (@window_min_x + @window_min_size)
    @window_max_x = @window_min_x + @window_min_size
  # Verify the right bar hasn't moved past the maximum size
  elsif @window_max_size and value_temp_max > (@window_min_x + @window_max_size)
    @window_max_x = @window_min_x + @window_max_size
  else
    @window_max_x = value_temp_max
  end

  # Verify the right bar hasn't been moved past the end
  if scale_value_to_graph_x(@window_max_x) > @graph_right_x
    @window_max_x = @x_max
  end

  # Update window size
  @window_min_x = @x_min if @window_min_x < @x_min
  @window_max_x = @x_max if @window_max_x > @x_max
  @window_size = @window_max_x - @window_min_x

  @redraw_needed = true
  graph()
end

#drag_lasso(x) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 189

def drag_lasso(x)
  if (x - @drag_start_x).abs > 5
    @drag_bar = true
    @window_min_start = @drag_start_x
    @window_max_start = @drag_start_x
    if x > @drag_start_x
      @cursor_on_bar = :RIGHT
    else
      @cursor_on_bar = :LEFT
    end
  end
end

#drag_window(x) ⇒ Object



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
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 110

def drag_window(x)
  @cursor = Qt::Cursor.new(Qt::ClosedHandCursor)
  self.setCursor(@cursor)

  # Calculate the movement in terms of the graph coordinates (not the x values)
  temp_min = (@window_min_start + (x - @drag_start_x))
  temp_max = (@window_max_start + (x - @drag_start_x))

  # Only graph if the user hasn't dragged the window outside the graph
  if temp_min >= @graph_left_x and temp_max <= @graph_right_x
    #Now convert the graph coordinates back to the x values
    @window_min_x = scale_graph_to_value_x(temp_min)
    @window_max_x = scale_graph_to_value_x(temp_max)
  else # The user dragged outside the allowable area
    if temp_min < @graph_left_x
      @window_min_x = @x_min
      @window_max_x = @x_min + @window_size
    elsif temp_max > @graph_right_x
      @window_max_x = scale_graph_to_value_x(@graph_right_x)
      @window_min_x = @window_max_x - @window_size
    end
  end
  @window_min_x = @x_min if @window_min_x < @x_min
  @window_max_x = @x_max if @window_max_x > @x_max
  @redraw_needed = true
  graph()
end

#focusInEvent(event) ⇒ Object



392
393
394
395
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 392

def focusInEvent(event)
  self.frame_color = 'gray'
  update()
end

#focusOutEvent(event) ⇒ Object



397
398
399
400
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 397

def focusOutEvent(event)
  self.frame_color = 'black'
  update()
end

#graph(move_window = false) ⇒ Object

Draws the overview graph



301
302
303
304
305
306
307
308
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 301

def graph(move_window = false)
  @move_window = move_window
  determine_graph_size()
  scale_graph()
  calculate_scaling_factors()
  calculate_window_lines()
  super()
end

#keyPressEvent(event) ⇒ Object



378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 378

def keyPressEvent(event)
  case event.key
  when Qt::Key_Up
    zoom_in()
  when Qt::Key_Down
    zoom_out()
  when Qt::Key_Left
    pan_left()
  when Qt::Key_Right
    pan_right()
  end
  super(event)
end

#leaveEvent(event) ⇒ Object

Handler for leaveEvent



246
247
248
249
250
251
252
253
254
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 246

def leaveEvent(event)
  super(event)
  self.unsetCursor()
  @cursor_in_window = false
  @cursor_on_bar = nil
  @drag_start_x = nil
  @drag_bar = false
  @drag_window = false
end

#mouseMoveEvent(event) ⇒ Object

Handles the mouse moving over the overview graph



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 229

def mouseMoveEvent(event)
  super(event)
  # Get the cursor position
  x = mapFromGlobal(self.cursor.pos).x

  if @drag_window
    drag_window(x)
  elsif @drag_bar # We're in the middle of moving one of the window bars
    drag_bar(x)
  elsif @drag_start_x # Lasso start
    drag_lasso(x)
  else # We're not dragging the window, the user just moused around so show the right cursor
    show_cursor(x)
  end
end

#mousePressEvent(event) ⇒ Object

Handles the left button being pressed in the overview graph



257
258
259
260
261
262
263
264
265
266
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 257

def mousePressEvent(event)
  super(event)

  # Store off the mouse click x coordinate on the graph and the window x coordinates
  @drag_start_x = mapFromGlobal(self.cursor.pos).x
  @window_min_start = scale_value_to_graph_x(@window_min_x)
  @window_max_start = scale_value_to_graph_x(@window_max_x)
  @drag_window = true if @cursor_in_window
  @drag_bar = true if @cursor_on_bar
end

#mouseReleaseEvent(event) ⇒ Object

Handles the left button being released



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 269

def mouseReleaseEvent(event)
  self.unsetCursor()
  if !(@drag_bar or @drag_window) and @drag_start_x
    # Figure out what x value the click corresponds to
    xpoint = scale_graph_to_value_x(@drag_start_x)

    # Move the window values to make them equidistant from the click
    @window_min_x = xpoint - @window_size / 2
    @window_max_x = xpoint + @window_size / 2

    # Ensure we don't put the window outside the graphable points
    if @window_min_x < @x_min
      @window_min_x = @x_min
      @window_max_x = @x_min + @window_size
    elsif @window_max_x > @x_max
      @window_max_x = @x_max
      @window_min_x = @x_max - @window_size
    end

    @drag_window = true
    @redraw_needed = true
    graph()
    @drag_window = false
  end

  @drag_start_x = nil
  @drag_bar = false
  @drag_window = false
  @callback.call(self) unless @callback.nil?
end

#pan(amount) ⇒ Object



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 344

def pan(amount)
  # Move the window values to make them equidistant from the click
  new_window_min_x = @window_min_x + amount
  new_window_max_x = @window_max_x + amount

  # Ensure we don't put the window outside the graphable points
  if new_window_min_x >= @x_min and new_window_max_x <= @x_max
    @window_min_x = new_window_min_x
    @window_max_x = new_window_max_x
  elsif new_window_min_x < @x_min
    @window_min_x = @x_min
    @window_max_x = @x_min + @window_size
  else
    @window_min_x = @x_max - @window_size
    @window_max_x = @x_max
  end
  @window_min_x = @x_min if @window_min_x < @x_min
  @window_max_x = @x_max if @window_max_x > @x_max

  @drag_window = true
  @redraw_needed = true
  graph()
  @drag_window = false
  @callback.call(self) unless @callback.nil?
end

#pan_leftObject



370
371
372
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 370

def pan_left
  pan(-@window_size * DEFAULT_PAN_FACTOR)
end

#pan_rightObject



374
375
376
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 374

def pan_right
  pan(@window_size * DEFAULT_PAN_FACTOR)
end

#set_window_pos(window_min_x, window_max_x, use_callback) ⇒ Object



104
105
106
107
108
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 104

def set_window_pos(window_min_x, window_max_x, use_callback)
  @window_min_x = window_min_x
  @window_max_x = window_max_x
  @callback.call(self) if use_callback and @callback
end

#set_window_size(new_window_size, use_callback) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 85

def set_window_size(new_window_size, use_callback)
  @window_size = new_window_size
  @window_size = @window_min_size if @window_min_size and @window_size < @window_min_size
  @window_size = @window_max_size if @window_max_size and @window_size > @window_max_size
  if @lines.empty?
    @window_max_x = 1.0
    @window_min_x = -1.0
  else
    @window_min_x = @window_max_x - @window_size
  end
  graph(false)
  @callback.call(self) if use_callback and @callback
end

#show_cursor(x) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 202

def show_cursor(x)
  # Initially set the variables so the cursor isn't anywhere special
  @cursor_on_bar = nil
  @cursor_in_window = false

  # Figure out if the mouse is inside the graph so we show the Hand cursor
  gx = scale_graph_to_value_x(x)

  # Allow the user to drag the white window bars to increase the main view width
  if (scale_value_to_graph_x(@window_min_x) - x).abs < 5
    @cursor = Qt::Cursor.new(Qt::SizeHorCursor)
    self.setCursor(@cursor)
    @cursor_on_bar = :LEFT
  elsif (scale_value_to_graph_x(@window_max_x) - x).abs < 5
    @cursor = Qt::Cursor.new(Qt::SizeHorCursor)
    self.setCursor(@cursor)
    @cursor_on_bar = :RIGHT
  elsif (@window_min_x) < gx and gx < (@window_max_x)
    @cursor = Qt::Cursor.new(Qt::OpenHandCursor)
    self.setCursor(@cursor)
    @cursor_in_window = true
  else
    self.unsetCursor()
  end
end

#sizeHintObject



81
82
83
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 81

def sizeHint
  return @size_hint
end

#zoom(zoom_factor) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 315

def zoom(zoom_factor)
  @window_size = @window_size * zoom_factor
  @window_size = @window_min_size if @window_min_size and @window_size < @window_min_size
  @window_size = @window_max_size if @window_max_size and @window_size > @window_max_size
  if @lines.empty?
    @window_max_x = 1.0
    @window_min_x = -1.0
  else
    center = (@window_max_x + @window_min_x) / 2
    @window_min_x = center - @window_size / 2
    @window_max_x = center + @window_size / 2
  end
  @window_min_x = @x_min if @window_min_x < @x_min
  @window_max_x = @x_max if @window_max_x > @x_max
  @drag_window = true
  @redraw_needed = true
  graph()
  @drag_window = false
  @callback.call(self) if @callback
end

#zoom_inObject



336
337
338
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 336

def zoom_in
  zoom(1 - DEFAULT_ZOOM_FACTOR)
end

#zoom_outObject



340
341
342
# File 'lib/cosmos/gui/line_graph/overview_graph.rb', line 340

def zoom_out
  zoom(1 + DEFAULT_ZOOM_FACTOR)
end