Class: XYP::GUI

Inherits:
Object
  • Object
show all
Defined in:
lib/xyp/gui.rb

Instance Method Summary collapse

Constructor Details

#initializeGUI

Returns a new instance of GUI.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/xyp/gui.rb', line 18

def initialize
  glade_file = File.expand_path(__dir__)+"/gui_v0.glade"
  builder=Gtk::Builder.new
  builder.add_from_file(glade_file)
  builder.connect_signals{|handler| method(handler)}
  @main_window = builder['window1']
  @main_window.signal_connect("destroy"){Gtk.main_quit}

  @drawing =builder['drawingarea1']

  @drawing.add_events [:leave_notify_mask,
                       :button_press_mask,
                       :pointer_motion_mask,
                       :pointer_motion_hint_mask]
  create_callbacks
  dummy_test
end

Instance Method Details

#create_callbacksObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/xyp/gui.rb', line 36

def create_callbacks
  @drawing.signal_connect("draw"){redraw}

  @drawing.signal_connect("button-press-event") do |widget, event|
    @start_drag=Point.new(event.x,event.y)
end

  @drawing.signal_connect("motion-notify-event") do |widget, event|
    do_it=false
    if @start_drag
       modify_center event
       @start_drag=@end_drag
       redraw
    end
end

  @drawing.signal_connect("button-release-event") do |widget, event|
    modify_center event
    @start_drag=nil
    redraw
end
end

#dummy_testObject



171
172
173
174
175
176
177
178
179
# File 'lib/xyp/gui.rb', line 171

def dummy_test
  @plot=Plot.new(:test)
  @plot.set_background_rgba GREY
  @plot.set_data_set @dataset=SINUS
  center_1_1=Point.new(1,1)
  @view=View.new(center_1_1,[60,4])
  @plot.set_view @view
  # plot is done during redraw
end

#handle_window_redimensioningObject



159
160
161
162
# File 'lib/xyp/gui.rb', line 159

def handle_window_redimensioning
  width,height=@drawing.window.width,@drawing.window.height
  @plot.set_window_size [width,height]
end

#load_data(filename) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/xyp/gui.rb', line 104

def load_data filename
  @dataset=IO.readlines(filename).inject({}) do |hash,line|
    x,y=*line.split.map(&:to_f)
    hash[x]=y
    hash
  end

  @plot=Plot.new(filename)
  @plot.set_background_rgba GREY
  @plot.set_data_set @dataset
  on_button_zoom_fit_clicked
end

#modify_center(event) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/xyp/gui.rb', line 59

def modify_center event
  @end_drag=Point.new(event.x,event.y)
  delta_x=((@end_drag.x-@start_drag.x)/@plot.ratio.x)
  delta_y=((@end_drag.y-@start_drag.y)/@plot.ratio.y)
  delta=Point.new(delta_x,-delta_y)
  @view.center=@view.center-delta
end

#on_button_info_clickedObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/xyp/gui.rb', line 80

def on_button_info_clicked
  about = Gtk::AboutDialog.new
  about.set_program_name "xyp"
  about.set_version VERSION
  about.set_copyright "(c) Jean-Christophe Le Lann"
  about.set_comments "Ruby GTK3 XY Plotter"
  about.set_website "http://www.jcll.fr"
  begin
    dir = File.expand_path(__dir__)
     = GdkPixbuf::Pixbuf.new :file => "#{dir}/../../doc/screen.png"
    about. 
  rescue IOError => e
      puts e
      puts "cannot load image"
      exit
  end
  about.run
  about.destroy
end

#on_button_unzoom_x_clickedObject



142
143
144
145
# File 'lib/xyp/gui.rb', line 142

def on_button_unzoom_x_clicked
  @view.dims.x*=2
  redraw
end

#on_button_unzoom_y_clickedObject



152
153
154
155
# File 'lib/xyp/gui.rb', line 152

def on_button_unzoom_y_clicked
  @view.dims.y*=2
  redraw
end

#on_button_zoom_fit_clickedObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/xyp/gui.rb', line 117

def on_button_zoom_fit_clicked
  if @dataset
    min_x,max_x=@dataset.keys.minmax
    min_y,max_y=@dataset.values.minmax

    center_x=min_x+(max_x-min_x).abs/2
    center_y=min_y+(max_y-min_y).abs/2
    center=Point.new(center_x,center_y)

    diff_x=max_x-min_x
    diff_y=max_y-min_y
    dims=[diff_x,diff_y]

    @view=View.new(center,dims)

    @plot.set_view @view
    redraw
  end
end

#on_button_zoom_x_clickedObject



137
138
139
140
# File 'lib/xyp/gui.rb', line 137

def on_button_zoom_x_clicked
  @view.dims.x/=2
  redraw
end

#on_button_zoom_y_clickedObject



147
148
149
150
# File 'lib/xyp/gui.rb', line 147

def on_button_zoom_y_clicked
  @view.dims.y/=2
  redraw
end

#on_filechooserbutton1_file_set(chooser) ⇒ Object



100
101
102
# File 'lib/xyp/gui.rb', line 100

def on_filechooserbutton1_file_set chooser
  load_data(chooser.filename)
end

#quitObject

signal handler for main window destory event



76
77
78
# File 'lib/xyp/gui.rb', line 76

def quit
  Gtk.main_quit
end

#redrawObject



164
165
166
167
168
# File 'lib/xyp/gui.rb', line 164

def redraw
  handle_window_redimensioning
  cr = @drawing.window.create_cairo_context
  @plot.plot(cr)
end

#run(options) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/xyp/gui.rb', line 67

def run options
  @main_window.show
  if filename=options[:data_file]
    load_data(filename)
  end
  Gtk.main
end