Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make window state an init option to SubprocessWidget #240

Merged
merged 2 commits into from
Jun 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions qcodes/widgets/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,33 @@ require([
});

me.$el.find('.js-state').click(function() {
var oldState = me.$el.attr('qcodes-state'),
state = this.className.substr(this.className.indexOf('qcodes'))
var state = this.className.substr(this.className.indexOf('qcodes'))
.split('-')[1].split(' ')[0];
me.model.set('_state', state);
});

$(window)
.off('resize.qcodes')
.on('resize.qcodes', function() {me.clipBounds();});

me.update();
},

updateState: function() {
var me = this,
oldState = me.$el.attr('qcodes-state'),
state = me.model.get('_state');

if(state === oldState) return;

setTimeout(function() {
// not sure why I can't pop it out of the widgetarea in render, but it seems that
// some other bit of code resets the parent after render if I do it there.
// To be safe, just do it on every state click.
me.$el.appendTo('body');

if(oldState === 'floated') {
console.log('here');
me.$el.draggable('destroy').css({left:'', top: ''});
}

Expand All @@ -133,15 +150,10 @@ require([
});
}

// any previous highlighting is
// any previous highlighting is now moot
me.$el.removeClass('qcodes-highlight');
});

$(window)
.off('resize.qcodes')
.on('resize.qcodes', function() {me.clipBounds();});
}, 0);

me.update();
},

clipBounds: function() {
Expand All @@ -158,7 +170,6 @@ require([

if(bounds.top > maxTop) me.$el.css('top', maxTop);
else if(bounds.top < 0) me.$el.css('top', 0);
console.log(bounds);
}
},

Expand Down Expand Up @@ -204,20 +215,21 @@ require([
me.outputArea.prop('scrollHeight') : initialScroll);
}

var processes = me.model.get('_processes');
me.abortButton.toggleClass('disabled', processes.indexOf('Measurement')===-1);
me._processes = processes;
me.showSubprocesses();
me.updateState();
},

showSubprocesses: function() {
var me = this,
replacer = me.subprocessesMultiline ? '<br>' : ', ',
processes = (me._processes || '').replace(/\n/g, '&gt;' + replacer + '&lt;');
processes = (me.model.get('_processes') || '')
.replace(/\n/g, '&gt;' + replacer + '&lt;');

if(processes) processes = '&lt;' + processes + '&gt;';
else processes = 'No subprocesses';

me.abortButton.toggleClass('disabled', processes.indexOf('Measurement')===-1);

me.subprocessList.html(processes);
}
});
Expand Down
33 changes: 24 additions & 9 deletions qcodes/widgets/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from IPython.display import display
from ipywidgets import widgets
from multiprocessing import active_children
from traitlets import Unicode, Float
from traitlets import Unicode, Float, Enum

from qcodes.process.stream_queue import get_stream_queue
from .display import display_auto
Expand Down Expand Up @@ -77,6 +77,9 @@ def restart(self, **kwargs):

TODO: why did I include kwargs?
"""
if not hasattr(self, 'previous_interval'):
self.previous_interval = 1

if self.interval != self.previous_interval:
self.interval = self.previous_interval

Expand Down Expand Up @@ -110,17 +113,20 @@ def __init__(self, *args, first_call=False, **kwargs):
super().__init__(*args, first_call=first_call, **kwargs)


def get_subprocess_widget():
def get_subprocess_widget(**kwargs):
"""
Convenience function to get a singleton SubprocessWidget.

Restarts widget updates if it has been halted.

Args:
**kwargs: passed to SubprocessWidget constructor

Returns:
SubprocessWidget
"""
if SubprocessWidget.instance is None:
w = SubprocessWidget()
w = SubprocessWidget(**kwargs)
else:
w = SubprocessWidget.instance

Expand All @@ -129,9 +135,14 @@ def get_subprocess_widget():
return w


def show_subprocess_widget():
"""Display the subprocess widget, creating it if needed."""
display(get_subprocess_widget())
def show_subprocess_widget(**kwargs):
"""
Display the subprocess widget, creating it if needed.

Args:
**kwargs: passed to SubprocessWidget constructor
"""
display(get_subprocess_widget(**kwargs))


class SubprocessWidget(UpdateWidget):
Expand All @@ -149,28 +160,32 @@ class SubprocessWidget(UpdateWidget):
interval (number): The call period, in seconds. Can be changed later
by setting the ``interval`` attribute. ``interval=0`` or the
``halt()`` method disables updates. Default 0.5.
state (str): starting window state of the widget. Options are
'docked' (default), 'minimized', 'floated'
"""

_view_name = Unicode('SubprocessView', sync=True) # see widgets.js
_processes = Unicode(sync=True)
_state = Enum(('minimized', 'docked', 'floated'), sync=True)

instance = None

# max seconds to wait for a measurement to abort
abort_timeout = 30

def __init__(self, interval=0.5):
def __init__(self, interval=0.5, state='docked'):
if self.instance is not None:
raise RuntimeError(
'Only one instance of SubprocessWidget should exist at '
'a time. Use the function get_subprocess_output to find or '
'create it.')

self.__class__.instance = self

self.stream_queue = get_stream_queue()
self._state = state
super().__init__(fn=None, interval=interval)

self.__class__.instance = self

def do_update(self, content=None, buffers=None):
"""
Update the information to be displayed in the widget.
Expand Down