1 # Copyright (C) 2008-2010 Fabrizio Benedetti
2 # Massimo Sandal <devicerandom@gmail.com>
3 # Rolf Schmidt <rschmidt@alcor.concordia.ca>
4 # W. Trevor King <wking@drexel.edu>
6 # This file is part of Hooke.
8 # Hooke is free software: you can redistribute it and/or modify it
9 # under the terms of the GNU Lesser General Public License as
10 # published by the Free Software Foundation, either version 3 of the
11 # License, or (at your option) any later version.
13 # Hooke is distributed in the hope that it will be useful, but WITHOUT
14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
16 # Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with Hooke. If not, see
20 # <http://www.gnu.org/licenses/>.
22 """Defines :class:`GUI` providing a wxWidgets interface to Hooke.
29 wxversion.select(WX_GOOD)
41 import wx.lib.evtmgr as evtmgr
42 # wxPropertyGrid is included in wxPython >= 2.9.1, see
43 # http://wxpropgrid.sourceforge.net/cgi-bin/index?page=download
44 # until then, we'll avoid it because of the *nix build problems.
45 #import wx.propgrid as wxpg
47 from ...command import CommandExit, Exit, Success, Failure, Command, Argument
48 from ...config import Setting
49 from ...engine import CommandMessage
50 from ...interaction import Request, BooleanRequest, ReloadUserInterfaceConfig
51 from ...ui import UserInterface
52 from .dialog.selection import Selection as SelectionDialog
53 from .dialog.save_file import select_save_file
54 from . import menu as menu
55 from . import navbar as navbar
56 from . import panel as panel
57 from .panel.propertyeditor import props_from_argument, props_from_setting
58 from . import statusbar as statusbar
61 class HookeFrame (wx.Frame):
62 """The main Hooke-interface window.
64 def __init__(self, gui, commands, inqueue, outqueue, *args, **kwargs):
65 super(HookeFrame, self).__init__(*args, **kwargs)
66 self.log = logging.getLogger('hooke')
68 self.commands = commands
69 self.inqueue = inqueue
70 self.outqueue = outqueue
71 self._perspectives = {} # {name: perspective_str}
74 self.SetIcon(wx.Icon(self.gui.config['icon image'], wx.BITMAP_TYPE_ICO))
77 self._c['manager'] = aui.AuiManager()
78 self._c['manager'].SetManagedWindow(self)
80 # set the gradient and drag styles
81 self._c['manager'].GetArtProvider().SetMetric(
82 aui.AUI_DOCKART_GRADIENT_TYPE, aui.AUI_GRADIENT_NONE)
83 self._c['manager'].SetFlags(
84 self._c['manager'].GetFlags() ^ aui.AUI_MGR_TRANSPARENT_DRAG)
86 # Min size for the frame itself isn't completely done. See
87 # the end of FrameManager::Update() for the test code. For
88 # now, just hard code a frame minimum size.
89 #self.SetMinSize(wx.Size(500, 500))
92 self._setup_toolbars()
93 self._c['manager'].Update() # commit pending changes
95 # Create the menubar after the panes so that the default
96 # perspective is created with all panes open
97 panels = [p for p in self._c.values() if isinstance(p, panel.Panel)]
98 self._c['menu bar'] = menu.HookeMenuBar(
102 'close': self._on_close,
103 'about': self._on_about,
104 'view_panel': self._on_panel_visibility,
105 'save_perspective': self._on_save_perspective,
106 'delete_perspective': self._on_delete_perspective,
107 'select_perspective': self._on_select_perspective,
109 self.SetMenuBar(self._c['menu bar'])
111 self._c['status bar'] = statusbar.StatusBar(
113 style=wx.ST_SIZEGRIP)
114 self.SetStatusBar(self._c['status bar'])
116 self._setup_perspectives()
118 return # TODO: cleanup
119 self._displayed_plot = None
120 #load default list, if possible
121 self.do_loadlist(self.GetStringFromConfig('core', 'preferences', 'playlists'))
126 def _setup_panels(self):
127 client_size = self.GetClientSize()
129 # ('folders', wx.GenericDirCtrl(
131 # dir=self.gui.config['folders-workdir'],
133 # style=wx.DIRCTRL_SHOW_FILTERS,
134 # filter=self.gui.config['folders-filters'],
135 # defaultFilter=self.gui.config['folders-filter-index']), 'left'),
136 (panel.PANELS['playlist'](
138 'delete_playlist':self._on_user_delete_playlist,
139 '_delete_playlist':self._on_delete_playlist,
140 'delete_curve':self._on_user_delete_curve,
141 '_delete_curve':self._on_delete_curve,
142 '_on_set_selected_playlist':self._on_set_selected_playlist,
143 '_on_set_selected_curve':self._on_set_selected_curve,
146 style=wx.WANTS_CHARS|wx.NO_BORDER,
147 # WANTS_CHARS so the panel doesn't eat the Return key.
150 (panel.PANELS['note'](
152 '_on_update':self._on_update_note,
155 style=wx.WANTS_CHARS|wx.NO_BORDER,
158 # ('notebook', Notebook(
160 # pos=wx.Point(client_size.x, client_size.y),
161 # size=wx.Size(430, 200),
162 # style=aui.AUI_NB_DEFAULT_STYLE
163 # | aui.AUI_NB_TAB_EXTERNAL_MOVE | wx.NO_BORDER), 'center'),
164 (panel.PANELS['commands'](
165 commands=self.commands,
166 selected=self.gui.config['selected command'],
168 'execute': self.execute_command,
169 'select_plugin': self.select_plugin,
170 'select_command': self.select_command,
171 # 'selection_changed': self.panelProperties.select(self, method, command), #SelectedTreeItem = selected_item,
174 style=wx.WANTS_CHARS|wx.NO_BORDER,
175 # WANTS_CHARS so the panel doesn't eat the Return key.
178 (panel.PANELS['propertyeditor'](
181 style=wx.WANTS_CHARS,
182 # WANTS_CHARS so the panel doesn't eat the Return key.
184 (panel.PANELS['plot'](
186 '_set_status_text': self._on_plot_status_text,
189 style=wx.WANTS_CHARS|wx.NO_BORDER,
190 # WANTS_CHARS so the panel doesn't eat the Return key.
193 (panel.PANELS['output'](
196 size=wx.Size(150, 90),
197 style=wx.TE_READONLY|wx.NO_BORDER|wx.TE_MULTILINE),
199 # ('results', panel.results.Results(self), 'bottom'),
201 self._add_panel(p, style)
202 self.execute_command( # setup already loaded playlists
203 command=self._command_by_name('playlists'))
204 self.execute_command( # setup already loaded curve
205 command=self._command_by_name('get curve'))
207 def _add_panel(self, panel, style):
208 self._c[panel.name] = panel
209 m_name = panel.managed_name
210 info = aui.AuiPaneInfo().Name(m_name).Caption(m_name)
211 info.PaneBorder(False).CloseButton(True).MaximizeButton(False)
214 elif style == 'center':
216 elif style == 'left':
218 elif style == 'right':
221 assert style == 'bottom', style
223 self._c['manager'].AddPane(panel, info)
225 def _setup_toolbars(self):
226 self._c['navigation bar'] = navbar.NavBar(
228 'next': self._next_curve,
229 'previous': self._previous_curve,
232 style=wx.TB_FLAT | wx.TB_NODIVIDER)
233 self._c['manager'].AddPane(
234 self._c['navigation bar'],
235 aui.AuiPaneInfo().Name('Navigation').Caption('Navigation'
236 ).ToolbarPane().Top().Layer(1).Row(1).LeftDockable(False
237 ).RightDockable(False))
239 def _bind_events(self):
240 # TODO: figure out if we can use the eventManager for menu
241 # ranges and events of 'self' without raising an assertion
243 self.Bind(wx.EVT_ERASE_BACKGROUND, self._on_erase_background)
244 self.Bind(wx.EVT_SIZE, self._on_size)
245 self.Bind(wx.EVT_CLOSE, self._on_close)
246 self.Bind(aui.EVT_AUI_PANE_CLOSE, self.OnPaneClose)
247 self.Bind(aui.EVT_AUINOTEBOOK_PAGE_CLOSE, self._on_notebook_page_close)
249 return # TODO: cleanup
250 treeCtrl = self._c['folders'].GetTreeCtrl()
251 treeCtrl.Bind(wx.EVT_LEFT_DCLICK, self._on_dir_ctrl_left_double_click)
254 self.panelProperties.pg.Bind(wxpg.EVT_PG_CHANGED, self.OnPropGridChanged)
256 self.panelResults.results_list.OnCheckItem = self.OnResultsCheck
258 def _on_about(self, *args):
259 dialog = wx.MessageDialog(
261 message=self.gui._splash_text(extra_info={
262 'get-details':'click "Help -> License"'},
264 caption='About Hooke',
265 style=wx.OK|wx.ICON_INFORMATION)
269 def _on_close(self, *args):
270 self.log.info('closing GUI framework')
272 self._set_config('main height', self.GetSize().GetHeight())
273 self._set_config('main left', self.GetPosition()[0])
274 self._set_config('main top', self.GetPosition()[1])
275 self._set_config('main width', self.GetSize().GetWidth())
276 self._c['manager'].UnInit()
277 del self._c['manager']
282 # Panel utility functions
284 def _file_name(self, name):
285 """Cleanup names according to configured preferences.
287 if self.gui.config['hide extensions'] == True:
288 name,ext = os.path.splitext(name)
295 def _command_by_name(self, name):
296 cs = [c for c in self.commands if c.name == name]
300 raise Exception('Multiple commands named "%s"' % name)
303 def execute_command(self, _class=None, method=None,
304 command=None, args=None):
307 if ('property editor' in self._c
308 and self.gui.config['selected command'] == command.name):
309 for name,value in self._c['property editor'].get_values().items():
310 arg = self._c['property editor']._argument_from_label.get(
315 args[arg.name] = value
317 # deal with counted arguments
318 if arg.name not in args:
320 index = int(name[len(arg.name):])
321 args[arg.name][index] = value
322 for arg in command.arguments:
323 if arg.name not in args:
324 continue # undisplayed argument, e.g. 'driver' types.
326 if hasattr(arg, '_display_count'): # support HACK in props_from_argument()
327 count = arg._display_count
328 if count != 1 and arg.name in args:
329 keys = sorted(args[arg.name].keys())
330 assert keys == range(count), keys
331 args[arg.name] = [args[arg.name][i]
332 for i in range(count)]
334 while (len(args[arg.name]) > 0
335 and args[arg.name][-1] == None):
337 if len(args[arg.name]) == 0:
338 args[arg.name] = arg.default
339 cm = CommandMessage(command.name, args)
340 self.gui._submit_command(cm, self.inqueue)
341 return self._handle_response(command_message=cm)
343 def _handle_response(self, command_message):
346 msg = self.outqueue.get()
348 if isinstance(msg, Exit):
351 elif isinstance(msg, CommandExit):
352 # TODO: display command complete
354 elif isinstance(msg, ReloadUserInterfaceConfig):
355 self.gui.reload_config(msg.config)
357 elif isinstance(msg, Request):
358 h = handler.HANDLERS[msg.type]
359 h.run(self, msg) # TODO: pause for response?
362 self, '_postprocess_%s' % command_message.command.replace(' ', '_'),
363 self._postprocess_text)
364 pp(command=command_message.command,
365 args=command_message.arguments,
369 def _handle_request(self, msg):
370 """Repeatedly try to get a response to `msg`.
373 raise NotImplementedError('_%s_request_prompt' % msg.type)
374 prompt_string = prompt(msg)
375 parser = getattr(self, '_%s_request_parser' % msg.type, None)
377 raise NotImplementedError('_%s_request_parser' % msg.type)
381 self.cmd.stdout.write(''.join([
382 error.__class__.__name__, ': ', str(error), '\n']))
383 self.cmd.stdout.write(prompt_string)
384 value = parser(msg, self.cmd.stdin.readline())
386 response = msg.response(value)
388 except ValueError, error:
390 self.inqueue.put(response)
392 def _set_config(self, option, value, section=None):
393 self.gui._set_config(section=section, option=option, value=value,
394 ui_to_command_queue=self.inqueue,
395 response_handler=self._handle_response)
398 # Command-specific postprocessing
400 def _postprocess_text(self, command, args={}, results=[]):
401 """Print the string representation of the results to the Results window.
403 This is similar to :class:`~hooke.ui.commandline.DoCommand`'s
404 approach, except that :class:`~hooke.ui.commandline.DoCommand`
405 doesn't print some internally handled messages
406 (e.g. :class:`~hooke.interaction.ReloadUserInterfaceConfig`).
408 for result in results:
409 if isinstance(result, CommandExit):
410 self._c['output'].write(result.__class__.__name__+'\n')
411 self._c['output'].write(str(result).rstrip()+'\n')
413 def _postprocess_playlists(self, command, args={}, results=None):
414 """Update `self` to show the playlists.
416 if not isinstance(results[-1], Success):
417 self._postprocess_text(command, results=results)
419 assert len(results) == 2, results
420 playlists = results[0]
421 if 'playlist' in self._c:
422 for playlist in playlists:
423 if self._c['playlist'].is_playlist_loaded(playlist):
424 self._c['playlist'].update_playlist(playlist)
426 self._c['playlist'].add_playlist(playlist)
428 def _postprocess_new_playlist(self, command, args={}, results=None):
429 """Update `self` to show the new playlist.
431 if not isinstance(results[-1], Success):
432 self._postprocess_text(command, results=results)
434 assert len(results) == 2, results
435 playlist = results[0]
436 if 'playlist' in self._c:
437 loaded = self._c['playlist'].is_playlist_loaded(playlist)
438 assert loaded == False, loaded
439 self._c['playlist'].add_playlist(playlist)
441 def _postprocess_load_playlist(self, command, args={}, results=None):
442 """Update `self` to show the playlist.
444 if not isinstance(results[-1], Success):
445 self._postprocess_text(command, results=results)
447 assert len(results) == 2, results
448 playlist = results[0]
449 self._c['playlist'].add_playlist(playlist)
451 def _postprocess_get_playlist(self, command, args={}, results=[]):
452 if not isinstance(results[-1], Success):
453 self._postprocess_text(command, results=results)
455 assert len(results) == 2, results
456 playlist = results[0]
457 if 'playlist' in self._c:
458 loaded = self._c['playlist'].is_playlist_loaded(playlist)
459 assert loaded == True, loaded
460 self._c['playlist'].update_playlist(playlist)
462 def _postprocess_get_curve(self, command, args={}, results=[]):
463 """Update `self` to show the curve.
465 if not isinstance(results[-1], Success):
466 self._postprocess_text(command, results=results)
468 assert len(results) == 2, results
470 if args.get('curve', None) == None:
471 # the command defaults to the current curve of the current playlist
472 results = self.execute_command(
473 command=self._command_by_name('get playlist'))
474 playlist = results[0]
476 raise NotImplementedError()
477 if 'note' in self._c:
478 self._c['note'].set_text(curve.info.get('note', ''))
479 if 'playlist' in self._c:
480 self._c['playlist'].set_selected_curve(
482 if 'plot' in self._c:
483 self._c['plot'].set_curve(curve, config=self.gui.config)
485 def _postprocess_next_curve(self, command, args={}, results=[]):
486 """No-op. Only call 'next curve' via `self._next_curve()`.
490 def _postprocess_previous_curve(self, command, args={}, results=[]):
491 """No-op. Only call 'previous curve' via `self._previous_curve()`.
495 def _postprocess_glob_curves_to_playlist(
496 self, command, args={}, results=[]):
497 """Update `self` to show new curves.
499 if not isinstance(results[-1], Success):
500 self._postprocess_text(command, results=results)
502 if 'playlist' in self._c:
503 if args.get('playlist', None) != None:
504 playlist = args['playlist']
505 pname = playlist.name
506 loaded = self._c['playlist'].is_playlist_name_loaded(pname)
507 assert loaded == True, loaded
508 for curve in results[:-1]:
509 self._c['playlist']._add_curve(pname, curve)
511 self.execute_command(
512 command=self._command_by_name('get playlist'))
514 def _postprocess_zero_block_surface_contact_point(
515 self, command, args={}, results=[]):
516 """Update the curve, since the available columns may have changed.
518 if isinstance(results[-1], Success):
519 self.execute_command(
520 command=self._command_by_name('get curve'))
522 def _postprocess_add_block_force_array(
523 self, command, args={}, results=[]):
524 """Update the curve, since the available columns may have changed.
526 if isinstance(results[-1], Success):
527 self.execute_command(
528 command=self._command_by_name('get curve'))
534 def _GetActiveFileIndex(self):
535 lib.playlist.Playlist = self.GetActivePlaylist()
536 #get the selected item from the tree
537 selected_item = self._c['playlist']._c['tree'].GetSelection()
538 #test if a playlist or a curve was double-clicked
539 if self._c['playlist']._c['tree'].ItemHasChildren(selected_item):
543 selected_item = self._c['playlist']._c['tree'].GetPrevSibling(selected_item)
544 while selected_item.IsOk():
546 selected_item = self._c['playlist']._c['tree'].GetPrevSibling(selected_item)
549 def _GetPlaylistTab(self, name):
550 for index, page in enumerate(self._c['notebook']._tabs._pages):
551 if page.caption == name:
555 def select_plugin(self, _class=None, method=None, plugin=None):
558 def AddPlaylistFromFiles(self, files=[], name='Untitled'):
560 playlist = lib.playlist.Playlist(self, self.drivers)
562 playlist.add_curve(item)
563 if playlist.count > 0:
564 playlist.name = self._GetUniquePlaylistName(name)
566 self.AddTayliss(playlist)
568 def AppliesPlotmanipulator(self, name):
570 Returns True if the plotmanipulator 'name' is applied, False otherwise
571 name does not contain 'plotmanip_', just the name of the plotmanipulator (e.g. 'flatten')
573 return self.GetBoolFromConfig('core', 'plotmanipulators', name)
575 def ApplyPlotmanipulators(self, plot, plot_file):
577 Apply all active plotmanipulators.
579 if plot is not None and plot_file is not None:
580 manipulated_plot = copy.deepcopy(plot)
581 for plotmanipulator in self.plotmanipulators:
582 if self.GetBoolFromConfig('core', 'plotmanipulators', plotmanipulator.name):
583 manipulated_plot = plotmanipulator.method(manipulated_plot, plot_file)
584 return manipulated_plot
586 def GetActiveFigure(self):
587 playlist_name = self.GetActivePlaylistName()
588 figure = self.playlists[playlist_name].figure
589 if figure is not None:
593 def GetActiveFile(self):
594 playlist = self.GetActivePlaylist()
595 if playlist is not None:
596 return playlist.get_active_file()
599 def GetActivePlot(self):
600 playlist = self.GetActivePlaylist()
601 if playlist is not None:
602 return playlist.get_active_file().plot
605 def GetDisplayedPlot(self):
606 plot = copy.deepcopy(self.displayed_plot)
608 #plot.curves = copy.deepcopy(plot.curves)
611 def GetDisplayedPlotCorrected(self):
612 plot = copy.deepcopy(self.displayed_plot)
614 plot.curves = copy.deepcopy(plot.corrected_curves)
617 def GetDisplayedPlotRaw(self):
618 plot = copy.deepcopy(self.displayed_plot)
620 plot.curves = copy.deepcopy(plot.raw_curves)
623 def GetDockArt(self):
624 return self._c['manager'].GetArtProvider()
626 def GetPlotmanipulator(self, name):
628 Returns a plot manipulator function from its name
630 for plotmanipulator in self.plotmanipulators:
631 if plotmanipulator.name == name:
632 return plotmanipulator
635 def HasPlotmanipulator(self, name):
637 returns True if the plotmanipulator 'name' is loaded, False otherwise
639 for plotmanipulator in self.plotmanipulators:
640 if plotmanipulator.command == name:
645 def _on_dir_ctrl_left_double_click(self, event):
646 file_path = self.panelFolders.GetPath()
647 if os.path.isfile(file_path):
648 if file_path.endswith('.hkp'):
649 self.do_loadlist(file_path)
652 def _on_erase_background(self, event):
655 def _on_notebook_page_close(self, event):
656 ctrl = event.GetEventObject()
657 playlist_name = ctrl.GetPageText(ctrl._curpage)
658 self.DeleteFromPlaylists(playlist_name)
660 def OnPaneClose(self, event):
663 def OnPropGridChanged (self, event):
664 prop = event.GetProperty()
666 item_section = self.panelProperties.SelectedTreeItem
667 item_plugin = self._c['commands']._c['tree'].GetItemParent(item_section)
668 plugin = self._c['commands']._c['tree'].GetItemText(item_plugin)
669 config = self.gui.config[plugin]
670 property_section = self._c['commands']._c['tree'].GetItemText(item_section)
671 property_key = prop.GetName()
672 property_value = prop.GetDisplayedString()
674 config[property_section][property_key]['value'] = property_value
676 def OnResultsCheck(self, index, flag):
677 results = self.GetActivePlot().results
678 if results.has_key(self.results_str):
679 results[self.results_str].results[index].visible = flag
680 results[self.results_str].update()
684 def _on_size(self, event):
687 def UpdatePlaylistsTreeSelection(self):
688 playlist = self.GetActivePlaylist()
689 if playlist is not None:
690 if playlist.index >= 0:
691 self._c['status bar'].set_playlist(playlist)
695 def _on_curve_select(self, playlist, curve):
696 #create the plot tab and add playlist to the dictionary
697 plotPanel = panel.plot.PlotPanel(self, ID_FirstPlot + len(self.playlists))
698 notebook_tab = self._c['notebook'].AddPage(plotPanel, playlist.name, True)
699 #tab_index = self._c['notebook'].GetSelection()
700 playlist.figure = plotPanel.get_figure()
701 self.playlists[playlist.name] = playlist
702 #self.playlists[playlist.name] = [playlist, figure]
703 self._c['status bar'].set_playlist(playlist)
708 def _on_playlist_left_doubleclick(self):
709 index = self._c['notebook'].GetSelection()
710 current_playlist = self._c['notebook'].GetPageText(index)
711 if current_playlist != playlist_name:
712 index = self._GetPlaylistTab(playlist_name)
713 self._c['notebook'].SetSelection(index)
714 self._c['status bar'].set_playlist(playlist)
718 def _on_playlist_delete(self, playlist):
719 notebook = self.Parent.plotNotebook
720 index = self.Parent._GetPlaylistTab(playlist.name)
721 notebook.SetSelection(index)
722 notebook.DeletePage(notebook.GetSelection())
723 self.Parent.DeleteFromPlaylists(playlist_name)
727 # Command panel interface
729 def select_command(self, _class, method, command):
730 #self.select_plugin(plugin=command.plugin)
731 self._c['property editor'].clear()
732 self._c['property editor']._argument_from_label = {}
733 for argument in command.arguments:
734 if argument.name == 'help':
737 results = self.execute_command(
738 command=self._command_by_name('playlists'))
739 if not isinstance(results[-1], Success):
740 self._postprocess_text(command, results=results)
743 playlists = results[0]
745 results = self.execute_command(
746 command=self._command_by_name('playlist curves'))
747 if not isinstance(results[-1], Success):
748 self._postprocess_text(command, results=results)
753 ret = props_from_argument(
754 argument, curves=curves, playlists=playlists)
756 continue # property intentionally not handled (yet)
758 self._c['property editor'].append_property(p)
759 self._c['property editor']._argument_from_label[label] = (
762 self._set_config('selected command', command.name)
766 # Note panel interface
768 def _on_update_note(self, _class, method, text):
769 """Sets the note for the active curve.
771 self.execute_command(
772 command=self._command_by_name('set note'),
777 # Playlist panel interface
779 def _on_user_delete_playlist(self, _class, method, playlist):
782 def _on_delete_playlist(self, _class, method, playlist):
783 if hasattr(playlist, 'path') and playlist.path != None:
784 os.remove(playlist.path)
786 def _on_user_delete_curve(self, _class, method, playlist, curve):
789 def _on_delete_curve(self, _class, method, playlist, curve):
790 # TODO: execute_command 'remove curve from playlist'
791 os.remove(curve.path)
793 def _on_set_selected_playlist(self, _class, method, playlist):
794 """Call the `jump to playlist` command.
796 results = self.execute_command(
797 command=self._command_by_name('playlists'))
798 if not isinstance(results[-1], Success):
800 assert len(results) == 2, results
801 playlists = results[0]
802 matching = [p for p in playlists if p.name == playlist.name]
803 assert len(matching) == 1, matching
804 index = playlists.index(matching[0])
805 results = self.execute_command(
806 command=self._command_by_name('jump to playlist'),
807 args={'index':index})
809 def _on_set_selected_curve(self, _class, method, playlist, curve):
810 """Call the `jump to curve` command.
812 self._on_set_selected_playlist(_class, method, playlist)
813 index = playlist.index(curve)
814 results = self.execute_command(
815 command=self._command_by_name('jump to curve'),
816 args={'index':index})
817 if not isinstance(results[-1], Success):
819 #results = self.execute_command(
820 # command=self._command_by_name('get playlist'))
821 #if not isinstance(results[-1], Success):
823 self.execute_command(
824 command=self._command_by_name('get curve'))
828 # Plot panel interface
830 def _on_plot_status_text(self, _class, method, text):
831 if 'status bar' in self._c:
832 self._c['status bar'].set_plot_text(text)
838 def _next_curve(self, *args):
839 """Call the `next curve` command.
841 results = self.execute_command(
842 command=self._command_by_name('next curve'))
843 if isinstance(results[-1], Success):
844 self.execute_command(
845 command=self._command_by_name('get curve'))
847 def _previous_curve(self, *args):
848 """Call the `previous curve` command.
850 results = self.execute_command(
851 command=self._command_by_name('previous curve'))
852 if isinstance(results[-1], Success):
853 self.execute_command(
854 command=self._command_by_name('get curve'))
858 # Panel display handling
860 def _on_panel_visibility(self, _class, method, panel_name, visible):
861 pane = self._c['manager'].GetPane(panel_name)
863 #if we don't do the following, the Folders pane does not resize properly on hide/show
864 if pane.caption == 'Folders' and pane.IsShown() and pane.IsDocked():
865 #folders_size = pane.GetSize()
866 self.panelFolders.Fit()
867 self._c['manager'].Update()
869 def _setup_perspectives(self):
870 """Add perspectives to menubar and _perspectives.
872 self._perspectives = {
873 'Default': self._c['manager'].SavePerspective(),
875 path = self.gui.config['perspective path']
876 if os.path.isdir(path):
877 files = sorted(os.listdir(path))
879 name, extension = os.path.splitext(fname)
880 if extension != self.gui.config['perspective extension']:
882 fpath = os.path.join(path, fname)
883 if not os.path.isfile(fpath):
886 with open(fpath, 'rU') as f:
887 perspective = f.readline()
889 self._perspectives[name] = perspective
891 selected_perspective = self.gui.config['active perspective']
892 if not self._perspectives.has_key(selected_perspective):
893 self._set_config('active perspective', 'Default')
895 self._restore_perspective(selected_perspective, force=True)
896 self._update_perspective_menu()
898 def _update_perspective_menu(self):
899 self._c['menu bar']._c['perspective'].update(
900 sorted(self._perspectives.keys()),
901 self.gui.config['active perspective'])
903 def _save_perspective(self, perspective, perspective_dir, name,
905 path = os.path.join(perspective_dir, name)
906 if extension != None:
908 if not os.path.isdir(perspective_dir):
909 os.makedirs(perspective_dir)
910 with open(path, 'w') as f:
912 self._perspectives[name] = perspective
913 self._restore_perspective(name)
914 self._update_perspective_menu()
916 def _delete_perspectives(self, perspective_dir, names,
918 self.log.debug('remove perspectives %s from %s'
919 % (names, perspective_dir))
921 path = os.path.join(perspective_dir, name)
922 if extension != None:
925 del(self._perspectives[name])
926 self._update_perspective_menu()
927 if self.gui.config['active perspective'] in names:
928 self._restore_perspective('Default')
929 # TODO: does this bug still apply?
930 # Unfortunately, there is a bug in wxWidgets for win32 (Ticket #3258
931 # http://trac.wxwidgets.org/ticket/3258
932 # ) that makes the radio item indicator in the menu disappear.
933 # The code should be fine once this issue is fixed.
935 def _restore_perspective(self, name, force=False):
936 if name != self.gui.config['active perspective'] or force == True:
937 self.log.debug('restore perspective %s' % name)
938 self._set_config('active perspective', name)
939 self._c['manager'].LoadPerspective(self._perspectives[name])
940 self._c['manager'].Update()
941 for pane in self._c['manager'].GetAllPanes():
942 view = self._c['menu bar']._c['view']
943 if pane.name in view._c.keys():
944 view._c[pane.name].Check(pane.window.IsShown())
946 def _on_save_perspective(self, *args):
947 perspective = self._c['manager'].SavePerspective()
948 name = self.gui.config['active perspective']
949 if name == 'Default':
950 name = 'New perspective'
951 name = select_save_file(
952 directory=self.gui.config['perspective path'],
954 extension=self.gui.config['perspective extension'],
956 message='Enter a name for the new perspective:',
957 caption='Save perspective')
960 self._save_perspective(
961 perspective, self.gui.config['perspective path'], name=name,
962 extension=self.gui.config['perspective extension'])
964 def _on_delete_perspective(self, *args, **kwargs):
965 options = sorted([p for p in self._perspectives.keys()
967 dialog = SelectionDialog(
969 message="\nPlease check the perspectives\n\nyou want to delete and click 'Delete'.\n",
970 button_id=wx.ID_DELETE,
971 selection_style='multiple',
973 title='Delete perspective(s)',
974 style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
975 dialog.CenterOnScreen()
977 if dialog.canceled == True:
979 names = [options[i] for i in dialog.selected]
981 self._delete_perspectives(
982 self.gui.config['perspective path'], names=names,
983 extension=self.gui.config['perspective extension'])
985 def _on_select_perspective(self, _class, method, name):
986 self._restore_perspective(name)
990 class HookeApp (wx.App):
991 """A :class:`wx.App` wrapper around :class:`HookeFrame`.
993 Tosses up a splash screen and then loads :class:`HookeFrame` in
996 def __init__(self, gui, commands, inqueue, outqueue, *args, **kwargs):
998 self.commands = commands
999 self.inqueue = inqueue
1000 self.outqueue = outqueue
1001 super(HookeApp, self).__init__(*args, **kwargs)
1004 self.SetAppName('Hooke')
1005 self.SetVendorName('')
1006 self._setup_splash_screen()
1008 height = self.gui.config['main height']
1009 width = self.gui.config['main width']
1010 top = self.gui.config['main top']
1011 left = self.gui.config['main left']
1013 # Sometimes, the ini file gets confused and sets 'left' and
1014 # 'top' to large negative numbers. Here we catch and fix
1015 # this. Keep small negative numbers, the user might want
1023 'frame': HookeFrame(
1024 self.gui, self.commands, self.inqueue, self.outqueue,
1025 parent=None, title='Hooke',
1026 pos=(left, top), size=(width, height),
1027 style=wx.DEFAULT_FRAME_STYLE|wx.SUNKEN_BORDER|wx.CLIP_CHILDREN),
1029 self._c['frame'].Show(True)
1030 self.SetTopWindow(self._c['frame'])
1033 def _setup_splash_screen(self):
1034 if self.gui.config['show splash screen'] == True:
1035 path = self.gui.config['splash screen image']
1036 if os.path.isfile(path):
1037 duration = self.gui.config['splash screen duration']
1039 bitmap=wx.Image(path).ConvertToBitmap(),
1040 splashStyle=wx.SPLASH_CENTRE_ON_SCREEN|wx.SPLASH_TIMEOUT,
1041 milliseconds=duration,
1044 # For some reason splashDuration and sleep do not
1045 # correspond to each other at least not on Windows.
1046 # Maybe it's because duration is in milliseconds and
1047 # sleep in seconds. Thus we need to increase the
1048 # sleep time a bit. A factor of 1.2 seems to work.
1050 time.sleep(sleepFactor * duration / 1000)
1053 class GUI (UserInterface):
1054 """wxWindows graphical user interface.
1057 super(GUI, self).__init__(name='gui')
1059 def default_settings(self):
1060 """Return a list of :class:`hooke.config.Setting`\s for any
1061 configurable UI settings.
1063 The suggested section setting is::
1065 Setting(section=self.setting_section, help=self.__doc__)
1068 Setting(section=self.setting_section, help=self.__doc__),
1069 Setting(section=self.setting_section, option='icon image',
1070 value=os.path.join('doc', 'img', 'microscope.ico'),
1072 help='Path to the hooke icon image.'),
1073 Setting(section=self.setting_section, option='show splash screen',
1074 value=True, type='bool',
1075 help='Enable/disable the splash screen'),
1076 Setting(section=self.setting_section, option='splash screen image',
1077 value=os.path.join('doc', 'img', 'hooke.jpg'),
1079 help='Path to the Hooke splash screen image.'),
1080 Setting(section=self.setting_section,
1081 option='splash screen duration',
1082 value=1000, type='int',
1083 help='Duration of the splash screen in milliseconds.'),
1084 Setting(section=self.setting_section, option='perspective path',
1085 value=os.path.join('resources', 'gui', 'perspective'),
1086 help='Directory containing perspective files.'), # TODO: allow colon separated list, like $PATH.
1087 Setting(section=self.setting_section, option='perspective extension',
1089 help='Extension for perspective files.'),
1090 Setting(section=self.setting_section, option='hide extensions',
1091 value=False, type='bool',
1092 help='Hide file extensions when displaying names.'),
1093 Setting(section=self.setting_section, option='plot legend',
1094 value=True, type='bool',
1095 help='Enable/disable the plot legend.'),
1096 Setting(section=self.setting_section, option='plot SI format',
1097 value='True', type='bool',
1098 help='Enable/disable SI plot axes numbering.'),
1099 Setting(section=self.setting_section, option='plot decimals',
1100 value=2, type='int',
1101 help='Number of decimal places to show if "plot SI format" is enabled.'),
1102 Setting(section=self.setting_section, option='folders-workdir',
1103 value='.', type='path',
1104 help='This should probably go...'),
1105 Setting(section=self.setting_section, option='folders-filters',
1106 value='.', type='path',
1107 help='This should probably go...'),
1108 Setting(section=self.setting_section, option='active perspective',
1110 help='Name of active perspective file (or "Default").'),
1111 Setting(section=self.setting_section,
1112 option='folders-filter-index',
1113 value=0, type='int',
1114 help='This should probably go...'),
1115 Setting(section=self.setting_section, option='main height',
1116 value=450, type='int',
1117 help='Height of main window in pixels.'),
1118 Setting(section=self.setting_section, option='main width',
1119 value=800, type='int',
1120 help='Width of main window in pixels.'),
1121 Setting(section=self.setting_section, option='main top',
1122 value=0, type='int',
1123 help='Pixels from screen top to top of main window.'),
1124 Setting(section=self.setting_section, option='main left',
1125 value=0, type='int',
1126 help='Pixels from screen left to left of main window.'),
1127 Setting(section=self.setting_section, option='selected command',
1128 value='load playlist',
1129 help='Name of the initially selected command.'),
1132 def _app(self, commands, ui_to_command_queue, command_to_ui_queue):
1136 app = HookeApp(gui=self,
1138 inqueue=ui_to_command_queue,
1139 outqueue=command_to_ui_queue,
1143 def run(self, commands, ui_to_command_queue, command_to_ui_queue):
1144 app = self._app(commands, ui_to_command_queue, command_to_ui_queue)