From 1c57abf8abef22a24bc6e88e1b1f56f7f1970cbf Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Sun, 20 May 2012 22:56:11 +0200 Subject: [PATCH] Refactored ScreenList into a true Pythonic singleton, though I sadly had to go with a "create" classmethod in order to retain the functionality of the previous constructor.. --- openlp/core/__init__.py | 2 +- openlp/core/lib/dockwidget.py | 2 +- openlp/core/lib/imagemanager.py | 4 +-- openlp/core/lib/renderer.py | 2 +- openlp/core/ui/generaltab.py | 2 +- openlp/core/ui/maindisplay.py | 2 +- openlp/core/ui/mainwindow.py | 2 +- openlp/core/ui/screen.py | 42 +++++++++++++++++-------------- openlp/core/ui/slidecontroller.py | 2 +- 9 files changed, 32 insertions(+), 28 deletions(-) diff --git a/openlp/core/__init__.py b/openlp/core/__init__.py index 71c27a1d0..d40d7c758 100644 --- a/openlp/core/__init__.py +++ b/openlp/core/__init__.py @@ -109,7 +109,7 @@ class OpenLP(QtGui.QApplication): QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'cursor_normal'), self.setNormalCursor) # Decide how many screens we have and their size - screens = ScreenList(self.desktop()) + screens = ScreenList.create(self.desktop()) # First time checks in settings has_run_wizard = QtCore.QSettings().value( u'general/has run wizard', QtCore.QVariant(False)).toBool() diff --git a/openlp/core/lib/dockwidget.py b/openlp/core/lib/dockwidget.py index e08b5eee5..23ce9efcb 100644 --- a/openlp/core/lib/dockwidget.py +++ b/openlp/core/lib/dockwidget.py @@ -52,7 +52,7 @@ class OpenLPDockWidget(QtGui.QDockWidget): if icon: self.setWindowIcon(build_icon(icon)) # Sort out the minimum width. - screens = ScreenList.get_instance() + screens = ScreenList() mainwindow_docbars = screens.current[u'size'].width() / 5 if mainwindow_docbars > 300: self.setMinimumWidth(300) diff --git a/openlp/core/lib/imagemanager.py b/openlp/core/lib/imagemanager.py index b32e36194..47a7ed3f6 100644 --- a/openlp/core/lib/imagemanager.py +++ b/openlp/core/lib/imagemanager.py @@ -163,7 +163,7 @@ class ImageManager(QtCore.QObject): def __init__(self): QtCore.QObject.__init__(self) - current_screen = ScreenList.get_instance().current + current_screen = ScreenList().current self.width = current_screen[u'size'].width() self.height = current_screen[u'size'].height() self._cache = {} @@ -177,7 +177,7 @@ class ImageManager(QtCore.QObject): Screen has changed size so rebuild the cache to new size. """ log.debug(u'update_display') - current_screen = ScreenList.get_instance().current + current_screen = ScreenList().current self.width = current_screen[u'size'].width() self.height = current_screen[u'size'].height() # Mark the images as dirty for a rebuild by setting the image and byte diff --git a/openlp/core/lib/renderer.py b/openlp/core/lib/renderer.py index 8694ca6b6..aa39e779b 100644 --- a/openlp/core/lib/renderer.py +++ b/openlp/core/lib/renderer.py @@ -69,7 +69,7 @@ class Renderer(object): log.debug(u'Initialisation started') self.themeManager = themeManager self.imageManager = imageManager - self.screens = ScreenList.get_instance() + self.screens = ScreenList() self.service_theme = u'' self.theme_level = u'' self.override_background = None diff --git a/openlp/core/ui/generaltab.py b/openlp/core/ui/generaltab.py index baf28f40f..d0647d829 100644 --- a/openlp/core/ui/generaltab.py +++ b/openlp/core/ui/generaltab.py @@ -42,7 +42,7 @@ class GeneralTab(SettingsTab): """ Initialise the general settings tab """ - self.screens = ScreenList.get_instance() + self.screens = ScreenList() self.iconPath = u':/icon/openlp-logo-16x16.png' generalTranslated = translate('OpenLP.GeneralTab', 'General') SettingsTab.__init__(self, parent, u'General', generalTranslated) diff --git a/openlp/core/ui/maindisplay.py b/openlp/core/ui/maindisplay.py index 15fb9eefe..a04203387 100644 --- a/openlp/core/ui/maindisplay.py +++ b/openlp/core/ui/maindisplay.py @@ -119,7 +119,7 @@ class MainDisplay(Display): def __init__(self, parent, imageManager, live, controller): Display.__init__(self, parent, live, controller) self.imageManager = imageManager - self.screens = ScreenList.get_instance() + self.screens = ScreenList() self.plugins = PluginManager.get_instance().plugins self.rebuildCSS = False self.hideMode = None diff --git a/openlp/core/ui/mainwindow.py b/openlp/core/ui/mainwindow.py index e4a4e1616..bff9203db 100644 --- a/openlp/core/ui/mainwindow.py +++ b/openlp/core/ui/mainwindow.py @@ -795,7 +795,7 @@ class MainWindow(QtGui.QMainWindow, Ui_MainWindow): if answer == QtGui.QMessageBox.No: return Receiver.send_message(u'cursor_busy') - screens = ScreenList.get_instance() + screens = ScreenList() FirstTimeForm(screens, self).exec_() self.firstTime() for plugin in self.pluginManager.plugins: diff --git a/openlp/core/ui/screen.py b/openlp/core/ui/screen.py index 21fbd6144..72c88908e 100644 --- a/openlp/core/ui/screen.py +++ b/openlp/core/ui/screen.py @@ -41,36 +41,40 @@ class ScreenList(object): """ Wrapper to handle the parameters of the display screen. - To get access to the screen list call ``ScreenList.get_instance()``. + To get access to the screen list call ``ScreenList()``. """ log.info(u'Screen loaded') - instance = None + __instance__ = None - @staticmethod - def get_instance(): - return ScreenList.instance + def __new__(cls): + if not cls.__instance__: + cls.__instance__ = object.__new__(cls) + return cls.__instance__ - def __init__(self, desktop): + @classmethod + def create(cls, desktop): """ Initialise the screen list. ``desktop`` A ``QDesktopWidget`` object. """ - ScreenList.instance = self - self.desktop = desktop - self.preview = None - self.current = None - self.override = None - self.screen_list = [] - self.display_count = 0 - self.screen_count_changed() - self._load_screen_settings() + screen_list = cls() + screen_list.desktop = desktop + screen_list.preview = None + screen_list.current = None + screen_list.override = None + screen_list.screen_list = [] + screen_list.display_count = 0 + screen_list.screen_count_changed() + screen_list._load_screen_settings() QtCore.QObject.connect(desktop, - QtCore.SIGNAL(u'resized(int)'), self.screen_resolution_changed) + QtCore.SIGNAL(u'resized(int)'), + screen_list.screen_resolution_changed) QtCore.QObject.connect(desktop, QtCore.SIGNAL(u'screenCountChanged(int)'), - self.screen_count_changed) + screen_list.screen_count_changed) + return screen_list def screen_resolution_changed(self, number): """ @@ -233,8 +237,8 @@ class ScreenList(object): y = window.y() + (window.height() / 2) for screen in self.screen_list: size = screen[u'size'] - if x >= size.x() and x <= (size.x() + size.width()) \ - and y >= size.y() and y <= (size.y() + size.height()): + if x >= size.x() and x <= (size.x() + size.width()) and \ + y >= size.y() and y <= (size.y() + size.height()): return screen[u'number'] def _load_screen_settings(self): diff --git a/openlp/core/ui/slidecontroller.py b/openlp/core/ui/slidecontroller.py index 1dc005aa6..99f2c8ad6 100644 --- a/openlp/core/ui/slidecontroller.py +++ b/openlp/core/ui/slidecontroller.py @@ -83,7 +83,7 @@ class SlideController(Controller): Set up the Slide Controller. """ Controller.__init__(self, parent, isLive) - self.screens = ScreenList.get_instance() + self.screens = ScreenList() try: self.ratio = float(self.screens.current[u'size'].width()) / \ float(self.screens.current[u'size'].height())