00001 import copy
00002 from math import *
00003
00004
00005 from qt import *
00006 from qtcanvas import *
00007
00008 from cdbVisCore import *
00009 from objectclasses import *
00010 import pdb
00011 import random
00012 import math
00013 import gc
00014 import sys
00015 import inout
00016 import time
00017 import threading
00018
00019
00020
00021
00022 zoom_factor = 100
00023 activeItem = 0
00024 timer = None
00025
00026
00027
00028
00029 class mySelecter(QCanvasRectangle):
00030 def __init__(self, x,y,w=1,h=1,canvas=None):
00031 QCanvasRectangle.__init__(self,x,y,w,h,canvas)
00032 self.setPen( QPen(QColor(0,0,0),1,Qt.DotLine) )
00033 def boundingRect(self):
00034 rect = QCanvasRectangle.boundingRect(self)
00035 return QRect(rect.x()-5,rect.y()-5,rect.width()+5,rect.height()+5)
00036
00037
00038
00039
00040
00041 class waiter(QDialog):
00042 def __init__(self,parent,fct):
00043 QDialog.__init__(self,parent,"Waiter",0,0)
00044 self.image1 = QPixmap()
00045 self.image1.loadFromData(bigClock_data,"PNG")
00046 self.setName("Please Wait")
00047 self.setIcon(self.image1)
00048 Form1Layout = QGridLayout(self,1,1,11,6,"Form1Layout")
00049 self.textLabel1 = QLabel(self,"textLabel1")
00050 Form1Layout.addWidget(self.textLabel1,0,0)
00051 self.pixmapLabel3 = QLabel(self,"pixmapLabel3")
00052 self.pixmapLabel3.setSizePolicy(QSizePolicy(0,0,0,0,self.pixmapLabel3.sizePolicy().hasHeightForWidth()))
00053 self.pixmapLabel3.setPixmap(self.image1)
00054 self.pixmapLabel3.setScaledContents(1)
00055 Form1Layout.addWidget(self.pixmapLabel3,0,1)
00056
00057 self.setCaption("Please wait ...")
00058 self.textLabel1.setText("<h2>Processing ...</h2>")
00059 self.resize(QSize(209,86).expandedTo(self.minimumSizeHint()))
00060 self.clearWState(Qt.WState_Polished)
00061 self.fct=fct
00062
00063 def execute(self):
00064 self.fct()
00065
00066 def GetZoomFactor():
00067 return zoom_factor
00068 def SetZoomFactor(value):
00069 global zoom_factor
00070 zoom_factor = value
00071 visWindow.main.myStatusbar.zoom.setText(str(int(zoom_factor))+" %")
00072 def setActiveItem(item):
00073 global activeItem
00074 if(item!=activeItem):
00075 activeItem = item
00076 def GetActiveItem():
00077 return activeItem
00078
00079
00080
00081
00082
00083 def distPoint2Line(sp,ep,mp):
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 x,y,sx,sy,ex,ey = mp.x(),mp.y(),sp.x(),sp.y(),ep.x(),ep.y()
00095 if sx == ex :
00096 return abs(x-sx)
00097
00098 b = -1
00099 a = (sy-ey)/float(sx-ex)
00100 c = ey - a*ex
00101 return abs(a*x+b*y+c)/sqrt(float(a*a+b*b))
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 class visWindow(QCanvasView, dbUpdate):
00115 contents = []
00116
00117
00118
00119
00120
00121
00122 def __init__(self,main,canvas,parent,name="my CanvasView",flags=0):
00123
00124 QCanvasView.__init__(self,canvas,parent,name,flags)
00125 canvas.resize(5000,5000)
00126 self.dirtyCanvas = QCanvas(200,200)
00127 self.main = main
00128 visWindow.main = main
00129 self.canvas = canvas
00130 self.maxWidth, self.maxHeight = 5000, 5000
00131 self.centerX = self.maxWidth/2
00132 self.centerY = self.maxHeight/2
00133 self.scrollUnit = 1
00134
00135 self.setSizePolicy(QSizePolicy(0,0,0,0,self.sizePolicy().hasHeightForWidth()))
00136 QScrollView.setDragAutoScroll(self,True)
00137
00138 self.viewMode = VIEW_CIRCLE
00139 self.xScale = 120
00140 self.yScale = 80
00141 self.radius = 200
00142 self.__absY = {}
00143
00144
00145 self.curTool = None
00146
00147
00148 self.selection = []
00149 self.dragMode = drag_NONE
00150
00151 self._AdjustMenus()
00152
00153 self.penColour = Qt.black
00154 self.fillColour = Qt.white
00155 self.lineSize = 1
00156
00157 self.zoom = 100
00158 self.Center()
00159
00160 self.missing_parentnodes = []
00161
00162 self.prevseldragobj = None
00163 self.startpointsel = False
00164
00165 self.levelzoom = 0
00166
00167 pic=QPixmap("lhcbdetector.png")
00168 self.label=QLabel(None)
00169 self.label.setPixmap(pic)
00170 self.label.setGeometry(0,0,670,365)
00171
00172 self.mem_image = None
00173 self.prevsystemover = ""
00174 self.levelselections = {}
00175 self.macroobjects = []
00176 self.chamber_found_at_location = False
00177
00178 self.autozoom = True
00179 self.vw_updated = True
00180
00181 self.__moving=0
00182 self.__moving_start= 0
00183 self.pendingItem = 0
00184 self.activeItem = 0
00185 self.lastPos = 0
00186 self.minZ, self.maxZ = 0, 20
00187
00188 self.inOutPutDialog = None
00189 i = 0
00190 self.linkConnect = QCanvasLine(self.canvas)
00191 self.selecter = mySelecter(0,0,1,1,self.canvas)
00192 self.keptItems = []
00193
00194
00195
00196
00197 def ZoomIn(self,factor=-1,default=False):
00198 wm=QWMatrix()
00199 prevZoom = GetZoomFactor()/100
00200 prevw = self.width()/2
00201 prevh = self.height()/2
00202 if default:
00203 newZoom = 1.0
00204 else:
00205 if factor == -1:
00206 factor = 1.5
00207 newZoom = factor*prevZoom
00208 wm.scale( newZoom, newZoom)
00209 self.setWorldMatrix( wm )
00210 SetZoomFactor(newZoom*100)
00211 self.setContentsPos(newZoom*2500-prevw,newZoom*2500 - prevh)
00212
00213
00214 def ZoomOut(self,factor=-1,default=False):
00215 wm=QWMatrix()
00216 prevZoom = GetZoomFactor()/100
00217 prevw = self.width()/2
00218 prevh = self.height()/2
00219 if default:
00220 newZoom = 1.0
00221 else:
00222 if factor == -1:
00223 factor = 2/3.0
00224 newZoom = factor*prevZoom
00225 wm.scale( newZoom, newZoom)
00226 self.setWorldMatrix( wm )
00227 SetZoomFactor(newZoom*100)
00228 self.setContentsPos(newZoom*2500-prevw,newZoom*2500 - prevh)
00229
00230 def keep(self):
00231 if self.__moving:
00232 self.keptItems = [self.__moving.GetObject()]
00233 print self.__moving.GetObject().GetName()
00234
00235 def contentsContextMenuEvent(self,e):
00236 if self.selection:
00237 contextMenu = QPopupMenu(self.main)
00238 contextMenu.insertItem(QString(self.__moving.GetName()))
00239 contextMenu.insertSeparator()
00240 self.main.aModify.addTo(contextMenu)
00241 if self.keptItems==[]:
00242 self.main.aKeep.addTo(contextMenu)
00243 contextMenu.insertSeparator()
00244 self.main.aDelete.addTo(contextMenu)
00245 contextMenu.exec_loop(e.globalPos())
00246 elif self.main.GetActiveLink():
00247 contextMenu = QPopupMenu(self.main)
00248 contextMenu.insertItem(QString("Link : "+self.main.GetActiveLink().GetObject().GetName()))
00249 contextMenu.insertSeparator()
00250 self.main.aModify.addTo(contextMenu)
00251 contextMenu.insertSeparator()
00252 self.main.aDelete.addTo(contextMenu)
00253 contextMenu.exec_loop(e.globalPos())
00254
00255 def noMoveEventAfter(self):
00256 for item in self.selection:
00257 item.setActive(False)
00258 self.selection = [self.__moving]
00259 self.__moving.setActive(False)
00260
00261 def contentsMousePressEvent(self,e):
00262 point = self.inverseWorldMatrix().map(e.pos())
00263 i=0
00264 self.__moving = None
00265 self.__moving_start=point
00266 links_selected = []
00267
00268 ilist = self.canvas.collisions(point)
00269
00270 for each_item in ilist:
00271 if isinstance(each_item,myLine):
00272 links_selected.insert(0,each_item)
00273
00274 for each_item in ilist:
00275
00276 if isinstance(each_item,Box):
00277 self.__moving=each_item
00278 self.main.GetSelectWindow().UpdateInfo(each_item.GetObject())
00279
00280 if self.main.aCreateLinkMode.isOn():
00281 self.linkConnect.setPoints(point.x(),point.y(),point.x(),point.y())
00282 self.linkConnect.setPen(QPen(Qt.blue,3))
00283 self.linkConnect.show()
00284
00285 if e.state()==Qt.ControlButton:
00286 if each_item in self.selection:
00287 self.selection.remove(each_item)
00288 each_item.setActive(False)
00289 else:
00290 self.selection.insert(0,each_item)
00291 each_item.setActive(True)
00292 else:
00293
00294 if each_item in self.selection:
00295 global timer
00296 timer = QTimer(self)
00297 self.connect(timer,SIGNAL("timeout()"),self.noMoveEventAfter)
00298 timer.start( 100, True )
00299 else:
00300 timer = None
00301 if isinstance(self.main.GetActiveDevice(),Box):
00302 self.main.GetActiveDevice().setActive(False)
00303 self.selection = [each_item]
00304 each_item.setActive(True)
00305 self.main.SetActiveDevice(each_item)
00306
00307 if self.main.GetActiveLink():
00308 self.main.GetActiveLink().setActive(False)
00309 self.main.SetActiveLink(None)
00310 self.canvas.update()
00311 return
00312
00313 if e.state() == Qt.ShiftButton and not self.__moving:
00314 self.selecter.setSize(1,1)
00315 self.selecter.move(point.x(),point.y())
00316 self.selecter.show()
00317 return
00318
00319
00320 if self.main.GetActiveLink():
00321 self.main.GetActiveLink().setActive(False)
00322 self.main.SetActiveLink(None)
00323 for item in self.selection:
00324 item.setActive(False)
00325 self.selection = []
00326 if self.__moving:
00327 self.selection=[self.__moving]
00328 self.__moving.setActive(True)
00329
00330
00331 dist = 3
00332 link_selected=None
00333 for link in links_selected:
00334 if isinstance(link,QCanvasLine):
00335 dl = distPoint2Line(link.startPoint(), link.endPoint(), point)
00336 if dl < dist:
00337 dl =dist
00338 link_selected = link
00339 if link_selected:
00340 self.main.panel.selectWindow.UpdateInfo(link_selected.object)
00341 if isinstance(self.main.GetActiveLink(),myLine):
00342 self.main.GetActiveLink().setActive(False)
00343 self.main.SetActiveLink(link_selected)
00344 link_selected.setActive(True)
00345 print link_selected.GetName()
00346 self.__moving_start=point
00347 self.__moving=0
00348
00349 self.canvas.update()
00350
00351 def contentsMouseDoubleClickEvent(self, event):
00352 point = self.inverseWorldMatrix().map(event.pos())
00353 ilist = self.canvas.collisions(point)
00354 tmp = False
00355 for each_item in ilist:
00356 if not ( isinstance(each_item,QCanvasText) or isinstance(each_item,QCanvasLine) ):
00357 tmp = True
00358 break
00359 if tmp:
00360 self.OnDeviceSelected(each_item.object)
00361
00362 if event.button()==2:
00363 pdb.set_trace()
00364
00365
00366 def contentsMouseMoveEvent(self,e):
00367 point = self.inverseWorldMatrix().map(e.pos());
00368 global timer
00369 if timer:
00370 timer.stop()
00371 timer = None
00372 print "timer stopeed"
00373 if self.selecter.isVisible():
00374 self.selecter.setSize(point.x()-self.__moving_start.x(),point.y()-self.__moving_start.y())
00375 elif self.linkConnect.isVisible():
00376 self.linkConnect.setPoints(self.linkConnect.startPoint().x(),self.linkConnect.startPoint().y(),point.x(),point.y())
00377 self.canvas.update()
00378 return
00379 elif self.selection:
00380 for item in self.selection:
00381 item.moveBy(point.x() - self.__moving_start.x(),point.y() - self.__moving_start.y())
00382 self.__moving_start = point
00383 else:
00384 self.setCursor(QCursor(Qt.PointingHandCursor))
00385
00386 self.scrollBy(GetZoomFactor()/100*(-point.x() + self.__moving_start.x()),GetZoomFactor()/100*(-point.y() + self.__moving_start.y()))
00387 self.canvas.update()
00388 def contentsMouseReleaseEvent(self,e):
00389 self.unsetCursor()
00390 if self.selecter.isVisible():
00391 for item in self.selecter.collisions(True):
00392 if isinstance(item,Box):
00393 self.selection.append(item)
00394 item.setActive(True)
00395 self.selecter.setVisible(False)
00396
00397 if self.linkConnect.isVisible():
00398 point = self.inverseWorldMatrix().map(e.pos())
00399 ilist = self.canvas.collisions(point)
00400 for item in ilist:
00401 if isinstance(item,Box) and item.GetObject().GetName()!=self.__moving.GetObject().GetName():
00402 self.main.CreateLinkClick(self.__moving.GetObject(),item.GetObject())
00403 self.main.aCreateLinkMode.setOn(False)
00404 break;
00405 self.linkConnect.setVisible(False)
00406 self.canvas.update()
00407
00408
00409 def EnableAutoZoom(self,enable=True):
00410 if(OFF):
00411 pass
00412 self.autozoom = enable
00413
00414 def ResetMacroObjects(self):
00415 self.macroobjects = []
00416
00417
00418
00419 def OnScroll(self,event=None):
00420 self.main.GetAreaWindow().DrawSystem()
00421
00422
00423
00424 def GetMaxSize(self):
00425 return self.maxWidth,self.maxHeight
00426
00427
00428
00429 def IsChamberFoundAtLocation(self):
00430 return self.chamber_found_at_location
00431
00432
00433
00434
00435
00436
00437 def SetZoomFactor(self,value):
00438 SetZoomFactor(value)
00439 self.zoom = value
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453 def GetAbsY(self, position):
00454 return self.__absY.get(position, 0)
00455
00456
00457
00458
00459
00460
00461
00462
00463 def SetAbsY(self, position, value):
00464 self.__absY[position] = value
00465
00466
00467
00468
00469
00470 def ResetAbsY(self):
00471 self.__absY.clear()
00472
00473
00474
00475
00476 def Clear(self):
00477 self.DoDeleteAll(refresh=True)
00478
00479
00480
00481
00482
00483
00484
00485
00486 def AddToContents(self,obj,paintevent=True):
00487
00488
00489
00490 if obj.GetType() == obj_NODE:
00491
00492 tmp_portinlist = obj.GetPortList(port_IN)[:]
00493 tmp_portoutlist = obj.GetPortList(port_OUT)[:]
00494
00495 obj.ClearPorts()
00496
00497
00498
00499
00500 for port in tmp_portinlist:
00501 if port != None:
00502 tmp_link = self.FindByName(port.GetName())
00503 if tmp_link:
00504 self.CreateConnectedLink(tmp_link.GetObject())
00505
00506
00507 for port in tmp_portoutlist:
00508 if port != None:
00509 tmp_link = self.FindByName(port.GetName())
00510 if tmp_link:
00511 self.CreateConnectedLink(tmp_link.GetObject())
00512
00513
00514
00515 i = 0
00516 for itm in self.missing_parentnodes:
00517 if itm[1] == obj.GetName():
00518 tmp_link = self.FindByName(itm[0])
00519 if tmp_link:
00520 self.CreateConnectedLink(tmp_link.GetObject())
00521
00522 self.missing_parentnodes.pop(i)
00523 i += 1
00524
00525 if paintevent:
00526 self.paintevent(None)
00527
00528
00529
00530
00531
00532
00533 def AddNode(self,object,index=-1,portnumber=0,portIN=True):
00534 deviceInfo = object.GetObjectInfo(True)
00535 node = self.FindByName(deviceInfo[DEV_NAME])
00536 number_created = 0
00537 if not node:
00538 node = self.CreateNode(0, 0, object,True)
00539 number_created = 1
00540
00541 if index == -1:
00542 node.move(2500,2500)
00543 node.show()
00544 return (node, number_created)
00545
00546 if self.main.GetViewMode() == ID_PATHS_MODE:
00547 node.move(2500,2500-40*index)
00548 node.show()
00549 return (node, number_created)
00550
00551 parent_x, parent_y = 2500, 2500
00552 teta = pi/(portnumber+4)
00553 index += 2
00554
00555
00556
00557
00558
00559 cosinus, sinus = math.cos(index*teta),math.sin(index*teta)
00560 x_factor = 200*cosinus
00561 y_factor = 100*sinus
00562 x_delta = 0.1*x_factor
00563 y_delta = 0.1*y_factor
00564 x = - x_factor + parent_x
00565 if portIN:
00566 y = - y_factor + parent_y
00567 else:
00568 y = y_factor + parent_y
00569 node.move(x,y)
00570 node.show()
00571 while([elt.__class__ for elt in node.collisions(False)].count(QCanvasText)>1):
00572
00573 x_factor += x_delta
00574 y_factor += y_delta
00575
00576 x = - x_factor + parent_x
00577 if portIN:
00578 y = - y_factor + parent_y
00579
00580 else:
00581 y = y_factor + parent_y
00582
00583 node.move(x,y)
00584
00585
00586 return (node, number_created)
00587
00588
00589
00590
00591
00592 def Center(self):
00593
00594 scroll_x = self.maxWidth/2 - self.width()/2
00595 scroll_y = self.maxHeight/2 - self.height()/2
00596 self.setContentsPos(scroll_x,scroll_y)
00597
00598
00599
00600
00601
00602
00603
00604
00605 def CenterToObject(self,obj):
00606 self.setContentsPos(GetZoomFactor()*25-self.width()/2,GetZoomFactor()*25-self.height()/2)
00607 return
00608
00609
00610 pin = obj.GetPorts(port_IN)
00611 pout = obj.GetPorts(port_OUT)
00612 maxp = 0
00613 if pin > pout:
00614 maxp = pin
00615 else:
00616 maxp = pout
00617 if maxp < 5:
00618 maxp = 5
00619 if maxp > 100:
00620 maxp = 100
00621
00622 new_height = NODE_HEIGHT
00623 new_width = NODE_WIDTH * maxp
00624 if maxp >= 20:
00625 new_height += maxp/20/10
00626
00627
00628 obj_pos = obj.GetPosition()
00629
00630
00631
00632
00633 scroll_x = obj_pos.x() - (self.size().width()/2) + (obj.GetSize().width()/2)
00634 scroll_y = obj_pos.y() - (self.size().height()/2) + (obj.GetSize().height()/2)
00635 self.setContentsPos(scroll_x,scroll_y)
00636
00637
00638
00639
00640
00641
00642 def CenterToScreen(self):
00643
00644 x, y = self.contentsX(), self.contentsY()
00645 x, y = x + self.GetSize().GetWidth()/2, y + self.GetSize().GetHeight()/2
00646
00647 new_zoom = float(self.zoom)/100.0
00648 prev_zoom = float(self.centervalue)/100.0
00649
00650 new_x = (float(x - self.maxWidth/2) / prev_zoom * new_zoom) + self.maxWidth/2
00651 new_y = (float(y - self.maxHeight/2) / prev_zoom * new_zoom) + self.maxHeight/2
00652
00653 new_x,new_y = new_x - self.GetSize().width()/2, new_y - self.GetSize().height()/2
00654
00655 new_x /= self.scrollUnit
00656 new_y /= self.scrollUnit
00657
00658 self.setContentsPos(new_x,new_y)
00659
00660
00661 '''def DoScroll(self, x_upperleft,y_upperleft):
00662 """ Move the scrollbars (scroll), so that the x and y coordinates given
00663 in the parameter list is located at the upper left corner in the visible
00664 visual window.
00665
00666 @x_upperleft - integer defining the x position in the upper left corner
00667 @y_upperleft - integer defining the y position in the upper left corner
00668 """
00669
00670 x_upperleft /= self.scrollUnit
00671 y_upperleft /= self.scrollUnit
00672 self.setContentsPos(x_upperleft,y_upperleft) #self.Scroll(x_upperleft,y_upperleft)
00673
00674 ##
00675 # Scroll to position 0.
00676 #
00677 def ScrollOutOfBounds(self):
00678
00679 self.setContentsPos(0, 0)'''
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700 def OnDeviceSelected(self, deviceobj,clear=True,subsystemview=False,treenumber_x=0,treenumber_y=0,refresh=True):
00701 self.ZoomIn(-1,True)
00702 prev_zoom = 100
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713 def _CountNodes(linkList):
00714
00715
00716 nodes = {}
00717 for linkobj in linkList:
00718 link = linkobj.GetObjectInfo(True)
00719 node = link[DEV_SWITCH_FROM]
00720 nodes[node] = node
00721 node = link[DEV_SWITCH_TO]
00722 nodes[node] = node
00723
00724
00725 return max(len(nodes), 1)
00726
00727
00728
00729 viewMode = self.main.GetViewMode()
00730 if (viewMode == ID_SUCCESSORS_MODE) or subsystemview:
00731
00732 if clear:
00733 self.DoDeleteAll(refresh=False)
00734
00735
00736 if self.keptItems:
00737
00738 for item in self.keptItems:
00739 kept,node = self.AddNode(item, -1)
00740 kept.move(2400,2400)
00741 kept.show()
00742 self.keptItems = []
00743
00744
00745 inputLinks = deviceobj.GetLinks(False)
00746 if inputLinks == False:
00747 self.main.ShowError(str(deviceobj.GetErrorMessage()),ERR_ERROR)
00748
00749
00750 return ([],[])
00751
00752 outputLinks = deviceobj.GetLinks(True)
00753 if outputLinks == False:
00754 self.main.ShowError(str(deviceobj.GetErrorMessage()),ERR_ERROR)
00755
00756
00757 return ([],[])
00758
00759
00760
00761
00762 self.SetAbsY(0, 1)
00763 origShape, newNodes = self.AddNode(deviceobj)
00764 origShape.SetExpanded(2)
00765
00766
00767 ylayer_pos = origShape.GetLayer()
00768 node_pos = origShape.GetPosition()
00769 parent_x_pos_center = node_pos.x() + (origShape.GetSize().width()/2)
00770
00771
00772
00773 if (deviceobj.GetPortsOut() > deviceobj.GetPortsIn()):
00774 max_ports = deviceobj.GetPortsOut()
00775 else:
00776 max_ports = deviceobj.GetPortsIn()
00777
00778 if len(outputLinks) > 30 or len(inputLinks) > 30:
00779 if not self.inOutPutDialog:
00780 self.inOutPutDialog = inout.Input_output(self.main,self)
00781 self.inOutPutDialog.clear()
00782 self.inOutPutDialog.setData(inputLinks,outputLinks,origShape)
00783 for i in range(len(outputLinks)):
00784 tmp_link = outputLinks[i].GetObjectInfo(True)
00785 self.inOutPutDialog.insertOutput(tmp_link[DEV_SWITCH_TO],i)
00786
00787 j,factor = 1,1.0
00788 for i in range(len(inputLinks)):
00789 tmp_link = inputLinks[i].GetObjectInfo(True)
00790 self.inOutPutDialog.insertInput(tmp_link[DEV_SWITCH_FROM],i)
00791
00792 self.inOutPutDialog.show()
00793 else:
00794
00795 position = 1
00796 nr_out_added = 0
00797 devices_on_output = {}
00798
00799 for i in range(len(outputLinks)):
00800 tmp_link = outputLinks[i].GetObjectInfo(True)
00801 tmp_devobj = Device(self.main.GetActiveSystem(),tmp_link[DEV_SWITCH_TO],False)
00802 index,portnr = origShape.FindCorrespondingPortIndex(outputLinks[i].GetPortFrom(),outputLinks[i].GetPortTypeFrom(),port_OUT)
00803 port_pos = origShape.GetPortPosition(index,port_OUT)
00804 shape,newNodes = self.AddNode(tmp_devobj,index,len(outputLinks),False)
00805
00806 self.CreateConnectedLink(outputLinks[i])
00807
00808 position = 1
00809 nr_in_added = 0
00810 devices_on_input = {}
00811 for i in range(len(inputLinks)):
00812 tmp_link = inputLinks[i].GetObjectInfo(True)
00813 tmp_devobj = Device(self.main.GetActiveSystem(),tmp_link[DEV_SWITCH_FROM],False)
00814
00815 index,portnr = origShape.FindCorrespondingPortIndex(inputLinks[i].GetPortTo(),inputLinks[i].GetPortTypeTo(),port_IN)
00816 port_pos = origShape.GetPortPosition(index,port_IN)
00817 print tmp_devobj.GetName()," ---> ",index,portnr,deviceobj.GetPortsIn()
00818 shape,newNodes = self.AddNode(tmp_devobj,index,len(inputLinks),True)
00819
00820 self.CreateConnectedLink(inputLinks[i])
00821
00822 if not subsystemview:
00823 self.Select(origShape,refresh=False)
00824 self.CenterToObject(origShape)
00825
00826 elif viewMode == ID_PATHS_MODE:
00827
00828 if clear:
00829 self.DoDeleteAll(refresh=False)
00830 self.Center()
00831 inputLinks,outputLinks = [],[]
00832
00833
00834 else:
00835 if clear:
00836 self.DoDeleteAll(refresh=False)
00837
00838 self.SetAbsY(0, 1)
00839 origShape, newNodes = self.AddNode(deviceobj, 0, 0,refresh=False)
00840
00841 if origShape != None:
00842 self.ShowNeighbourLinks(origShape,origShape.GetObject(),refresh=False)
00843 self.CenterToObject(origShape)
00844 else:
00845 self.Center()
00846
00847 if not subsystemview:
00848 self.Select(origShape,refresh=False)
00849
00850 inputLinks,outputLinks = [],[]
00851
00852
00853
00854
00855 if refresh:
00856 self.canvas.update()
00857 return (inputLinks,outputLinks)
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871 def OnPathSelected(self, my_device, pathNr,paintevent=True):
00872
00873 path = my_device.GetSpecificPathInfo(pathNr)
00874 deviceInfo = my_device.GetObjectInfo(True)
00875
00876 self.DoDeleteAll(refresh=False)
00877
00878
00879 pLen = len(path)-1
00880 pL = len(path)
00881 self.ResetAbsY()
00882 shapeBefore = None
00883
00884 pathposition = 0.0
00885 index = 0
00886 for i in range(pL):
00887
00888 if path[i]['reversed'] == True:
00889 device1 = path[i][DEV_SWITCH_TO]
00890 device2 = path[i][DEV_SWITCH_FROM]
00891 else:
00892 device1 = path[i][DEV_SWITCH_FROM]
00893 device2 = path[i][DEV_SWITCH_TO]
00894
00895
00896 tmp_device_1 = Device(deviceInfo[DEV_SYSTEM],device1,False)
00897 shape_1, newNodes_1 = self.AddNode(tmp_device_1, index)
00898 pathposition += 0.5; index += 1
00899 print tmp_device_1.GetName()
00900
00901 tmp_device_2 = Device(deviceInfo[DEV_SYSTEM],device2,False)
00902 shape_2, newNodes_2 = self.AddNode(tmp_device_2, index)
00903 pathposition += 0.5; index += 1
00904 print tmp_device_2.GetName()
00905
00906
00907 tmp_link = Link(path[i][DEV_SYSTEM],path[i][DEV_LINK],False,False)
00908 self.CreateConnectedLink(tmp_link)
00909
00910
00911 self.Select(self.FindByName(deviceInfo[DEV_NAME]),refresh=False)
00912
00913 if paintevent:
00914 self.paintevent(None)
00915
00916 self.canvas.update()
00917
00918
00919
00920
00921
00922
00923
00924 def OnKeyEvent(self, event):
00925
00926 key = event.GetKeyCode()
00927
00928 if key == WXK_UP:
00929 self._MoveObject(0, -1)
00930 elif key == WXK_DOWN:
00931 self._MoveObject(0, 1)
00932 elif key == WXK_LEFT:
00933 self._MoveObject(-1, 0)
00934 elif key == WXK_RIGHT:
00935 self._MoveObject(1, 0)
00936 else:
00937 event.Skip()
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947 def OnMouseEvent(self, event):
00948
00949 if not self.main.GetCdb():
00950 return False
00951
00952 if not (event.Dragging() or event.LeftUp() or event.LeftDown() or event.Moving()):
00953 return
00954
00955
00956 if (self.levelzoom == 0) and (event.Moving() or event.LeftUp() or event.LeftDown()):
00957
00958 w_and_h = [[55,25,50,-2,"","VELO"],[30,80,105,-1,"","RICH1"],[15,35,137,-1,"","TT"],[40,50,279,30,"","OT"],[40,13,279,-2,"","IT"],[40,50,279,-32,"","OT"],[60,160,319,-1,"","RICH2"],[12,140,379,-1,"1","MUON"],[6,75,391,-40,"","PRS"],[6,75,391,40,"","PUS"],[20,150,397,-1,"","ECAL"],[42,166,417,-1,"","HCAL"],[29,180,459,-1,"2","MUON"],[30,195,488,-1,"3","MUON"],[30,210,518,-1,"4","MUON"],[30,220,548,-1,"5","MUON"],[70,21,232,-155,"","ECS"],[70,20,302,-155,"","TFC"],[65,20,372,-155,"","DAQ"],[70,24,232,-131,"","L0DU"],[70,24,302,-131,"","L0MUON"],[65,24,372,-131,"","L0CALO"]]
00959 new_x,new_y = self.contentsX(), self.contentsY()
00960 my_w,my_h = self.GetSizeTuple()
00961 new_x = new_x + my_w/2
00962 new_y = new_y + my_h/2
00963
00964 if self.mem_image != None:
00965 image_w, image_h = self.mem_image.GetSizeTuple()
00966
00967 startx = new_x-image_w/2
00968 starty = new_y
00969
00970 mouse_x = event.GetX()
00971 mouse_y = event.GetY()
00972 mouse_x, mouse_y = self.contentsX() + mouse_x, self.contentsY() + mouse_y
00973
00974 i = 0
00975 oversystem = False
00976 for subsystem in w_and_h:
00977 x = startx + subsystem[2]
00978 y = starty - subsystem[1]/2 + subsystem[3]
00979 w = subsystem[0]
00980 h = subsystem[1]
00981 if mouse_x >= x and mouse_x <= x+w and mouse_y >= y and mouse_y <= y+h:
00982 oversystem = True
00983 self.SetCursor(wxStockCursor(wxCURSOR_HAND))
00984 if event.Moving():
00985 if self.prevsystemover != str(subsystem[5] + " " + subsystem[4]):
00986 self.prevsystemover = str(subsystem[5] + " " + subsystem[4])
00987 self.Refresh()
00988 dc = wxClientDC(self)
00989 dc.SetBrush(wxBrush(Qt.white, wxSOLID))
00990 self.PrepareDC(dc)
00991 dc.BeginDrawing()
00992 dc.DrawText(str(subsystem[5]) + " " + str(subsystem[4]),new_x-my_w/2,new_y-my_h/2)
00993 dc.EndDrawing()
00994 elif event.LeftUp():
00995
00996 self.main.SetActiveSystem(subsystem[5],"visual")
00997 self.SetLevelSelections(str(subsystem[4]),subsystem[5],1,reset=True)
00998 self.SetLevelZoom(1)
00999 break
01000 i += 1
01001 if not oversystem or event.LeftUp():
01002 self.SetCursor(wxStockCursor(wxCURSOR_ARROW))
01003
01004 return
01005
01006
01007 elif self.levelzoom == 1 and self.main.GetActiveSystem() == "MUON":
01008 if event.LeftUp():
01009 mousePt = self._GetEventCoordinates(event)
01010
01011 obj = self._GetObjectAt(mousePt,True)
01012 if (obj != None):
01013 self.macroobjects = []
01014
01015 self.SetLevelSelections(str(obj.GetMacroID()),self.main.GetActiveSystem(),2)
01016 self.SetLevelZoom(2)
01017 self.SetCursor(wxStockCursor(wxCURSOR_ARROW))
01018 elif event.Moving():
01019 mousePt = self._GetEventCoordinates(event)
01020
01021 obj = self._GetObjectAt(mousePt,True)
01022 if (obj != None):
01023
01024 self.SetCursor(wxStockCursor(wxCURSOR_HAND))
01025 else:
01026 self.SetCursor(wxStockCursor(wxCURSOR_ARROW))
01027
01028 return
01029
01030 elif self.levelzoom == 2 and self.main.GetActiveSystem() == "MUON":
01031 if event.LeftUp():
01032 mousePt = self._GetEventCoordinates(event)
01033
01034 obj = self._GetObjectAt(mousePt,True)
01035 if (obj != None):
01036 self.macroobjects = []
01037 self.SetLevelSelections(str(obj.GetMacroID()),"MUON",3)
01038 self.SetLevelZoom(3)
01039 self.SetCursor(wxStockCursor(wxCURSOR_ARROW))
01040 elif event.Moving():
01041 mousePt = self._GetEventCoordinates(event)
01042
01043 obj = self._GetObjectAt(mousePt,True)
01044 if (obj != None):
01045 self.SetCursor(wxStockCursor(wxCURSOR_HAND))
01046 else:
01047 self.SetCursor(wxStockCursor(wxCURSOR_ARROW))
01048
01049 return
01050
01051 elif self.levelzoom == 3 and self.main.GetActiveSystem() == "MUON":
01052 if event.LeftUp():
01053 mousePt = self._GetEventCoordinates(event)
01054
01055 obj = self._GetObjectAt(mousePt,True)
01056 if (obj != None):
01057 self.macroobjects = []
01058 self.SetLevelSelections(str(obj.GetMacroID()),"MUON",5)
01059 self.SetCursor(wxStockCursor(wxCURSOR_ARROW))
01060 elif event.Moving():
01061 mousePt = self._GetEventCoordinates(event)
01062
01063 obj = self._GetObjectAt(mousePt,True)
01064 if (obj != None):
01065 self.SetCursor(wxStockCursor(wxCURSOR_HAND))
01066 else:
01067 self.SetCursor(wxStockCursor(wxCURSOR_ARROW))
01068
01069 return
01070
01071
01072
01073 feedbackType = feedback_RECT
01074 action = self.SelectByRectangle
01075 actionParam = param_RECT
01076 selecting = True
01077 dashedLine = True
01078
01079 if event.LeftDown():
01080 mousePt = self._GetEventCoordinates(event)
01081 if selecting:
01082 obj, handle = self._GetObjectAndSelectionHandleAt(mousePt)
01083
01084 if selecting and (obj != None) and (handle != handle_NONE) and ((self.curTool == tool_EDIT and obj.GetType() == obj_LINK) or (obj.GetType() == obj_NODE)):
01085
01086
01087
01088
01089 self.dragMode = drag_RESIZE
01090 self.resizeObject = obj
01091
01092 if obj.GetType() in [obj_LINE, obj_LINK]:
01093 self.resizeFeedback = feedback_LINE
01094 pos = obj.GetPosition()
01095 startPt = QPoint(pos.x + obj.GetStartPt().x,
01096 pos.y + obj.GetStartPt().y)
01097 endPt = QPoint(pos.x + obj.GetEndPt().x,
01098 pos.y + obj.GetEndPt().y)
01099 if handle == handle_START_POINT:
01100 self.resizeAnchor = endPt
01101 self.resizeFloater = startPt
01102 self.startpointsel = True
01103 self.main.ShowError("You have chosen the start point of the link.",ERR_INFORMATION)
01104 else:
01105 self.resizeAnchor = startPt
01106 self.resizeFloater = endPt
01107 self.startpointsel = False
01108 self.main.ShowError("You have chosen the end point of the link.",ERR_INFORMATION)
01109 else:
01110 self.resizeFeedback = feedback_RECT
01111 pos = obj.GetPosition()
01112 size = obj.GetSize()
01113 topLeft = QPoint(pos.x, pos.y)
01114 topRight = QPoint(pos.x + size.width, pos.y)
01115 botLeft = QPoint(pos.x, pos.y + size.height)
01116 botRight = QPoint(pos.x + size.width, pos.y + size.height)
01117
01118 if handle == handle_TOP_LEFT:
01119 self.resizeAnchor = botRight
01120 self.resizeFloater = topLeft
01121 elif handle == handle_TOP_RIGHT:
01122 self.resizeAnchor = botLeft
01123 self.resizeFloater = topRight
01124 elif handle == handle_BOTTOM_LEFT:
01125 self.resizeAnchor = topRight
01126 self.resizeFloater = botLeft
01127 elif handle == handle_BOTTOM_RIGHT:
01128 self.resizeAnchor = topLeft
01129 self.resizeFloater = botRight
01130
01131 self.curPt = mousePt
01132 self.resizeOffsetX = self.resizeFloater.x - mousePt.x
01133 self.resizeOffsetY = self.resizeFloater.y - mousePt.y
01134 endPt = QPoint(self.curPt.x + self.resizeOffsetX,
01135 self.curPt.y + self.resizeOffsetY)
01136 self._DrawVisualFeedback(self.resizeAnchor, endPt,
01137 self.resizeFeedback, False)
01138
01139 elif selecting and (self._GetObjectAt(mousePt) != None):
01140
01141
01142
01143
01144
01145
01146 obj = self._GetObjectAt(mousePt)
01147 if obj not in self.selection:
01148
01149 if event.ControlDown() == False:
01150 self.Select(obj,refresh=True)
01151 else:
01152 self.Select(obj,True,refresh=True)
01153 elif obj in self.selection and event.ControlDown() == True:
01154 self.Deselect(obj,refresh=True)
01155
01156 self.dragMode = drag_MOVE
01157 self.moveOrigin = mousePt
01158 self.curPt = mousePt
01159 self._DrawObjectOutline(0, 0)
01160
01161 elif selecting and (self._GetObjectAt(mousePt) == None) and event.ControlDown() == True:
01162
01163 self.dragMode = drag_MOVE
01164 self.moveOrigin = mousePt
01165 self.curPt = mousePt
01166 self._DrawObjectOutline(0, 0)
01167 self.main.ResetActiveObjects()
01168 else:
01169
01170 self.dragOrigin = mousePt
01171 self.curPt = mousePt
01172 self.SetCursor(wxCROSS_CURSOR)
01173 self.CaptureMouse()
01174 self._DrawVisualFeedback(mousePt, mousePt, feedbackType,
01175 dashedLine)
01176 self.dragMode = drag_DRAG
01177 self.main.ResetActiveObjects()
01178 event.Skip()
01179 return
01180
01181 if event.Dragging():
01182 if self.dragMode == drag_RESIZE:
01183
01184
01185
01186 mousePt = self._GetEventCoordinates(event)
01187 if (self.curPt.x != mousePt.x) or (self.curPt.y != mousePt.y):
01188 if self.curTool == tool_EDIT:
01189
01190 dc = wxClientDC(self)
01191 self.PrepareDC(dc)
01192 dc.BeginDrawing()
01193
01194 dc.SetPen(wxPen(wxRED, 1, wxSOLID))
01195
01196 if self.resizeObject.GetType() == obj_LINK:
01197 obj = self._GetObjectAt(QPoint(mousePt.x,mousePt.y))
01198 if obj and obj.GetType() != obj_LINK:
01199 if obj != self.prevseldragobj and self.prevseldragobj != None:
01200 self.prevseldragobj.Draw(dc,draw_NORMAL,self.zoom >= 65,self.zoom >= 50)
01201
01202 self.prevseldragobj = obj
01203 obj.Draw(dc,draw_NORMAL,self.zoom >= 65,self.zoom >= 50,False)
01204 else:
01205 if self.prevseldragobj != None:
01206 self.prevseldragobj.Draw(dc,draw_NORMAL,self.zoom >= 65,self.zoom >= 50)
01207 self.prevseldragobj = None
01208
01209 dc.EndDrawing()
01210
01211
01212
01213 endPt = QPoint(self.curPt.x + self.resizeOffsetX,
01214 self.curPt.y + self.resizeOffsetY)
01215 self._DrawVisualFeedback(self.resizeAnchor, endPt,
01216 self.resizeFeedback, False)
01217 self.curPt = mousePt
01218
01219 endPt = QPoint(self.curPt.x + self.resizeOffsetX,
01220 self.curPt.y + self.resizeOffsetY)
01221 self._DrawVisualFeedback(self.resizeAnchor, endPt,
01222 self.resizeFeedback, False)
01223
01224
01225 elif self.dragMode == drag_MOVE:
01226
01227
01228
01229 mousePt = self._GetEventCoordinates(event)
01230 if (self.curPt.x != mousePt.x) or (self.curPt.y != mousePt.y):
01231
01232 self._DrawObjectOutline(self.curPt.x - self.moveOrigin.x,
01233 self.curPt.y - self.moveOrigin.y)
01234 self.curPt = mousePt
01235
01236 self._DrawObjectOutline(self.curPt.x - self.moveOrigin.x,
01237 self.curPt.y - self.moveOrigin.y)
01238
01239 elif self.dragMode == drag_DRAG:
01240
01241
01242 mousePt = self._GetEventCoordinates(event)
01243 if (self.curPt.x != mousePt.x) or (self.curPt.y != mousePt.y):
01244
01245 self._DrawVisualFeedback(self.dragOrigin, self.curPt,
01246 feedbackType, dashedLine)
01247 self.curPt = mousePt
01248
01249 self._DrawVisualFeedback(self.dragOrigin, self.curPt,
01250 feedbackType, dashedLine)
01251 event.Skip()
01252 return
01253
01254 if event.LeftUp():
01255 if self.dragMode == drag_RESIZE:
01256
01257 mousePt = self._GetEventCoordinates(event)
01258
01259 endPt = QPoint(self.curPt.x + self.resizeOffsetX,
01260 self.curPt.y + self.resizeOffsetY)
01261 self._DrawVisualFeedback(self.resizeAnchor, endPt,
01262 self.resizeFeedback, False)
01263
01264 resizePt = QPoint(mousePt.x + self.resizeOffsetX,
01265 mousePt.y + self.resizeOffsetY)
01266
01267 if (self.resizeFloater.x != resizePt.x) or \
01268 (self.resizeFloater.y != resizePt.y):
01269 self._ResizeObject(self.resizeObject,
01270 self.resizeAnchor,
01271 self.resizeFloater,
01272 resizePt)
01273 else:
01274 pass
01275 self.Refresh()
01276
01277
01278
01279 if self.curTool == tool_EDIT:
01280
01281 dc = wxClientDC(self)
01282 self.PrepareDC(dc)
01283 dc.BeginDrawing()
01284
01285 if self.resizeObject.GetType() == obj_LINK:
01286 obj = self._GetObjectAt(QPoint(mousePt.x,mousePt.y))
01287 if obj and obj.GetType() != obj_LINK:
01288
01289 obj.Draw(dc,draw_NORMAL,self.zoom >= 65,self.zoom >= 50,False)
01290 self.main.ModifyLinkClick(None,self.resizeObject.GetObject(),self.startpointsel,obj.GetObject())
01291
01292 else:
01293 pass
01294
01295 self.prevseldragobj = None
01296 dc.EndDrawing()
01297 self.Refresh()
01298
01299 elif self.dragMode == drag_MOVE:
01300
01301
01302
01303 mousePt = self._GetEventCoordinates(event)
01304
01305 self._DrawObjectOutline(self.curPt.x - self.moveOrigin.x,
01306 self.curPt.y - self.moveOrigin.y)
01307 if (self.moveOrigin.x != mousePt.x) or \
01308 (self.moveOrigin.y != mousePt.y):
01309 self._MoveObject(mousePt.x - self.moveOrigin.x,
01310 mousePt.y - self.moveOrigin.y)
01311 else:
01312 pass
01313
01314 elif self.dragMode == drag_DRAG:
01315
01316
01317 mousePt = self._GetEventCoordinates(event)
01318
01319 self._DrawVisualFeedback(self.dragOrigin, self.curPt,
01320 feedbackType, dashedLine)
01321 self.ReleaseMouse()
01322 self.SetCursor(wxSTANDARD_CURSOR)
01323
01324 if actionParam == param_RECT:
01325 x1 = min(self.dragOrigin.x, self.curPt.x)
01326 y1 = min(self.dragOrigin.y, self.curPt.y)
01327 x2 = max(self.dragOrigin.x, self.curPt.x)
01328 y2 = max(self.dragOrigin.y, self.curPt.y)
01329
01330 startX = x1
01331 startY = y1
01332 width = x2 - x1
01333 height = y2 - y1
01334
01335 if not selecting:
01336 if ((x2-x1) < 8) or ((y2-y1) < 8): return
01337
01338 action(x1, y1, x2-x1, y2-y1)
01339 elif actionParam == param_LINE:
01340 action(self.dragOrigin.x, self.dragOrigin.y,
01341 self.curPt.x, self.curPt.y)
01342 self.dragMode = drag_NONE
01343 event.Skip()
01344
01345
01346
01347
01348
01349
01350
01351
01352 def OnDoubleClickEvent(self, event=None,paintevent=True):
01353
01354 mousePt = self._GetEventCoordinates(event)
01355 obj = self._GetObjectAt(mousePt)
01356 if obj == None:
01357 return
01358
01359
01360 if ((self.main.GetViewMode() != self.main.GetPrevView()) or self.main.GetViewMode() == ID_SUCCESSORS_MODE or self.main.GetViewMode() == ID_PATHS_MODE):
01361
01362 self.main.SetPrevView(self.main.GetViewMode())
01363 self.main.WhenChangeView()
01364
01365
01366 if obj.GetType() == obj_NODE:
01367 if self.main.GetViewMode() == ID_DYNAMIC_MODE:
01368 self.ShowNeighbourLinks(obj,obj.GetObject(),refresh=False)
01369 elif self.main.GetViewMode() == ID_SUBSYSTEM_MODE:
01370
01371
01372 self.OnDeviceSelected(obj.GetObject(),False,True,refresh=False)
01373
01374 self.CenterToObject(obj)
01375 elif self.main.GetViewMode() == ID_PATHS_MODE:
01376 self.main.GetSelectWindow().UpdatePath(obj.GetObject())
01377
01378 else:
01379 self.main.GetSelectWindow().UpdateInfo(obj.GetObject())
01380 self.OnDeviceSelected(obj.GetObject(),refresh=False)
01381
01382 elif obj.GetType() == obj_TEXT:
01383 pass
01384
01385 else:
01386 pass
01387
01388 if paintevent:
01389 self.paintevent(None)
01390
01391
01392
01393
01394
01395
01396
01397
01398
01399
01400
01401
01402
01403 def ViewOnlyLinksOfGivenType(self):
01404 print "restricted: " + str(self.main.IsLinkTypeRestricted())
01405
01406 tbd_links = []
01407 total_links = 0
01408 tbd_devs = []
01409
01410 linktype = self.main.GetRestrictedLinkType()
01411 i = 0
01412 while i < len(visWindow.contents):
01413 my_itm = visWindow.contents[i]
01414 if my_itm.GetObject().__class__ == Link:
01415 if my_itm.GetObject().GetType() != linktype:
01416 tbd_links.append(my_itm)
01417 my_itm.show()
01418 else:
01419 my_itm.hide()
01420 total_links += 1
01421 i += 1
01422
01423 if (len(tbd_links) == total_links):
01424 self.main.ShowError("None of the links that are shown are of the given link type, it would therefore have resulted in an empty visual window, and the choice was skipped.",ERR_INFORMATION,True)
01425 return False
01426
01427
01428 i = 0
01429
01430
01431
01432
01433
01434
01435
01436
01437
01438
01439
01440
01441
01442
01443
01444
01445
01446
01447
01448 def ShowNeighbourLinks(self,origShape,deviceobj,refresh=True):
01449
01450 if GetZoomFactor() != 100:
01451 prev_zoom = GetZoomFactor()
01452 self.main.OnZoom(100,False)
01453 else:
01454 prev_zoom = 100
01455
01456
01457
01458
01459
01460
01461
01462
01463
01464
01465
01466 def _CountNodes(linkList):
01467
01468 nodes = {}
01469 for linkobj in linkList:
01470 link = linkobj.GetObjectInfo(True)
01471 node = link[DEV_SWITCH_FROM]
01472 nodes[node] = node
01473 node = link[DEV_SWITCH_TO]
01474 nodes[node] = node
01475
01476
01477
01478 return max(len(nodes), 1)
01479
01480
01481 cdb = self.main.GetCdb()
01482
01483
01484 ylayer_pos = origShape.GetLayer()
01485 node_pos = origShape.GetPosition()
01486
01487
01488 inputLinks = deviceobj.GetLinks(False)
01489 if inputLinks == False:
01490 self.ShowError(str(deviceobj.GetErrorMessage()),ERR_ERROR)
01491 if prev_zoom != 100:
01492 self.main.OnZoom(prev_zoom,False)
01493 return ([],[])
01494
01495
01496 outputLinks = deviceobj.GetLinks(True)
01497 if outputLinks == False:
01498 self.ShowError(str(deviceobj.GetErrorMessage()),ERR_ERROR)
01499 if prev_zoom != 100:
01500 self.main.OnZoom(prev_zoom,False)
01501 return ([],[])
01502
01503
01504
01505 if (deviceobj.GetPortsOut() > deviceobj.GetPortsIn()):
01506 max_ports = deviceobj.GetPortsOut()
01507 else:
01508 max_ports = deviceobj.GetPortsIn()
01509
01510
01511
01512
01513
01514
01515
01516
01517
01518
01519
01520
01521
01522
01523
01524
01525
01526
01527
01528
01529
01530 def countConnections(selfobj,portlist):
01531
01532 for item in portlist:
01533 if item != None and (item.GetObject().GetNodeTo() != selfobj.GetName() and item.GetObject().GetNodeFrom() != selfobj.GetName()):
01534 return -1
01535
01536 return 0
01537
01538
01539
01540
01541
01542
01543
01544
01545
01546
01547
01548 def removeConnection(obj,portlist,porttype):
01549
01550 i = 0
01551 if porttype == "IN":
01552 for item in portlist:
01553 if item != None:
01554 if item.GetObject().GetNodeFrom() == obj.GetName():
01555 tmp = portlist.pop(i)
01556 portlist.insert(i,None)
01557 if item in self.selection:
01558 self.selection.remove(item)
01559 if item in visWindow.contents:
01560 visWindow.contents.remove(item)
01561
01562 i += 1
01563
01564 elif porttype == "OUT":
01565 for item in portlist:
01566 if item != None:
01567 if item.GetObject().GetNodeTo() == obj.GetName():
01568 tmp = portlist.pop(i)
01569 portlist.insert(i,None)
01570 if item in self.selection:
01571 self.selection.remove(item)
01572 if item in visWindow.contents:
01573 visWindow.contents.remove(item)
01574
01575
01576 i += 1
01577
01578
01579 if origShape.GetExpanded() == 2:
01580 tbd_content = []
01581 tmp_collapsed = 0
01582
01583
01584 for lnk in inputLinks:
01585 visobj = self.FindByName(lnk.GetNodeFrom())
01586 visparent = self.FindByName(lnk.GetNodeTo())
01587 vislink = self.FindByName(lnk.GetName())
01588 if visobj != None:
01589 if (visobj.GetExpanded() == 0) \
01590 or (visobj.GetExpanded() == 2 \
01591 and countConnections(visparent,visobj.GetPortList(port_IN)) == 0 \
01592 and countConnections(visparent,visobj.GetPortList(port_OUT)) == 0) \
01593 or (visobj.GetExpanded() == 1 \
01594 and countConnections(visparent,visobj.GetPortList(port_IN)) == 0 \
01595 and countConnections(visparent,visobj.GetPortList(port_OUT)) == 0):
01596
01597
01598
01599
01600
01601
01602 removeConnection(visobj,origShape.GetPortList(port_IN),"IN")
01603 tbd_content.append(visobj)
01604
01605 elif (visobj.GetExpanded() == 1) \
01606 and (countConnections(visparent,visobj.GetPortList(port_IN)) != 0 \
01607 or countConnections(visparent,visobj.GetPortList(port_OUT)) != 0):
01608
01609
01610
01611 tmp_collapsed = 1
01612 elif (visobj.GetExpanded() == 2) \
01613 and (countConnections(visparent,visobj.GetPortList(port_IN)) != 0 or \
01614 countConnections(visparent,visobj.GetPortList(port_OUT)) != 0):
01615
01616
01617
01618 tmp_collapsed = 1
01619
01620 for lnk in outputLinks:
01621 visobj = self.FindByName(lnk.GetNodeTo())
01622 visparent = self.FindByName(lnk.GetNodeFrom())
01623 vislink = self.FindByName(lnk.GetName())
01624 if visobj != None:
01625 if (visobj.GetExpanded() == 0) or \
01626 (visobj.GetExpanded() == 2 \
01627 and countConnections(visparent,visobj.GetPortList(port_IN)) == 0 \
01628 and countConnections(visparent,visobj.GetPortList(port_OUT)) == 0) \
01629 or (visobj.GetExpanded() == 1 \
01630 and countConnections(visparent,visobj.GetPortList(port_IN)) == 0 \
01631 and countConnections(visparent,visobj.GetPortList(port_OUT)) == 0):
01632
01633 removeConnection(visobj,origShape.GetPortList(port_OUT),"OUT")
01634 tbd_content.append(visobj)
01635
01636 elif (visobj.GetExpanded() == 1) \
01637 and (countConnections(visparent,visobj.GetPortList(port_IN)) != 0 \
01638 or countConnections(visparent,visobj.GetPortList(port_OUT)) != 0):
01639 tmp_collapsed = 1
01640 elif (visobj.GetExpanded() == 2) \
01641 and (countConnections(visparent,visobj.GetPortList(port_IN)) != 0 \
01642 or countConnections(visparent,visobj.GetPortList(port_OUT)) != 0):
01643 tmp_collapsed = 1
01644
01645
01646
01647 for delitm in tbd_content:
01648 if delitm in self.selection:
01649 self.selection.remove(delitm)
01650 if delitm in visWindow.contents:
01651 visWindow.contents.remove(delitm)
01652
01653
01654 origShape.SetExpanded(tmp_collapsed)
01655 self.CenterToObject(origShape)
01656 if prev_zoom != 100:
01657 self.main.OnZoom(prev_zoom,False)
01658 return True
01659
01660
01661
01662 ylayer_pos = origShape.GetLayer()
01663 node_pos = origShape.GetPosition()
01664 parent_x_pos_center = node_pos.x+(origShape.GetSize().GetWidth()/2)
01665
01666
01667 position = 1
01668 nr_out_added = 0
01669 devices_on_output = {}
01670 for i in range(len(outputLinks)):
01671 tmp_link = outputLinks[i].GetObjectInfo(True)
01672 tmp_devobj = Device(self.main.GetActiveSystem(),tmp_link[DEV_SWITCH_TO],False)
01673 index,portnr = origShape.FindCorrespondingPortIndex(outputLinks[i].GetPortFrom(),outputLinks[i].GetPortTypeFrom(),port_OUT)
01674 port_pos = origShape.GetPortPosition(index,port_OUT)
01675 shape,newNodes = self.AddNode(tmp_devobj,port_pos.x,1,False,deviceobj.GetPortsOut(),parent_x_pos_center,port_pos.y,max_ports,refresh=False)
01676
01677
01678 if outputLinks[i].GetNodeTo() not in devices_on_output.keys():
01679 devices_on_output[outputLinks[i].GetNodeTo()] = []
01680
01681 my_x = shape.GetPosition().x
01682 my_y = shape.GetPosition().y
01683 devices_on_output[outputLinks[i].GetNodeTo()].append([my_x,my_y])
01684
01685
01686 self.CreateConnectedLink(outputLinks[i])
01687 position += 1
01688 nr_out_added += newNodes
01689
01690
01691
01692 for out_node in devices_on_output:
01693 sum_x = 0
01694 sum_y = 0
01695 for out_link in devices_on_output[out_node]:
01696 sum_x += out_link[0]
01697 sum_y += out_link[1]
01698
01699 vis_node = self.FindByName(out_node)
01700 avg_x = int(sum_x / len(devices_on_output[out_node]))
01701 avg_y = int(sum_y / len(devices_on_output[out_node]))
01702 if vis_node != None:
01703 vis_node.SetPosition(QPoint(avg_x,avg_y))
01704
01705 position = 1
01706 nr_in_added = 0
01707 devices_on_input = {}
01708 for i in range(len(inputLinks)):
01709 tmp_link = inputLinks[i].GetObjectInfo(True)
01710 tmp_devobj = Device(self.main.GetActiveSystem(),tmp_link[DEV_SWITCH_FROM],False)
01711 index,portnr = origShape.FindCorrespondingPortIndex(inputLinks[i].GetPortTo(),inputLinks[i].GetPortTypeTo(),port_IN)
01712 port_pos = origShape.GetPortPosition(index,port_IN)
01713 shape,newNodes = self.AddNode(tmp_devobj,port_pos.x,-1,False,deviceobj.GetPortsIn(),parent_x_pos_center,port_pos.y,max_ports,refresh=False)
01714
01715 if inputLinks[i].GetNodeFrom() not in devices_on_input.keys():
01716 devices_on_input[inputLinks[i].GetNodeFrom()] = []
01717
01718 my_x = shape.GetPosition().x
01719 my_y = shape.GetPosition().y
01720 devices_on_input[inputLinks[i].GetNodeFrom()].append([my_x,my_y])
01721
01722
01723 self.CreateConnectedLink(inputLinks[i])
01724 position += 1
01725 nr_in_added += newNodes
01726
01727
01728 for in_node in devices_on_input:
01729 sum_x = 0
01730 sum_y = 0
01731 for in_link in devices_on_input[in_node]:
01732 sum_x += in_link[0]
01733 sum_y += in_link[1]
01734
01735 vis_node = self.FindByName(in_node)
01736 avg_x = int(sum_x / len(devices_on_input[in_node]))
01737 avg_y = int(sum_y / len(devices_on_input[in_node]))
01738 if vis_node != None:
01739 vis_node.SetPosition(QPoint(avg_x,avg_y))
01740
01741
01742
01743 absY_out = self.GetAbsY(ylayer_pos+1)
01744 self.SetAbsY(1, absY_out + nr_out_added)
01745
01746 absY_in = self.GetAbsY(ylayer_pos-1)
01747 self.SetAbsY(1, absY_in + nr_in_added)
01748
01749 self.Select(origShape,refresh=False)
01750 origShape.SetExpanded(2)
01751 self.CenterToObject(origShape)
01752
01753 if prev_zoom != 100:
01754 self.main.OnZoom(prev_zoom,False)
01755
01756 if refresh:
01757 self.Refresh()
01758
01759
01760
01761
01762
01763
01764
01765
01766 def OnRightClick(self, event):
01767
01768 if self.GetLevelZoom() != -1:
01769 return
01770
01771 mousePt = self._GetEventCoordinates(event)
01772 obj = self._GetObjectAt(mousePt)
01773
01774 if obj == None:
01775 if not self.main.GetCdb():
01776 return False
01777
01778 menu = wxMenu()
01779 menu.Append(menu_SELECTALL, "Select All")
01780 menu.Append(menu_DESELECTALL, "Deselect All")
01781
01782 EVT_MENU(self, menu_SELECTALL, self.SelectAll)
01783 EVT_MENU(self, menu_DESELECTALL, self.DeselectAll)
01784
01785 zoommenu = wxMenu()
01786 zoommenu.Append(menu_ZOOMIN, "Zoom In")
01787 zoommenu.Append(menu_ZOOMOUT, "Zoom Out")
01788 zoommenu.Append(menu_ZOOMCUSTOM, "Custom Zoom")
01789
01790 menu.AppendMenu(-1,"Zoom",zoommenu,"")
01791
01792
01793 EVT_MENU(self, menu_ZOOMIN, self.main.ZoomIn)
01794 EVT_MENU(self, menu_ZOOMOUT, self.main.ZoomOut)
01795 EVT_MENU(self, menu_ZOOMCUSTOM, self.main.ZoomCustom)
01796
01797
01798
01799
01800 clickPt = QPoint(event.GetX(),event.GetY())
01801 self.PopupMenu(menu,clickPt)
01802 menu.Destroy()
01803
01804 else:
01805
01806
01807 if obj not in self.selection:
01808 self.Select(obj,refresh=True)
01809
01810
01811 if self.curTool == tool_EDIT:
01812
01813
01814 menu = wxMenu()
01815 menu.Append(menu_CLONE, "Clone")
01816 menu.Append(menu_DUPLICATE, "Duplicate")
01817 menu.Append(menu_DELETE, "Delete")
01818 menu.Append(menu_MODIFY,"Modify")
01819
01820 EVT_MENU(self, menu_DUPLICATE, self.DoDuplicateHelper)
01821 EVT_MENU(self, menu_DELETE, self.DoDelete)
01822 EVT_MENU(self,menu_CLONE,self.DoCloneHelper)
01823
01824 if obj.GetObject().__class__ == Device:
01825 EVT_MENU(self, menu_MODIFY, self.main.ModifyDeviceClick)
01826 else:
01827 EVT_MENU(self, menu_MODIFY, self.main.ModifyLinkClick)
01828
01829
01830
01831 clickPt = QPoint(event.GetX(), event.GetY())
01832 self.PopupMenu(menu, clickPt)
01833 menu.Destroy()
01834
01835
01836
01837
01838
01839
01840 def SetLevelZoom(self,factor):
01841 self.levelzoom = factor
01842
01843
01844
01845
01846
01847
01848
01849 def GetLevelZoom(self):
01850 return self.levelzoom
01851
01852
01853
01854
01855
01856
01857
01858 def GetLevelInfo(self):
01859 try:
01860 levelinfo = self.levelselections[self.main.GetActiveSystem()]
01861 return levelinfo
01862 except KeyError,err:
01863 return False
01864
01865
01866
01867
01868
01869
01870
01871 def GetLevelSelections(self):
01872 try:
01873 levelinfo = self.levelselections[self.main.GetActiveSystem()][self.levelzoom-1]
01874 return levelinfo
01875 except IndexError,err:
01876 return False
01877
01878
01879
01880
01881
01882
01883 def GetLevelSelection(self,level):
01884 return self.levelselections[self.main.GetActiveSystem()][level]
01885
01886
01887
01888
01889
01890
01891
01892
01893
01894
01895
01896 def SetLevelSelections(self,id,system,level,reset=False):
01897 try:
01898 if self.levelselections[system]:
01899 if reset:
01900 self.levelselections[system] = []
01901 while len(self.levelselections[system]) >= level:
01902 self.levelselections[system].pop(level-1)
01903
01904 self.levelselections[system].append(id)
01905 except KeyError,err:
01906 self.levelselections[system] = []
01907 self.levelselections[system].append(id)
01908
01909
01910
01911
01912
01913
01914
01915
01916
01917
01918
01919
01920
01921
01922
01923
01924 def ResizeToFitContents(self):
01925
01926
01927
01928 if GetZoomFactor() < 100:
01929 self.main.OnZoom(100,False)
01930
01931
01932 if self.levelzoom == -1:
01933 vw_size_w, vw_size_h = self.width(), self.height()
01934 vw_start_x, vw_start_y = self.contentsX(), self.contentsY()
01935
01936
01937 vw_start_x = float(vw_start_x)
01938 vw_start_y = float(vw_start_y)
01939 vw_size_w = float(vw_size_w)
01940 vw_size_h = float(vw_size_h)
01941
01942
01943 vw_end_x = vw_start_x + vw_size_w
01944 vw_end_y = vw_start_y + vw_size_h
01945
01946
01947 center_x = vw_start_x + vw_size_w/2.0
01948 center_y = vw_start_y + vw_size_h/2.0
01949 greatest_distance = 1.0
01950
01951
01952
01953 zoom = 100.0
01954
01955
01956 max_distance_rect = ((vw_size_w/2.0)*(vw_size_w/2.0) + (vw_size_h/2.0)*(vw_size_h/2.0))**(0.5)
01957 max_angle_rect = (vw_size_h/2.0)/(vw_size_w/2.0)
01958
01959 max_angle_rect_deg = degrees(atan(max_angle_rect))
01960
01961 for vis_obj in visWindow.contents:
01962 if vis_obj.GetType() == obj_NODE:
01963 obj_start_x, obj_start_y = vis_obj.GetPosition().x(), vis_obj.GetPosition().y()
01964 obj_size_w, obj_size_h = vis_obj.GetSize().width(), vis_obj.GetSize().height()
01965
01966 obj_start_x = float(obj_start_x)
01967 obj_start_y = float(obj_start_y)
01968 obj_size_w = float(obj_size_w)
01969 obj_size_h = float(obj_size_h)
01970
01971 obj_end_x = obj_start_x + obj_size_w
01972 obj_end_y = obj_start_y + obj_size_h
01973
01974
01975 if obj_start_x < vw_start_x:
01976 x_dist = fabs(center_x - obj_start_x)
01977
01978
01979 if (obj_start_y < center_y):
01980 y_dist = fabs(center_y - obj_start_y)
01981 else:
01982 y_dist = fabs(center_y - obj_end_y)
01983
01984
01985 tot_dist = (x_dist*x_dist + y_dist*y_dist)**(0.5)
01986
01987
01988 if x_dist == 0.0:
01989 x_dist = 0.001
01990
01991 angle = y_dist/x_dist
01992 angle_deg = degrees(atan(angle))
01993
01994
01995 if angle_deg <= max_angle_rect_deg:
01996 distance_to_rect = ((tot_dist/x_dist) * vw_size_w/2.0)
01997
01998
01999 my_distance = tot_dist/distance_to_rect
02000
02001
02002 if my_distance > greatest_distance:
02003 zoom = 100.0 / my_distance
02004 greatest_distance = my_distance
02005
02006 if (obj_end_x > vw_end_x - 20):
02007 x_dist = fabs(center_x - obj_end_x)
02008 if (obj_end_y > center_y):
02009 y_dist = fabs(center_y - obj_end_y)
02010 else:
02011 y_dist = fabs(center_y - obj_start_y)
02012
02013 tot_dist = (x_dist*x_dist + y_dist*y_dist)**(0.5)
02014
02015 if x_dist == 0.0:
02016 x_dist = 0.001
02017
02018 angle = y_dist/x_dist
02019 angle_deg = degrees(atan(angle))
02020
02021
02022 if angle_deg <= max_angle_rect_deg:
02023 distance_to_rect = ((tot_dist/x_dist) * vw_size_w/2.0)
02024 my_distance = (tot_dist+30.0)/distance_to_rect
02025
02026 if my_distance > greatest_distance:
02027 zoom = 100.0 / my_distance
02028 greatest_distance = my_distance
02029
02030 if obj_end_y > vw_end_y:
02031 if (obj_end_x > center_x):
02032 x_dist = fabs(center_x - obj_end_x)
02033 else:
02034 x_dist = fabs(center_x - obj_start_x)
02035
02036 y_dist = fabs(center_y - obj_end_y)
02037
02038 tot_dist = (x_dist*x_dist + y_dist*y_dist)**(0.5)
02039
02040 if y_dist == 0.0:
02041 y_dist = 0.001
02042
02043 angle = x_dist/y_dist
02044 angle_deg = degrees(atan(angle))
02045
02046
02047 if angle_deg <= max_angle_rect_deg:
02048 distance_to_rect = ((tot_dist/y_dist) * vw_size_h/2.0)
02049 my_distance = tot_dist/distance_to_rect
02050
02051 if my_distance > greatest_distance:
02052 zoom = 100.0 / my_distance
02053 greatest_distance = my_distance
02054
02055 if obj_start_y > vw_start_y:
02056 if (obj_start_x < center_x):
02057 x_dist = fabs(center_x - obj_start_x)
02058 else:
02059 x_dist = fabs(center_x - obj_end_x)
02060
02061 y_dist = fabs(center_y - obj_start_y)
02062
02063 tot_dist = (x_dist*x_dist + y_dist*y_dist)**(0.5)
02064
02065 if y_dist == 0.0:
02066 y_dist = 0.001
02067
02068 angle = x_dist/y_dist
02069 angle_deg = degrees(atan(angle))
02070
02071
02072 if angle_deg <= max_angle_rect_deg:
02073 distance_to_rect = ((tot_dist/y_dist) * vw_size_h/2.0)
02074 my_distance = tot_dist/distance_to_rect
02075
02076 if my_distance > greatest_distance:
02077 zoom = 100.0 / my_distance
02078 greatest_distance = my_distance
02079
02080 zoom = (GetZoomFactor() * (zoom/100.0))
02081
02082
02083 if zoom < 25:
02084 zoom = 25
02085 else:
02086 zoom = int(zoom)
02087
02088 if zoom < GetZoomFactor():
02089 self.main.OnZoom(zoom,False)
02090
02091
02092 def paintevent(self, event):
02093 pass
02094
02095
02096
02097
02098
02099 def DoSelectAll(self, event):
02100 self.SelectAll()
02101
02102
02103
02104
02105
02106
02107 def DoCloneHelper(self,addtoundo=True):
02108 if len(self.selection) == 0:
02109 self.main.ShowError("Cannot clone a device and/or link because nothing is seleted.",ERR_ERROR)
02110 return False
02111
02112
02113 nrofduplicates, valid = QInputDialog.getInteger("Nr. of clones","How many clones do you want to make?",1,1,10000,1,self)
02114 if not valid:
02115 return False
02116 res = self.DoClone(addtoundo,nrofduplicates)
02117 self.canvas.update()
02118
02119
02120
02121
02122
02123
02124
02125
02126
02127 def DoClone(self,addtoundo=True,nrofclones=1):
02128
02129 if len(self.selection) == 0:
02130 self.main.ShowError("Cannot clone a device and/or link because nothing is seleted.",ERR_ERROR)
02131 return False
02132
02133 if addtoundo:
02134 undoindex = self.main.GetFirstNewUndoIndex()
02135
02136 if len(self.selection) == 0:
02137 return False
02138 elif len(self.selection) == 1 and self.selection[0].GetType() == obj_NODE:
02139 descr = "clone device: " + str(self.selection[0].GetName()) + " " + str(nrofclones) + " times"
02140 elif len(self.selection) == 1 and self.selection[0].GetType() == obj_LINK:
02141 descr = "clone link: " + str(self.selection[0].GetName()) + " " + str(nrofclones) + " times"
02142 else:
02143 descr = "clone objects " + str(nrofclones) + " times"
02144
02145 selecteditems = []
02146 for node in self.selection:
02147 selecteditems.append(node.GetObject().GetName())
02148
02149
02150 to_links = {}
02151 from_links = {}
02152 free_links_available_to = {}
02153 free_links_to_arr = []
02154 free_links_available_from = {}
02155 free_links_from_arr = []
02156
02157 sorted_links = {}
02158 port_reference = {}
02159 for itm in self.selection:
02160
02161 if itm.GetType() == obj_LINK:
02162 if itm.GetObject().GetNodeTo() not in selecteditems:
02163 is_in_hash = False
02164 if itm.GetObject().GetNodeTo() in free_links_to_arr:
02165 inp = free_links_available_to[itm.GetObject().GetNodeTo()]
02166 is_in_hash = True
02167 else:
02168 dev_tmp = Device(itm.GetObject().GetSystem(),itm.GetObject().GetNodeTo(),False)
02169 totp,inp_arr,outp_arr = dev_tmp.GetFreePorts()
02170 if totp == False and inp_arr == False and outp_arr == False:
02171 self.main.ShowError("Cannot clone device: " + str(dev_tmp.GetName()) + ": " + str(dev_tmp.GetErrorMessage()),ERR_ERROR)
02172 return False
02173
02174 inp = len(inp_arr)
02175 outp = len(outp_arr)
02176
02177 if inp < nrofclones:
02178 self.main.ShowError("Can not clone " + str(itm.GetObject().GetName()) + " because there are not enough free input ports on the device " + str(dev_tmp.GetName()) + ".",ERR_ERROR)
02179 return False
02180
02181 if is_in_hash:
02182 free_links_available_to[itm.GetObject().GetNodeTo()] -= nrofclones
02183 else:
02184 free_links_to_arr.append(itm.GetObject().GetNodeTo())
02185 free_links_available_to[itm.GetObject().GetNodeTo()] = inp-nrofclones
02186
02187 to_links[itm.GetObject().GetName()] = dev_tmp
02188
02189 if itm.GetObject().GetNodeFrom() not in selecteditems:
02190 is_in_hash = False
02191 if itm.GetObject().GetNodeFrom() in free_links_from_arr:
02192 outp = free_links_available_from[itm.GetObject().GetNodeFrom()]
02193 is_in_hash = True
02194 else:
02195 dev_tmp = Device(itm.GetObject().GetSystem(),itm.GetObject().GetNodeFrom(),False)
02196 totp,inp_arr,outp_arr = dev_tmp.GetFreePorts()
02197 inp = len(inp_arr)
02198 outp = len(outp_arr)
02199
02200 if outp < nrofclones:
02201 self.main.ShowError("Can not clone " + str(itm.GetObject().GetName()) + " because there are not enough free output ports on the device " + str(dev_tmp.GetName()) + ".",ERR_ERROR)
02202 return False
02203
02204 if is_in_hash:
02205 free_links_available_from[itm.GetObject().GetNodeFrom()] -= nrofclones
02206 else:
02207 free_links_from_arr.append(itm.GetObject().GetNodeFrom())
02208 free_links_available_from[itm.GetObject().GetNodeFrom()] = outp-nrofclones
02209
02210 from_links[itm.GetObject().GetName()] = dev_tmp
02211
02212 porttype = itm.GetObject().GetPortTypeFrom()
02213 try:
02214 portnbr = int(itm.GetObject().GetPortFrom())
02215 except ValueError,err:
02216 portnbr = itm.GetObject().GetPortFrom()
02217
02218 if porttype not in sorted_links.keys():
02219 sorted_links[porttype] = []
02220
02221 sorted_links[porttype].append(portnbr)
02222 port_reference[porttype+str(portnbr)] = itm
02223
02224 from_links_copy = []
02225 to_links_copy = []
02226 for porttype in sorted_links:
02227 sorted_links[porttype].sort()
02228 for port in sorted_links[porttype]:
02229 linkname = port_reference[porttype+str(port)].GetObject().GetName()
02230
02231 if linkname in from_links:
02232 from_links_copy.append([linkname,from_links[linkname]])
02233 if linkname in to_links:
02234 to_links_copy.append([linkname,to_links[linkname]])
02235
02236 pendinglinks = []
02237 i = 0
02238 while i < nrofclones:
02239 prev_sel = self.selection[:]
02240 duplicateoriginlist = self.DoDuplicate(False,1,True,False)
02241
02242
02243
02244
02245
02246
02247
02248
02249
02250 def sortmyports(portlist,orgporttype):
02251
02252 ports = {}
02253
02254 for port in portlist:
02255 tmp_port = port.split(":")
02256 porttype = tmp_port[1].strip()
02257
02258 if porttype not in ports.keys():
02259 ports[porttype] = []
02260
02261 try:
02262 portnbr = int(tmp_port[0].strip())
02263 except ValueError,err:
02264 portnbr = tmp_port[0].strip()
02265
02266 ports[porttype].append(portnbr)
02267
02268 for porttype in ports:
02269 ports[porttype].sort()
02270 if orgporttype not in ports.keys():
02271 return "-1"
02272 else:
02273 return str(ports[orgporttype].pop(0))
02274
02275
02276
02277
02278 for tolink in to_links_copy:
02279
02280 totalp,inp,outp = tolink[1].GetSavedFreePorts()
02281 if len(inp) <= 0:
02282 self.main.ShowError("Could not clone " + str(duplicateoriginlist[tolink[0]].GetName()) + " to " + str(tolink[1].GetName()) + " because there are not enough free ports.",ERR_ERROR)
02283
02284 else:
02285
02286 tmp_link = duplicateoriginlist[tolink[0]]
02287 if not tmp_link:
02288 return False
02289
02290 if tmp_link not in pendinglinks:
02291 pendinglinks.append(tmp_link)
02292
02293 if tmp_link.GetNodeTo() not in selecteditems:
02294
02295 newportnbr = sortmyports(inp,tmp_link.GetPortTypeTo())
02296 if newportnbr == "-1":
02297 self.main.ShowError("Could not clone " + str(duplicateoriginlist[tolink[0]].GetName()) + " to " + str(tolink[1].GetName()) + " because there are not any ports left with the same port type.",ERR_ERROR)
02298 continue
02299 else:
02300 inp.remove(newportnbr + " : " + tmp_link.GetPortTypeTo())
02301 else:
02302 newportnbr = tmp_link.GetPortTo()
02303
02304 tmp_link.SetNodeTo(tolink[1].GetName())
02305 tmp_link.SetPortTo(newportnbr)
02306 tmp_link.SetPortTypeTo(tmp_link.GetPortTypeTo())
02307
02308
02309
02310 self.DoDeleteObj(tmp_link.GetName(),False,refresh=False)
02311 self.CreateConnectedLink(tmp_link)
02312
02313 for fromlink in from_links_copy:
02314
02315 totalp,inp,outp = fromlink[1].GetSavedFreePorts()
02316 if len(outp) <= 0:
02317 self.main.ShowError("Could not clone " + str(duplicateoriginlist[fromlink[0]].GetName()) + " from " + str(fromlink[1].GetName()) + " because there are not enough free ports.",ERR_ERROR)
02318
02319 else:
02320
02321 tmp_link = duplicateoriginlist[fromlink[0]]
02322 if not tmp_link:
02323
02324 return False
02325
02326 if tmp_link not in pendinglinks:
02327 pendinglinks.append(tmp_link)
02328
02329 if tmp_link.GetNodeFrom() not in selecteditems:
02330 newportnbr = sortmyports(outp,tmp_link.GetPortTypeFrom())
02331 if newportnbr == "-1":
02332 self.main.ShowError("Could not clone " + str(duplicateoriginlist[fromlink[0]].GetName()) + " from " + str(fromlink[1].GetName()) + " because there are not any ports left with the same port type.",ERR_ERROR)
02333 continue
02334 else:
02335 outp.remove(newportnbr + " : " + tmp_link.GetPortTypeFrom())
02336 else:
02337 newportnbr = tmp_link.GetPortFrom()
02338
02339 tmp_link.SetNodeFrom(fromlink[1].GetName())
02340 tmp_link.SetPortFrom(newportnbr)
02341 tmp_link.SetPortTypeFrom(tmp_link.GetPortTypeFrom())
02342
02343 self.DoDeleteObj(tmp_link.GetName(),False,refresh=False)
02344 self.CreateConnectedLink(tmp_link)
02345
02346
02347
02348 for link in pendinglinks:
02349 link.Create()
02350
02351 pendinglinks = []
02352
02353 self.selection = prev_sel
02354 i += 1
02355
02356
02357
02358 if addtoundo:
02359
02360
02361 pass
02362
02363
02364
02365
02366
02367
02368
02369
02370
02371 def DoDuplicateHelper(self,addtoundo=True):
02372
02373 if len(self.selection) == 0:
02374 self.main.ShowError("Cannot duplicate a device and/or link because nothing is seleted.",ERR_ERROR)
02375 return False
02376
02377 nrofduplicates = wxGetNumberFromUser("How many duplicates do you want to make?","Nr. of duplicates","Nr. of duplicates",1,1,10000,self.main)
02378 if nrofduplicates == -1:
02379 self.main.ShowError("Invalid number of duplicates.",ERR_ERROR)
02380 return False
02381 teststr = str(nrofduplicates)
02382 if teststr.find(".",0,len(teststr)) != -1:
02383 self.main.ShowError("Float number is not supported.",ERR_ERROR)
02384 return False
02385
02386 res = self.DoDuplicate(addtoundo,int(nrofduplicates),False,False)
02387 self.paintevent(None)
02388
02389
02390
02391
02392
02393
02394
02395
02396
02397
02398
02399
02400 def DoDuplicate(self,addtoundo=True,nrofduplicates=1,fromclone=False,paintevent=True):
02401
02402 duplicateoriginlist = {}
02403
02404 if len(self.selection) == 0:
02405 self.main.ShowError("Cannot duplicate a device and/or link because nothing is seleted.",ERR_ERROR)
02406 return False
02407
02408
02409 if not fromclone:
02410 devnames = []
02411 links = []
02412 for myitm in self.selection:
02413 if obj_NODE == myitm.GetType():
02414 devnames.append(myitm.GetName())
02415 elif obj_LINK == myitm.GetType():
02416 links.append(myitm.GetObject())
02417
02418 for link in links:
02419 if not (link.GetNodeTo() in devnames and link.GetNodeFrom() in devnames):
02420 self.main.ShowError("Cannot duplicate the link " + str(link.GetName()) + " because it has to be connected to two devices after duplication.",ERR_ERROR)
02421 return False
02422
02423 if addtoundo:
02424 undoindex = self.main.GetFirstNewUndoIndex()
02425
02426
02427
02428 i = 0
02429 while i < nrofduplicates:
02430 duplicatelist = {}
02431 tmplinks = []
02432 prev_portListIn = {}
02433 prev_portListOut = {}
02434 objs = []
02435
02436
02437 for obj in self.selection:
02438 type = obj.GetType()
02439 if type == obj_NODE:
02440
02441 newObj = Box(self.canvas,type)
02442 newObj.SetData(obj.GetData())
02443 prev_portListIn[obj.GetName()] = obj.GetPortList(port_IN)[:]
02444 prev_portListOut[obj.GetName()] = obj.GetPortList(port_OUT)[:]
02445
02446 tmp_obj = newObj.GetObject()
02447
02448 devid = self.main.GetNextDeviceID(obj.GetObject().GetSystem(),obj.GetObject().GetType())
02449 if devid == False:
02450
02451 devid = ((str(self.main.GetActiveSystem()) + "_tmp_device_" + str(self.main.GetNewLinkNr())).upper())
02452
02453 tmp_obj.SetName(devid)
02454
02455
02456 old_ports = obj.GetObject().GetPorts()
02457 for tmp_oldport in old_ports:
02458 tmp_newport = Port(tmp_oldport.GetSystem(),tmp_oldport.GetPortNbr(),tmp_oldport.GetPortType(),tmp_oldport.GetPortWay(),tmp_oldport.GetDevice(),True)
02459 tmp_newport.SetDict(tmp_oldport.GetObjectInfo(True))
02460 tmp_newport.SetDevice(tmp_obj.GetName())
02461 tmp_newport.SetPortID(-1)
02462
02463
02464 tmp_newport.Create()
02465
02466
02467 try:
02468 pass
02469
02470 except KeyError,err:
02471
02472 pass
02473
02474 tmp_obj.SetID("")
02475 tmp_obj.Create()
02476 newObj.SetText(tmp_obj.GetName())
02477
02478 duplicateoriginlist[obj.GetObject().GetName()] = devid
02479 newObj.ClearPorts()
02480 objs.append(newObj)
02481 duplicatelist[obj.GetObject().GetName()] = tmp_obj.GetName()
02482
02483 newObj.show()
02484 elif obj_LINK == type:
02485 tmplinks.append(obj)
02486
02487 for link in tmplinks:
02488 newObj = myLine(self.canvas,type)
02489 newObj.SetData(link.GetData())
02490
02491 newObj.SetPortIndexFrom(link.GetPortIndexFrom())
02492 newObj.SetPortIndexTo(link.GetPortIndexTo())
02493
02494 tmp_obj = newObj.GetObject()
02495 tmp_obj.SetName(("tmp_link_" + str(self.main.GetNewLinkNr())).upper())
02496 if fromclone:
02497 if link.GetObject().GetNodeTo() not in duplicatelist:
02498 tmp_obj.SetNodeTo(link.GetObject().GetNodeTo())
02499 else:
02500 tmp_obj.SetNodeTo(duplicatelist[link.GetObject().GetNodeTo()])
02501
02502 if link.GetObject().GetNodeFrom() not in duplicatelist:
02503 tmp_obj.SetNodeFrom(link.GetObject().GetNodeFrom())
02504 else:
02505 tmp_obj.SetNodeFrom(duplicatelist[link.GetObject().GetNodeFrom()])
02506
02507 tmp_obj.SetPortFrom(link.GetObject().GetPortFrom())
02508 tmp_obj.SetPortTypeFrom(link.GetObject().GetPortTypeFrom())
02509 tmp_obj.SetPortTo(link.GetObject().GetPortTo())
02510 tmp_obj.SetPortTypeTo(link.GetObject().GetPortTypeTo())
02511 newObj.SetFixed(False)
02512
02513 if not fromclone:
02514 tmp_obj.Create()
02515 newObj.SetText(tmp_obj.GetName())
02516 newObj.SetFixed(True)
02517
02518 duplicateoriginlist[link.GetObject().GetName()] = tmp_obj
02519
02520 if not fromclone:
02521 if link.GetObject().GetNodeTo() not in duplicatelist:
02522 tmp_obj.SetNodeTo("-1")
02523 tmp_obj.SetPortTo("-1")
02524 tmp_obj.SetPortTypeTo("")
02525 newObj.SetFixed(False)
02526 else:
02527 tmp_obj.SetNodeTo(duplicatelist[link.GetObject().GetNodeTo()])
02528
02529 if link.GetObject().GetNodeFrom() not in duplicatelist:
02530 tmp_obj.SetNodeFrom("-1")
02531 tmp_obj.SetPortFrom("-1")
02532 tmp_obj.SetPortTypeFrom("")
02533 newObj.SetFixed(False)
02534 else:
02535 tmp_obj.SetNodeFrom(duplicatelist[link.GetObject().GetNodeFrom()])
02536
02537 objs.append(newObj)
02538
02539 duplicatelist[link.GetObject().GetName()] = tmp_obj.GetName()
02540
02541 newObj.show()
02542
02543 visWindow.contents = objs + visWindow.contents
02544
02545
02546 for itm in prev_portListIn:
02547 tmp_inlist = []
02548 for port in prev_portListIn[itm]:
02549 if port != None:
02550 if port.GetObject().GetName() not in duplicatelist:
02551 tmp_inlist.append(None)
02552 else:
02553 tmp_inlist.append(self.FindByName(duplicatelist[port.GetObject().GetName()]))
02554
02555 else:
02556 tmp_inlist.append(None)
02557
02558 self.FindByName(duplicatelist[itm]).SetPortList(port_IN,tmp_inlist[:])
02559
02560 for itm in prev_portListOut:
02561 tmp_outlist = []
02562 for port in prev_portListOut[itm]:
02563 if port != None:
02564 if port.GetObject().GetName() not in duplicatelist:
02565 tmp_outlist.append(None)
02566 else:
02567 tmp_outlist.append(self.FindByName(duplicatelist[port.GetObject().GetName()]))
02568 else:
02569 tmp_outlist.append(None)
02570 self.FindByName(duplicatelist[itm]).SetPortList(port_OUT,tmp_outlist[:])
02571
02572
02573 tmp_sel = self.selection[:]
02574 self.selection = []
02575
02576 self.SelectMany(objs,refresh=False)
02577 tab = [-1,1]
02578 tabentry = i%2
02579
02580 self._MoveObject(-100,tab[tabentry]*i*10)
02581
02582 self.selection = tmp_sel
02583
02584 i += 1
02585
02586
02587 if addtoundo:
02588
02589
02590 if len(self.selection) == 0:
02591 return False
02592 elif len(self.selection) == 1 and self.selection[0].GetType() == obj_NODE:
02593 descr = "duplicate device: " + str(tmp_obj.GetName()) + " " + str(nrofduplicates) + " times"
02594 elif len(self.selection) == 1 and self.selection[0].GetType() == obj_LINK:
02595 descr = "duplicate link: " + str(tmp_obj.GetName()) + " " + str(nrofduplicates) + " times"
02596 else:
02597 descr = "duplicate objects " + str(nrofduplicates) + " times"
02598
02599 self.main.AddToUndo(undoindex,descr)
02600
02601
02602 if paintevent:
02603 self.paintevent(None)
02604
02605 return duplicateoriginlist
02606
02607
02608
02609
02610
02611
02612
02613
02614
02615
02616 def DoDelete(self, event=None,addtoundo=True,refresh=True):
02617
02618
02619 ans = QMessageBox.question(self,"Delete ?","Are you sur you want to delete this Device.","Yes","No","",0,1)
02620 if ans == 1:
02621 return
02622 if addtoundo:
02623 for obj in self.selection:
02624 if obj.GetType() == obj_NODE:
02625 test_obj = Device(self.main.GetActiveSystem(),obj.GetObject().GetName(),True)
02626 success = test_obj.Update(True)
02627
02628 if success != False and success != {}:
02629 self.main.ShowError("Cannot delete the objects in the selection because at least one of the devices (" + str(obj.GetObject().GetName()) + "), is already stored in the database.",ERR_ERROR)
02630 return False
02631
02632
02633 if addtoundo:
02634
02635
02636 if len(self.selection) == 0:
02637 self.main.ShowError("Cannot delete a device and/or link because nothing is seleted.",ERR_ERROR)
02638 return False
02639 elif len(self.selection) == 1 and self.selection[0].GetType() == obj_NODE:
02640 descr = "delete device: " + str(self.selection[0].GetName())
02641 elif len(self.selection) == 1 and self.selection[0].GetType() == obj_LINK:
02642 descr = "delete link: " + str(self.selection[0].GetName())
02643 else:
02644 descr = "delete objects"
02645
02646 undoindex = self.main.GetFirstNewUndoIndex()
02647
02648
02649 addlast = []
02650 addfirst = []
02651 i = len(self.selection) - 1
02652 while i >= 0:
02653 if self.selection[i].GetType() == obj_NODE:
02654 addlast.append(self.selection.pop(i))
02655 else:
02656 addfirst.append(self.selection.pop(i))
02657 i -= 1
02658
02659
02660 my_list = addfirst + addlast
02661 tmp_obj = None
02662 for obj in my_list:
02663
02664 if addtoundo:
02665 if obj.GetType() == obj_NODE:
02666 isfree = obj.GetObject().IsAllPortsFree()
02667 else:
02668 isfree = True
02669
02670 if isfree == False:
02671 self.main.ShowError("An error occured while trying to fetch port information for " + str(obj.GetObject().GetName()) + ". Could not delete object.",ERR_ERROR)
02672 addlast.remove(obj)
02673 continue
02674 elif isfree == -1:
02675 self.main.ShowError("Could not delete the device " + str(obj.GetObject().GetName()) + ", because it is connected to a non-deleted link. To delete this device, first delete the link, then the device.",ERR_ERROR)
02676 addlast.remove(obj)
02677 continue
02678
02679 if addtoundo:
02680 try:
02681
02682 my_id = self.main.GetSelectWindow().GetItemID(obj.GetName())
02683 self.main.GetSelectWindow().RemoveItem(my_id,obj.GetObject().GetType())
02684 self.main.GetSelectWindow().SetItemToDeleted(obj.GetName())
02685 except KeyError,err:
02686 print "treeitemid not found"
02687
02688
02689 if addtoundo:
02690 if not self.main.FindDirtyObject(obj.GetName(),obj.GetObject().__class__):
02691 obj.GetObject().Delete()
02692
02693 else:
02694
02695 if obj.GetType() == obj_NODE:
02696 tmp_obj = Device(self.main.GetActiveSystem(),obj.GetObject().GetName(),False)
02697 elif obj.GetType() == obj_LINK:
02698 tmp_obj = Link(self.main.GetActiveSystem(),obj.GetObject().GetName(),False,False)
02699
02700 tmp_obj.Delete()
02701
02702
02703 if obj.GetType() == obj_LINK:
02704 tmp_nodefrom = obj.GetObject().GetNodeFrom()
02705 tmp_nodeto = obj.GetObject().GetNodeTo()
02706
02707 tmp_visnodefrom = self.FindByName(tmp_nodefrom)
02708 tmp_visnodeto = self.FindByName(tmp_nodeto)
02709
02710 if tmp_visnodefrom != None and tmp_visnodefrom not in addlast:
02711 tmp_portoutlist = tmp_visnodefrom.GetPortList(port_OUT)
02712 i = 0
02713 for port in tmp_portoutlist:
02714 if port == obj:
02715 tmp_portoutlist[i] = None
02716 break
02717 i += 1
02718 else:
02719
02720 pass
02721
02722 if tmp_visnodeto != None and tmp_visnodeto not in addlast:
02723 tmp_portinlist = tmp_visnodeto.GetPortList(port_IN)
02724 i = 0
02725 for port in tmp_portinlist:
02726 if port == obj:
02727 tmp_portinlist[i] = None
02728 break
02729 i += 1
02730
02731 else:
02732
02733 pass
02734
02735 visWindow.contents.remove(obj)
02736
02737 elif obj.GetType() == obj_NODE:
02738
02739 if addtoundo:
02740 my_ports = obj.GetObject().GetPorts()
02741 if my_ports == False:
02742 self.main.ShowError("An error occured while retrieving port data from the ConfDB: " + str(obj.GetObject().GetErrorMessage()),ERR_ERROR)
02743 else:
02744 for port in my_ports:
02745 tmp_port = Port(self.main.GetActiveSystem(),port.GetPortNbr(),port.GetPortType(),port.GetPortWay(),port.GetDevice(),False)
02746 tmp_port.Delete()
02747
02748 visWindow.contents.remove(obj)
02749
02750
02751 if obj.GetObject().__class__ == Device and self.main.GetActiveDevice() == obj.GetName():
02752 self.main.SetActiveDevice(None)
02753 elif obj.GetObject().__class__ == Link and self.main.GetActiveLink() == obj.GetName():
02754 self.main.SetActiveLink(None)
02755
02756 if (addfirst + addlast) != [] and addtoundo:
02757 self.main.AddToUndo(undoindex,descr)
02758
02759 self.DeselectAll(refresh=refresh)
02760
02761
02762
02763
02764
02765
02766 def GetContents(self):
02767 return visWindow.contents
02768
02769
02770
02771
02772
02773
02774
02775 def GetSelection(self):
02776 return self.selection
02777
02778
02779
02780
02781
02782
02783
02784
02785
02786
02787 def DoDeleteObj(self, name,addtoundo=True,refresh=True):
02788
02789 self.DeselectAll(refresh=False)
02790 visobj = self.FindByName(name)
02791
02792 if visobj != None:
02793 self.Select(visobj,refresh=False)
02794 else:
02795 return False
02796 self.DoDelete(None,addtoundo,refresh=refresh)
02797
02798
02799
02800
02801
02802
02803
02804
02805
02806 def DoDeleteAll(self, event = None,onlyselected=False,refresh=False):
02807
02808
02809
02810
02811
02812
02813
02814
02815
02816
02817
02818
02819
02820
02821
02822
02823
02824
02825
02826
02827
02828 self.selection = []
02829 self.main.SetActiveDevice(None)
02830 self.main.SetActiveLink(None)
02831 self.pendingItem, self.activeItem, = None, None
02832 self.__moving = None
02833
02834
02835
02836
02837
02838
02839
02840
02841
02842
02843
02844
02845
02846
02847
02848
02849 for item in visWindow.contents:
02850 if isinstance(item,Box) or isinstance(item,myLine):
02851 if item not in self.keptItems:
02852 item.hide()
02853 item._delete()
02854
02855 for item in visWindow.contents:
02856 if item not in self.keptItems:
02857 item.setCanvas(None)
02858 if item in visWindow.contents: visWindow.contents.remove(item)
02859 del item
02860 list = []
02861
02862
02863
02864
02865
02866
02867
02868
02869
02870
02871 print " ############ WARNING : " + str(len(self.canvas.allItems())) + " Items still in the Canvas"
02872 visWindow.contents = []
02873
02874
02875
02876
02877
02878
02879
02880
02881
02882
02883 self.canvas.update()
02884
02885
02886
02887
02888
02889
02890 def DoMoveForward(self, event):
02891 if len(self.selection) != 1: return
02892
02893 obj = self.selection[0]
02894 index = visWindow.contents.index(obj)
02895 if index == 0: return
02896
02897 del visWindow.contents[index]
02898 visWindow.contents.insert(index-1, obj)
02899
02900 self.Refresh()
02901 self._AdjustMenus()
02902
02903
02904
02905
02906
02907
02908
02909 def DoMoveToFront(self, event):
02910 if len(self.selection) != 1: return
02911
02912 obj = self.selection[0]
02913 visWindow.contents.remove(obj)
02914 visWindow.contents.insert(0, obj)
02915
02916 self.Refresh()
02917 self._AdjustMenus()
02918
02919
02920
02921
02922
02923
02924 def DoMoveBackward(self, event):
02925
02926 if len(self.selection) != 1: return
02927
02928 obj = self.selection[0]
02929 index = visWindow.contents.index(obj)
02930 if index == len(visWindow.contents) - 1: return
02931
02932 del visWindow.contents[index]
02933 visWindow.contents.insert(index+1, obj)
02934
02935 self.Refresh()
02936 self._AdjustMenus()
02937
02938
02939
02940
02941
02942
02943
02944 def DoMoveToBack(self, event):
02945 if len(self.selection) != 1: return
02946
02947 obj = self.selection[0]
02948 visWindow.contents.remove(obj)
02949 visWindow.contents.append(obj)
02950
02951 self.Refresh()
02952 self._AdjustMenus()
02953
02954
02955
02956
02957
02958
02959
02960
02961
02962
02963
02964
02965
02966
02967 def CreateNode(self, x, y, obj,refresh=True):
02968 info = obj.GetObjectInfo(True)
02969
02970
02971 pin = info[DEV_PORTS_IN]
02972 pout = info[DEV_PORTS_OUT]
02973 maxp = 0
02974 if pin > pout:
02975 maxp = pin
02976 else:
02977 maxp = pout
02978 if maxp < 5:
02979 maxp = 5
02980 if maxp > 100:
02981 maxp = 100
02982
02983 new_height = NODE_HEIGHT
02984 new_width = NODE_WIDTH * maxp
02985 if maxp >= 20:
02986 new_height += maxp/20/10
02987
02988 obj_tmp = Box(self.canvas,obj_NODE, obj, QPoint(x, y),
02989 QSize(new_width, new_height),
02990 self.penColour,
02991 Color(info[DEV_COLOUR]),
02992 self.lineSize)
02993
02994
02995
02996
02997
02998
02999
03000 visWindow.contents.insert(0, obj_tmp)
03001 obj_tmp.SetText(str(info[DEV_NAME]))
03002 obj_tmp.SetPorts(port_IN, info[DEV_PORTS_IN])
03003 obj_tmp.SetPorts(port_OUT, info[DEV_PORTS_OUT])
03004
03005 if refresh:
03006 self.canvas.update()
03007
03008 return obj_tmp
03009
03010
03011
03012
03013
03014
03015
03016
03017
03018
03019
03020 def CreateLink(self, x1, y1, x2, y2, obj):
03021
03022 topLeftX = min(x1, x2)
03023 topLeftY = min(y1, y2)
03024 botRightX = max(x1, x2)
03025 botRightY = max(y1, y2)
03026
03027
03028 obj_tmp = myLine(self.canvas,obj_LINK, obj, QPoint(topLeftX, topLeftY),
03029 QSize(botRightX-topLeftX, botRightY-topLeftY),
03030 Qt.black,
03031 self.fillColour,
03032 self.lineSize,
03033 QPoint(x1 - topLeftX, y1 - topLeftY),
03034 QPoint(x2 - topLeftX, y2 - topLeftY))
03035
03036
03037
03038
03039
03040
03041
03042
03043
03044 obj_tmp.SetText(str(obj.GetObjectInfo(True)[DEV_LINK]))
03045 visWindow.contents.append(obj_tmp)
03046 return obj_tmp
03047
03048
03049
03050
03051
03052
03053
03054
03055
03056
03057
03058
03059
03060
03061 def CreateConnectedLink(self, object,linkpos=QPoint(0,0),linksize=QSize(0,0),paintevent=False):
03062 deviceInfo = object.GetObjectInfo(True)
03063
03064 obj = self.FindByName(str(deviceInfo[DEV_LINK]))
03065 if not obj:
03066 obj = self.CreateLink(linkpos.x(), linkpos.y(), linkpos.x() + linksize.width(), linkpos.y() + linksize.height(), object)
03067 obj.SetFixed(True)
03068 else:
03069
03070 tmp_nodefrom = obj.GetObject().GetNodeFrom()
03071 tmp_nodeto = obj.GetObject().GetNodeTo()
03072
03073 tmp_visnodefrom = self.FindByName(tmp_nodefrom)
03074 tmp_visnodeto = self.FindByName(tmp_nodeto)
03075
03076 if tmp_visnodefrom != None:
03077 tmp_portoutlist = tmp_visnodefrom.GetPortList(port_OUT)
03078 i = 0
03079 for port in tmp_portoutlist:
03080 if port == obj:
03081 tmp_portoutlist[i] = None
03082 break
03083 i += 1
03084 else:
03085
03086 pass
03087
03088 if tmp_visnodeto != None:
03089 tmp_portinlist = tmp_visnodeto.GetPortList(port_IN)
03090 i = 0
03091 for port in tmp_portinlist:
03092 if port == obj:
03093 tmp_portinlist[i] = None
03094 break
03095 i += 1
03096
03097 else:
03098
03099 pass
03100
03101 connected = False
03102 connected2 = False
03103 node_from = self.FindByName(deviceInfo[DEV_SWITCH_FROM])
03104
03105 if node_from:
03106 deviceindex,portnr = node_from.FindCorrespondingPortIndex(object.GetPortFrom(),object.GetPortTypeFrom(),port_OUT)
03107
03108
03109
03110
03111 obj.SetPortIndexFrom(deviceindex)
03112 if portnr != -1:
03113 connected = obj.SetConnection(node_from,port_OUT,portnr)
03114 if not connected:
03115 self.missing_parentnodes.append([obj.GetName(),deviceInfo[DEV_SWITCH_FROM]])
03116 node_to = self.FindByName(deviceInfo[DEV_SWITCH_TO])
03117 if node_to:
03118 deviceindex,portnr = node_to.FindCorrespondingPortIndex(object.GetPortTo(),object.GetPortTypeTo(),port_IN)
03119 obj.SetPortIndexTo(deviceindex)
03120 if portnr != -1:
03121 connected2 = obj.SetConnection(node_to,port_IN,portnr)
03122 if not connected2:
03123 self.missing_parentnodes.append([obj.GetName(),deviceInfo[DEV_SWITCH_FROM]])
03124
03125 obj.SetText(str(deviceInfo[DEV_LINK]))
03126
03127 obj.to_delta = obj.nodeTo.GetPortPosition(obj.portindexto,port_IN).x() - obj.nodeTo.x()
03128 obj.from_delta = obj.nodeFrom.GetPortPosition(obj.portindexfrom,port_OUT).x() - obj.nodeFrom.x()
03129 obj.show()
03130 if paintevent:
03131 self.paintevent(None)
03132 return obj
03133
03134
03135
03136
03137
03138
03139
03140
03141
03142
03143
03144 def SelectAll(self,refresh=True):
03145
03146 self.selection = []
03147 for obj in visWindow.contents:
03148 self.selection.append(obj)
03149
03150 if refresh:
03151 self.Refresh()
03152
03153 self._AdjustMenus()
03154 self.main.GetSelectWindow().UpdateInfo(None)
03155
03156
03157
03158
03159
03160
03161
03162 def DeselectAll(self,refresh=True):
03163
03164 self.selection = []
03165
03166 if refresh:
03167 pass
03168
03169 self._AdjustMenus()
03170 self.main.GetSelectWindow().UpdateInfo(None)
03171
03172
03173
03174
03175
03176
03177
03178 def Deselect(self,obj,refresh=True):
03179
03180 tmp_name = obj.GetObject().GetName()
03181 i = 0
03182 for item in self.selection:
03183 if item.GetObject().GetName() == tmp_name:
03184 self.selection.pop(i)
03185 i += 1
03186
03187 if refresh:
03188 self.Refresh()
03189
03190 self._AdjustMenus()
03191
03192
03193
03194
03195
03196
03197
03198
03199
03200
03201
03202
03203
03204 def Select(self, obj,add=False,showinfo=True,refresh=True):
03205 if add == False:
03206 self.selection = [obj]
03207 else:
03208 self.selection.append(obj)
03209
03210
03211 self._AdjustMenus()
03212 if obj.GetType() in [obj_NODE]:
03213
03214 self.main.SetActiveDeviceType(obj.GetObject().GetObjectInfo(True)[DEV_TYPE])
03215 self.main.SetActiveDevice(obj.GetObject().GetName())
03216 self.main.SetActiveLinkType(None)
03217 self.main.SetActiveLink(None)
03218
03219 if showinfo:
03220 if add == False:
03221 self.main.GetSelectWindow().UpdateInfo(obj.GetObject())
03222 else:
03223 self.main.GetSelectWindow().UpdateInfo(None)
03224
03225 elif obj.GetType() in [obj_LINK]:
03226 self.main.SetActiveLinkType(obj.GetObject().GetType())
03227 self.main.SetActiveLink(obj.GetObject().GetName())
03228 self.main.SetActiveDeviceType(None)
03229 self.main.SetActiveDevice(None)
03230 if showinfo:
03231 if add == False:
03232 self.main.GetSelectWindow().UpdateInfo(obj.GetObject())
03233 else:
03234 self.main.GetSelectWindow().UpdateInfo(None)
03235
03236 if refresh:
03237 self.Refresh()
03238
03239
03240
03241
03242
03243
03244
03245
03246 def SelectMany(self, objs,refresh=True):
03247 self.selection = objs
03248
03249 if refresh:
03250 self.Refresh()
03251
03252 self._AdjustMenus()
03253 self.main.GetSelectWindow().UpdateInfo(None)
03254
03255
03256
03257
03258
03259
03260
03261
03262
03263
03264
03265 def SelectByRectangle(self, x, y, width, height):
03266
03267 self.selection = []
03268 for obj in visWindow.contents:
03269 if obj.ObjectWithinRect(x, y, width, height):
03270 self.selection.append(obj)
03271 self.Refresh()
03272 self._AdjustMenus()
03273 if len(self.selection) == 1:
03274 if obj.GetType() in [obj_NODE, obj_LINK]:
03275 self.main.GetSelectWindow().UpdateInfo(obj.GetObject())
03276 else:
03277 self.main.GetSelectWindow().UpdateInfo(None)
03278
03279
03280
03281
03282
03283
03284
03285
03286
03287
03288 def FindByName(self, name):
03289 for obj in visWindow.contents:
03290 if obj.GetName() == name:
03291 return obj
03292 return None
03293
03294
03295
03296
03297
03298
03299
03300
03301
03302 def _AdjustMenus(self):
03303 selection = len(self.selection) > 0
03304 onlyOne = len(self.selection) == 1
03305 isText = onlyOne and (self.selection[0].GetType() == obj_TEXT)
03306 front = onlyOne and (self.selection[0] == visWindow.contents[0])
03307 back = onlyOne and (self.selection[0] == visWindow.contents[-1])
03308
03309
03310
03311
03312
03313
03314 def SetCurrentTool(self, newTool):
03315 if self.curTool == newTool: return
03316 self.curTool = newTool
03317
03318
03319
03320
03321
03322
03323
03324
03325 def _SetPenColour(self, colour,refresh=True):
03326 if len(self.selection) > 0:
03327 for obj in self.selection:
03328 obj.SetPenColour(colour)
03329
03330 if refresh:
03331 self.Refresh()
03332 else:
03333 self.penColour = colour
03334 self.optionIndicator.setPenColour(colour)
03335
03336
03337
03338
03339
03340
03341
03342
03343 def _SetFillColour(self, colour, refresh=True):
03344 if len(self.selection) > 0:
03345 for obj in self.selection:
03346 obj.SetFillColour(colour)
03347 if refresh:
03348 self.Refresh()
03349 else:
03350 self.fillColour = colour
03351 self.optionIndicator.SetFillColour(colour)
03352
03353
03354
03355
03356
03357
03358
03359
03360 def _SetLineSize(self, size,refresh=True):
03361 if len(self.selection) > 0:
03362 for obj in self.selection:
03363 obj.SetLineSize(size)
03364 if refresh:
03365 self.Refresh()
03366 else:
03367 self.lineSize = size
03368 self.optionIndicator.SetLineSize(size)
03369
03370
03371
03372
03373
03374
03375
03376
03377
03378
03379
03380
03381
03382
03383
03384 def _ResizeObject(self, obj, anchorPt, oldPt, newPt,refresh=True):
03385 if obj.GetType() == obj_TEXT:
03386
03387 wxBell()
03388 return
03389
03390 if obj.IsFixed():
03391
03392 return
03393
03394 topLeft = QPoint(min(anchorPt.x, newPt.x),
03395 min(anchorPt.y, newPt.y))
03396 botRight = QPoint(max(anchorPt.x, newPt.x),
03397 max(anchorPt.y, newPt.y))
03398
03399 newWidth = botRight.x - topLeft.x
03400 newHeight = botRight.y - topLeft.y
03401
03402 if obj.GetType() in [obj_LINE, obj_LINK]:
03403
03404
03405
03406 startPt = obj.GetStartPt()
03407 endPt = obj.GetEndPt()
03408
03409 slopesDown = ((startPt.x < endPt.x) and (startPt.y < endPt.y)) or \
03410 ((startPt.x > endPt.x) and (startPt.y > endPt.y))
03411
03412
03413
03414 hFlip = ((anchorPt.x < oldPt.x) and (anchorPt.x > newPt.x)) or \
03415 ((anchorPt.x > oldPt.x) and (anchorPt.x < newPt.x))
03416 vFlip = ((anchorPt.y < oldPt.y) and (anchorPt.y > newPt.y)) or \
03417 ((anchorPt.y > oldPt.y) and (anchorPt.y < newPt.y))
03418
03419 if (hFlip and not vFlip) or (vFlip and not hFlip):
03420 slopesDown = not slopesDown
03421
03422 if slopesDown:
03423 obj.SetStartPt(QPoint(0, 0))
03424 obj.SetEndPt(QPoint(newWidth, newHeight))
03425 else:
03426 obj.SetStartPt(QPoint(0, newHeight))
03427 obj.SetEndPt(QPoint(newWidth, 0))
03428
03429
03430
03431 obj.SetPosition(topLeft)
03432 obj.SetSize(QSize(botRight.x - topLeft.x, botRight.y - topLeft.y))
03433
03434 if refresh:
03435 self.Refresh()
03436
03437
03438
03439
03440
03441
03442
03443
03444
03445 def _MoveObject(self, offsetX, offsetY,refresh=True):
03446
03447 for obj in self.selection:
03448 if not obj.IsFixed():
03449 obj.move(obj.x() + offsetX, obj.y() + offsetY)
03450
03451 if refresh:
03452 self.canvas.update()
03453
03454
03455
03456
03457
03458
03459
03460
03461
03462 def MoveObjectToPos(self, obj, posX, posY,refresh=True):
03463 pos = obj.GetPosition()
03464 pos=QPoint(posX,posY)
03465 obj.SetPosition(pos)
03466
03467 if refresh:
03468 self.Refresh()
03469
03470
03471
03472
03473
03474
03475
03476
03477
03478
03479 def _GetEventCoordinates(self, event):
03480
03481 originX, originY = self.GetViewStart()
03482 unitX, unitY = self.GetScrollPixelsPerUnit()
03483 return QPoint(event.GetX() + (originX * unitX),
03484 event.GetY() + (originY * unitY))
03485
03486
03487
03488
03489
03490
03491
03492
03493
03494
03495
03496
03497
03498
03499
03500
03501
03502 def _GetObjectAndSelectionHandleAt(self, pt):
03503 for obj in self.selection:
03504 handle = obj.GetSelectionHandleContainingPoint(pt.x, pt.y)
03505 if handle != handle_NONE:
03506 return obj, handle
03507
03508 return None, handle_NONE
03509
03510
03511
03512
03513
03514
03515
03516
03517
03518
03519
03520 def _GetObjectAt(self, pt,macrodevice=False):
03521 if macrodevice:
03522 for obj in self.macroobjects:
03523 if obj.ObjectContainsPoint(pt.x,pt.y):
03524 if obj.IsClickable():
03525 return obj
03526 elif not obj.IsClickable():
03527 continue
03528 return None
03529
03530 else:
03531 for obj in visWindow.contents:
03532 if obj.ObjectContainsPoint(pt.x, pt.y):
03533 return obj
03534 return None
03535
03536
03537
03538
03539
03540
03541
03542
03543
03544
03545
03546
03547
03548
03549
03550 def _DrawObjectOutline(self, offsetX, offsetY):
03551
03552 if len(self.selection) == 0: return
03553
03554 dc = wxClientDC(self)
03555 self.PrepareDC(dc)
03556 dc.BeginDrawing()
03557 dc.SetPen(Qt.black_DASHED_PEN)
03558 dc.SetBrush(wxTRANSPARENT_BRUSH)
03559 dc.SetLogicalFunction(wxINVERT)
03560
03561
03562 for obj in self.selection:
03563 position = obj.GetPosition()
03564 size = obj.GetSize()
03565 dc.DrawRectangle(position.x + offsetX, position.y + offsetY,
03566 size.width, size.height)
03567
03568 dc.EndDrawing()
03569
03570
03571
03572
03573
03574
03575
03576
03577
03578
03579
03580
03581
03582
03583
03584
03585
03586
03587
03588
03589
03590
03591
03592
03593
03594
03595 def _DrawVisualFeedback(self, startPt, endPt, type, dashedLine):
03596 dc = wxClientDC(self)
03597 self.PrepareDC(dc)
03598 dc.BeginDrawing()
03599 if dashedLine:
03600 dc.SetPen(Qt.black_DASHED_PEN)
03601 else:
03602 dc.SetPen(Qt.black_PEN)
03603 dc.SetBrush(wxTRANSPARENT_BRUSH)
03604 dc.SetLogicalFunction(wxINVERT)
03605
03606 if type == feedback_RECT:
03607 dc.DrawRectangle(startPt.x, startPt.y,
03608 endPt.x - startPt.x,
03609 endPt.y - startPt.y)
03610 elif type == feedback_LINE:
03611 dc.DrawLine(startPt.x, startPt.y, endPt.x, endPt.y)
03612 elif type == feedback_ELLIPSE:
03613 dc.DrawEllipse(startPt.x, startPt.y,
03614 endPt.x - startPt.x,
03615 endPt.y - startPt.y)
03616
03617 dc.EndDrawing()
03618
03619
03620
03621
03622
03623
03624
03625
03626
03627
03628
03629
03630
03631
03632
03633
03634
03635
03636
03637
03638
03639
03640
03641
03642
03643
03644
03645
03646
03647
03648
03649
03650
03651
03652
03653
03654
03655
03656
03657
03658
03659
03660 class DrawingObject:
03661
03662 def __init__(self, canvas, type, object = None, position=QPoint(0, 0), size=QSize(0, 0),
03663 penColour=Qt.black, fillColour=Qt.white, lineSize=1,
03664 text=None, startPt=QPoint(0, 0), endPt=QPoint(0,0),zoomFactor=100,clickable=True,originalsize=QSize(0,0),macroid=-1,textvalign=text_CENTER,texthalign=text_CENTER):
03665 """ Standard constructor.
03666
03667 'type' is the type of object being created. This should be one of
03668 the following constants: obj_NODE, obj_LINK, obj_MACRODEVICE
03669
03670 The remaining parameters is explained above for the class.
03671 """
03672 QCanvasRectangle.__init__(self, canvas)
03673 self.i = 0
03674 self.type = type
03675 self.position = position
03676 self.size = size
03677 self.penColour = penColour
03678 self.fillColour = fillColour
03679 self.lineSize = lineSize
03680 self.startPt = startPt
03681 self.endPt = endPt
03682 self.text = text
03683 self.layer = 0
03684 self.textFont = "Arial"
03685 self.textvalign = textvalign
03686 self.texthalign = texthalign
03687 self.textSize = 10
03688 self.textBoldface = False
03689 self.textItalic = False
03690 self.textUnderline = False
03691 self.canvas = canvas
03692
03693 self.linkLength = 7
03694 self.fixed = False
03695 self.zoomFactor = zoomFactor
03696 if type == obj_LINK:
03697 self.portindexfrom = -1
03698 self.portindexto = -1
03699
03700 self.object = object
03701
03702 self.expanded = 0
03703
03704 self.clickable = clickable
03705
03706 self.originalsize = originalsize
03707 self.macroid = macroid
03708
03709
03710 if self.type == obj_NODE and self.object:
03711 deviceinfo_tmp = self.object.GetObjectInfo(True)
03712 self.portList = {port_IN: [None for i in range(max(deviceinfo_tmp[DEV_PORTS_IN], 0))],
03713 port_OUT: [None for i in range(max(deviceinfo_tmp[DEV_PORTS_OUT], 0))]}
03714 else: self.portList = {port_IN: [], port_OUT: []}
03715
03716
03717
03718
03719
03720
03721
03722
03723
03724
03725
03726
03727
03728 def GetData(self):
03729 return [self.type, self.x(), self.y(),
03730 self.width(), self.height(),
03731 self.penColour.red(),
03732 self.penColour.green(),
03733 self.penColour.blue(),
03734 self.fillColour.red(),
03735 self.fillColour.green(),
03736 self.fillColour.blue(),
03737 self.lineSize,
03738 self.startPt.x(), self.startPt.y(),
03739 self.endPt.x(), self.endPt.y(),
03740 self.text,
03741 self.textFont,
03742 self.textSize,
03743 self.textBoldface,
03744 self.textItalic,
03745 self.textUnderline,
03746 self.linkLength,
03747 self.fixed,
03748 self.object,
03749 self.portList,self.expanded,self.zoomFactor]
03750
03751
03752
03753
03754
03755
03756
03757
03758
03759 def SetData(self, data):
03760
03761 self.type = data[0]
03762
03763 self.position = QPoint(data[1], data[2])
03764 self.size = QSize(data[3], data[4])
03765 self.penColour = QColor(data[5], data[6], data[7])
03766 self.fillColour = QColor(data[8], data[9], data[10])
03767 self.lineSize = data[11]
03768 self.startPt = QPoint(data[12], data[13])
03769 self.endPt = QPoint(data[14], data[15])
03770 self.text = data[16]
03771 self.textFont = data[17]
03772 self.textSize = data[18]
03773 self.textBoldface = data[19]
03774 self.textItalic = data[20]
03775 self.textUnderline = data[21]
03776 self.linkLength = data[22]
03777 self.fixed = data[23]
03778
03779
03780 if self.type == obj_NODE:
03781
03782 self.object = Device(data[24].GetSystem(),data[24].GetName(),False)
03783 elif self.type == obj_LINK:
03784 self.object = Link(data[24].GetSystem(),data[24].GetName(),False,False)
03785
03786 if self.type == obj_NODE:
03787 self.portList = {}
03788 for item in data[25]:
03789 self.portList[item] = data[25][item]
03790
03791 self.expanded = data[26]
03792
03793 self.zoomFactor = data[27]
03794
03795
03796
03797
03798 def GetType(self):
03799 return self.type
03800
03801 def SetLayer(self,layer):
03802 self.layer = layer
03803 def GetLayer(self):
03804 return self.layer
03805
03806
03807
03808
03809
03810
03811
03812
03813 def SetPosition(self, position, adjust = True):
03814 self.position = QPoint(position.x(),position.y())
03815 if self.type == obj_NODE or self.type == obj_LINK:
03816 if adjust: self._AdjustLinks()
03817
03818
03819
03820
03821
03822 def GetPosition(self):
03823 return self.position
03824
03825
03826
03827
03828
03829
03830 def GetPortIndexFrom(self):
03831 return self.portindexfrom
03832
03833
03834
03835
03836
03837
03838 def GetPortIndexTo(self):
03839 return self.portindexto
03840
03841
03842
03843
03844 def SetPortIndexFrom(self,index):
03845 self.portindexfrom = index
03846
03847
03848
03849 def SetPortIndexTo(self,index):
03850 self.portindexto = index
03851
03852
03853
03854
03855 def GetExpanded(self):
03856 return self.expanded
03857
03858
03859
03860
03861
03862
03863
03864 def SetExpanded(self,expanded):
03865 self.expanded = expanded
03866
03867 def SetZoomFactor(self,zf):
03868 self.zoomFactor = zf
03869 def GetZoomFactor(self):
03870 return self.zoomFactor
03871
03872
03873
03874
03875
03876
03877
03878 def SetSize(self, size, adjust = True):
03879 self.size = size
03880 if self.type == obj_NODE or self.type == obj_LINK:
03881 if adjust: self._AdjustLinks()
03882
03883
03884
03885
03886
03887 def GetSize(self):
03888 return self.size
03889
03890
03891
03892
03893
03894 def SetPenColour(self, colour):
03895 self.penColour = colour
03896
03897
03898
03899
03900
03901 def GetPenColour(self):
03902 return self.penColour
03903
03904
03905
03906
03907
03908 def SetFillColour(self, colour):
03909 self.fillColour = colour
03910
03911
03912
03913
03914
03915 def GetFillColour(self):
03916 return self.fillColour
03917
03918
03919
03920
03921
03922 def SetLineSize(self, lineSize):
03923 self.lineSize = lineSize
03924
03925
03926
03927
03928
03929 def GetLineSize(self):
03930 return self.lineSize
03931
03932
03933
03934
03935
03936 def SetStartPt(self, startPt):
03937 self.startPt = startPt
03938
03939
03940
03941
03942
03943 def GetStartPt(self):
03944 return self.startPt
03945
03946
03947
03948
03949
03950 def SetEndPt(self, endPt):
03951 self.endPt = endPt
03952
03953
03954
03955
03956
03957 def GetEndPt(self):
03958 return self.endPt
03959
03960
03961
03962
03963
03964 def SetText(self, text):
03965 self.text = text
03966
03967
03968
03969
03970
03971 def GetText(self):
03972 return self.text
03973
03974
03975
03976
03977
03978 def SetTextFont(self, font):
03979 self.textFont = font
03980
03981
03982
03983
03984
03985 def GetTextFont(self):
03986 return self.textFont
03987
03988
03989
03990
03991
03992 def SetTextSize(self, size):
03993 self.textSize = size
03994
03995
03996
03997
03998
03999 def GetTextSize(self):
04000 return self.textSize
04001
04002
04003
04004
04005
04006 def SetTextBoldface(self, boldface):
04007 self.textBoldface = boldface
04008
04009
04010
04011
04012
04013 def GetTextBoldface(self):
04014 return self.textBoldface
04015
04016
04017
04018
04019
04020 def SetTextItalic(self, italic):
04021 self.textItalic = italic
04022
04023
04024
04025
04026
04027 def GetTextItalic(self):
04028 return self.textItalic
04029
04030
04031
04032
04033
04034 def SetTextUnderline(self, underline):
04035 self.textUnderline = underline
04036
04037
04038
04039
04040
04041 def GetTextUnderline(self):
04042 return self.textUnderline
04043
04044 def IsClickable(self):
04045 return self.clickable
04046 def GetOriginalSize(self):
04047 return self.originalsize
04048 def GetMacroID(self):
04049 return self.macroid
04050 def SetMacroID(self,macroid):
04051 self.macroid = macroid
04052
04053
04054
04055
04056
04057
04058
04059
04060 def SetObject(self, object):
04061 if self.type == obj_NODE:
04062 self.object = Device(object.GetSystem(),object.GetName(),False)
04063 elif self.type == obj_LINK:
04064 self.object = Link(object.GetSystem(),object.GetName(),False,False)
04065
04066
04067
04068
04069
04070 def SetObjectRef(self,object):
04071 self.object = object
04072 self.text = self.object.GetName()
04073
04074
04075
04076
04077 def GetObject(self):
04078 return self.object
04079
04080
04081
04082
04083
04084
04085 def GetName(self):
04086 return self.object.GetName()
04087
04088
04089
04090
04091 def SetFixed(self, fixed):
04092 self.fixed = fixed
04093
04094
04095
04096
04097 def IsFixed(self):
04098 return self.fixed
04099
04100
04101
04102
04103
04104
04105
04106
04107
04108 def SetPorts(self, portType, nr):
04109 self.portList[portType] = [None for i in range(nr)]
04110
04111
04112
04113
04114
04115
04116
04117
04118 def GetPorts(self, portType):
04119 return len(self.portList[portType])
04120
04121
04122
04123
04124
04125 def ClearPorts(self):
04126 self.SetPorts(port_IN, self.GetPorts(port_IN))
04127 self.SetPorts(port_OUT, self.GetPorts(port_OUT))
04128
04129
04130
04131
04132
04133
04134
04135
04136 def SetPortList(self, portType, list):
04137 self.portList[portType] = list
04138
04139
04140
04141
04142 def GetPortList(self, portType):
04143 return self.portList[portType]
04144
04145
04146
04147
04148
04149
04150
04151
04152 def GetLinks(self):
04153 list = []
04154 if obj_NODE == self.GetType():
04155 for obj in self.GetPortList(port_IN) + self.GetPortList(port_OUT):
04156 if obj: list.append(obj)
04157 return list
04158
04159
04160
04161
04162
04163
04164
04165
04166
04167
04168
04169
04170
04171
04172
04173 def FindCorrespondingPortIndex(self,portnbr="",porttype="",portdirection=port_IN):
04174
04175
04176
04177 port = -1
04178 i = 0
04179 for obj in self.GetPortList(portdirection):
04180 if obj == None:
04181 port = i+1
04182 break
04183 i += 1
04184 if port == -1:
04185 print "No free ports found."
04186 return -1,-1
04187
04188 try:
04189 if self.GetObject().GetType() == "M5R4":
04190 if porttype.lower() in ["a","b"]:
04191 if porttype.lower() == "a":
04192 offset = int(portnbr)/8 * 8
04193 else:
04194 offset = (int(portnbr)+8)/8 * 8
04195 index = int(portnbr) + offset
04196 elif porttype.lower() == "ab":
04197 offset = 48
04198 index = int(portnbr) + offset
04199 else:
04200 print "The M5R4 device does not have correct syntax on all ports. Fix this according to the syntax given in the documentation!"
04201 return -1,-1
04202 else:
04203
04204
04205
04206
04207
04208
04209 index = int(portnbr)
04210
04211
04212 return index,port
04213 except ValueError,err:
04214 print "An error occured while trying to find corresponding index to portnbr, porttype and portdirection. The portnbr for M5R4s must have an integer as portnbr. Correct this, and try to connect."
04215 return -1,-1
04216
04217
04218
04219
04220
04221
04222
04223
04224
04225
04226
04227
04228
04229
04230
04231 def GetPortPosition(self, index,portdir):
04232
04233
04234 if self.GetType() == obj_NODE:
04235 if self.GetObject().GetType() == "M5R4":
04236
04237
04238 if index >= 0:
04239 if index < 48:
04240 if GetZoomFactor() < 50:
04241 xPos = self.position.x() + self.size.width()/2
04242 yPos = self.position.y() + self.size.height()
04243 else:
04244 xPos = self.position.x() + self.size.width()*(index+1)/(49)
04245 yPos = self.position.y() + self.size.height() + self.linkLength
04246 elif index < 52:
04247 if GetZoomFactor() < 50:
04248 xPos = self.position.x() + self.size.width()
04249 yPos = self.position.y() + self.size.height()/2
04250 else:
04251 xPos = self.position.x() + self.size.width() + self.linkLength - 1
04252 yPos = self.position.y() + self.size.height() - self.size.height()*(index+1-48)/(5)
04253 elif index < 54:
04254 if GetZoomFactor() < 50:
04255 xPos = self.position.x()
04256 yPos = self.position.y() + self.size.height()/2
04257 else:
04258 xPos = self.position.x() - self.linkLength + 1
04259 yPos = self.position.y() + self.size.height()*(index+1-52)/(5)
04260 else:
04261 print "Portindex is not valid. The given portindex cannot be found for the given devicetype: M5R4"
04262 return QPoint(-1,-1)
04263 else:
04264 if GetZoomFactor() >= 50:
04265
04266 xPos = self.x() + self.width()*(index+1)/(self.GetPorts(portdir)+1)
04267 if portdir == port_IN:
04268 yPos = self.y() - self.linkLength
04269 else:
04270 yPos = self.y() + self.height() + self.linkLength
04271 else:
04272 xPos = self.x() + self.width()/2
04273 if portdir == port_IN:
04274 yPos = self.y()
04275 else:
04276 yPos = self.y() + self.height()
04277
04278 return QPoint(xPos, yPos)
04279 else:
04280 return (-1,-1)
04281
04282
04283
04284
04285
04286
04287
04288
04289
04290
04291
04292
04293
04294
04295
04296 def SetSinglePort(self, portType, portNr, link, override = True):
04297 if link.__class__ == myLine and link.GetType() == obj_LINK:
04298 if portNr > self.GetPorts(portType):
04299 return False
04300 if self.portList[portType][portNr-1]:
04301
04302 if override:
04303 print "WARNING: Overwriting port already connected (port %i (type %i) of %s)" \
04304 %(portNr, portType, self.GetName())
04305 else:
04306
04307 print "ERROR: Tried to overwrite port already connected. \
04308 (port %i (type %i) of %s). NO overwritign activated" \
04309 %(portNr, portType, self.GetName())
04310
04311 return False
04312
04313 self.portList[portType][portNr-1] = link
04314 return True
04315
04316
04317
04318
04319
04320
04321
04322
04323
04324
04325
04326
04327
04328
04329 def SetConnection(self, partner, portType, portNr, setPartner = True):
04330 connected = False
04331 if self.GetType() == obj_NODE:
04332 if partner.__class__ == myLine and \
04333 partner.GetType() == obj_LINK and \
04334 portNr <= self.GetPorts(portType) and \
04335 portType in [port_IN, port_OUT]:
04336 connected = self.SetSinglePort(portType, portNr, partner)
04337 if setPartner:
04338 partner.SetConnection(self, portType, portNr, False)
04339 else:
04340 print "Connection failed (node)"
04341
04342 elif self.GetType() == obj_LINK:
04343 if partner.__class__ == Box and \
04344 partner.GetType() == obj_NODE and \
04345 portNr <= partner.GetPorts(portType) and \
04346 portType in [port_IN, port_OUT]:
04347 if portType == port_IN:
04348 self.nodeTo = partner
04349 self.portTo = portNr
04350 self._AdjustLinks(None, partner.GetPortPosition(self.GetPortIndexTo(),port_IN))
04351 else:
04352 self.nodeFrom = partner
04353 self.portFrom = portNr
04354
04355 if setPartner:
04356 connected = partner.SetConnection(self, portType, portNr, False)
04357 else:
04358 print "Connection failed (link)"
04359
04360 else:
04361 print "Connection failed: wrong type : %s"%str(self.GetType())
04362 return connected
04363
04364
04365
04366
04367
04368
04369
04370
04371
04372
04373
04374
04375
04376 def AddPoints(self, point1, point2):
04377 return QPoint(point1.x() + point2.x(), point1.y() + point2.y())
04378
04379
04380
04381
04382
04383
04384
04385
04386
04387
04388
04389
04390 def _AdjustLinks(self, startPt = None, endPt = None):
04391
04392 if self.GetType() == obj_NODE:
04393 for i in range(len(self.portList[port_IN])):
04394 link = self.portList[port_IN][i]
04395 if link:
04396 pos = link.GetPosition()
04397 start = link.GetStartPt()
04398
04399 link._AdjustLinks(QPoint(pos.x()+start.x(), pos.y()+start.y()), self.GetPortPosition(link.GetPortIndexTo(),port_IN))
04400 for i in range(len(self.portList[port_OUT])):
04401 link = self.portList[port_OUT][i]
04402 if link:
04403 pos = link.GetPosition()
04404 end = link.GetEndPt()
04405 link._AdjustLinks(self.GetPortPosition(link.GetPortIndexFrom(),port_OUT), QPoint(pos.x()+end.x(), pos.y()+end.y()))
04406
04407 elif self.GetType() == obj_LINK:
04408 if startPt == None:
04409 startPt = self.AddPoints(self.GetPosition(), self.GetStartPt())
04410 if endPt == None:
04411 endPt = self.AddPoints(self.GetPosition(), self.GetEndPt())
04412 topLeftX = min(startPt.x(), endPt.x())
04413 topLeftY = min(startPt.y(), endPt.y())
04414 botRightX = max(startPt.x(), endPt.x())
04415 botRightY = max(startPt.y(), endPt.y())
04416
04417 self.SetPosition(QPoint(topLeftX, topLeftY), False)
04418 self.SetSize(QSize(botRightX-topLeftX, botRightY-topLeftY), False)
04419 self.SetStartPt(QPoint(startPt.x() - topLeftX, startPt.y() - topLeftY))
04420 self.SetEndPt(QPoint(endPt.x() - topLeftX, endPt.y() - topLeftY))
04421
04422 def _delete(self):
04423 self.i = 0
04424
04425 del self.position
04426 del self.size
04427 del self.penColour
04428 del self.fillColour
04429 del self.lineSize
04430 del self.startPt
04431 del self.endPt
04432 del self.text
04433 del self.layer
04434 del self.textFont
04435 del self.textvalign
04436 del self.texthalign
04437 del self.textSize
04438 del self.textBoldface
04439 del self.textItalic
04440 del self.textUnderline
04441 del self.canvas
04442
04443 del self.fixed
04444 del self.zoomFactor
04445
04446
04447 del self.object
04448 del self.expanded
04449 del self.clickable
04450 del self.originalsize
04451 del self.macroid
04452
04453 del self.portList
04454
04455
04456 i = 0
04457 class Box(QCanvasRectangle,DrawingObject):
04458
04459 def __init__(self, canvas, type, object = None, position=QPoint(0,0), size=QSize(3, 3),
04460 penColour=Qt.black, fillColour=Qt.white, lineSize=1,
04461 text=None,zoomFactor=100,clickable=True,originalsize=QSize(0,0),macroid=-1,textvalign=text_CENTER,texthalign=text_CENTER):
04462 DrawingObject.__init__(self, canvas, type, object, position, size, penColour, fillColour, lineSize,text, QPoint(0,0), QPoint(0,0),zoomFactor,clickable,originalsize,macroid,textvalign,texthalign)
04463 QCanvasRectangle.__init__(self, canvas)
04464
04465 if self.object:
04466 self.Canvastext = QCanvasText(self.GetName(),canvas)
04467 self.Canvastext.setTextFlags(Qt.AlignCenter)
04468 self.Canvastext.setFont(QFont("Arial",10))
04469 self.setSize(size.width(),size.height())
04470 self.move(position.x(),position.y())
04471
04472 fillColour = Color(self.fillColour)
04473 self.setPen( QPen(QColor(0,0,0)) )
04474 self.setBrush(QBrush(Color(self.fillColour)))
04475 if fillColour.red()+fillColour.blue()+fillColour.green() < 200:
04476 self.Canvastext.setColor(QColor(150,150,150))
04477
04478 def SetData(self,data):
04479 DrawingObject.SetData(self,data)
04480 self.Canvastext = QCanvasText(self.GetName(),self.canvas)
04481 self.Canvastext.setTextFlags(Qt.AlignCenter)
04482 self.Canvastext.setFont(QFont("Arial",10,QFont.Bold))
04483 self.setSize(self.size.width(),self.size.height())
04484 fillColour = Color(self.fillColour)
04485 self.setPen( QPen(QColor(0,0,0)) )
04486 self.setBrush(QBrush(Color(self.fillColour)))
04487 if self.fillColour.red()+self.fillColour.blue()+self.fillColour.green() < 200:
04488 self.Canvastext.setColor(QColor(150,150,150))
04489
04490 def setCanvas(self,c):
04491 try:
04492 self.Canvastext.setCanvas(c)
04493 except:
04494 print "WARNING : CanvasText not found"
04495 QCanvasItem.setCanvas(self,c)
04496
04497 def hide(self):
04498 QCanvasRectangle.hide(self)
04499 try:
04500 self.Canvastext.hide()
04501 except:
04502 pass
04503 def _delete(self):
04504 try:
04505
04506 self.Canvastext = None
04507
04508
04509 self.object = None
04510 del self.portList
04511 self.canvas = None
04512 except:
04513 pass
04514
04515 def show(self):
04516 QCanvasRectangle.show(self)
04517 self.Canvastext.show()
04518 self.Canvastext.setZ(self.z()+1)
04519 self.Canvastext.move(self.x()+self.width()/2,self.y()+self.height()/2)
04520 self.Canvastext.move(self.x()+self.width()/2,self.y()+self.height()/2)
04521
04522 def moveBy(self,x,y):
04523 QCanvasRectangle.moveBy(self,x,y)
04524 self.Canvastext.moveBy(x,y)
04525
04526 def move(self,x,y):
04527 QCanvasRectangle.move(self,abs(x)%5000,abs(y)%5000)
04528
04529 def SetSize(self, size, adjust = True):
04530 DrawingObject.SetSize(self,size,adjust)
04531 self.setSize(size.width(),size.height())
04532
04533 def SetPosition(self, position, adjust = True):
04534 DrawingObject.SetPosition(self, position, adjust)
04535 self.move(position.x(),position.y())
04536
04537 def areaPoints(self):
04538 points = QPointArray(4)
04539 A = QPoint(self.x(),self.y())
04540 B = QPoint(self.x()+self.width(),self.y()+self.height())
04541 margin = 0
04542 if self.isActive(): margin = 2
04543 points.setPoint(0,A.x()-margin,A.y()-self.linkLength)
04544 points.setPoint(1,B.x()+margin,A.y()-self.linkLength)
04545 points.setPoint(2,A.x()-margin,B.y()+self.linkLength)
04546 points.setPoint(3,B.x()+margin,B.y()+self.linkLength)
04547 return points
04548
04549 def drawShape(self,painter):
04550 if not (self in visWindow.contents):
04551 self._delete()
04552 self.setCanvas(None)
04553 return
04554
04555 if self.GetObject().GetType() in ["M5R4","M4R4"]:
04556
04557
04558 i = 0
04559 colour = dc.GetPen()
04560 while i < 48:
04561 xPos = position.x + self.size.width*(i+1)/(49)
04562 if (i > 7 and i < 16) or (i > 23 and i < 32) or (i > 39 and i < 48):
04563 dc.SetPen(wxGREEN_PEN)
04564 else:
04565 dc.SetPen(wxRED_PEN)
04566 dc.DrawLine(xPos, position.y+self.size.height,xPos, position.y+self.size.height+self.linkLength)
04567 i += 1
04568
04569 dc.SetPen(colour)
04570 i = 0
04571 while i < 4:
04572 yPos = position.y + self.size.height*(i+1)/(5)
04573 dc.drawLine(position.x+self.size.width,yPos, position.x+self.size.width+self.linkLength,yPos)
04574 i += 1
04575 i = 0
04576 while i < 4:
04577 yPos = position.y + self.size.height*(i+1)/(5)
04578 dc.drawLine(position.x, yPos, position.x-self.linkLength,yPos)
04579 i += 2
04580 else:
04581 ports = self.GetPorts(port_IN)
04582 if 2*ports < GetZoomFactor()/100*self.width():
04583 xPos = self.x()
04584 for x in range(ports):
04585 xPos = self.x() + self.width()*(x+1)/(ports+1)
04586 painter.drawLine(xPos, self.y()-self.linkLength, xPos, self.y())
04587 else:
04588 painter.save()
04589 painter.setPen(Qt.NoPen)
04590 painter.setBrush(QBrush(QColor(200,200,200)))
04591 painter.drawRect(QRect(QPoint(self.x(),self.y()-self.linkLength),QPoint(self.x()+self.width()-1,self.y()-1)))
04592 painter.restore()
04593 ports = self.GetPorts(port_OUT)
04594 if 2*ports < GetZoomFactor()/100*self.width():
04595 xPos = self.x()
04596 for x in range(ports):
04597 xPos = self.x() + self.width()*(x+1)/(ports+1)
04598 painter.drawLine(xPos, self.y()+self.height(), xPos, self.y()+self.height()+self.linkLength)
04599 else:
04600 painter.save()
04601 painter.setPen(Qt.NoPen)
04602 painter.setBrush(QBrush(QColor(200,200,200)))
04603 painter.drawRect(QRect(QPoint(self.x(),self.y()+self.height()+self.linkLength),QPoint(self.x()+self.width()-1,self.y()+self.height()-1)))
04604 painter.restore()
04605
04606 if self.isActive():
04607 painter.setPen(QPen(Qt.black,2,Qt.DashLine))
04608 if self.GetObject().GetName() in visWindow.main.DoNotShow:
04609 painter.setBrush(QBrush(painter.brush().color(),Qt.Dense7Pattern))
04610
04611 QCanvasRectangle.drawShape(self,painter)
04612
04613 QCanvasRectangle.drawShape(self,painter)
04614
04615 def dragEnterEvent(self, event):
04616 event.ignore()
04617 def dragMoveEvent(self, event):
04618 event.ignore()
04619
04620 class myLine(QCanvasLine,DrawingObject):
04621 def __init__(self,canvas,obj_link,object,position=QPoint(2500,2500),size=0,penColour=Qt.black, \
04622 fillColour=Qt.black,lineSize=1,startPt=QPoint(2500,2500),endPt=QPoint(2500,2500)):
04623 DrawingObject.__init__(self,canvas,obj_link,object,position,size,penColour,fillColour,lineSize,startPt,endPt)
04624 QCanvasLine.__init__(self,canvas)
04625 self.penColour = Color(self.GetObject().GetColor())
04626
04627 def dragEnterEvent(self, event):
04628 event.ignore()
04629 def dragMoveEvent(self, event):
04630 event.ignore()
04631 def show(self):
04632 try:
04633 self.setPoints(self.nodeFrom.x()+self.from_delta, self.nodeFrom.y()+self.nodeFrom.height()+self.linkLength,self.nodeTo.x()+self.to_delta ,self.nodeTo.y()-self.linkLength)
04634 QCanvasLine.show(self)
04635 except:
04636 print "There is an error in the Configuration database"
04637
04638 def drawShape(self, painter):
04639 if not (self in visWindow.contents) or self.GetObject().GetName() in visWindow.main.DoNotShow:
04640 self._delete()
04641 self.setCanvas(None)
04642 return
04643
04644 self.setPoints(self.nodeFrom.x()+self.from_delta, self.nodeFrom.y()+self.nodeFrom.height()+self.linkLength,self.nodeTo.x()+self.to_delta ,self.nodeTo.y()-self.linkLength)
04645 if self.isActive():
04646 painter.setPen(QPen(self.penColour,2))
04647 else:
04648 painter.setPen(QPen(self.penColour,1))
04649 QCanvasLine.drawShape(self,painter)
04650
04651 def boundingRect(self):
04652
04653 return QRect(min(self.startPoint().x(),self.endPoint().x()),min(self.startPoint().y(),self.endPoint().y()), abs(self.startPoint().x()-self.endPoint().x()), abs(self.startPoint().y()-self.endPoint().y()))
04654
04655 def areaPoints(self):
04656 points = QPointArray(4)
04657 A = self.startPoint()
04658 B = self.endPoint()
04659 zoomfactor=GetZoomFactor()/100
04660 delta_x = abs(A.x()-B.x())*zoomfactor
04661 delta_y = abs(A.y()-B.y())*zoomfactor
04662 delta = 4*max(delta_x,delta_y)
04663 points.setPoint(0,A.x(),A.y()-delta)
04664 points.setPoint(1,B.x(),B.y()-delta)
04665 points.setPoint(2,B.x(),B.y()+delta)
04666 points.setPoint(3,A.x(),A.y()+delta)
04667 return points
04668 def _delete(self):
04669
04670 try:
04671 self.nodeFrom, self.nodeTo = None, None
04672 self.object = None
04673 self.canvas = None
04674 except:
04675 pass
04676 def setCanvas(self,c):
04677 QCanvasItem.setCanvas(self,c)
04678