Skip to content

Commit

Permalink
Merge pull request #489 from mattgibbs/deferred_connections
Browse files Browse the repository at this point in the history
Defer connections until after PyDMMainWindow loads the display.
  • Loading branch information
hhslepicka authored May 24, 2019
2 parents bd5cdda + 80e1194 commit ad54a15
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pydm/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def load_py_file(self, pyfile, args=None, macros=None):
kwargs['macros'] = macros
return cls(**kwargs)

def open_file(self, ui_file, macros=None, command_line_args=None, **kwargs):
def open_file(self, ui_file, macros=None, command_line_args=None, defer_connections=False, **kwargs):
"""
Open a .ui or .py file, and return a widget from the loaded file.
This method is the entry point for all opening of new displays,
Expand Down Expand Up @@ -401,7 +401,7 @@ def open_file(self, ui_file, macros=None, command_line_args=None, **kwargs):
merged_macros = self.macro_stack[-1].copy()
merged_macros.update(macros)
self.macro_stack.append(merged_macros)
with data_plugins.connection_queue():
with data_plugins.connection_queue(defer_connections=defer_connections):
if extension == '.ui':
widget = self.load_ui_file(filepath, merged_macros)
elif extension == '.py':
Expand Down
28 changes: 21 additions & 7 deletions pydm/data_plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import uuid
from collections import deque
from contextlib import contextmanager

from qtpy.QtWidgets import QApplication
from .plugin import PyDMPlugin
from ..utilities import protocol_and_address
from .. import config
Expand All @@ -21,25 +21,39 @@
__read_only = False
global __CONNECTION_QUEUE__
__CONNECTION_QUEUE__ = None
global __DEFER_CONNECTIONS__
__DEFER_CONNECTIONS__ = False

@contextmanager
def connection_queue():
def connection_queue(defer_connections=False):
global __CONNECTION_QUEUE__
global __DEFER_CONNECTIONS__
if __CONNECTION_QUEUE__ is None:
__CONNECTION_QUEUE__ = deque()
__DEFER_CONNECTIONS__ = defer_connections
yield
if __DEFER_CONNECTIONS__:
return
establish_queued_connections()

def establish_queued_connections():
global __DEFER_CONNECTIONS__
global __CONNECTION_QUEUE__
if __CONNECTION_QUEUE__ is None:
return
try:
yield
if __CONNECTION_QUEUE__ is None:
return
while not len(__CONNECTION_QUEUE__) == 0:
channel = __CONNECTION_QUEUE__.pop()
channel = __CONNECTION_QUEUE__.popleft()
establish_connection_immediately(channel)
QApplication.instance().processEvents()
finally:
__CONNECTION_QUEUE__ = None
__DEFER_CONNECTIONS__ = False


def establish_connection(channel):
global __CONNECTION_QUEUE__
if __CONNECTION_QUEUE__:
if __CONNECTION_QUEUE__ is not None:
__CONNECTION_QUEUE__.append(channel)
else:
establish_connection_immediately(channel)
Expand Down
3 changes: 2 additions & 1 deletion pydm/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def open_abs_file(self, filename, macros=None, command_line_args=None):
if command_line_args is None:
command_line_args = []
merged_macros = self.merge_with_current_macros(macros)
widget = self.app.open_file(filename, merged_macros, command_line_args)
widget = self.app.open_file(filename, merged_macros, command_line_args, defer_connections=True)
if (len(self.back_stack) == 0) or (self.current_file() != filename):
self.back_stack.append((filename, merged_macros, command_line_args))
self.set_display_widget(widget)
Expand All @@ -160,6 +160,7 @@ def open_abs_file(self, filename, macros=None, command_line_args=None):
self.ui.actionEdit_in_Designer.setText(edit_in_text)
if self.designer_path:
self.ui.actionEdit_in_Designer.setEnabled(True)
data_plugins.establish_queued_connections()

def new_window(self, ui_file, macros=None, command_line_args=None):
filename = self.join_to_current_file_path(ui_file)
Expand Down
3 changes: 2 additions & 1 deletion pydm/widgets/template_repeater.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def rebuild(self):
QWidget().setLayout(self.layout())
l = layout_class(self)
self.setLayout(l)
with pydm.data_plugins.connection_queue():
with pydm.data_plugins.connection_queue(defer_connections=True):
for i, variables in enumerate(self.data):
if is_qt_designer() and i > self.countShownInDesigner - 1:
break
Expand All @@ -339,6 +339,7 @@ def rebuild(self):
w.setParent(self)
self.layout().addWidget(w)
self.setUpdatesEnabled(True)
pydm.data_plugins.establish_queued_connections()

def clear(self):
""" Clear out any existing instances of the template inside
Expand Down

0 comments on commit ad54a15

Please sign in to comment.