Skip to content

Commit

Permalink
Merge pull request #644 from astrofrog/menubar-plugins
Browse files Browse the repository at this point in the history
Added the ability to register menubar plugins
  • Loading branch information
astrofrog committed May 28, 2015
2 parents 632f8bc + ce33e39 commit 9029c13
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
v0.5 (Unreleased)
-----------------

* Added a ``menu_plugin`` registry to add custom tools to the registry
* Support for 'lazy-loading' plugins which means their import is deferred until they are needed
* Support for connecting custom importers
* ``qglue`` now correctly interprets HDUList objects
Expand Down
21 changes: 21 additions & 0 deletions doc/python_guide/customization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,27 @@ An importer can be defined using the ``@importer`` decorator::
The label in the ``@importer`` decorator is the text that will appear in the
``Import`` menu in Glue.

Custom menubar tools
--------------------

In some cases, it might be desirable to add tools to Glue that can operate on
any aspects of the data or subsets, and can be accessed from the menubar. To
do this, you can define a function that takes two arguments (the session
object, and the data collection object), and decorate it with the
``@menubar_plugin`` decorator, giving it the label that will appear in the
**Tools** menubar::

from glue.config import menubar_plugin

@menubar_plugin("Do something")
def my_plugin(session, data_collection):
# do anything here
return

The function can do anything, such as launch a QWidget, or anything else
(such as a web browser, etc.), and does not need to return anything (instead
it can operate by directly modifying the data collection or subsets).

Custom Colormaps
----------------

Expand Down
31 changes: 31 additions & 0 deletions glue/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,36 @@ def adder(func):
return adder


class MenubarPluginRegistry(Registry):
"""
Stores menubar plugins.
The members property is a list of menubar plugins, each represented as a
``(label, function)`` tuple. The ``function`` should take two items which
are a reference to the session and to the data collection respectively.
"""

def default_members(self):
return []

def add(self, label, function):
"""
Add a new menubar plugin
:param label: Short label for the plugin
:type label: str
:param function: function
:type function: function()
"""
self.members.append((label, function))

def __call__(self, label):
def adder(func):
self.add(label, func)
return func
return adder


class ExporterRegistry(Registry):

"""Stores functions which can export an applocation to an output file
Expand Down Expand Up @@ -444,6 +474,7 @@ def __call__(self, state=None):
settings = SettingRegistry()
fit_plugin = ProfileFitterRegistry()
single_subset_action = SingleSubsetLayerActionRegistry()
menubar_plugin = MenubarPluginRegistry()

# watch loaded data files for changes?
auto_refresh = BooleanSetting(False)
Expand Down
20 changes: 20 additions & 0 deletions glue/qt/glue_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,15 @@ def _create_menu(self):
menu.addActions(tbar.actions())
mbar.addMenu(menu)

menu = QMenu(mbar)
menu.setTitle("&Tools")

if 'plugins' in self._actions:
for plugin in self._actions['plugins']:
menu.addAction(plugin)

mbar.addMenu(menu)

# trigger inclusion of Mac Native "Help" tool
menu = mbar.addMenu("&Help")
a = QAction("&Online Documentation", menu)
Expand Down Expand Up @@ -591,6 +600,17 @@ def _create_actions(self):
a.setEnabled(False)
self._actions['redo'] = a

# Create actions for menubar plugins
from glue.config import menubar_plugin
acts = []
for label, function in menubar_plugin:
a = act(label, self, tip=label)
a.triggered.connect(nonpartial(function,
self.session,
self.data_collection))
acts.append(a)
self._actions['plugins'] = acts

def choose_new_data_viewer(self, data=None):
""" Create a new visualization window in the current tab
"""
Expand Down

0 comments on commit 9029c13

Please sign in to comment.