1
# -*- coding: utf-8 -*-
2
# This file is part of Pate, Kate' Python scripting plugin.
4
# Copyright (C) 2006 Paul Giannaros <paul@giannaros.org>
5
# Copyright (C) 2013 Shaheed Haque <srhaque@theiet.org>
6
# Copyright (C) 2013 Alex Turbov <i.zaufi@gmail.com>
8
# This library is free software; you can redistribute it and/or
9
# modify it under the terms of the GNU Library General Public
10
# License as published by the Free Software Foundation; either
11
# version 2 of the License, or (at your option) version 3.
13
# This library is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
# Library General Public License for more details.
18
# You should have received a copy of the GNU Library General Public License
19
# along with this library; see the file COPYING.LIB. If not, write to
20
# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21
# Boston, MA 02110-1301, USA.
23
'''Shared code for view/document decorators'''
25
from PyQt4 import QtCore
27
from .api import qDebug
28
from .decorators import viewCreated
31
class _SubscriberInfo:
32
# TODO Turn to named tuples?
33
def __init__(self, signal, receiver):
35
self.receiver = receiver
38
def _subscribe_for_events(subscribedPlugins, source):
39
for plugin, subscribers in subscribedPlugins.items():
40
assert(isinstance(subscribers, list))
41
for subscriber in subscribers:
42
qDebug('Subscribe {}/{} to receive {}'.format(
44
, subscriber.receiver.__name__
47
source.connect(source, QtCore.SIGNAL(subscriber.signal), subscriber.receiver)
50
def _make_sure_subscribers_queue_exists(plugin, dst, queue):
51
if not hasattr(dst, queue):
52
setattr(dst, queue, dict())
53
if not plugin in getattr(dst, queue):
54
getattr(dst, queue)[plugin] = list()
58
def _on_view_created(view):
59
assert(view is not None)
61
# Subscribe registered handlers for view events
62
if hasattr(_on_view_created, 'view_event_subscribers'):
63
assert(isinstance(_on_view_created.view_event_subscribers, dict))
64
_subscribe_for_events(_on_view_created.view_event_subscribers, view)
66
# Subscribe registered handlers for document events
67
if hasattr(_on_view_created, 'document_event_subscribers'):
68
assert(isinstance(_on_view_created.document_event_subscribers, dict))
69
_subscribe_for_events(_on_view_created.document_event_subscribers, view.document())