qt_makepins.py
1 #!/usr/bin/python 2 # -*- encoding: utf-8 -*- 3 # QT_VCP 4 # Copyright 2016 Chris Morley 5 # 6 # This program is free software; you can redistribute it and/or modify 7 # it under the terms of the GNU General Public License as published by 8 # the Free Software Foundation; either version 2 of the License, or 9 # (at your option) any later version. 10 # 11 # This program is distributed in the hope that it will be useful, 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 # GNU General Public License for more details. 15 # 16 # You should have received a copy of the GNU General Public License 17 # along with this program; if not, write to the Free Software 18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 20 21 import gobject 22 from qtvcp.widgets.simple_widgets import _HalWidgetBase 23 from qtvcp.widgets.screen_options import ScreenOptions 24 from PyQt5.QtCore import QObject 25 from PyQt5.QtWidgets import QDesktopWidget 26 27 # Set up logging 28 import logger 29 LOG = logger.getLogger(__name__) 30 # Set the log level for this module 31 LOG.setLevel(logger.INFO) # One of DEBUG, INFO, WARNING, ERROR, CRITICAL 32 33 class QTPanel(): 34 def __init__(self,halcomp,path,window,debug): 35 xmlname = path.XML 36 self.window = window 37 self.window['PREFS_'] = None 38 self._screenOptions = None 39 self._geo_string = '' 40 41 # see if a screenoptions widget is present 42 # if is is then initiate the preference file 43 # and pass a preference object to the window 44 # it's then available to all HALified objects 45 # we must do this first of course 46 for widget in window.findChildren(QObject): 47 if isinstance(widget, _HalWidgetBase): 48 if isinstance(widget, ScreenOptions): 49 self._screenOptions = widget 50 try: 51 self.window['PREFS_'], pref_fn = widget._pref_init(path.CONFIGPATH) 52 except Exception as e: 53 LOG.warning('Preference instance error: {}'.format(e)) 54 self.window['PREFS_'], pref_fn = (None,None) 55 path.PREFS_FILENAME = pref_fn 56 # parse for HAL objects: 57 # initiate the hal function on each 58 # keep a register list of these widgets for later 59 LOG.debug('QTVCP: Parcing for hal widgets') 60 for widget in window.findChildren(QObject): 61 if isinstance(widget, _HalWidgetBase): 62 self.window.registerHalWidget(widget) 63 idname = widget.objectName() 64 LOG.debug('HAL-ified instance found: {}'.format(idname)) 65 widget.hal_init(halcomp, str(idname), widget, window, window.PATHS, self.window['PREFS_']) 66 67 # Search all hal-ifed widgets for closing clean up functions and call them 68 # used for such things as preference recording current settings 69 def shutdown(self): 70 if self.window['PREFS_']: 71 self.record_preference_geometry() 72 LOG.debug('search for widget closing cleanup functions') 73 for widget in self.window.getRegisteredHalWidgetList(): 74 if 'closing_cleanup__' in dir(widget): 75 idname = widget.objectName() 76 LOG.info('Closing cleanup on: {}'.format(idname)) 77 widget.closing_cleanup__() 78 79 # if there is a prefrence file and it is has digits (so no key word), then record 80 # the window geometry 81 def record_preference_geometry(self): 82 temp = self._geo_string.replace(' ','') 83 temp = temp.strip('-') 84 if temp == '' or temp.isdigit(): 85 LOG.debug('Saving Main Window geometry to preference file.') 86 x = self.window.geometry().x() 87 y = self.window.geometry().y() 88 w = self.window.geometry().width() 89 h = self.window.geometry().height() 90 geo = '%s %s %s %s'% (x,y,w,h) 91 self.window['PREFS_'].putpref('mainwindow_geometry', geo, str, 'SCREEN_OPTIONS') 92 93 # if there is a screen option widget and we haven't set INI switch geometry 94 # then call screenoptions function to set preference geometry 95 def set_preference_geometry(self): 96 if self.window['PREFS_']: 97 self.geometry_parsing() 98 else: 99 LOG.info('No preference file - can not set preference geometry.') 100 101 def geometry_parsing(self): 102 def go(x,y,w,h): 103 self.window.setGeometry(x,y,w,h) 104 try: 105 self._geo_string = self.window.PREFS_.getpref('mainwindow_geometry', '', str, 'SCREEN_OPTIONS') 106 LOG.debug('Calculating geometry of main window using natural placement:{}'.format(self._geo_string)) 107 # If there is a preference file object use it to load the geometry 108 if self._geo_string in('default',''): 109 return 110 elif 'center' in self._geo_string.lower(): 111 geom = self.window.frameGeometry() 112 geom.moveCenter(QDesktopWidget().availableGeometry().center()) 113 self.window.setGeometry(geom) 114 return 115 else: 116 temp = self._geo_string.split(' ') 117 go(int(temp[0]), int(temp[1]), int(temp[2]), int(temp[3])) 118 except Exception as e: 119 LOG.error('main window gometry python error: {}'.format(e)) 120 LOG.error('Calculating geometry of main window using natural placement.') 121 x = self.window.geometry().x() 122 y = self.window.geometry().y() 123 w = self.window.geometry().width() 124 h = self.window.geometry().height() 125 go( x,y,w,h) 126 127 if __name__ == "__main__": 128 print "qtvcp_make_pins cannot be run on its own" 129 print "It must be called by qtscreen or a python program" 130 print "that loads and displays the QT panel and creates a HAL component" 131 132 # vim: sts=4 sw=4 et