From ad69898d53c7ed9ca3acd824bbc0f72b298c66ea Mon Sep 17 00:00:00 2001 From: alexcjohnson Date: Thu, 16 Jun 2016 17:14:25 +0200 Subject: [PATCH 1/2] feat: Make window state an init option to SubprocessWidget We do this by putting _state into the model and providing it as a constructor arg --- qcodes/widgets/widgets.js | 44 +++++++++++++++++++++++++++------------ qcodes/widgets/widgets.py | 33 +++++++++++++++++++++-------- 2 files changed, 55 insertions(+), 22 deletions(-) diff --git a/qcodes/widgets/widgets.js b/qcodes/widgets/widgets.js index 0e99c2169ff..abdd7016a4f 100644 --- a/qcodes/widgets/widgets.js +++ b/qcodes/widgets/widgets.js @@ -109,16 +109,39 @@ 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]; + if(me.model.get('_state') !== state) { + me.model.set('_state', state); + // me.updateState(); + } + + }); + + $(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'); + // me.$el.attr('position', ''); + // console.log(me.el); if(oldState === 'floated') { + console.log('here'); me.$el.draggable('destroy').css({left:'', top: ''}); } @@ -135,13 +158,8 @@ require([ // any previous highlighting is me.$el.removeClass('qcodes-highlight'); - }); + }, 0); - $(window) - .off('resize.qcodes') - .on('resize.qcodes', function() {me.clipBounds();}); - - me.update(); }, clipBounds: function() { @@ -158,7 +176,6 @@ require([ if(bounds.top > maxTop) me.$el.css('top', maxTop); else if(bounds.top < 0) me.$el.css('top', 0); - console.log(bounds); } }, @@ -204,20 +221,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 ? '
' : ', ', - processes = (me._processes || '').replace(/\n/g, '>' + replacer + '<'); + processes = (me.model.get('_processes') || '') + .replace(/\n/g, '>' + replacer + '<'); if(processes) processes = '<' + processes + '>'; else processes = 'No subprocesses'; + me.abortButton.toggleClass('disabled', processes.indexOf('Measurement')===-1); + me.subprocessList.html(processes); } }); diff --git a/qcodes/widgets/widgets.py b/qcodes/widgets/widgets.py index 2f53b974293..56ea36dd45c 100644 --- a/qcodes/widgets/widgets.py +++ b/qcodes/widgets/widgets.py @@ -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 @@ -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 @@ -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 @@ -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): @@ -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. From deab356be126f606f1a245be06ddf3b7a6971cc9 Mon Sep 17 00:00:00 2001 From: alexcjohnson Date: Thu, 16 Jun 2016 18:01:48 +0200 Subject: [PATCH 2/2] fix: Missed a couple of changes should have been in the previous commit --- qcodes/widgets/widgets.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/qcodes/widgets/widgets.js b/qcodes/widgets/widgets.js index abdd7016a4f..c93f0882d38 100644 --- a/qcodes/widgets/widgets.js +++ b/qcodes/widgets/widgets.js @@ -111,11 +111,7 @@ require([ me.$el.find('.js-state').click(function() { var state = this.className.substr(this.className.indexOf('qcodes')) .split('-')[1].split(' ')[0]; - if(me.model.get('_state') !== state) { - me.model.set('_state', state); - // me.updateState(); - } - + me.model.set('_state', state); }); $(window) @@ -137,8 +133,6 @@ require([ // 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'); - // me.$el.attr('position', ''); - // console.log(me.el); if(oldState === 'floated') { console.log('here'); @@ -156,7 +150,7 @@ require([ }); } - // any previous highlighting is + // any previous highlighting is now moot me.$el.removeClass('qcodes-highlight'); }, 0);