File indexing completed on 2024-04-14 03:59:08

0001 """
0002 Authors of original libkmahjongg in C++:
0003     Copyright (C) 1997 Mathias Mueller <in5y158@public.uni-hamburg.de>
0004     Copyright (C) 2006 Mauricio Piacentini <mauricio@tabuleiro.com>
0005 
0006 this python code:
0007     Copyright (C) 2008-2016 Wolfgang Rohdewald <wolfgang@rohdewald.de>
0008 
0009 SPDX-License-Identifier: GPL-2.0
0010 
0011 """
0012 
0013 from qt import Qt, QPainter, QBrush, QPalette, QPixmapCache, QPixmap
0014 from qt import QSvgRenderer
0015 
0016 from log import logException, i18n
0017 from mjresource import Resource
0018 
0019 
0020 class Background(Resource):
0021 
0022     """represents a background"""
0023 
0024     resourceName = 'background'
0025     configGroupName = 'KMahjonggBackground'
0026     cache = {}
0027 
0028     def __init__(self, name):
0029         """continue __build"""
0030         super().__init__(name)
0031         self.__svg = None
0032         self.__pmap = None
0033         self.graphicsPath = None
0034         QPixmapCache.setCacheLimit(20480)  # the chinese landscape needs much
0035 
0036         self.tiled = self.group.readEntry('Tiled') == '1'
0037         if self.tiled:
0038             try:
0039                 self.imageWidth = self.group.readInteger('Width')
0040                 self.imageHeight = self.group.readInteger('Height')
0041             except Exception as exc:
0042                 logException(exc)  # TODO: simplify if we switch to twisted logger
0043                 raise
0044         self.isPlain = bool(self.group.readEntry('Plain'))
0045         if not self.isPlain:
0046             graphName = self.group.readEntry("FileName")
0047             self.graphicsPath = self.locate(graphName)
0048             if not self.graphicsPath:
0049                 logException(
0050                     'cannot find kmahjongglib/backgrounds/%s for %s' %
0051                     (graphName, self.desktopFileName))
0052 
0053     def pixmap(self, size):
0054         """return a background pixmap or None for isPlain"""
0055         self.__pmap = QBrush()
0056         if not self.isPlain:
0057             width = size.width()
0058             height = size.height()
0059             if self.tiled:
0060                 width = self.imageWidth
0061                 height = self.imageHeight
0062             cachekey = '{name}W{width}H{height}'.format(name=self.name, width=width, height=height)
0063             self.__pmap = QPixmapCache.find(cachekey)
0064             if not self.__pmap:
0065                 renderer = QSvgRenderer(self.graphicsPath)
0066                 if not renderer.isValid():
0067                     logException(
0068                         i18n('file <filename>%1</filename> contains no valid SVG', self.graphicsPath))
0069                 self.__pmap = QPixmap(width, height)
0070                 self.__pmap.fill(Qt.transparent)
0071                 painter = QPainter(self.__pmap)
0072                 renderer.render(painter)
0073                 QPixmapCache.insert(cachekey, self.__pmap)
0074         return self.__pmap
0075 
0076     def brush(self, size):
0077         """background brush"""
0078         return QBrush(self.pixmap(size))
0079 
0080     def setPalette(self, onto):
0081         """set a background palette for widget onto"""
0082         palette = QPalette()
0083         mybrush = self.brush(onto.size())
0084         palette.setBrush(QPalette.Window, mybrush)
0085         onto.setPalette(palette)