def colorValue(self, key, default = QColor()): if type(default) != QColor: default = QColor(default) name = self.mSettings.value(key, default.name()) if (not QColor.isValidColor(name)): return QColor() return QColor(name)
def validate(self, value): self._basic_validation(value) if not value: return elif QColor.isValidColor(value): pass else: raise configexc.ValidationError(value, "must be a valid color")
def _parse_qcolor_string(color_spec): if re.match(r'#?[0-9a-f]{2,}', color_spec): return _parse_qcolor_html(color_spec) elif QColor.isValidColor(color_spec): color = QColor() color.setNamedColor(color_spec) return color raise ValueError('Color name not recognized and not a HTML format')
def validate(self, value): if not value: if self._none_ok: return else: raise configexc.ValidationError(value, "may not be empty!") elif QColor.isValidColor(value): pass else: raise configexc.ValidationError(value, "must be a valid color")
def __init__(self, parent=None, color_name="green"): super().__init__(parent) self.setAutoFillBackground(True) self.name = "" self.color = None if QColor.isValidColor(color_name): self.color = QColor(color_name) self.name = color_name self.clicked.connect(self.pick_color) self.setStyleSheet('background-color: {}'.format(self.color.name()))
def validate(self, value): if not value: if self._none_ok: return else: raise ValidationError(value, "may not be empty!") elif QColor.isValidColor(value): pass else: raise ValidationError(value, "must be a valid color")
def validate(self, value): self._basic_validation(value) if not value: return elif value.startswith('-'): # custom function name, won't validate. pass elif QColor.isValidColor(value): pass else: raise configexc.ValidationError(value, "must be a valid color")
def validate(self, value): self._basic_validation(value) if not value: return elif any(re.match(r, value) for r in self.color_func_regexes): # QColor doesn't handle these, so we do the best we can easily pass elif QColor.isValidColor(value): pass else: raise configexc.ValidationError(value, "must be a valid color")
def validate(self, value): if not value: if self._none_ok: return else: raise ValidationError(value, "may not be empty!") elif any(re.match(r, value) for r in self.color_func_regexes): # QColor doesn't handle these, so we do the best we can easily pass elif QColor.isValidColor(value): pass else: raise ValidationError(value, "must be a valid color")
def validate(self, value): if not value: if self._none_ok: return else: raise ValidationError(value, "may not be empty!") if value.startswith('-'): # custom function name, won't validate. pass elif QColor.isValidColor(value): pass else: raise ValidationError(value, "must be a valid color")
def validate(self, value): if not value: if self._none_ok: return else: raise configexc.ValidationError(value, "may not be empty!") if value.startswith('-'): # custom function name, won't validate. pass elif QColor.isValidColor(value): pass else: raise configexc.ValidationError(value, "must be a valid color")
def validate(self, value): functions = ['rgb', 'rgba', 'hsv', 'hsva', 'qlineargradient', 'qradialgradient', 'qconicalgradient'] self._basic_validation(value) if not value: return elif (any(value.startswith(func + '(') for func in functions) and value.endswith(')')): # QColor doesn't handle these pass elif QColor.isValidColor(value): pass else: raise configexc.ValidationError(value, "must be a valid color")
def toMap(self, variant, mapDir): self.mGidMapper.clear() self.mMapDir = mapDir variantMap = variant orientationString = variantMap.get("orientation", '') orientation = orientationFromString(orientationString) if (orientation == Map.Orientation.Unknown): self.mError = self.tr("Unsupported map orientation: \"%s\""%orientationString) return None staggerAxisString = variantMap.get("staggeraxis", '') staggerAxis = staggerAxisFromString(staggerAxisString) staggerIndexString = variantMap.get("staggerindex", '') staggerIndex = staggerIndexFromString(staggerIndexString) renderOrderString = variantMap.get("renderorder", '') renderOrder = renderOrderFromString(renderOrderString) nextObjectId = variantMap.get("nextobjectid", 0) map = Map(orientation, variantMap.get("width",0), variantMap.get("height",0), variantMap.get("tilewidth",0), variantMap.get("tileheight",0)) map.setHexSideLength(variantMap.get("hexsidelength", 0)) map.setStaggerAxis(staggerAxis) map.setStaggerIndex(staggerIndex) map.setRenderOrder(renderOrder) if (nextObjectId): map.setNextObjectId(nextObjectId) self.mMap = map map.setProperties(self.toProperties(variantMap.get("properties", {}))) bgColor = variantMap.get("backgroundcolor", '') if (bgColor!='' and QColor.isValidColor(bgColor)): map.setBackgroundColor(QColor(bgColor)) for tilesetVariant in variantMap.get("tilesets", []): tileset = self.__toTileset(tilesetVariant) if not tileset: return None map.addTileset(tileset) for layerVariant in variantMap.get("layers", []): layer = self.toLayer(layerVariant) if not layer: return None map.addLayer(layer) return map
def to_py(self, value): self._basic_py_validation(value, str) if not value: return None functions = ['rgb', 'rgba', 'hsv', 'hsva', 'qlineargradient', 'qradialgradient', 'qconicalgradient'] if (any(value.startswith(func + '(') for func in functions) and value.endswith(')')): # QColor doesn't handle these return value if not QColor.isValidColor(value): raise configexc.ValidationError(value, "must be a valid color") return value
def validate(self, value): functions = [ 'rgb', 'rgba', 'hsv', 'hsva', 'qlineargradient', 'qradialgradient', 'qconicalgradient' ] self._basic_validation(value) if not value: return elif (any(value.startswith(func + '(') for func in functions) and value.endswith(')')): # QColor doesn't handle these pass elif QColor.isValidColor(value): pass else: raise configexc.ValidationError(value, "must be a valid color")
def toMap(self, variant, mapDir): self.mGidMapper.clear() self.mMapDir = mapDir variantMap = variant orientationString = variantMap.get("orientation", '') orientation = orientationFromString(orientationString) if (orientation == Map.Orientation.Unknown): self.mError = self.tr("Unsupported map orientation: \"%s\"" % orientationString) return None staggerAxisString = variantMap.get("staggeraxis", '') staggerAxis = staggerAxisFromString(staggerAxisString) staggerIndexString = variantMap.get("staggerindex", '') staggerIndex = staggerIndexFromString(staggerIndexString) renderOrderString = variantMap.get("renderorder", '') renderOrder = renderOrderFromString(renderOrderString) nextObjectId = variantMap.get("nextobjectid", 0) map = Map(orientation, variantMap.get("width", 0), variantMap.get("height", 0), variantMap.get("tilewidth", 0), variantMap.get("tileheight", 0)) map.setHexSideLength(variantMap.get("hexsidelength", 0)) map.setStaggerAxis(staggerAxis) map.setStaggerIndex(staggerIndex) map.setRenderOrder(renderOrder) if (nextObjectId): map.setNextObjectId(nextObjectId) self.mMap = map map.setProperties(self.toProperties(variantMap.get("properties", {}))) bgColor = variantMap.get("backgroundcolor", '') if (bgColor != '' and QColor.isValidColor(bgColor)): map.setBackgroundColor(QColor(bgColor)) for tilesetVariant in variantMap.get("tilesets", []): tileset = self.__toTileset(tilesetVariant) if not tileset: return None map.addTileset(tileset) for layerVariant in variantMap.get("layers", []): layer = self.toLayer(layerVariant) if not layer: return None map.addLayer(layer) return map
def toImageLayer(self, variantMap): imageLayer = ImageLayer(variantMap.get("name", ''), variantMap.get("x", 0), variantMap.get("y", 0), variantMap.get("width", 0), variantMap.get("height", 0)) opacity = variantMap.get("opacity", 0.0) visible = variantMap.get("visible", True) imageLayer.setOpacity(opacity) imageLayer.setVisible(visible) trans = variantMap.get("transparentcolor", '') if (not trans.isEmpty() and QColor.isValidColor(trans)): imageLayer.setTransparentColor(QColor(trans)) imageVariant = variantMap.get("image", '') if imageVariant != '': imagePath = resolvePath(self.mMapDir, imageVariant) if (not imageLayer.loadFromImage(QImage(imagePath), imagePath)): self.mError = self.tr("Error loading image:\n'%s'" % imagePath) return None return imageLayer
def toImageLayer(self, variantMap): imageLayer = ImageLayer(variantMap.get("name",''), variantMap.get("x",0), variantMap.get("y",0), variantMap.get("width",0), variantMap.get("height",0)) opacity = variantMap.get("opacity", 0.0) visible = variantMap.get("visible", True) imageLayer.setOpacity(opacity) imageLayer.setVisible(visible) trans = variantMap.get("transparentcolor", '') if (not trans.isEmpty() and QColor.isValidColor(trans)): imageLayer.setTransparentColor(QColor(trans)) imageVariant = variantMap.get("image",'') if imageVariant != '': imagePath = resolvePath(self.mMapDir, imageVariant) if (not imageLayer.loadFromImage(QImage(imagePath), imagePath)): self.mError = self.tr("Error loading image:\n'%s'"%imagePath) return None return imageLayer
def __toTileset(self, variant): variantMap = variant firstGid = variantMap.get("firstgid",0) # Handle external tilesets sourceVariant = variantMap.get("source", '') if sourceVariant != '': source = resolvePath(self.mMapDir, sourceVariant) tileset, error = readTileset(source) if not tileset: self.mError = self.tr("Error while loading tileset '%s': %s"%(source, error)) else: self.mGidMapper.insert(firstGid, tileset) return tileset name = variantMap.get("name",'') tileWidth = variantMap.get("tilewidth",0) tileHeight = variantMap.get("tileheight",0) spacing = variantMap.get("spacing",0) margin = variantMap.get("margin",0) tileOffset = variantMap.get("tileoffset", {}) tileOffsetX = tileOffset.get("x",0) tileOffsetY = tileOffset.get("y",0) if (tileWidth <= 0 or tileHeight <= 0 or (firstGid == 0 and not self.mReadingExternalTileset)): self.mError = self.tr("Invalid tileset parameters for tileset '%s'"%name) return None tileset = Tileset.create(name, tileWidth, tileHeight, spacing, margin) tileset.setTileOffset(QPoint(tileOffsetX, tileOffsetY)) trans = variantMap.get("transparentcolor", '') if (trans!='' and QColor.isValidColor(trans)): tileset.setTransparentColor(QColor(trans)) imageVariant = variantMap.get("image",'') if imageVariant != '': imagePath = resolvePath(self.mMapDir, imageVariant) if (not tileset.loadFromImage(imagePath)): self.mError = self.tr("Error loading tileset image:\n'%s'"%imagePath) return None tileset.setProperties(self.toProperties(variantMap.get("properties", {}))) # Read terrains terrainsVariantList = variantMap.get("terrains", []) for terrainMap in terrainsVariantList: tileset.addTerrain(terrainMap.get("name", ''), terrainMap.get("tile", 0)) # Read tile terrain and external image information tilesVariantMap = variantMap.get("tiles", {}) for it in tilesVariantMap.items(): ok = False tileIndex = Int(it[0]) if (tileIndex < 0): self.mError = self.tr("Tileset tile index negative:\n'%d'"%tileIndex) if (tileIndex >= tileset.tileCount()): # Extend the tileset to fit the tile if (tileIndex >= len(tilesVariantMap)): # If tiles are defined this way, there should be an entry # for each tile. # Limit the index to number of entries to prevent running out # of memory on malicious input.f self.mError = self.tr("Tileset tile index too high:\n'%d'"%tileIndex) return None for i in range(tileset.tileCount(), tileIndex+1): tileset.addTile(QPixmap()) tile = tileset.tileAt(tileIndex) if (tile): tileVar = it[1] terrains = tileVar.get("terrain", []) if len(terrains) == 4: for i in range(0, 4): terrainId, ok = Int2(terrains[i]) if (ok and terrainId >= 0 and terrainId < tileset.terrainCount()): tile.setCornerTerrainId(i, terrainId) probability, ok = Float2(tileVar.get("probability", 0.0)) if (ok): tile.setProbability(probability) imageVariant = tileVar.get("image",'') if imageVariant != '': imagePath = resolvePath(self.mMapDir, imageVariant) tileset.setTileImage(tileIndex, QPixmap(imagePath), imagePath) objectGroupVariant = tileVar.get("objectgroup", {}) if len(objectGroupVariant) > 0: tile.setObjectGroup(self.toObjectGroup(objectGroupVariant)) frameList = tileVar.get("animation", []) lenFrames = len(frameList) if lenFrames > 0: frames = QVector() for i in range(lenFrames): frames.append(Frame()) for i in range(lenFrames - 1, -1, -1): frameVariantMap = frameList[i] frame = frames[i] frame.tileId = frameVariantMap.get("tileid", 0) frame.duration = frameVariantMap.get("duration", 0) tile.setFrames(frames) # Read tile properties propertiesVariantMap = variantMap.get("tileproperties", {}) for it in propertiesVariantMap.items(): tileIndex = Int(it[0]) propertiesVar = it[1] if (tileIndex >= 0 and tileIndex < tileset.tileCount()): properties = self.toProperties(propertiesVar) tileset.tileAt(tileIndex).setProperties(properties) if not self.mReadingExternalTileset: self.mGidMapper.insert(firstGid, tileset) return tileset
def __toTileset(self, variant): variantMap = variant firstGid = variantMap.get("firstgid", 0) # Handle external tilesets sourceVariant = variantMap.get("source", '') if sourceVariant != '': source = resolvePath(self.mMapDir, sourceVariant) tileset, error = readTileset(source) if not tileset: self.mError = self.tr("Error while loading tileset '%s': %s" % (source, error)) else: self.mGidMapper.insert(firstGid, tileset) return tileset name = variantMap.get("name", '') tileWidth = variantMap.get("tilewidth", 0) tileHeight = variantMap.get("tileheight", 0) spacing = variantMap.get("spacing", 0) margin = variantMap.get("margin", 0) tileOffset = variantMap.get("tileoffset", {}) tileOffsetX = tileOffset.get("x", 0) tileOffsetY = tileOffset.get("y", 0) if (tileWidth <= 0 or tileHeight <= 0 or (firstGid == 0 and not self.mReadingExternalTileset)): self.mError = self.tr( "Invalid tileset parameters for tileset '%s'" % name) return None tileset = Tileset.create(name, tileWidth, tileHeight, spacing, margin) tileset.setTileOffset(QPoint(tileOffsetX, tileOffsetY)) trans = variantMap.get("transparentcolor", '') if (trans != '' and QColor.isValidColor(trans)): tileset.setTransparentColor(QColor(trans)) imageVariant = variantMap.get("image", '') if imageVariant != '': imagePath = resolvePath(self.mMapDir, imageVariant) if (not tileset.loadFromImage(imagePath)): self.mError = self.tr("Error loading tileset image:\n'%s'" % imagePath) return None tileset.setProperties( self.toProperties(variantMap.get("properties", {}))) # Read terrains terrainsVariantList = variantMap.get("terrains", []) for terrainMap in terrainsVariantList: tileset.addTerrain(terrainMap.get("name", ''), terrainMap.get("tile", 0)) # Read tile terrain and external image information tilesVariantMap = variantMap.get("tiles", {}) for it in tilesVariantMap.items(): ok = False tileIndex = Int(it[0]) if (tileIndex < 0): self.mError = self.tr("Tileset tile index negative:\n'%d'" % tileIndex) if (tileIndex >= tileset.tileCount()): # Extend the tileset to fit the tile if (tileIndex >= len(tilesVariantMap)): # If tiles are defined this way, there should be an entry # for each tile. # Limit the index to number of entries to prevent running out # of memory on malicious input.f self.mError = self.tr( "Tileset tile index too high:\n'%d'" % tileIndex) return None for i in range(tileset.tileCount(), tileIndex + 1): tileset.addTile(QPixmap()) tile = tileset.tileAt(tileIndex) if (tile): tileVar = it[1] terrains = tileVar.get("terrain", []) if len(terrains) == 4: for i in range(0, 4): terrainId, ok = Int2(terrains[i]) if (ok and terrainId >= 0 and terrainId < tileset.terrainCount()): tile.setCornerTerrainId(i, terrainId) probability, ok = Float2(tileVar.get("probability", 0.0)) if (ok): tile.setProbability(probability) imageVariant = tileVar.get("image", '') if imageVariant != '': imagePath = resolvePath(self.mMapDir, imageVariant) tileset.setTileImage(tileIndex, QPixmap(imagePath), imagePath) objectGroupVariant = tileVar.get("objectgroup", {}) if len(objectGroupVariant) > 0: tile.setObjectGroup(self.toObjectGroup(objectGroupVariant)) frameList = tileVar.get("animation", []) lenFrames = len(frameList) if lenFrames > 0: frames = QVector() for i in range(lenFrames): frames.append(Frame()) for i in range(lenFrames - 1, -1, -1): frameVariantMap = frameList[i] frame = frames[i] frame.tileId = frameVariantMap.get("tileid", 0) frame.duration = frameVariantMap.get("duration", 0) tile.setFrames(frames) # Read tile properties propertiesVariantMap = variantMap.get("tileproperties", {}) for it in propertiesVariantMap.items(): tileIndex = Int(it[0]) propertiesVar = it[1] if (tileIndex >= 0 and tileIndex < tileset.tileCount()): properties = self.toProperties(propertiesVar) tileset.tileAt(tileIndex).setProperties(properties) if not self.mReadingExternalTileset: self.mGidMapper.insert(firstGid, tileset) return tileset