From 4d4c0cb56ad8c4845939d113cb2d91824325be03 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Wed, 24 Feb 2016 11:33:49 +0300 Subject: [PATCH] callbacks update - now callbacks can ask window for repaint --- src/callbacks.py | 36 ++++++++++++++++++++++++++++++++---- src/main.py | 2 +- src/util.py | 6 +++++- tests/tests.py | 2 +- 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/callbacks.py b/src/callbacks.py index ae9c172..4f19d12 100644 --- a/src/callbacks.py +++ b/src/callbacks.py @@ -1,11 +1,39 @@ +from PySide import QtCore # TODO: add all callbacks (replace test callbacks and use wrappers) +class InvokeEvent(QtCore.QEvent): + EVENT_TYPE = QtCore.QEvent.Type(QtCore.QEvent.registerEventType()) + + def __init__(self, fn, *args, **kwargs): + QtCore.QEvent.__init__(self, InvokeEvent.EVENT_TYPE) + self.fn = fn + self.args = args + self.kwargs = kwargs + + +class Invoker(QtCore.QObject): + + def event(self, event): + event.fn(*event.args, **event.kwargs) + return True + +_invoker = Invoker() + + +def invoke_in_main_thread(fn, *args, **kwargs): + QtCore.QCoreApplication.postEvent(_invoker, InvokeEvent(fn, *args, **kwargs)) + + +def repaint_widget(widget): + return widget.repaint + + def self_connection_status(st): - def wrapped(a, b, c): - print 'WOW, it works!' - print 'Status: ', str(b) - st.status = b + def wrapped(tox, connection, user_data): + print 'Connection status: ', str(connection) + st.status = connection + invoke_in_main_thread(repaint_widget(st)) return wrapped diff --git a/src/main.py b/src/main.py index b6448bf..7bc9548 100644 --- a/src/main.py +++ b/src/main.py @@ -5,7 +5,6 @@ from profile import Profile, tox_factory import sys from PySide import QtCore, QtGui from callbacks import init_callbacks -from tox import Tox from bootstrap import node_generator @@ -74,6 +73,7 @@ def main(): # bootstrap for data in node_generator(): tox.bootstrap(*data) + # initializing callbacks init_callbacks(tox, ms) # starting thread for tox iterate mainloop = ToxIterateThread(tox) diff --git a/src/util.py b/src/util.py index 8b5928c..0aac000 100644 --- a/src/util.py +++ b/src/util.py @@ -1,4 +1,4 @@ - +import os program_version = '0.0.1 (alpha)' @@ -15,3 +15,7 @@ def string_to_bin(tox_id): def bin_to_string(raw_id): res = ''.join('{:02x}'.format(ord(x)) for x in raw_id) return res.upper() + + +def curr_directory(): + return os.path.dirname(os.path.realpath(__file__)) diff --git a/tests/tests.py b/tests/tests.py index 8c19bcb..c488884 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -59,7 +59,7 @@ class TestNodeGen(): class TestTox(): - def test_creation(self): + def test_loading(self): data = Profile.open_profile(Settings.get_default_path(), 'tox_save') settings = Settings.get_default_settings() tox = tox_factory(data, settings)