Class: Viewer

Inherits:
Gtk::Window
  • Object
show all
Defined in:
lib/rtl/viewer.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Viewer

I want to show it’s possible to pass some args



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
# File 'lib/rtl/viewer.rb', line 8

def initialize args={} # I want to show it's possible to pass some args
  super()              # mandatory parenthesis ! otherwise : wrong arguments: Gtk::Window#initialize({})
  set_title 'jcll_3'
  set_default_size 900,600
  set_border_width 10
  set_window_position :center
  set_destroy_callback

  @algorithm=Fdgd.new
  @zoom_factor=1
  @shift=Vector.new(0,0)

  hbox = Gtk::Box.new(:horizontal, spacing=6)
  add hbox
  @canvas = Canvas.new
  hbox.pack_start(@canvas,:expand=>true,:fill=> true)
  #...instead of :
  # hbox.add canvas

  vbox   = Gtk::Box.new(:vertical,spacing=6)
  hbox.add vbox

  button = Gtk::Button.new(label:"open")
  button.signal_connect("clicked"){on_open_clicked(button)}
  vbox.pack_start(button,:expand => false, :fill => false, :padding => 0)

  button = Gtk::Button.new(label:"random graph")
  button.signal_connect("clicked"){on_random_clicked(button)}
  vbox.pack_start(button,:expand => false, :fill => false, :padding => 0)

  label = Gtk::Label.new("number of nodes : ")

  vbox.pack_start(label,:expand => false, :fill => false, :padding => 0)

  spinner = Gtk::SpinButton.new(1,100,1)
  spinner.value= @nb_value || 20
  spinner.signal_connect("value-changed"){on_spin_changed(spinner)}
  vbox.pack_start(spinner,:expand => false, :fill => false, :padding => 0)

  button = Gtk::Button.new(:label => "run")
  button.signal_connect("clicked"){on_run_clicked(button)}
  vbox.pack_start(button,:expand => false, :fill => false, :padding => 0)

  button = Gtk::Button.new(:label => "stop")
  button.signal_connect("clicked"){on_stop_clicked(button)}
  vbox.pack_start(button,:expand => false, :fill => false, :padding => 0)

  button = Gtk::Button.new(:label => "step")
  button.signal_connect("clicked"){on_step_clicked(button)}
  vbox.pack_start(button,:expand => false, :fill => false, :padding => 0)

  button = Gtk::Button.new(:label => "shuffle")
  button.signal_connect("clicked"){on_shuffle_clicked(button)}
  vbox.pack_start(button,:expand => false, :fill => false, :padding => 0)

  button = Gtk::Button.new(:label => "center")
  button.signal_connect("clicked"){on_center_clicked(button)}
  vbox.pack_start(button,:expand => false, :fill => false, :padding => 0)

  button = Gtk::Button.new(:label => "zoom+")
  button.signal_connect("clicked"){on_zoom_clicked(button)}
  vbox.pack_start(button,:expand => false, :fill => false, :padding => 0)

  button = Gtk::Button.new(:label => "zoom-")
  button.signal_connect("clicked"){on_unzoom_clicked(button)}
  vbox.pack_start(button,:expand => false, :fill => false, :padding => 0)

  button = Gtk::Button.new(:label => "zoom fit")
  button.signal_connect("clicked"){on_fit_clicked(button)}
  vbox.pack_start(button,:expand => false, :fill => false, :padding => 0)

  button = Gtk::Button.new(:label => "save")
  button.signal_connect("clicked"){on_save_clicked(button)}
  vbox.pack_start(button,:expand => false, :fill => false, :padding => 0)

  button = Gtk::Button.new(:label => "quit")
  button.signal_connect("clicked"){on_quit_clicked(button)}
  vbox.pack_start(button,:expand => false, :fill => false, :padding => 0)
  show_all
end

Instance Method Details

#compute_shift(enclosing_rect) ⇒ Object



220
221
222
223
224
# File 'lib/rtl/viewer.rb', line 220

def compute_shift enclosing_rect
  rect_center_x=(enclosing_rect.first.x+enclosing_rect.last.x)/2.0
  rect_center_y=(enclosing_rect.first.y+enclosing_rect.last.y)/2.0
  @shift=Vector.new(-rect_center_x,-rect_center_y)
end

#compute_zoom(enclosing_rect) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/rtl/viewer.rb', line 207

def compute_zoom enclosing_rect
  min_x=enclosing_rect.first.x
  min_y=enclosing_rect.first.y
  max_x=enclosing_rect.last.x
  max_y=enclosing_rect.last.y

  graph_size=[(max_x-min_x).abs,(max_y-min_y).abs]
  canvas_size=[@canvas.allocation.width,@canvas.allocation.height]
  ratios=[(canvas_size.first.to_f)/graph_size.first,(canvas_size.last.to_f)/graph_size.last]
  @zoom_factor=ratios.min*0.8
  puts "zoom=#{@zoom_factor}"
end

#compute_zoom_and_shiftObject



201
202
203
204
205
# File 'lib/rtl/viewer.rb', line 201

def compute_zoom_and_shift
  enclosing_rect=get_enclosing_rect()
  compute_zoom(enclosing_rect)
  compute_shift(enclosing_rect)
end

#get_enclosing_rectObject



193
194
195
196
197
198
199
# File 'lib/rtl/viewer.rb', line 193

def get_enclosing_rect
  min_x=@graph.nodes.min_by{|node| node.x}.x
  min_y=@graph.nodes.min_by{|node| node.y}.y
  max_x=@graph.nodes.max_by{|node| node.x}.x
  max_y=@graph.nodes.max_by{|node| node.y}.y
  [Vector.new(min_x,min_y),Vector.new(max_x,max_y)]
end

#on_center_clicked(button) ⇒ Object



161
162
163
164
165
166
167
# File 'lib/rtl/viewer.rb', line 161

def on_center_clicked button
  puts 'button "center" clicked'
  if @graph
    compute_shift get_enclosing_rect
    @canvas.redraw @graph,@zoom_factor,@shift
  end
end

#on_fit_clicked(button) ⇒ Object



185
186
187
188
189
190
191
# File 'lib/rtl/viewer.rb', line 185

def on_fit_clicked button
  puts 'button "fit" clicked'
  if @graph
    compute_zoom_and_shift
    @canvas.redraw @graph,@zoom_factor,@shift
  end
end

#on_open_clicked(button) ⇒ Object



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
# File 'lib/rtl/viewer.rb', line 89

def on_open_clicked button
  puts '"open" button was clicked'
  dialog=Gtk::FileChooserDialog.new(
           :title => "choose",
           :parent => self,
           :action => Gtk::FileChooserAction::OPEN,
		     :buttons => [[Gtk::Stock::OPEN, Gtk::ResponseType::ACCEPT],
		                  [Gtk::Stock::CANCEL, Gtk::ResponseType::CANCEL]])
  filter_json = Gtk::FileFilter.new
  filter_json.name = "json filter"
  filter_json.add_pattern("*.json")
  dialog.add_filter(filter_json)

  dialog.show_all

  case dialog.run
  when Gtk::ResponseType::ACCEPT
    puts "filename = #{dialog.filename}"
    #puts "uri = #{dialog.uri}"
    @graph=Graph.read_file dialog.filename
    puts @graph
    @canvas.redraw @graph
    set_title @graph.id
    dialog.destroy
  else
    dialog.destroy
  end
end

#on_quit_clicked(button) ⇒ Object



255
256
257
258
# File 'lib/rtl/viewer.rb', line 255

def on_quit_clicked button
  puts "Closing application"
  Gtk.main_quit
end

#on_random_clicked(button) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/rtl/viewer.rb', line 118

def on_random_clicked button
  puts 'button "random" clicked'
  set_title "random"
  @graph=Graph.random(@nb_nodes || 20)
  @canvas.running=true
  @canvas.redraw @graph,@zoom_factor,@shift
end

#on_run_clicked(button) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/rtl/viewer.rb', line 135

def on_run_clicked button
  puts 'button "run" clicked'
  @canvas.running=true
  @algorithm.stop=false
  @algorithm.graph=@graph
  @algorithm.run(iter=1000){@canvas.redraw @graph,@zoom_factor,@shift}
end

#on_save_clicked(button) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/rtl/viewer.rb', line 226

def on_save_clicked button
  puts 'button "save" clicked'

  dialog=Gtk::FileChooserDialog.new(
           :title => "choose",
           :parent => self,
           :action => Gtk::FileChooserAction::SAVE,
		     :buttons => [[Gtk::Stock::SAVE, Gtk::ResponseType::ACCEPT],
		                  [Gtk::Stock::CANCEL, Gtk::ResponseType::CANCEL]])
  filter_sexp = Gtk::FileFilter.new
  filter_sexp.name = "s-expr filter"
  filter_sexp.add_pattern("*.sexp")
  filter_sexp.add_pattern("*.sxp")
  dialog.add_filter(filter_sexp)

  dialog.show_all

  case dialog.run
  when Gtk::ResponseType::ACCEPT
    puts "filename = #{dialog.filename}"
    #puts "uri = #{dialog.uri}"
    @graph.id=File.basename(dialog.filename,'.sexp')
    @graph.write_file dialog.filename
    dialog.destroy
  else
    dialog.destroy
  end
end

#on_shuffle_clicked(button) ⇒ Object



153
154
155
156
157
158
159
# File 'lib/rtl/viewer.rb', line 153

def on_shuffle_clicked button
  puts 'button "shuffle" clicked'
  if @graph
    @graph.shuffle
    @canvas.redraw @graph,@zoom_factor,@shift
  end
end

#on_spin_changed(spinbutton) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/rtl/viewer.rb', line 126

def on_spin_changed spinbutton
  value=spinbutton.value
  puts "spin button modified #{value}"
  @nb_nodes=value.to_i
  @graph=Graph.random(value.to_i)
  @canvas.running=true
  @canvas.redraw @graph
end

#on_step_clicked(button) ⇒ Object



148
149
150
151
# File 'lib/rtl/viewer.rb', line 148

def on_step_clicked button
  puts 'button "step" clicked'
  @algorithm.run(iter=1){@canvas.redraw @graph,@zoom_factor,@shift}
end

#on_stop_clicked(button) ⇒ Object



143
144
145
146
# File 'lib/rtl/viewer.rb', line 143

def on_stop_clicked button
  puts 'button "stop" clicked'
  @algorithm.stop=true
end

#on_unzoom_clicked(button) ⇒ Object



177
178
179
180
181
182
183
# File 'lib/rtl/viewer.rb', line 177

def on_unzoom_clicked button
  puts 'button "unzoom" clicked'
  if @graph
    @zoom_factor/=1.2
    @canvas.redraw @graph,@zoom_factor,@shift
  end
end

#on_zoom_clicked(button) ⇒ Object



169
170
171
172
173
174
175
# File 'lib/rtl/viewer.rb', line 169

def on_zoom_clicked button
  puts 'button "zoom" clicked'
  if @graph
    @zoom_factor*=1.2
    @canvas.redraw @graph,@zoom_factor,@shift
  end
end

#set_destroy_callbackObject



260
261
262
# File 'lib/rtl/viewer.rb', line 260

def set_destroy_callback
  signal_connect("destroy"){Gtk.main_quit}
end