def __init__(self):
        self.defaultScale = 1./64
        self.objects = []
        self.pixmaps = []
        self.polyStyles = []
        self.edgeStyles = []
        self.options = MapOptions()

        ls = LineStyle("internal", 0, 0xFFFF, LineStyle.STYLE_INTERNAL)
        self.edgeStyles.append(ls)
        ls = LineStyle("default", 0, 0xFF, LineStyle.STYLE_SOLID)
        self.edgeStyles.append(ls)
        self.defEdgeStyleIndex = 1
        ps = PolygonStyle()
        ps.id = "default"
        ps.visible = ps.visibleInRadar= 1
        ps.filled = ps.textured = 0
        ps.defaultEdgeStyle = ls
        self.polyStyles.append(ps)
        self.defPolygonStyleIndex = 1
class MapModel:
    def __init__(self):
        self.defaultScale = 1./64
        self.objects = []
        self.pixmaps = []
        self.polyStyles = []
        self.edgeStyles = []
        self.options = MapOptions()

        ls = LineStyle("internal", 0, 0xFFFF, LineStyle.STYLE_INTERNAL)
        self.edgeStyles.append(ls)
        ls = LineStyle("default", 0, 0xFF, LineStyle.STYLE_SOLID)
        self.edgeStyles.append(ls)
        self.defEdgeStyleIndex = 1
        ps = PolygonStyle()
        ps.id = "default"
        ps.visible = ps.visibleInRadar= 1
        ps.filled = ps.textured = 0
        ps.defaultEdgeStyle = ls
        self.polyStyles.append(ps)
        self.defPolygonStyleIndex = 1

    def getDefaultScale(self):
        return self.defaultScale

    def setDefaultScale(self, ds):
        self.defaultScale = ds

    def getDefaultPolygonStyle(self):
        if self.defPolygonStyleIndex >= len(self.polyStyles):
            self.defPolygonStyleIndex = len(self.polyStyles) - 1
        return self.polyStyles[self.defPolygonStyleIndex]

    def setDefaultPolygonStyle(self, index):
        self.defPolygonStyleIndex = index

    def load(self, file, bmstore):
        self.edgeStyles = []
        ls = LineStyle("internal", 0, 0xFFFF, LineStyle.STYLE_INTERNAL)
        self.edgeStyles.append(ls)
        self.polyStyles = []

        xml.sax.parse(file, MapDocumentHandler(self, bmstore))
        self.defEdgeStyleIndex = len(self.edgeStyles) - 1
        self.defPolygonStyleIndex = len(self.polyStyles) - 1

    def save(self, file):
        file.write('<XPilotMap>\n')
        self.options.printXml(file)
        for pm in self.pixmaps:
            pm.printXml(file)
        for es in self.edgeStyles:
            es.printXml(file)
        for ps in self.polyStyles:
            ps.printXml(file)
        o = self.objects[:]
        o.reverse()
        for obj in o:
            obj.printXml(file)
        file.write('</XPilotMap>\n')
        file.close()

    def addToFront(self, moNew):
        self.addObject(moNew, 1)

    def addToBack(self, moNew):
        self.addObject(moNew, 0)

    def addObject(self, moNew, front):
        znew = moNew.getZOrder()
        for i in range(len(self.objects)):
            zold = self.objects[i].getZOrder()
            if znew < zold or front and znew == zold:
                self.objects.insert(i, moNew)
                return
        self.objects.append(moNew)

    def removeObject(self, mo):
        self.objects.remove(mo)