def write(self, tileset, fileName): file = QSaveFile(fileName) if (not file.open(QIODevice.WriteOnly | QIODevice.Text)): self.mError = self.tr("Could not open file for writing.") return False converter = MapToVariantConverter() variant = converter.toVariant(tileset, QFileInfo(fileName).dir()) writer = json try: result = writer.dumps(variant, indent=4) except: # This can only happen due to coding error self.mError = self.tr('Unknow error.') return False out = QTextStream(file) out << result out.flush() if (file.error() != QFile.NoError): self.mError = self.tr("Error while writing file:\n%1").arg(file.errorString()) return False if (not file.commit()): self.mError = file.errorString() return False return True
def writeMap(self, *args): l = len(args) if l==3: map, device, path = args self.d.writeMap(map, device, path) elif l==2: tp = type(args[1]) if tp==QIODevice: map, device, path = args, QString() self.d.writeMap(map, device, path) elif tp in [QString, str]: ## # Writes a TMX map to the given \a fileName. # # Returns False and sets errorString() when reading failed. # \overload ## map, fileName = args file = QSaveFile(fileName) if (not self.d.openFile(file)): return False self.writeMap(map, file, QFileInfo(fileName).absolutePath()) if (file.error() != QFile.NoError): self.d.mError = file.errorString() return False if (not file.commit()): self.d.mError = file.errorString() return False return True
def write(self, map, fileName): # Get file paths for each layer layerPaths = self.outputFiles(map, fileName) # Traverse all tile layers currentLayer = 0 for layer in map.layers(): if layer.layerType() != Layer.TileLayerType: continue tileLayer = layer file = QSaveFile(layerPaths[currentLayer]) if (not file.open(QIODevice.WriteOnly | QIODevice.Text)): self.mError = self.tr("Could not open file for writing.") return False # Write out tiles either by ID or their name, if given. -1 is "empty" for y in range(0, tileLayer.height()): for x in range(0, tileLayer.width()): if (x > 0): file.write(",") cell = tileLayer.cellAt(x, y) tile = cell.tile if (tile and tile.hasProperty("name")) : file.write(tile.property("name").encode()) else: if tile: id = tile.id() else: id = -1 file.write(QByteArray.number(id)) file.write("\n") if (file.error() != QFile.NoError) : self.mError = file.errorString() return False if (not file.commit()) : self.mError = file.errorString() return False return True
def write(self, map, fileName): # Get file paths for each layer layerPaths = self.outputFiles(map, fileName) # Traverse all tile layers currentLayer = 0 for layer in map.layers(): if layer.layerType() != Layer.TileLayerType: continue tileLayer = layer file = QSaveFile(layerPaths[currentLayer]) if (not file.open(QIODevice.WriteOnly | QIODevice.Text)): self.mError = self.tr("Could not open file for writing.") return False # Write out tiles either by ID or their name, if given. -1 is "empty" for y in range(0, tileLayer.height()): for x in range(0, tileLayer.width()): if (x > 0): file.write(",") cell = tileLayer.cellAt(x, y) tile = cell.tile if (tile and tile.hasProperty("name")): file.write(tile.property("name").encode()) else: if tile: id = tile.id() else: id = -1 file.write(QByteArray.number(id)) file.write("\n") if (file.error() != QFile.NoError): self.mError = file.errorString() return False if (not file.commit()): self.mError = file.errorString() return False return True
def write(self, map, fileName): file = QSaveFile(fileName) if (not file.open(QIODevice.WriteOnly | QIODevice.Text)) : self.mError = self.tr("Could not open file for writing.") return False converter = MapToVariantConverter() variant = converter.toVariant(map, QFileInfo(fileName).dir()) writer = json try: result = writer.dumps(variant, indent=4) except: # This can only happen due to coding error self.mError = self.tr('Unknow error.') return False out = QTextStream(file) if self.mSubFormat == JsonMapFormat.SubFormat.JavaScript: # Trim and escape name nameWriter = json baseName = QFileInfo(fileName).baseName() baseNameResult = nameWriter.dumps(baseName) out << "(function(name,data){\n if(typeof onTileMapLoaded === 'undefined') {\n" out << " if(typeof TileMaps === 'undefined') TileMaps = {};\n" out << " TileMaps[name] = data;\n" out << " } else {\n" out << " onTileMapLoaded(name,data);\n" out << " }})(" + baseNameResult + ",\n" out << result if self.mSubFormat == JsonMapFormat.SubFormat.JavaScript: out << ");" out.flush() if (file.error() != QFile.NoError) : self.mError = self.tr("Error while writing file:\n%1").arg(file.errorString()) return False if (not file.commit()) : self.mError = file.errorString() return False return True
def write(self, map, fileName): file = QSaveFile(fileName) if (not file.open(QIODevice.WriteOnly | QIODevice.Text)) : self.mError = self.tr("Could not open file for writing.") return False self.mMapDir = QDir(QFileInfo(fileName).path()) writer = LuaTableWriter(file) writer.writeStartDocument() self.writeMap(writer, map) writer.writeEndDocument() if (file.error() != QFile.NoError) : self.mError = file.errorString() return False if (not file.commit()) : self.mError = file.errorString() return False return True