def __init__(self, desktopFileName=None): if desktopFileName is None: desktopFileName = 'default' self.tileSize = None self.faceSize = None self.__renderer = None self.__shadowOffsets = None self.defineCatalog() self.path = locateTileset(desktopFileName + '.desktop') if self.path.isEmpty(): self.path = locateTileset('default.desktop') if self.path.isEmpty(): directories = '\n\n' +'\n'.join(str(x) for x in KGlobal.dirs().resourceDirs("kmahjonggtileset")) logException(TileException(m18n( \ 'cannot find any tileset in the following directories, is libkmahjongg installed?') + directories)) else: logWarning(m18n('cannot find tileset %1, using default', desktopFileName)) self.desktopFileName = 'default' else: self.desktopFileName = desktopFileName self.darkenerAlpha = 120 if self.desktopFileName == 'jade' else 50 tileconfig = KConfig(self.path, KConfig.SimpleConfig) group = KConfigGroup(tileconfig.group("KMahjonggTileset")) self.name = group.readEntry("Name", "unknown tileset").toString() # Returns translated data self.author = group.readEntry("Author", "unknown author").toString() self.description = group.readEntry("Description", "no description available").toString() self.authorEmail = group.readEntry("AuthorEmail", "no E-Mail address available").toString() #Version control tileversion, entryOK = group.readEntry("VersionFormat", QVariant(0)).toInt() #Format is increased when we have incompatible changes, meaning that # older clients are not able to use the remaining information safely if not entryOK or tileversion > TILESETVERSIONFORMAT: logException(TileException('tileversion file / program: %d/%d' % \ (tileversion, TILESETVERSIONFORMAT))) graphName = QString(group.readEntry("FileName")) self.__graphicspath = locateTileset(graphName) if self.__graphicspath.isEmpty(): logException(TileException('cannot find kmahjongglib/tilesets/%s for %s' % \ (graphName, self.desktopFileName ))) self.renderer() # now that we get the sizes from the svg, we need the renderer right away self.svgName = { 'wn': 'WIND_1', 'ws': 'WIND_2', 'we': 'WIND_3', 'ww': 'WIND_4', 'db': 'DRAGON_1', 'dg': 'DRAGON_2', 'dr': 'DRAGON_3'} for value in '123456789': self.svgName['s%s' % value] = 'ROD_%s' % value self.svgName['b%s' % value] = 'BAMBOO_%s' % value self.svgName['c%s' % value] = 'CHARACTER_%s' % value for idx, wind in enumerate('eswn'): self.svgName['f%s' % wind] = 'FLOWER_%d' % (idx + 1) self.svgName['y%s' % wind] = 'SEASON_%d' % (idx + 1)
def __init__(self, desktopFileName=None): if desktopFileName is None: desktopFileName = 'default' self.__svg = None self.__pmap = None QPixmapCache.setCacheLimit(20480) # the chinese landscape needs much self.defineCatalog() self.desktopFileName = desktopFileName self.path = locatebackground(desktopFileName + '.desktop') if self.path.isEmpty(): self.path = locatebackground('default.desktop') if self.path.isEmpty(): directories = '\n\n' + \ '\n'.join(str(x) for x in KGlobal.dirs().resourceDirs("kmahjonggbackground")) logException(BackgroundException(m18n( 'cannot find any background in the following directories, is libkmahjongg installed?') + directories)) else: logWarning( m18n( 'cannot find background %1, using default', desktopFileName)) self.desktopFileName = 'default' config = KConfig(self.path) group = config.group("KMahjonggBackground") self.name = group.readEntry("Name") or m18n("unknown background") # Version control backgroundversion = int(group.readEntry("VersionFormat")) or 0 # Format is increased when we have incompatible changes, meaning that # older clients are not able to use the remaining information safely if backgroundversion > BACKGROUNDVERSIONFORMAT: logException(BackgroundException('backgroundversion file / program: %d/%d' % (backgroundversion, BACKGROUNDVERSIONFORMAT))) self.tiled = group.readEntry('Tiled') == '1' if self.tiled: self.imageWidth, entryOk = group.readEntry('Width').toInt() if not entryOk: raise Exception('cannot scan Width from background file') self.imageHeight, entryOk = group.readEntry('Height').toInt() if not entryOk: raise Exception('cannot scan Height from background file') self.isPlain = bool(group.readEntry('Plain')) if not self.isPlain: graphName = QString(group.readEntry("FileName")) self.__graphicspath = locatebackground(graphName) if self.__graphicspath.isEmpty(): logException(BackgroundException( 'cannot find kmahjongglib/backgrounds/%s for %s' % (graphName, self.desktopFileName)))
def __init__(self, dummyName): """continue __build""" self.group = KConfig(self.path).group(self.configGroupName) self.name = self.group.readEntry("Name") or i18n("unknown name") self.author = self.group.readEntry("Author") or i18n("unknown author") self.description = self.group.readEntry("Description") or i18n( "no description available") self.authorEmail = self.group.readEntry("AuthorEmail") or i18n( "no E-Mail address available") # Version control resourceVersion = self.group.readInteger("VersionFormat", default=0) # Format is increased when we have incompatible changes, meaning that # older clients are not able to use the remaining information safely if resourceVersion > RESOURCEFORMAT: logException('version file / program: %d/%d' % (resourceVersion, RESOURCEFORMAT))
def backgroundRowChanged(self): """user selected a new background, update our information about it and paint preview""" selBackground = self.backgroundList[ self.backgroundNameList.currentRow()] self.kcfg_backgroundName.setText(selBackground.desktopFileName) config = KConfig(selBackground.path) group = config.group("KMahjonggBackground") author = group.readEntry("Author") or m18n("unknown author") description = group.readEntry("Description") or "" authorEmail = group.readEntry( "AuthorEmail") or m18n( "no E-Mail address available") self.backgroundAuthor.setText(author) self.backgroundContact.setText(authorEmail) self.backgroundDescription.setText(description) selBackground.setPalette(self.backgroundPreview) self.backgroundPreview.setAutoFillBackground(True)
def __init__(self, desktopFileName=None): if desktopFileName is None: desktopFileName = 'default' self.tileSize = None self.faceSize = None self.__renderer = None self.__shadowOffsets = None self.defineCatalog() self.path = locateTileset(desktopFileName + '.desktop') if self.path.isEmpty(): self.path = locateTileset('default.desktop') if self.path.isEmpty(): self.__noTilesetFound() else: logWarning( m18n( 'cannot find tileset %1, using default', desktopFileName)) self.desktopFileName = 'default' else: self.desktopFileName = desktopFileName self.darkenerAlpha = 120 if self.desktopFileName == 'jade' else 50 tileconfig = KConfig(self.path, KConfig.SimpleConfig) group = tileconfig.group("KMahjonggTileset") self.name = group.readEntry("Name") or m18n("unknown tileset") self.author = group.readEntry("Author") or m18n("unknown author") self.description = group.readEntry( "Description") or m18n( "no description available") self.authorEmail = group.readEntry( "AuthorEmail") or m18n( "no E-Mail address available") # Version control tileversion = int(group.readEntry("VersionFormat")) or 0 # Format is increased when we have incompatible changes, meaning that # older clients are not able to use the remaining information safely if tileversion > TILESETVERSIONFORMAT: logException(TileException('tileversion file / program: %d/%d' % (tileversion, TILESETVERSIONFORMAT))) graphName = group.readEntry("FileName") self.__graphicspath = locateTileset(graphName) if self.__graphicspath.isEmpty(): logException( TileException('cannot find kmahjongglib/tilesets/%s for %s' % (graphName, self.desktopFileName))) self.renderer() # now that we get the sizes from the svg, we need the # renderer right away self.svgName = { 'wn': North.svgName, 'ws': South.svgName, 'we': East.svgName, 'ww': West.svgName, 'db': 'DRAGON_1', 'dg': 'DRAGON_2', 'dr': 'DRAGON_3'} for value in '123456789': self.svgName['s%s' % value] = 'ROD_%s' % value self.svgName['b%s' % value] = 'BAMBOO_%s' % value self.svgName['c%s' % value] = 'CHARACTER_%s' % value for idx, wind in enumerate('eswn'): self.svgName['f%s' % wind] = 'FLOWER_%d' % (idx + 1) self.svgName['y%s' % wind] = 'SEASON_%d' % (idx + 1)
class Resource: """Common code for backgrounds and tilesets""" resourceName = None # to be overridden in Tileset and Background configGroupName = None """represents a complete tileset""" # pylint: disable=too-many-instance-attributes cache = {} def __new__(cls, name): return cls.cache.get(name) or cls.cache.get( cls.__name(name)) or cls.__build(name) @classmethod def __directories(cls): """where to look for resources""" result = QStandardPaths.locateAll( QStandardPaths.GenericDataLocation, 'kmahjongglib/{}s'.format(cls.resourceName), QStandardPaths.LocateDirectory) result.insert( 0, os.path.join('share', 'kmahjongglib', '{}s'.format(cls.resourceName))) return (x for x in result if os.path.exists(x)) @classmethod def locate(cls, which): """locate the file with a resource""" for directory in cls.__directories(): path = os.path.join(directory, which) if os.path.exists(path): return path logException('cannot find kmahjongg%s %s in %s' % (cls.resourceName, which, cls.__directories())) @classmethod def loadAll(cls): """loads all available resources into cache""" resourceDirectories = cls.__directories() for directory in resourceDirectories: for name in os.listdir(directory): if name.endswith('.desktop'): if not name.endswith( 'alphabet.desktop') and not name.endswith( 'egypt.desktop'): cls(os.path.join(directory, name)) @classmethod def available(cls): """ready for the selector dialog, default first""" cls.loadAll() return sorted(set(cls.cache.values()), key=lambda x: x.desktopFileName != 'default') @classmethod def __noTilesetFound(cls): """No resources found""" directories = '\n\n' + '\n'.join(cls.__directories()) logException( i18n( 'cannot find any %1 in the following directories, ' 'is libkmahjongg installed?', cls.resourceName) + directories) # TODO: nicht schoen @staticmethod def __name(path): """extract the name from path: this is the filename minus the .desktop ending""" return os.path.split(path)[1].replace('.desktop', '') @classmethod def __build(cls, name): """build a new Resource. name is either a full file path or a desktop name. None stands for 'default'.""" result = object.__new__(cls) if os.path.exists(name): result.path = name result.desktopFileName = cls.__name(name) else: result.desktopFileName = name or 'default' result.path = cls.locate(result.desktopFileName + '.desktop') if not result.path: result.path = cls.locate('default.desktop') result.desktopFileName = 'default' if not result.path: cls.__noTilesetFound() else: logWarning(i18n('cannot find %1, using default', name)) cls.cache[result.desktopFileName] = result cls.cache[result.path] = result return result def __init__(self, dummyName): """continue __build""" self.group = KConfig(self.path).group(self.configGroupName) self.name = self.group.readEntry("Name") or i18n("unknown name") self.author = self.group.readEntry("Author") or i18n("unknown author") self.description = self.group.readEntry("Description") or i18n( "no description available") self.authorEmail = self.group.readEntry("AuthorEmail") or i18n( "no E-Mail address available") # Version control resourceVersion = self.group.readInteger("VersionFormat", default=0) # Format is increased when we have incompatible changes, meaning that # older clients are not able to use the remaining information safely if resourceVersion > RESOURCEFORMAT: logException('version file / program: %d/%d' % (resourceVersion, RESOURCEFORMAT)) def __str__(self): return "%s id=%d name=%s, name id=%d" % \ (self.resourceName, id(self), self.desktopFileName, id(self.desktopFileName)) @staticmethod def current(): """the currently wanted tileset. If not yet defined, do so""" pass
def konfigGroup(path, groupName): """returns access to a group of config options""" config = KConfig(path, KConfig.SimpleConfig) return config, KConfigGroup(config.group(groupName))