def json_hook(dct): """Hook method for json.load().""" if not dct.get('__type__'): return dct elif dct['__type__'] == "Simulation": return Simulation(dct['options'], dct['trackItems'], dct['routes'], dct['trainTypes'], dct['services'], dct['trains'], dct['messageLogger']) elif dct['__type__'] == "SignalItem": return signalitem.SignalItem(parameters=dct) elif dct['__type__'] == "EndItem": return enditem.EndItem(parameters=dct) elif dct['__type__'] == "InvisibleLinkItem": return invisiblelinkitem.InvisibleLinkItem(parameters=dct) elif dct['__type__'] == "LineItem": return lineitem.LineItem(parameters=dct) elif dct['__type__'] == "Place": return placeitem.Place(parameters=dct) elif dct['__type__'] == "PlatformItem": return platformitem.PlatformItem(parameters=dct) elif dct['__type__'] == "PointsItem": return pointsitem.PointsItem(parameters=dct) elif dct['__type__'] == "TextItem": return textitem.TextItem(parameters=dct) elif dct['__type__'] == "Route": return route.Route(parameters=dct) elif dct['__type__'] == "Position": return position.Position(parameters=dct) elif dct['__type__'] == "TrainType": return trains.TrainType(parameters=dct) elif dct['__type__'] == "Service": return trains.Service(parameters=dct) elif dct['__type__'] == "ServiceLine": return trains.ServiceLine(parameters=dct) elif dct['__type__'] == "Train": return trains.Train(parameters=dct) elif dct['__type__'] == "MessageLogger": return logger.MessageLogger(parameters=dct) elif dct['__type__'] == "Message": return logger.Message(dct) else: raise utils.FormatException( translate("json_hook", "Unknown __type__ '%s' in JSON file") % dct['__type__'])
def loadTrackItems(self, trackItems): for key, dct in trackItems.items(): dct["tiId"] = key trackItem = None if dct['__type__'] == "SignalItem": trackItem = signalitem.SignalItem(parameters=dct) elif dct['__type__'] == "EndItem": trackItem = enditem.EndItem(parameters=dct) elif dct['__type__'] == "InvisibleLinkItem": trackItem = invisiblelinkitem.InvisibleLinkItem(parameters=dct) elif dct['__type__'] == "LineItem": trackItem = lineitem.LineItem(parameters=dct) elif dct['__type__'] == "Place": trackItem = placeitem.Place(parameters=dct) elif dct['__type__'] == "PlatformItem": trackItem = platformitem.PlatformItem(parameters=dct) elif dct['__type__'] == "PointsItem": trackItem = pointsitem.PointsItem(parameters=dct) elif dct['__type__'] == "TextItem": trackItem = textitem.TextItem(parameters=dct) self._trackItems[key] = trackItem
def createAllTrackItems(self, conn): """Creates the instances of TrackItem and its subclasses (including Places) from the database.""" for p in conn.execute("SELECT * FROM trackitems WHERE titype='A'"): parameters = dict(p) tiId = parameters["tiid"] place = placeitem.Place(self, parameters) self.servicesLoaded.connect(place.sortTimetable) self._trackItems[tiId] = place self._places[place.placeCode] = place for trackItem in \ conn.execute("SELECT * FROM trackitems WHERE titype<>'A'"): parameters = dict(trackItem) tiId = parameters["tiid"] tiType = parameters["titype"] if tiType == "L": ti = lineitem.LineItem(self, parameters) elif tiType == "ZP": ti = platformitem.PlatformItem(self, parameters) elif tiType == "LI": ti = invisiblelinkitem.InvisibleLinkItem(self, parameters) elif tiType == "S": ti = signalitem.SignalItem(self, parameters) elif tiType == "P": ti = pointsitem.PointsItem(self, parameters) elif tiType == "E": ti = enditem.EndItem(self, parameters) elif tiType == "ZT": ti = textitem.TextItem(self, parameters) else: self.messageLogger.addMessage( self.tr("File error. Unknown tiType %s") % tiType) continue self.makeTrackItemSignalSlotConnections(ti) self._trackItems[tiId] = ti
def createTrackItem(self, tiType, pos, posEnd=None): """Creates a TrackItem of type type at the position pos. :param tiType: The type of the TrackItem to create (class name) :type tiType: str :param pos: the position at which to create the item :type pos: QtCore.QPointF :param posEnd: the position of the other end of the item (when applicable) :type posEnd: QtCore.QPointF """ pos = QtCore.QPointF( round(pos.x() / self.grid) * self.grid, round(pos.y() / self.grid) * self.grid) if posEnd is None: if tiType == "PointsItem": posEnd = QtCore.QPointF(-5, 0) elif tiType == "LineItem" or tiType == "InvisibleLinkItem": posEnd = pos + QtCore.QPointF(60, 0) elif tiType == "PlatformItem": posEnd = pos + QtCore.QPointF(60, 20) else: posEnd = QtCore.QPointF(0, 0) parameters = { "tiId": self._nextId, "name": "%i" % self._nextId, "x": pos.x(), "y": pos.y(), "xf": posEnd.x(), "yf": posEnd.y(), "xn": 5, "yn": 0, "xr": 5, "yr": -5, "reverse": 0, "maxSpeed": 0.0, "realLength": 1.0, "placeCode": None, "trackCode": None, "signalType": "UK_3_ASPECTS", "routesSetParams": "{}", "trainNotPresentParams": "{}" } if tiType == "LineItem": ti = lineitem.LineItem(parameters) elif tiType == "PlatformItem": ti = platformitem.PlatformItem(parameters) elif tiType == "InvisibleLinkItem": ti = invisiblelinkitem.InvisibleLinkItem(parameters) elif tiType == "SignalItem": parameters.update({"xn": pos.x() - 45, "yn": pos.y() + 5}) ti = signalitem.SignalItem(parameters) elif tiType == "PointsItem": ti = pointsitem.PointsItem(parameters) elif tiType == "EndItem": ti = enditem.EndItem(parameters) elif tiType == "Place": ti = placeitem.Place(parameters) elif tiType == "TextItem": ti = textitem.TextItem(parameters) else: ti = abstract.TrackItem(parameters) ti.initialize(self) self.expandBackgroundTo(ti) self._trackItems[self._nextId] = ti self._nextId += 1 self.updateSelection() return ti
def __init__(self, options=None, trackItems=None, routes=None, trainTypes=None, services=None, trns=None, messageLogger=None, fileName=None): """Constructor for the Editor class""" options = options or simulation.BUILTIN_OPTIONS trackItems = trackItems or {} routes = routes or {} trainTypes = trainTypes or {} services = services or {} trns = trns or [] messageLogger = messageLogger or logger.MessageLogger({}) super().__init__(options, trackItems, routes, trainTypes, services, trns, messageLogger) self._context = utils.Context.EDITOR_GENERAL self._libraryScene = QtWidgets.QGraphicsScene(0, 0, 200, 250, self) self._sceneBackground = editorscenebackground.EditorSceneBackground( self, 0, 0, 800, 600) self._sceneBackground.setZValue(-100) self._scene.addItem(self._sceneBackground) # Lines WhiteLineItem(0, 0, 0, 300, None, self._libraryScene) WhiteLineItem(100, 0, 100, 300, None, self._libraryScene) WhiteLineItem(200, 0, 200, 300, None, self._libraryScene) WhiteLineItem(0, 0, 200, 0, None, self._libraryScene) WhiteLineItem(0, 50, 200, 50, None, self._libraryScene) WhiteLineItem(0, 100, 200, 100, None, self._libraryScene) WhiteLineItem(0, 150, 200, 150, None, self._libraryScene) WhiteLineItem(0, 200, 200, 200, None, self._libraryScene) WhiteLineItem(0, 250, 200, 250, None, self._libraryScene) WhiteLineItem(0, 300, 200, 300, None, self._libraryScene) # Items self.librarySignalItem = signalitem.SignalItem({ "tiId": -1, "name": "Signal", "x": 65, "y": 25, "reverse": 0, "xn": 20, "yn": 30, "signalType": "UK_3_ASPECTS", "maxSpeed": 0.0, "routesSetParams": "{}", "trainNotPresentParams": "{}" }) self.libraryLineItem = lineitem.LineItem({ "tiId": -5, "name": "Line", "x": 120, "y": 25, "xf": 180, "yf": 25, "maxSpeed": 0.0, "realLength": 1.0, "placeCode": None, "trackCode": None }) self.libraryPointsItem = pointsitem.PointsItem({ "tiId": -3, "name": "Points", "maxSpeed": 0.0, "x": 50, "y": 75, "xf": -5, "yf": 0, "xn": 5, "yn": 0, "xr": 5, "yr": -5 }) self.libraryPlatformItem = platformitem.PlatformItem({ "tiId": -6, "name": "Platform", "x": 120, "y": 65, "xf": 180, "yf": 85, "maxSpeed": 0.0, "realLength": 1.0, "placeCode": None, "trackCode": None }) self.libraryPlaceItem = placeitem.Place({ "tiId": -8, "name": "PLACE", "placeCode": "", "maxSpeed": 0.0, "x": 132, "y": 115, "xf": 0, "yf": 0 }) self.libraryEndItem = enditem.EndItem({ "tiId": -7, "name": "End", "maxSpeed": 0.0, "x": 50, "y": 125, "xf": 0, "yf": 0 }) self.libraryTextItem = textitem.TextItem({ "tiId": -11, "name": "TEXT", "x": 36, "y": 165, "maxSpeed": 0.0, "realLength": 1.0 }) self.libraryInvisibleLinkItem = invisiblelinkitem.InvisibleLinkItem({ "tiId": -10, "name": "Invisible link", "x": 120, "y": 175, "xf": 180, "yf": 175, "maxSpeed": 0.0, "realLength": 1.0, "placeCode": None, "trackCode": None }) # Setup Models self._routesModel = route.RoutesModel(self) self._trainTypesModel = trains.TrainTypesModel(self) self._servicesModel = trains.ServicesModel(self) self._serviceLinesModel = trains.ServiceLinesModel(self) self._trainsModel = trains.TrainsModel(self) self._optionsModel = OptionsModel(self) self._placesModel = placeitem.PlacesModel(self) self._sceneryValidated = False self.fileName = fileName self._nextId = 1 self._nextRouteId = 1 self._grid = 5.0 self._preparedRoute = None self._selectedRoute = None self._selectedTrain = None self._selectedItems = [] self._clipbooard = [] self._displayedPositionGI = position.PositionGraphicsItem(self) self.registerGraphicsItem(self._displayedPositionGI) self.trainsChanged.connect(self.unselectTrains) self.scene.selectionChanged.connect(self.updateSelection)