Skip to content

Commit

Permalink
use signals in Leo core
Browse files Browse the repository at this point in the history
Leo build: 20170327105418
  • Loading branch information
tbnorth committed Mar 27, 2017
1 parent 904614c commit d9dcffe
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 30 deletions.
4 changes: 2 additions & 2 deletions leo/core/commit_timestamp.json
@@ -1,4 +1,4 @@
{
"asctime": "Mon, Mar 27, 2017 10:08:36 AM",
"timestamp": "20170327100836"
"asctime": "Mon, Mar 27, 2017 10:54:18 AM",
"timestamp": "20170327105418"
}
38 changes: 14 additions & 24 deletions leo/core/editpane/editpane.py
@@ -1,20 +1,21 @@
def DBG(text):
"""DBG - temporary debugging function
:param str text: text to print
"""
print("LEP: %s" % text)

import imp
import os

import leo.core.leoGlobals as g
from leo.core.leoNodes import vnode
from leo.core.leoQt import QtCore, QtGui, QtWidgets, QtConst

if g.isPython3:
from importlib import import_module

import signal_manager as sig
import leo.core.signal_manager as sig

def DBG(text):
"""DBG - temporary debugging function
:param str text: text to print
"""
print("LEP: %s" % text)

class LeoEditPane(QtWidgets.QWidget):
"""
Expand All @@ -30,9 +31,6 @@ def __init__(self, c=None, p=None, mode='edit', show_head=True, show_control=Fal
super(LeoEditPane, self).__init__(*args, **kwargs)

self.c = c
if not hasattr(self.c, '_LEPs'):
self.c._LEPs = []
self.c._LEPs.append(self)
p = p or self.c.p
self.mode = mode

Expand Down Expand Up @@ -60,7 +58,7 @@ def __init__(self, c=None, p=None, mode='edit', show_head=True, show_control=Fal
self.handlers = [
('select1', self._before_select),
('select2', self._after_select),
('bodykey2', self._after_body_key),
# ('bodykey2', self._after_body_key),
]
self._register_handlers()

Expand Down Expand Up @@ -122,7 +120,9 @@ def _after_body_key(self, tag, keywords={}):
if c != self.c:
return None
p = keywords['p']
else: # signal
elif isinstance(tag, vnode): # signal with vnode
p = self.c.vnode2position(tag)
else: # signal with position
if sig.is_locked(self):
return
p = tag
Expand Down Expand Up @@ -285,7 +285,7 @@ def close(self):
"""
do_close = QtWidgets.QWidget.close(self)
if do_close:
self.c._LEPs.remove(self)
sig.disconnect_all(self)
DBG("unregister handlers\n")
for hook, handler in self.handlers:
g.unregisterHandler(hook, handler)
Expand Down Expand Up @@ -415,16 +415,6 @@ def text_changed(self, new_text):
p.b = new_text

sig.emit(self.c, 'body_changed', p, _sig_lock=self)

for lep in self.c._LEPs:
break
if lep == self:
if self.update and self.mode != 'edit':
# don't update the edit part, could be infinite loop
self.update_position_view(p)
else:
lep.update_position(lep.get_position())

def update_position(self, p):
"""update_position - update editor and view for current Leo position
Expand Down
4 changes: 0 additions & 4 deletions leo/core/editpane/plaintextedit.py
Expand Up @@ -31,10 +31,6 @@ def focusInEvent (self, event):
def focusOutEvent (self, event):
QtWidgets.QTextEdit.focusOutEvent(self, event)
DBG("focusout()")
#X p = self.lep.get_position()
#X p.b = self.toPlainText()
#X self.lep.c.redraw()

def new_position(self, p):
"""new_position - update for new position
Expand Down
2 changes: 2 additions & 0 deletions leo/core/leoNodes.py
Expand Up @@ -5,6 +5,7 @@
#@+<< imports >>
#@+node:ekr.20060904165452.1: ** << imports >> (leoNodes)
import leo.core.leoGlobals as g
import leo.core.signal_manager as sig
# if g.app and g.app.use_psyco:
# # g.pr("enabled psyco classes",__file__)
# try: from psyco.classes import *
Expand Down Expand Up @@ -2500,6 +2501,7 @@ def setBodyString(self, s):
# if not g.isUnicode(s):
# g.trace('converting to unicode', type(s), repr(s), g.callers(10))
v._bodyString = g.toUnicode(s, reportErrors=True)
sig.emit(self.context, 'body_changed', self)

def setHeadString(self, s):
# fn = self.context.shortFileName()
Expand Down
14 changes: 14 additions & 0 deletions leo/core/editpane/signal_manager.py → leo/core/signal_manager.py
Expand Up @@ -12,6 +12,7 @@
class SignalData:
def __init__(self):
self.listeners = defaultdict(lambda: list())
self.emitters = []
self.locked = False


Expand Down Expand Up @@ -49,6 +50,19 @@ def connect(source, signal_name, listener):
_setup(source)
source._signal_data.listeners[signal_name].append(listener)

if hasattr(listener, '__self__'):
obj = listener.__self__
_setup(obj)
obj._signal_data.emitters.append(source)

def disconnect_all(listener):
"""Disconnect from all signals"""
for emitter in listener._signal_data.emitters:
for signal in emitter._signal_data.listeners:
emitter._signal_data.listeners[signal] = [
i for i in emitter._signal_data.listeners[signal]
if getattr(i, '__self__', None) != listener
]
def is_locked(obj):
return hasattr(obj, '_signal_data') and obj._signal_data.locked

Expand Down

0 comments on commit d9dcffe

Please sign in to comment.