Class: QDA::GUI::ScriptWindow

Inherits:
WorkAreaWindow show all
Defined in:
lib/weft/wxgui/inspectors/script.rb

Constant Summary

Constants inherited from WorkAreaWindow

WorkAreaWindow::W_WINDOW_DEF_SIZE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from WorkAreaWindow

#active?, #layout, #layout=, #on_focus

Constructor Details

#initialize(obj, client, workarea, layout) ⇒ ScriptWindow

Returns a new instance of ScriptWindow.



46
47
48
49
50
51
# File 'lib/weft/wxgui/inspectors/script.rb', line 46

def initialize(obj, client, workarea, layout)
  super(workarea, "Script #{obj.dbid}", nil)
  @client = client
  @obj = obj
  self.construct()
end

Instance Attribute Details

#objObject (readonly)

Returns the value of attribute obj.



43
44
45
# File 'lib/weft/wxgui/inspectors/script.rb', line 43

def obj
  @obj
end

Instance Method Details

#constructObject



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
# File 'lib/weft/wxgui/inspectors/script.rb', line 54

def construct()
  @splitter = Wx::SplitterWindow.new(self, -1,
                                     Wx::DEFAULT_POSITION,
                                     Wx::DEFAULT_SIZE,Wx::SP_3DSASH)
  @splitter.minimum_pane_size = 75
  
  # the input pane and buttons
  panel_code = Wx::Panel.new(@splitter)
  code_panel_sizer = Wx::BoxSizer.new(Wx::VERTICAL)
  
  code_sizer = LabelledVSizer.new(panel_code, 'Script')

  @code = CodeTextCtrl.new(panel_code)
  code_sizer.add(@code, 1, Wx::GROW|Wx::ALL, 4)
  
  b_panel = Wx::Panel.new(panel_code)
  b_sizer = Wx::BoxSizer.new(Wx::HORIZONTAL)
  @button_run = Wx::Button.new(b_panel, -1, 'Run')
  @button_run.evt_button(@button_run.get_id) { | e | on_run(e) }
  b_sizer.add(@button_run, 0, Wx::ALL, 4)

  @button_load = Wx::Button.new(b_panel, -1, 'Load from file...')
  @button_load.evt_button(@button_load.get_id) { | e | on_load_script(e) }
  b_sizer.add(@button_load, 0, Wx::ALL|Wx::ALIGN_RIGHT, 4)

  @button_save = Wx::Button.new(b_panel, -1, 'Save to file...')
  @button_save.evt_button(@button_save.get_id) { | e | on_save_script(e) }
  b_sizer.add(@button_save, 0, Wx::ALL|Wx::ALIGN_RIGHT, 4)
  b_panel.set_sizer(b_sizer)

  code_sizer.add(b_panel, 0, Wx::ADJUST_MINSIZE|Wx::ALL)
  code_panel_sizer.add(code_sizer, 1, Wx::ALL|Wx::GROW, 4)
  panel_code.set_sizer(code_panel_sizer)
    

  # the output pane
  panel_outp = Wx::Panel.new(@splitter)
  outp_panel_sizer = Wx::BoxSizer.new(Wx::HORIZONTAL)
  outp_sizer = LabelledVSizer.new(panel_outp, 'Output')
  
  @output  = Wx::TextCtrl.new(panel_outp, -1, '', DEF_POS, DEF_SIZE,
                               Wx::TE_MULTILINE|Wx::TE_READONLY)
  outp_sizer.add(@output, 1, Wx::GROW|Wx::ALL, 4)
  outp_panel_sizer.add(outp_sizer, 1, Wx::GROW|Wx::ALL, 4)
  panel_outp.set_sizer(outp_panel_sizer)
  
  @splitter.split_horizontally( panel_code, panel_outp, 
                                get_client_size.height / 2)
end

#on_load_script(e) ⇒ Object



141
142
143
144
145
# File 'lib/weft/wxgui/inspectors/script.rb', line 141

def on_load_script(e)
  file_dialog = ScriptFileDialog.new(self, false)
  return unless file_dialog.show_modal == Wx::ID_OK
  @code.load_file( file_dialog.get_path() )
end

#on_run(e) ⇒ Object



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
# File 'lib/weft/wxgui/inspectors/script.rb', line 104

def on_run(e)
  $stdout = result = StringIO.new()
  $stderr = errors = StringIO.new()
  output.value = ''
  code.reset() 

  file_id = "Script #{obj.dbid}"
  @button_run.disable()
  @button_load.disable()
  @button_save.disable()
  
  set_cursor( Wx::BUSY_CURSOR )
  client.app.instance_eval( @code.get_value(), file_id)

  result.rewind()
  output.value = result.read()
rescue Exception => err
  script_line = /\A#{file_id}:(\d)/
  trace = err.backtrace
  # remove backtrace info unrelated to the specific script
  trace.pop while trace[-1] !~ script_line
  # find the first backtrace item from within the script
  first_script_err = trace.detect { | x | x =~ script_line }
  # find the script line that caused the problem
  line = first_script_err.match(script_line)[1].to_i - 1
  code.highlight_line(line)
  # spit the error out in the output box
  output.value += "#{err}\n"
  output.value += trace.join("\n")
ensure
  $stdout, $stderr = STDOUT, STDERR
  set_cursor( Wx::NORMAL_CURSOR )
  @button_run.enable()
  @button_load.enable()
  @button_save.enable()
end

#on_save_output(e) ⇒ Object



153
154
155
156
157
# File 'lib/weft/wxgui/inspectors/script.rb', line 153

def on_save_output(e)
  file_dialog = ScriptOutputFileDialog.new(self)
  return unless file_dialog.show_modal == Wx::ID_OK
  @output.save_file( file_dialog.get_path() )
end

#on_save_script(e) ⇒ Object



147
148
149
150
151
# File 'lib/weft/wxgui/inspectors/script.rb', line 147

def on_save_script(e)
  file_dialog = ScriptFileDialog.new(self, true)
  return unless file_dialog.show_modal == Wx::ID_OK
  @code.save_file( file_dialog.get_path() )
end