From 557114fd7fd66bcc2b2b912c3afebdaeafa2cc2b Mon Sep 17 00:00:00 2001 From: Tim Bentley Date: Sun, 8 Feb 2009 15:25:00 +0000 Subject: [PATCH] Add distributed event handling Added display updates when Bibles load Try to fix the creeping progress bar. bzr-revno: 299 --- openlp/core/lib/__init__.py | 3 +- openlp/core/lib/eventreceiver.py | 60 +++++++++++++++++++ openlp/core/lib/mediamanageritem.py | 2 +- openlp/core/lib/plugin.py | 6 -- openlp/core/pluginmanager.py | 5 -- openlp/core/ui/__init__.py | 1 + openlp/core/ui/mainwindow.py | 17 +++--- openlp/plugins/bibles/bibleplugin.py | 12 ++-- .../plugins/bibles/forms/bibleimportform.py | 29 +++++---- openlp/plugins/bibles/lib/bibleDBimpl.py | 2 +- openlp/plugins/bibles/lib/bibleOSISimpl.py | 13 +++- 11 files changed, 111 insertions(+), 39 deletions(-) create mode 100644 openlp/core/lib/eventreceiver.py diff --git a/openlp/core/lib/__init__.py b/openlp/core/lib/__init__.py index cd3c727ca..a5c83d45c 100644 --- a/openlp/core/lib/__init__.py +++ b/openlp/core/lib/__init__.py @@ -25,6 +25,7 @@ from mediamanageritem import MediaManagerItem from event import Event from xmlrootclass import XmlRootClass from serviceitem import ServiceItem +from eventreceiver import Receiver __all__ = ['PluginConfig', 'Plugin', 'PluginUtils', 'SettingsTab', 'MediaManagerItem', 'Event', - 'XmlRootClass', 'ServiceItem'] + 'XmlRootClass', 'ServiceItem', "Receiver"] diff --git a/openlp/core/lib/eventreceiver.py b/openlp/core/lib/eventreceiver.py new file mode 100644 index 000000000..99d325a44 --- /dev/null +++ b/openlp/core/lib/eventreceiver.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- +""" +OpenLP - Open Source Lyrics Projection +Copyright (c) 2008 Raoul Snyman +Portions copyright (c) 2008 Martin Thompson, Tim Bentley, + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; version 2 of the License. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +this program; if not, write to the Free Software Foundation, Inc., 59 Temple +Place, Suite 330, Boston, MA 02111-1307 USA +""" + +from PyQt4.QtCore import * + +class EventReceiver(QObject): + """ + Class to allow events to be passed from different parts of the system. + This is a private class and should not be used directly but via the Receiver class + """ + def __init__(self): + QObject.__init__(self) + + def send_repaint(self): + self.emit(SIGNAL("openlprepaint"), None) + + def received(self, msg=None): + print msg + +class Receiver(): + """ + Class to allow events to be passed from different parts of the system. + This is a static wrapper arounf the EventReceiver class. + As there is only one instance of it in the systems the QT signal/slot architecture + can send messages across the system + Send message + receiver = Receiver() + receiver.send_repaint() + + Receive Message + self.receiver = Receiver() + QtCore.QObject.connect(self.receiver.get_receiver(),QtCore.SIGNAL("openlprepaint"),<>) + """ + eventreceiver=EventReceiver() + @staticmethod + def send_repaint(): + Receiver.eventreceiver.send_repaint() + @staticmethod + def receive(): + Receiver.eventreceiver.receive() + @staticmethod + def get_receiver(): + return Receiver.eventreceiver + diff --git a/openlp/core/lib/mediamanageritem.py b/openlp/core/lib/mediamanageritem.py index 49504cd07..a9785cb77 100644 --- a/openlp/core/lib/mediamanageritem.py +++ b/openlp/core/lib/mediamanageritem.py @@ -42,7 +42,7 @@ class MediaManagerItem(QtGui.QWidget): self.title = title self.Toolbar = None #self.ToolbarButtons = [] - + def addToolbar(self): """ A method to help developers easily add a toolbar to the media manager diff --git a/openlp/core/lib/plugin.py b/openlp/core/lib/plugin.py index a8c03916f..0acb1bc4a 100644 --- a/openlp/core/lib/plugin.py +++ b/openlp/core/lib/plugin.py @@ -116,12 +116,6 @@ class Plugin(object): """ pass - def add_repaint_main_window(self, repaint_main_window): - """ - Create ability to repaint main window. - """ - self.repaint_main_window = repaint_main_window - def get_settings_tab(self): """ Create a menu item and add it to the "Import" menu. diff --git a/openlp/core/pluginmanager.py b/openlp/core/pluginmanager.py index efe74978d..da2a0e618 100644 --- a/openlp/core/pluginmanager.py +++ b/openlp/core/pluginmanager.py @@ -114,8 +114,3 @@ class PluginManager(object): def hook_handle_event(self, event): pass - - def hook_repaint_main_window(self, repaint_main_window): - for plugin in self.plugins: - plugin.add_repaint_main_window(repaint_main_window) - diff --git a/openlp/core/ui/__init__.py b/openlp/core/ui/__init__.py index 34d10026c..5f4c2a850 100644 --- a/openlp/core/ui/__init__.py +++ b/openlp/core/ui/__init__.py @@ -24,4 +24,5 @@ from alertform import AlertForm from settings import SettingsDialog from mainwindow import MainWindow + __all__ = ['SplashScreen', 'AboutForm', 'AlertForm', 'SettingsDialog', 'MainWindow'] diff --git a/openlp/core/ui/mainwindow.py b/openlp/core/ui/mainwindow.py index ad939137b..12c232043 100644 --- a/openlp/core/ui/mainwindow.py +++ b/openlp/core/ui/mainwindow.py @@ -18,12 +18,14 @@ this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """ import os + from time import sleep +from PyQt4 import * from PyQt4 import QtCore, QtGui from openlp.core.resources import * from openlp.core.ui import AboutForm, AlertForm, SettingsDialog -from openlp.core.lib import Plugin, MediaManagerItem, SettingsTab +from openlp.core.lib import Plugin, MediaManagerItem, SettingsTab, Receiver from openlp.core import PluginManager class MainWindow(object): @@ -37,10 +39,12 @@ class MainWindow(object): pluginpath = os.path.abspath(os.path.join(pluginpath, '..', '..','plugins')) self.plugin_manager = PluginManager(pluginpath) self.setupUi() - - def repaint_window(self): - self.main_window.update() - + self.receiver = Receiver() + QtCore.QObject.connect(self.receiver.get_receiver(),QtCore.SIGNAL("openlprepaint"),self.repaint) + + def repaint(self): + self.main_window.repaint() + def setupUi(self): self.main_window.setObjectName("main_window") self.main_window.resize(1087, 847) @@ -122,8 +126,6 @@ class MainWindow(object): self.FileExportMenu.setObjectName("FileExportMenu") # Call the hook method to pull in export menus. self.plugin_manager.hook_import_menu(self.FileExportMenu) - # Call the hook method to export refresh. - self.plugin_manager.hook_repaint_main_window(self.repaint_window) # self.OptionsMenu = QtGui.QMenu(self.MenuBar) self.OptionsMenu.setObjectName("OptionsMenu") @@ -405,6 +407,7 @@ class MainWindow(object): QtCore.QObject.connect(self.ToolsAlertItem, QtCore.SIGNAL("triggered()"), self.onToolsAlertItemClicked) QtCore.QObject.connect(self.OptionsSettingsItem, QtCore.SIGNAL("triggered()"), self.onOptionsSettingsItemClicked) + def retranslateUi(self): self.main_window.setWindowTitle(QtGui.QApplication.translate("main_window", "openlp.org 2.0", None, QtGui.QApplication.UnicodeUTF8)) self.FileMenu.setTitle(QtGui.QApplication.translate("main_window", "&File", None, QtGui.QApplication.UnicodeUTF8)) diff --git a/openlp/plugins/bibles/bibleplugin.py b/openlp/plugins/bibles/bibleplugin.py index 985ec8475..7a372981f 100644 --- a/openlp/plugins/bibles/bibleplugin.py +++ b/openlp/plugins/bibles/bibleplugin.py @@ -25,7 +25,7 @@ from PyQt4.QtCore import * from PyQt4.QtGui import * from openlp.core.resources import * -from openlp.core.lib import Plugin,PluginUtils, MediaManagerItem +from openlp.core.lib import Plugin,PluginUtils, MediaManagerItem, Receiver from openlp.plugins.bibles.lib import BibleManager from openlp.plugins.bibles.forms import BibleImportForm @@ -48,7 +48,12 @@ class BiblePlugin(Plugin, PluginUtils): #Register the bible Manager self.biblemanager = BibleManager(self.config) self.searchresults = {} # place to store the search results - + self.receiver = Receiver() + QtCore.QObject.connect(self.receiver.get_receiver(),QtCore.SIGNAL("openlprepaint"),self.repaint) + + def repaint(self): + self.MediaManagerItem.repaint() + def get_media_manager_item(self): # Create the MediaManagerItem object self.MediaManagerItem = MediaManagerItem(self.icon, 'Bible Verses') @@ -251,9 +256,6 @@ class BiblePlugin(Plugin, PluginUtils): def onBibleAddClick(self): pass - def refresh(self): - self.repaint_main_window() - def reload_bibles(self): self.biblemanager.reload_bibles() self._initialise_form() diff --git a/openlp/plugins/bibles/forms/bibleimportform.py b/openlp/plugins/bibles/forms/bibleimportform.py index f9c19048c..a4ff9b27c 100644 --- a/openlp/plugins/bibles/forms/bibleimportform.py +++ b/openlp/plugins/bibles/forms/bibleimportform.py @@ -16,7 +16,8 @@ from PyQt4.QtGui import QDialog from PyQt4.QtCore import pyqtSignature from bibleimportdialog import Ui_BibleImportDialog -from openlp.core.lib import PluginUtils +from openlp.core.lib import PluginUtils, Receiver + class BibleImportForm(QDialog, Ui_BibleImportDialog, PluginUtils): global log @@ -36,11 +37,17 @@ class BibleImportForm(QDialog, Ui_BibleImportDialog, PluginUtils): self.bibleplugin = bibleplugin self.bibletype = None self.barmax = 0 + self.receiver = Receiver() QtCore.QObject.connect(self.LocationComboBox, QtCore.SIGNAL("activated(int)"), self.onLocationComboBox) QtCore.QObject.connect(self.TypeComboBox, QtCore.SIGNAL("activated(int)"), self.onTypeComboBox) QtCore.QObject.connect(self.BibleComboBox, QtCore.SIGNAL("activated(int)"), self.onBibleComboBox) + QtCore.QObject.connect(self.ProgressBar, QtCore.SIGNAL("valueChanged(int)"), self.on_ProgressBar_changed) + QtCore.QObject.connect(self.receiver.get_receiver(),QtCore.SIGNAL("openlprepaint"),self.on_ProgressBar_changed) + def on_ProgressBar_changed(self): + self.repaint() + @pyqtSignature("") def on_VersesFileButton_clicked(self): filename = QtGui.QFileDialog.getOpenFileName(self, 'Open file',self._get_last_dir()) @@ -121,6 +128,7 @@ class BibleImportForm(QDialog, Ui_BibleImportDialog, PluginUtils): if self.biblemanager != None: if not self.bibletype == None or len(self.BibleNameEdit.displayText()) > 0: self.MessageLabel.setText("Import Started") + self.ProgressBar.setMinimum(0) self.ProgressBar.setValue(0) self.progress = 0 self.biblemanager.process_dialog(self) @@ -132,21 +140,20 @@ class BibleImportForm(QDialog, Ui_BibleImportDialog, PluginUtils): self.close() def setMax(self, max): + log.debug("set Max %s", max) self.barmax = max self.ProgressBar.setMaximum(max) - def incrementBar(self, text = None): - print self.progress, text, self.barmax - if text != None: - self.MessageLabel.setText("Import processed " + text) - else: - self.MessageLabel.setText("Import progressing") + def incrementBar(self, text ): + log.debug("IncrementBar %s", text) + self.MessageLabel.setText("Import processing " + text) self.progress +=1 - self.ProgressBar.setValue(self.progress) - #self.update() - self.bibleplugin.refresh() - + self.ProgressBar.setValue(self.progress) + print self.ProgressBar.value() + print text + " " + str(self.progress) + def _import_bible(self): + log.debug("Import Bible ") if self.bibletype == "OSIS": self.biblemanager.register_osis_file_bible(str(self.BibleNameEdit.displayText()), self.OSISLocationEdit.displayText()) elif self.bibletype == "CSV": diff --git a/openlp/plugins/bibles/lib/bibleDBimpl.py b/openlp/plugins/bibles/lib/bibleDBimpl.py index df530fbb3..71300effd 100644 --- a/openlp/plugins/bibles/lib/bibleDBimpl.py +++ b/openlp/plugins/bibles/lib/bibleDBimpl.py @@ -62,7 +62,7 @@ class BibleDBImpl(BibleCommon): self._load_testament("Apocrypha") def add_verse(self, bookid, chap, vse, text): - log.debug( "add_verse %s,%s,%s", bookid, chap, vse) + #log.debug( "add_verse %s,%s,%s", bookid, chap, vse) metadata.bind.echo = False session = self.session() verse = Verse() diff --git a/openlp/plugins/bibles/lib/bibleOSISimpl.py b/openlp/plugins/bibles/lib/bibleOSISimpl.py index e529dc08e..360a8e9fe 100644 --- a/openlp/plugins/bibles/lib/bibleOSISimpl.py +++ b/openlp/plugins/bibles/lib/bibleOSISimpl.py @@ -17,6 +17,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA """ import logging from openlp.plugins.bibles.lib.bibleDBimpl import BibleDBImpl +from openlp.core.lib import Receiver class BibleOSISImpl(): global log @@ -27,6 +28,7 @@ class BibleOSISImpl(): self.booksOfBible = {} # books of the bible linked to bibleid {osis , name} self.abbrevOfBible = {} # books of the bible linked to bibleid {osis ,Abbrev } fbibles=open(biblepath+"/osisbooks_en.txt", 'r') + self.receiver = Receiver() for line in fbibles: p = line.split(",") self.booksOfBible[p[0]] = p[1].replace('\n', '') @@ -38,6 +40,7 @@ class BibleOSISImpl(): book_ptr = None id = 0 + count = 0 verseText = "