This commit is contained in:
leo 2022-02-14 15:53:18 +01:00
parent e0132fa617
commit d72d720f22
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
5 changed files with 78 additions and 14 deletions

View File

@ -5,8 +5,17 @@ class CarController:
def __init__(self,parentMap):
self.map=parentMap
self.cars=[]
self.path = None
def fromPath(self,path):
def setPath(self,path):
self.path = path
def fromPath(self,path=None):
if(path == None):
if(self.path == None):
return
else:
path = self.path
for vehicle in sumolib.xml.parse(path,"vehicle"):
route=vehicle.route[0].edges.split()
self.cars.append(Car(vehicle.id,route,self.map,self))

17
Map.py
View File

@ -10,10 +10,22 @@ class Map:
def __init__(self,path=None):
if path!=None:
self.fromPath(path)
self.net = None
def fromPath(self,path):
self.net = sumolib.net.readNet(path,withInternal=True,withConnections=True)
def isLoaded(self):
return not (self.net == None)
def safeCall(func):
def inner(*args, **kwargs):
if not args[0].isLoaded():
return
return func(*args, **kwargs)
return inner
@safeCall
def draw(self, painter):
for edge in self.net.getEdges():
color=Qt.white
@ -25,18 +37,23 @@ class Map:
poly = QPolygonF.fromList(pts)
painter.drawPolyline(poly)
@safeCall
def getEdge(self,edgeID):
return self.net.getEdge(edgeID)
@safeCall
def getNode(self,nodeID):
return self.net.getNode(nodeID)
@safeCall
def getLane(self,laneID):
return self.net.getLane(laneID)
@safeCall
def getBounds(self):
return self.net.getBBoxXY()
@safeCall
def convertPos(self,pos):
bounds=self.net.getBoundary()
bounds[0]-=200

23
main.py
View File

@ -8,7 +8,7 @@ else:
from PySide6.QtCore import Qt, QTimer
from PySide6.QtWidgets import QApplication, QMainWindow
from PySide6.QtGui import QSurfaceFormat
from PySide6.QtGui import QSurfaceFormat, QAction
from window import Ui_MainWindow
from mainLoop import mainLoop
@ -19,7 +19,23 @@ class MainWindow(QMainWindow):
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.mainLoop = mainLoop(self.ui.mainSurf)
self.setAttribute(Qt.WA_TranslucentBackground)
self.setAttribute(Qt.WA_NoSystemBackground, False)
self.mainLoop = mainLoop(self)
fileMenu = self.menuBar().addMenu("&File")
openMenu = fileMenu.addMenu("&Open")
openNet = QAction("&Open Network",self)
openNet.setStatusTip("Open Network file (.net.xml)")
openNet.triggered.connect(self.mainLoop.openNetwork)
openMenu.addAction(openNet)
openVeh = QAction("&Open Vehicles",self)
openVeh.setStatusTip("Open Vehicle description (.rou.xml)")
openVeh.triggered.connect(self.mainLoop.openVehicles)
openMenu.addAction(openVeh)
timer = QTimer(self)
timer.timeout.connect(self.mainLoop.update)
@ -34,9 +50,8 @@ if __name__ == "__main__":
format = QSurfaceFormat()
format.setDepthBufferSize(24)
format.setStencilBufferSize(8)
format.setVersion(3, 2)
format.setSamples(16)
format.setSamples(4)
format.setProfile(QSurfaceFormat.CoreProfile)
QSurfaceFormat.setDefaultFormat(format)

View File

@ -1,15 +1,16 @@
from Map import Map
from CarController import CarController
from PySide6.QtWidgets import QFileDialog
class mainLoop():
def __init__(self, painter):
self.painter = painter
def __init__(self, parent):
self.parent = parent
self.painter = parent.ui.mainSurf
self.map = Map()
self.map.fromPath("test2.net.xml")
self.controller = CarController(self.map)
self.controller.fromPath("test5.rou.xml")
self.painter.addMap(self.map)
self.painter.addCarController(self.controller)
@ -17,3 +18,21 @@ class mainLoop():
def update(self):
self.controller.update()
self.painter.update()
def openNetwork(self):
fileName = QFileDialog.getOpenFileName(self.parent,"Open Network", "./", "Network File (*.net.xml)")
if(fileName[0] == ''):
return
self.map.fromPath(fileName[0])
self.painter.generateTransform()
self.controller.fromPath()
def openVehicles(self):
fileName = QFileDialog.getOpenFileName(self.parent,"Open Vehicle trip description", "./", "Route File (*.rou.xml)")
if(fileName[0] == ''):
return
self.controller.setPath(fileName[0])
if(self.map.isLoaded()):
self.controller.fromPath()

View File

@ -1,10 +1,12 @@
from PySide6.QtOpenGLWidgets import QOpenGLWidget
from PySide6.QtCore import Qt
from PySide6.QtGui import QPainter, QFont, QColor, QTransform
from PySide6.QtGui import QPainter, QFont, QColor, QTransform, QOpenGLContext, QOpenGLFunctions
from PySide6 import QtOpenGL
class mainPainter(QOpenGLWidget):
def __init__(self, parent=None):
super(mainPainter, self).__init__(parent)
self.map = None
self.carController = None
self.transform = QTransform()
@ -17,6 +19,8 @@ class mainPainter(QOpenGLWidget):
def generateTransform(self):
bounds = self.map.getBounds()
if(bounds == None):
return
bounds[0] = list(map(lambda b: b-10,bounds[0]))
bounds[1] = list(map(lambda b: b+10, bounds[1]))
self.transform.reset()