Skip to content

Instantly share code, notes, and snippets.

@robmcmullen
Created February 7, 2014 20:24
Show Gist options
  • Save robmcmullen/8871023 to your computer and use it in GitHub Desktop.
Save robmcmullen/8871023 to your computer and use it in GitHub Desktop.
Small example tasks application to manage multiple windows
"""Simple self-contained tasks application
Use Window -> New Window to create new windows. Without a default layout,
File -> Exit will cause the following exception if there are multilpe
windows open:
Traceback (most recent call last):
File "/data/home/rob/src/enthought/pyface/pyface/ui/wx/action/action_item.py", line 260, in _on_menu
self.controller.perform(action, action_event)
File "/data/home/rob/src/enthought/pyface/pyface/tasks/action/task_action_controller.py", line 31, in perform
return action.perform(event)
File "/data/home/rob/src/enthought/pyface/pyface/tasks/action/listening_action.py", line 58, in perform
method()
File "step2a.py", line 85, in exit
self.window.application.exit()
File "/data/home/rob/src/enthought/envisage/envisage/ui/tasks/tasks_application.py", line 252, in exit
self._prepare_exit()
File "/data/home/rob/src/enthought/envisage/envisage/ui/tasks/tasks_application.py", line 308, in _prepare_exit
self._save_state()
File "/data/home/rob/src/enthought/envisage/envisage/ui/tasks/tasks_application.py", line 356, in _save_state
window_layouts = [ w.get_window_layout() for w in self.windows ]
File "/data/home/rob/src/enthought/pyface/pyface/tasks/task_window.py", line 306, in get_window_layout
size_state=self.size_state)
"""
# Enthought library imports.
from envisage.api import ExtensionPoint, Plugin
from envisage.ui.tasks.api import TasksApplication, TaskFactory
from pyface.api import GUI, ConfirmationDialog, FileDialog, \
ImageResource, YES, OK, CANCEL
from pyface.tasks.api import Task, TaskWindow, TaskLayout, TaskWindowLayout, \
PaneItem, IEditor, IEditorAreaPane, EditorAreaPane, Editor
from pyface.tasks.action.api import DockPaneToggleGroup, SMenuBar, \
SMenu, SToolBar, TaskAction
from traits.api import on_trait_change, Property, Instance, List
from envisage.core_plugin import CorePlugin
from envisage.ui.tasks.tasks_plugin import TasksPlugin
class ExampleTask(Task):
""" A simple task for opening a blank editor.
"""
#### Task interface #######################################################
id = 'example.example_task'
name = 'Multi-Tab Editor'
active_editor = Property(Instance(IEditor),
depends_on='editor_area.active_editor')
editor_area = Instance(IEditorAreaPane)
###########################################################################
# 'Task' interface.
###########################################################################
def _menu_bar_default(self):
return SMenuBar(SMenu(TaskAction(name='New', method='new',
accelerator='Ctrl+N'),
TaskAction(name='Exit', method='exit',
accelerator='Ctrl+Q'),
id='File', name='&File'),
SMenu(TaskAction(name='New Window', method='new_window',
accelerator='Ctrl+W'),
id='Window', name='&Window'),
)
def create_central_pane(self):
""" Create the central pane: the script editor.
"""
self.editor_area = EditorAreaPane()
return self.editor_area
###########################################################################
# 'ExampleTask' interface.
###########################################################################
def new(self):
""" Opens a new empty window
"""
editor = Editor()
self.editor_area.add_editor(editor)
self.editor_area.activate_editor(editor)
self.activated()
def new_window(self):
""" Opens a new empty window
"""
print "Opening new window!!!"
window = self.window.application.create_window()
print " window=%s" % str(window)
print " self=%s" % str(self.window)
task = ExampleTask()
window.add_task(task)
window.activate_task(task)
window.open()
print "All windows: %s" % self.window.application.windows
def exit(self):
""" Opens a new empty window
"""
print "Quitting!!!"
self.window.application.exit()
#### Trait property getter/setters ########################################
def _get_active_editor(self):
if self.editor_area is not None:
return self.editor_area.active_editor
return None
class ExamplePlugin(Plugin):
""" The sample framework plugin.
"""
# Extension point IDs.
TASKS = 'envisage.ui.tasks.tasks'
#### 'IPlugin' interface ##################################################
# The plugin's unique identifier.
id = 'example.plugin'
# The plugin's name (suitable for displaying to the user).
name = 'Plugin'
#### Contributions to extension points made by this plugin ################
tasks = List(contributes_to=TASKS)
###########################################################################
# Protected interface.
###########################################################################
def _tasks_default(self):
return [
TaskFactory(id = 'example.example_task',
name = 'ExampleTask',
factory = ExampleTask),
]
class ExampleApplication(TasksApplication):
""" The sample framework Tasks application.
"""
#### 'IApplication' interface #############################################
# The application's globally unique identifier.
id = 'example.application'
# The application's user-visible name.
name = 'Application'
###########################################################################
# Private interface.
###########################################################################
#### Trait initializers ###################################################
def _default_layout_default(self):
tasks = [ factory.id for factory in self.task_factories ]
return [ TaskWindowLayout(*tasks,
size = (800, 600)) ]
def main(argv):
""" A simple example of using Tasks.
"""
plugins = [ CorePlugin(), TasksPlugin(), ExamplePlugin() ]
app = ExampleApplication(plugins=plugins)
app.run()
if __name__ == '__main__':
import sys
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment