Class: AimsProject::AppController

Inherits:
Wx::App
  • Object
show all
Includes:
Wx
Defined in:
lib/aims_project/app_controller.rb

Constant Summary collapse

ID_NEW =
102
ID_SAVE_IMAGE =
103
ID_MOVE_CLIP_PLANE =
104
ID_SAVE_AS =
105
ID_INSPECTOR =
201
ID_DELETE_ATOM =
301

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#frameObject

The root frame



28
29
30
# File 'lib/aims_project/app_controller.rb', line 28

def frame
  @frame
end

#projectObject

The project



22
23
24
# File 'lib/aims_project/app_controller.rb', line 22

def project
  @project
end

#working_dirObject

Used to synchronize directory in open/save dialogs



25
26
27
# File 'lib/aims_project/app_controller.rb', line 25

def working_dir
  @working_dir
end

Instance Method Details

#error_dialog(exception) ⇒ Object

Display an error dialog for the exception



265
266
267
268
269
270
271
272
273
# File 'lib/aims_project/app_controller.rb', line 265

def error_dialog(exception)
  message = if exception.is_a? String
    exception
  elsif exception.is_a? Exception
    exception.message + "\n\n" + exception.backtrace[0..2].join("\n")
  end
  puts message
  MessageDialog.new(@frame, message, "Error", Wx::ICON_ERROR).show_modal
end

#hide_inspectorObject

Hide the inspector



179
180
181
# File 'lib/aims_project/app_controller.rb', line 179

def hide_inspector
  @inspector.hide
end

#inspectorObject

Get the inspector



166
167
168
169
170
171
# File 'lib/aims_project/app_controller.rb', line 166

def inspector
  if @inspector.nil?
    @inspector = Inspector.new(self, @frame)
  end
  @inspector
end

Return the menubar. If it is undefined, then define it and attach the event handler



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
154
155
156
157
158
159
# File 'lib/aims_project/app_controller.rb', line 127

def menubar
  unless @menubar
    fileMenu = Menu.new
    fileMenu.append(ID_NEW, "New Geometry ...")
    fileMenu.append(Wx::ID_OPEN, "Open ...\tCTRL+o")
    fileMenu.append(Wx::ID_SAVE, "Save Geometry\tCTRL+s")
    fileMenu.append(ID_SAVE_AS, "Save Geometry As...")
    fileMenu.append(ID_SAVE_IMAGE, "Export Image ...")
    fileMenu.append(Wx::ID_EXIT, "Exit")
    
    editMenu = Menu.new
    editMenu.append(ID_DELETE_ATOM, "Delete Atom\tCTRL+d")
    
    toolsMenu = Menu.new       
    # toolsMenu.append(ID_ROTATE, "rotate", "Rotate", Wx::ITEM_CHECK)
    # toolsMenu.append(ID_ZOOM, "zoom", "Zoom", Wx::ITEM_CHECK)
    # toolsMenu.append(ID_PAN, "pan", "Pan", Wx::ITEM_CHECK)
    # toolsMenu.append(ID_MOVE_CLIP_PLANE, "move cilp plane", "Move", Wx::ITEM_CHECK)
    
    viewMenu = Menu.new
    viewMenu.append(ID_INSPECTOR, "inspector\tCTRL+i")
    
    
    @menubar = MenuBar.new
    @menubar.append(fileMenu, "File")
    @menubar.append(editMenu, "Edit")
    @menubar.append(viewMenu, "Views")
    @menubar.append(toolsMenu, "Tools")
    
    evt_menu @menubar, :process_menu_event
  end
  @menubar
end

#new_geometry_file(file = nil) ⇒ Object

Create a new geometry file



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/aims_project/app_controller.rb', line 184

def new_geometry_file(file = nil)
  fd = TextEntryDialog.new(@frame, :message => "New Geometry", :caption => "Specify name of geometry:")
   if Wx::ID_OK == fd.show_modal
     begin
       geom_name = fd.get_value
       geometry = GeometryFile.new("")
       geometry = geometry.save_as(File.new(File.join(GEOMETRY_DIR, geom_name), "w"))
       @geomWindow.add_geometry(geometry)
     rescue Exception => e
       error_dialog(e)
     end
   end
end

#on_initObject

Build the application



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
# File 'lib/aims_project/app_controller.rb', line 31

def on_init
  
     self.app_name = "AimsViewer"
     # Create the frame, toolbar and menubar and define event handlers
     size = [1000,700]
     @frame = Frame.new(nil, -1, "AimsViewer", DEFAULT_POSITION, size)
     @statusbar = @frame.create_status_bar

     # This timer will cause the main thread to pass every 2 ms so that other threads
     # can get work done.
     timer = Wx::Timer.new(self, Wx::ID_ANY)
     evt_timer(timer.id) {Thread.pass}
     timer.start(2)

     # Initialize the selection
     @selection = {}
     
     # Initialize the inspector
     @inspector = Inspector.new(self, @frame)
     
     # Create the notebook
     @notebook = Notebook.new(@frame)
     
     # Create the geometry notebook page
     @geomWindow = GeometryWindow.new(self, @notebook)

     # Create the control window
     # The base window is a horizontal splitter
     # Left side is a list of control files
     # right side is a Rich Text Control
     controlWindow = SplitterWindow.new(@notebook)
     @controlList = ListCtrl.new(controlWindow);
     @controlEditor = RichTextCtrl.new(controlWindow)
     controlWindow.split_horizontally(@controlList, @controlEditor)
     
     # Create the calculations window
     # Similar to the geometryWindow
     # Left side is a list control
     # Right side is a crystal viewer
     calcWindow = CalculationWindow.new(self, @notebook)
     
     # Add windows to the notebook
     @notebook.add_page(@geomWindow, 'Geometry')
     @notebook.add_page(controlWindow, "Control")
     @notebook.add_page(calcWindow, "Calculations")
     
     evt_notebook_page_changed(@notebook) {|evt|
       cp = @notebook.get_current_page
       if cp.respond_to? :show_inspector
         @notebook.get_current_page.show_inspector
       end
     }
     
     # Set the selected notebook page
     @notebook.set_selection(2)
     
     # @tree = ProjectTree.new(self, hsplitter)
     @frame.set_menu_bar(menubar)
     # @toolbar = @frame.create_tool_bar
     # populate_toolbar(@toolbar) if @toolbar
             

     # Check off the current tool
     # set_tool
     
     # Display
     @frame.show        
end

#open_geometry_file(file = nil) ⇒ Object Also known as: open_file

Display a file dialog and attempt to open and display the file



199
200
201
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
227
# File 'lib/aims_project/app_controller.rb', line 199

def open_geometry_file(file = nil)
  begin
    unless file
      fd = FileDialog.new(@frame, :message => "Open", :style => FD_OPEN, :default_dir => @working_dir)
      if ID_OK == fd.show_modal
        file = fd.get_path
        @working_dir = fd.get_directory
      else
        return
      end
    end
    puts "Opening #{file}"

    @frame.set_title(file)
    @geomEditor.show
    @calcTree.hide
    
    if (project)
      erb = ERB.new(File.read(file))
      show_geometry Aims::GeometryParser.parse_string(erb.result(project.get_binding))
    else
      show_geometry Aims::GeometryParser.parse(file)
    end
    
    
  rescue Exception => dang
    error_dialog(dang)
  end
end

#process_menu_event(event) ⇒ Object

Process a menu event



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/aims_project/app_controller.rb', line 101

def process_menu_event(event)
  case event.id
  when ID_INSPECTOR
    show_inspector
  when ID_DELETE_ATOM
    delete_atom
  when Wx::ID_OPEN
    open_file
  when Wx::ID_SAVE
    save_geometry
  when ID_NEW
    new_geometry_file
  when ID_SAVE_AS
    save_geometry_as
  when ID_SAVE_IMAGE
    save_image
  when Wx::ID_EXIT
     exit(0)
  else
    event.skip
  end
  
end

#save_geometryObject



230
231
232
233
234
235
236
237
238
239
240
# File 'lib/aims_project/app_controller.rb', line 230

def save_geometry
  page = @notebook.get_current_page
  if (page.respond_to? :geometry)  && not(page.geometry.nil?)
    geometry = @notebook.get_current_page.geometry
    begin
      geometry.save
    rescue Exception => e
      error_dialog(e)
    end
  end     
end

#save_geometry_asObject

Save the geometry



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/aims_project/app_controller.rb', line 243

def save_geometry_as
  
  page = @notebook.get_current_page
  if (page.respond_to? :geometry)  && not(page.geometry.nil?)
    geometry = @notebook.get_current_page.geometry
    
    fd = TextEntryDialog.new(@frame, :message => "Save Geometry", :caption => "Specify name of geometry:")
    if Wx::ID_OK == fd.show_modal
      begin
        geom_name = fd.get_value
        new_geom  = geometry.save_as(File.new(File.join(GEOMETRY_DIR, geom_name), "w"))
        @geomWindow.add_geometry(new_geom)
      rescue Exception => e
        error_dialog(e)
      end
    end 
  else
    error_dialog("No geometry selected.")
  end
end

#save_imageObject



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/aims_project/app_controller.rb', line 275

def save_image

 begin
   page = @notebook.get_current_page
   if (page.respond_to? :image) 
     image = page.image
     fd = FileDialog.new(@frame, :message => "Save Image", :style => FD_SAVE, :default_dir => @working_dir)
     if Wx::ID_OK == fd.show_modal
       @working_dir = fd.get_directory
        puts "Writing #{fd.get_path}"
        image.mirror(false).save_file(fd.get_path)
      end
   else
     error_dialog("Sorry, could not generate an image.")
   end

  rescue Exception => e
    error_dialog(e)
  end
end

#set_status(string) ⇒ Object



161
162
163
# File 'lib/aims_project/app_controller.rb', line 161

def set_status(string)
  @frame.set_status_text(string)
end

#show_inspectorObject

Show the inspector



174
175
176
# File 'lib/aims_project/app_controller.rb', line 174

def show_inspector
  @inspector.show(true)
end