C:/mesDocs/QT_confDbVis_canvas/CreateDevice.py

Go to the documentation of this file.
00001 from qt import *
00002 import string # string manipulation functions needed
00003 
00004 from cdbVisCore import *
00005 from objectclasses import *
00006 import helper
00007 from GUIcreatedevices import *
00008 from CreatePorts import *
00009 
00010 #####################################################################
00011 # CreateDeviceWindow class                                      #####
00012 #####################################################################
00013 
00014 ##
00015 #  This class is responsible for showing the dialog for creating and modifying devices,
00016 #           let the user create or modify devices, and to handle the ports for the devices.
00017 #       
00018 class CreateDeviceWindow(GUIcreateDevices):
00019         ##
00020         #  The Constructor.
00021         # 
00022         #                   Parameters:
00023         #                   @parent         - parent window to this window (MainWindow)
00024         #                   @selectedsystem - the system to create this device in (the user can also add others)
00025         #                   @creationmode   - if True, then a device will be created, 
00026         #                                     if False, a device will be modified (already exists)
00027         #                   @deviceobj      - if the user are about to modify a device, then the previous values of
00028         #                                     the device will have to be set, and we use this object for that
00029         #                   @devicetypes    - A list of strings of the device types that are available in the active
00030         #                                     subsystem; user can choose device type in the combo box.
00031         #                   @devtype        - If device type is already chosen for the new device (create), it is set here
00032         #                   @location       - When create, set location string.
00033         #               
00034         def __init__(self,parent,selectedsystem,creationmode=True,deviceobj=None,devicetypes=[],devtype="",location="",functionlist=[]):
00035                 GUIcreateDevices.__init__(self,parent,"Create_Devices",0,0)
00036                 self._creationmode = creationmode
00037                 in_db = False # Whether devices is stored in ConfDB or not..yet
00038                 self.main = parent
00039                 close_window = False # If we get an unrecoverable error, we have to close the window to avoid the user to do any "damage"
00040                 self._deletedports = []
00041 
00042                 if creationmode == True:
00043                         title = "Create Device(s)"
00044                         self._deviceports = []
00045                         if devtype == None:
00046                                 devtype = ""
00047                 else:
00048                         self._deviceports = deviceobj.GetPorts() # Port objects for the device; if modifying
00049                         
00050                         if self._deviceports == False:
00051                                 self.main.ShowError("A database error occured while fetching information about the ports for the current device: " + str(deviceobj.GetErrorMessage()) + ". We are unable to continue.",ERR_ERROR,True)
00052                                 close_window = True
00053                         
00054                         title = "Modify Device " + str(deviceobj.GetName())
00055                         selectedsystem = deviceobj.GetSystem()
00056                         self.renameobj = None 
00057                         
00058                         # We have to know the current values of these variables in order 
00059                         # to know what modify status we have to set for the device
00060                         self.oldname = deviceobj.GetName()
00061                         self.oldnode = deviceobj.GetNode()
00062                         self.oldsystem = deviceobj.GetSystem()
00063                         
00064                         self.deviceid = deviceobj.GetID()
00065                         self.nodechanged = deviceobj.IsNodeChanged()
00066                         self.systemchanged = deviceobj.IsSystemChanged()
00067                         
00068                         # Check if device exists in db, if not we can do more edits than restricted by db functions
00069                         test_device = Device(deviceobj.GetSystem(),deviceobj.GetName(),True)
00070                         res = test_device.Update(True)
00071                         if res == {}:
00072                                 in_db = False
00073                         elif res == False:
00074                                 in_db = False
00075                                 close_window = True
00076                                 self.main.ShowError("A database error occured while trying to fetch information about the current device: " + str(test_device.GetErrorMessage()) + ". We are unable to continue.",ERR_ERROR,True)
00077                         else:
00078                                 in_db = True
00079 
00080                 
00081                  
00082                 if True:
00083                         pass
00084                         '''Create_DevicesLayout = QGridLayout(self,1,1,11,6,"Create_DevicesLayout")
00085 
00086                         self._serialtxtbox = QLineEdit(self,"__serialtxtbox")
00087 
00088                         Create_DevicesLayout.addMultiCellWidget(self._serialtxtbox,1,1,8,10)
00089 
00090                         self._hwtypetxtbox = QLineEdit(self,"__hwtypetxtbox")
00091 
00092                         Create_DevicesLayout.addMultiCellWidget(self._hwtypetxtbox,3,3,8,10)
00093 
00094                         self._responsibletxtbox = QLineEdit(self,"__responsibletxtbox")
00095 
00096                         Create_DevicesLayout.addMultiCellWidget(self._responsibletxtbox,5,5,8,10)
00097 
00098                         self.textLabel2_4_3 = QLabel(self,"textLabel2_4_3")
00099 
00100                         Create_DevicesLayout.addMultiCellWidget(self.textLabel2_4_3,0,0,8,9)
00101 
00102                         self.textLabel2_4_3_2 = QLabel(self,"textLabel2_4_3_2")
00103 
00104                         Create_DevicesLayout.addMultiCellWidget(self.textLabel2_4_3_2,2,2,8,9)
00105 
00106                         self.textLabel2_4_3_2_2_2 = QLabel(self,"textLabel2_4_3_2_2_2")
00107 
00108                         Create_DevicesLayout.addMultiCellWidget(self.textLabel2_4_3_2_2_2,6,6,8,9)
00109 
00110                         self.textLabel2_4_3_2_2 = QLabel(self,"textLabel2_4_3_2_2")
00111 
00112                         Create_DevicesLayout.addMultiCellWidget(self.textLabel2_4_3_2_2,4,4,8,9)
00113 
00114                         self.textLabel2_4_3_2_2_2_2 = QLabel(self,"textLabel2_4_3_2_2_2_2")
00115 
00116                         Create_DevicesLayout.addMultiCellWidget(self.textLabel2_4_3_2_2_2_2,9,9,8,9)
00117 
00118                         layout6 = QVBoxLayout(None,0,6,"layout6")
00119 
00120                         self.textLabel2_3_2 = QLabel(self,"textLabel2_3_2")
00121                         layout6.addWidget(self.textLabel2_3_2)
00122 
00123                         self._functiontxtbox = QComboBox(0,self,"__functiontxtbox")
00124                         layout6.addWidget(self._functiontxtbox)
00125 
00126                         Create_DevicesLayout.addMultiCellLayout(layout6,13,13,0,6)
00127 
00128                         layout11 = QVBoxLayout(None,0,6,"layout11")
00129 
00130                         self.textLabel2_2 = QLabel(self,"textLabel2_2")
00131                         layout11.addWidget(self.textLabel2_2)
00132 
00133                         self._sysnamescmbbox = QComboBox(0,self,"__sysnamescmbbox")
00134                         layout11.addWidget(self._sysnamescmbbox)
00135 
00136                         Create_DevicesLayout.addMultiCellLayout(layout11,0,1,2,4)
00137 
00138                         self._delbutton = QPushButton(self,"__delbutton")
00139                         self._delbutton.setMaximumSize(QSize(21,21))
00140 
00141                         Create_DevicesLayout.addWidget(self._delbutton,1,6)
00142 
00143                         self._addbutton = QPushButton(self,"__addbutton")
00144                         self._addbutton.setMaximumSize(QSize(21,21))
00145 
00146                         Create_DevicesLayout.addWidget(self._addbutton,1,5)
00147 
00148                         self._chk_prommode = QCheckBox(self,"__chk_prommode")
00149 
00150                         Create_DevicesLayout.addMultiCellWidget(self._chk_prommode,11,11,0,1)
00151 
00152                         layout5 = QVBoxLayout(None,0,6,"layout5")
00153 
00154                         self.textLabel2_4_2_2 = QLabel(self,"textLabel2_4_2_2")
00155                         layout5.addWidget(self.textLabel2_4_2_2)
00156 
00157                         self._startspinbox = QSpinBox(self,"__startspinbox")
00158                         layout5.addWidget(self._startspinbox)
00159 
00160                         Create_DevicesLayout.addMultiCellLayout(layout5,10,10,0,1)
00161 
00162                         layout4 = QVBoxLayout(None,0,6,"layout4")
00163 
00164                         self.textLabel2_4_2_2_2 = QLabel(self,"textLabel2_4_2_2_2")
00165                         layout4.addWidget(self.textLabel2_4_2_2_2)
00166 
00167                         self._endspinbox = QSpinBox(self,"__endspinbox")
00168                         layout4.addWidget(self._endspinbox)
00169 
00170                         Create_DevicesLayout.addMultiCellLayout(layout4,10,10,2,6)
00171 
00172                         self._paddingchkbox = QCheckBox(self,"__paddingchkbox")
00173 
00174                         Create_DevicesLayout.addWidget(self._paddingchkbox,8,0)
00175 
00176                         self._paddingspinbox = QSpinBox(self,"__paddingspinbox")
00177                         self._paddingspinbox.setEnabled(0)
00178 
00179                         Create_DevicesLayout.addMultiCellWidget(self._paddingspinbox,8,8,1,3)
00180 
00181                         self.textLabel2_4_2 = QLabel(self,"textLabel2_4_2")
00182 
00183                         Create_DevicesLayout.addMultiCellWidget(self.textLabel2_4_2,8,8,4,5)
00184                         spacer1 = QSpacerItem(310,20,QSizePolicy.Expanding,QSizePolicy.Minimum)
00185                         Create_DevicesLayout.addMultiCell(spacer1,14,14,0,8)
00186 
00187                         self._okbutton = QPushButton(self,"__okbutton")
00188 
00189                         Create_DevicesLayout.addWidget(self._okbutton,14,9)
00190 
00191                         self._cancelbutton = QPushButton(self,"__cancelbutton")
00192 
00193                         Create_DevicesLayout.addWidget(self._cancelbutton,14,10)
00194 
00195                         self._portbutton = QPushButton(self,"__portbutton")
00196 
00197                         Create_DevicesLayout.addMultiCellWidget(self._portbutton,13,13,8,10)
00198 
00199                         self._commentstxtbox = QTextEdit(self,"__commentstxtbox")
00200 
00201                         Create_DevicesLayout.addMultiCellWidget(self._commentstxtbox,10,12,8,10)
00202                         spacer4 = QSpacerItem(20,20,QSizePolicy.Fixed,QSizePolicy.Minimum)
00203                         Create_DevicesLayout.addItem(spacer4,8,7)
00204 
00205                         self._locationtxtbox = QLineEdit(self,"__locationtxtbox")
00206 
00207                         Create_DevicesLayout.addMultiCellWidget(self._locationtxtbox,7,7,8,10)
00208 
00209                         layout9 = QVBoxLayout(None,0,6,"layout9")
00210 
00211                         self.textLabel2 = QLabel(self,"textLabel2")
00212                         layout9.addWidget(self.textLabel2)
00213 
00214                         self._systemnametxtbox = QLineEdit(self,"__systemnametxtbox")
00215                         self._systemnametxtbox.setEnabled(0)
00216                         layout9.addWidget(self._systemnametxtbox)
00217 
00218                         Create_DevicesLayout.addMultiCellLayout(layout9,0,1,0,0)
00219 
00220                         layout7 = QVBoxLayout(None,0,6,"layout7")
00221 
00222                         self.textLabel2_4 = QLabel(self,"textLabel2_4")
00223                         layout7.addWidget(self.textLabel2_4)
00224 
00225                         self._prefixtxtbox = QLineEdit(self,"__prefixtxtbox")
00226                         layout7.addWidget(self._prefixtxtbox)
00227 
00228                         Create_DevicesLayout.addMultiCellLayout(layout7,6,7,0,6)
00229 
00230                         self._chk_node = QCheckBox(self,"__chk_node")
00231 
00232                         Create_DevicesLayout.addMultiCellWidget(self._chk_node,12,12,0,4)
00233 
00234                         layout8 = QVBoxLayout(None,0,6,"layout8")
00235 
00236                         self.textLabel2_3 = QLabel(self,"textLabel2_3")
00237                         layout8.addWidget(self.textLabel2_3)
00238 
00239                         self._devicetypetxtbox = QComboBox(0,self,"__devicetypetxtbox")
00240                         layout8.addWidget(self._devicetypetxtbox)
00241 
00242                         Create_DevicesLayout.addMultiCellLayout(layout8,4,5,0,6)
00243 
00244                         layout7_2_2 = QVBoxLayout(None,0,6,"layout7_2_2")
00245 
00246                         self.textLabel2_4_4_2 = QLabel(self,"textLabel2_4_4_2")
00247                         layout7_2_2.addWidget(self.textLabel2_4_4_2)
00248 
00249                         self._prefixtxtbox = QLineEdit(self,"__prefixtxtbox")
00250                         self._prefixtxtbox.setEnabled(False)
00251                         layout7_2_2.addWidget(self._prefixtxtbox)
00252 
00253                         Create_DevicesLayout.addMultiCellLayout(layout7_2_2,2,3,0,2)
00254 
00255                         layout7_2 = QVBoxLayout(None,0,6,"layout7_2")
00256 
00257                         self.textLabel2_4_4 = QLabel(self,"textLabel2_4_4")
00258                         layout7_2.addWidget(self.textLabel2_4_4)
00259 
00260                         self._deviceidtxtbox = QLineEdit(self,"__deviceidtxtbox")
00261                         self._deviceidtxtbox.setEnabled(False)
00262                         layout7_2.addWidget(self._deviceidtxtbox)
00263 
00264                         Create_DevicesLayout.addMultiCellLayout(layout7_2,2,3,3,6)
00265 
00266                         self.languageChange()
00267 
00268                         self.resize(QSize(579,461).expandedTo(self.minimumSizeHint()))
00269                         self.clearWState(Qt.WState_Polished)
00270                         '''
00271                 
00272                 self.system = selectedsystem
00273                 self.deviceobj = deviceobj
00274                 self.devs = []
00275                 self.deviceobjs = []
00276 
00277 
00278                 # Set up the subsystem text box (which subsystems the device will be created in)
00279                 if self._creationmode:
00280                         mysystems = selectedsystem + ","
00281                 else:
00282                         mysystems = deviceobj.GetSystem() + ","
00283                 # Subsystem text box and combo box
00284                 self._systemnametxtbox.setText(mysystems)
00285                 tmpsystems = self.main.GetCdb().GetSubSystems()
00286                 subsystems = []
00287                 for tmp in tmpsystems:
00288                         subsystems.append(tmp.GetName())
00289                 for system in subsystems:
00290                         self._sysnamescmbbox.insertItem(system)
00291                 self.connect(self._addbutton,SIGNAL("released()"),self.OnAdd)
00292                 self.connect(self._delbutton,SIGNAL("released()"),self.OnRemove)
00293 
00294                 if not self._creationmode:
00295                         self._deviceurl.setText(deviceobj.GetURL())
00296                 
00297                 # Device type name combo box    #added by Lana
00298                 print "devicetypes=%s " % (devicetypes);
00299                 
00300                 for type in devicetypes:
00301                         self._devicetypetxtbox.insertItem(type)
00302 
00303                 if not self._creationmode:
00304                         devtype = deviceobj.GetType()
00305                         self._devicetypetxtbox.setCurrentText(devtype) #Select the active device type:
00306                         self._devicetypetxtbox.setEnabled(False)
00307                         if in_db:
00308                                 self._deviceidtxtbox.setText(str(self.deviceobj.GetID()))
00309                         
00310 
00311                 # Device name, shown if we modify a device
00312                 if not self._creationmode:
00313                         self._prefixtxtbox.setEnabled(True)  # Enabling "Device Name"
00314                         self._prefixtxtbox.setText(self.deviceobj.GetName())
00315                 #added by Lana
00316                 # print "in lana section %s" %(functionlist)
00317                 # function_name=""
00318                 # for fct in functionlist:
00319                         # self._functiontxtbox.insertItem(str(fct))
00320                 #self._functiontxtbox.setCurrentText(function_name)
00321 
00322 
00323                 # What properties we can change on the right hand side depends on whether the device is already created
00324                 # and if so if it is stored in ConfDB.
00325                 if not self._creationmode:
00326                         self._chk_prommode.setChecked(self.deviceobj.GetPromMode())
00327                         self._chk_node.setChecked(self.deviceobj.GetNode())
00328                         self._serialtxtbox.setText(self.deviceobj.GetSerial())
00329                         self._hwtypetxtbox.setText(self.deviceobj.GetHWType())
00330                         self._responsibletxtbox.setText(self.deviceobj.GetResponsible())
00331                         self._locationtxtbox.setText(self.deviceobj.GetLocation())
00332                         self._commentstxtbox.setText(self.deviceobj.GetComments())
00333                         #added by Lana
00334                         ## PROBLEME : no method   GetFunction defined !!!!!!!!!!!
00335                         ##self._functiontxtbox.insertItem(self.deviceobj.GetFunction()) 
00336 
00337                         if in_db:
00338                                 self._serialtxtbox.setEnabled(False)
00339                                 self._hwtypetxtbox.setEnabled(False)
00340                                 self._responsibletxtbox.setEnabled(False)
00341                                 self._commentstxtbox.setEnabled(False)
00342                                 ##self._functiontxtbox.setEnabled(False)
00343                 else: # Default settings
00344                         self._chk_prommode.setChecked(True)
00345                         self._locationtxtbox.setText(location)
00346 
00347                 # If we create a device, options for naming the device(s) is shown (prefix, padding, nr etc)
00348                 if self._creationmode:
00349                         self._prefixtxtbox.setEnabled(True)
00350                         self._paddingchkbox.setEnabled(True)
00351                         self._paddingspinbox.setEnabled(True)
00352                         self._startspinbox.setEnabled(True)
00353                         self._endspinbox.setEnabled(True)
00354                         ## WAL_PROB : self.connect(self._paddingchkbox,SIGNAL("toggled(bool)"),self.OnSelectPadding) #EVT_CHECKBOX(self._panel,PADDING_ID,self.OnSelectPadding)
00355 
00356                         if devtype != "": # If device type is set..we guess right away
00357                                 self.OnDeviceTypeChanged(None)
00358                         self._endspinbox.setValue(1) # nr of devices to be created
00359                 else:
00360                         self._prefixtxtbox.setEnabled(False)
00361                         self._paddingchkbox.setEnabled(False)
00362                         self._paddingspinbox.setEnabled(False)
00363                         self._paddingspinbox.setEnabled(True)
00364                         self._startspinbox.setEnabled(False)
00365                         self._endspinbox.setEnabled(False)
00366                 
00367                 if self._creationmode:
00368                         text = "Create/Modify ports..."
00369                 else:
00370                         text = "Modify ports..."
00371                 self._portbutton.setText(text)
00372                 self.connect(self._portbutton,SIGNAL("released()"),self.OnCreatePorts) # EVT_BUTTON(self,PORT_BTN,self.OnCreatePorts)
00373 
00374                 # OK and Cancel buttons
00375                 self.connect(self._okbutton,SIGNAL("released()"),self.OkButtonClicked)
00376                 self.connect(self._cancelbutton,SIGNAL("released()"),self.reject)
00377 
00378         ##
00379         #  Here we create device object, rename object if needed, validate all properties
00380         #               and create the ports needed.
00381         #               
00382         #               !return - True if successful, False if an error occur
00383         #               
00384         def SaveObjects(self):
00385                 try:
00386 
00387                         if self._creationmode: # Create
00388                                 
00389                                 i = self._startspinbox.value()
00390                                 # from start number to end number (start number + total number of devices to be created)
00391                                 while i < (int(str(self._endspinbox.text())) + int(str(self._startspinbox.text()))):
00392                                         padding = int(str(self._paddingspinbox.text())) # Padding...?
00393                                         nrlength = len(str(i)) # Length of number without padding
00394 
00395                                         if self._paddingchkbox.isChecked():
00396                                                 padstr = "".zfill(padding-nrlength) # Fill number with padding
00397                                         else:
00398                                                 padstr = ""
00399 
00400                                         # Replace the %d with the number (with or without padding) in the device name string
00401                                         devname = str(self._prefixtxtbox.text()).replace("%d",str(padstr)+str(i))
00402                                         serial = str(self._serialtxtbox.text()).replace("%d",str(padstr)+str(i))
00403                                         deviceobj = Device(str(self._systemnametxtbox.text())[:-1],devname,True)
00404                                         deviceobj.SetName(devname) # We have already checked that the device name is unique
00405                                         sysname = str(self._systemnametxtbox.text())[:-1] #Removing the last comma
00406                                         deviceobj.SetSystem(sysname,False)
00407                                         deviceobj.SetNode(int(self._chk_node.isChecked()),False)
00408                                         deviceobj.SetType(str(self._devicetypetxtbox.currentText()))
00409                                         deviceobj.SetSerial(serial)
00410                                         deviceobj.SetHWType(str(self._hwtypetxtbox.text()))
00411                                         deviceobj.SetResponsible(str(self._responsibletxtbox.text()))
00412                                         deviceobj.SetLocation(str(self._locationtxtbox.text()))
00413                                         deviceobj.SetComments(str(self._commentstxtbox.text()))
00414                                         deviceobj.SetPromMode(int(self._chk_prommode.isChecked()))
00415                                         deviceobj.SetURL(str(self._deviceurl.text()))
00416                                         ## deviceobj.SetFunction(str(self._functiontxtbox.currentText()))
00417                                         
00418                                         self.deviceobjs.append(deviceobj) # Add device obj to list of devices that we will create
00419                                         
00420                                         i += 1
00421                         else:
00422                                         devname = str(self._prefixtxtbox.text())
00423                                         if devname != self.oldname:
00424                                                 ans = QMessageBox.question(self,"Rename ?","You have renamed the device, was that your intention? Remember that you can not undo renaming, \nbut you can rename the object as many times as you want.","Yes","No","",0,1)
00425                                                 if ans == 1: # No
00426                                                         self._prefixtxtbox.setText(self.oldname)
00427                                                         return False
00428                                                 devtypename = str(self._devicetypetxtbox.currentText())
00429                                                 devtype = DeviceType(str(self._systemnametxtbox.text())[:-1],devtypename,False) #The [:-1] removes the last comma
00430                                                 devs = devtype.GetDevices()
00431                                                 if devs == False:
00432                                                         self.main.ShowError("Failed to fetch data from ConfDB: " + str(devtype.GetErrorMessage()),ERR_ERROR,True)
00433                                                         return False
00434                                                 else:
00435                                                         if devname in devs:
00436                                                                 self.main.ShowError("You cannot rename the device to this name because " + str(devname) + " is already in use by another device.",ERR_ERROR,True)
00437                                                                 self._prefixtxtbox.setText(self.oldname)
00438                                                                 return False
00439 
00440                                                 self.renameobj = Device(str(self._systemnametxtbox.text())[:-1],str(self._prefixtxtbox.text()),True)
00441                                                 self.renameobj.SetOldName(self.oldname)
00442                                                 self.renameobj.SetType(str(self._devicetypetxtbox.currentText()))
00443                                                 self.renameobj.SetID(self.deviceid)
00444                                                 self.renameobj.SetSaveStatus(RENAME)
00445         
00446                                         self.deviceobj = Device(str(self._systemnametxtbox.text())[:-1],devname,False)
00447                                         self.deviceobj.SetID(self.deviceid)
00448         
00449                                         self.deviceobj.SetName(str(self._prefixtxtbox.text()))
00450                                         self.deviceobj.SetType(str(self._devicetypetxtbox.currentText()))
00451                                         
00452                                         self.deviceobj.SetSerial(str(self._serialtxtbox.text()))
00453                                         self.deviceobj.SetHWType(str(self._hwtypetxtbox.text()))
00454                                         self.deviceobj.SetResponsible(str(self._responsibletxtbox.text()))
00455                                         self.deviceobj.SetLocation(str(self._locationtxtbox.text()))
00456                                         self.deviceobj.SetComments(str(self._commentstxtbox.text()))
00457                                         self.deviceobj.SetURL(str(self._deviceurl.text()))
00458                                         ## WALID PROB : The methode SetLocationFunction Doesnot exist
00459                                         ## self.deviceobj.SetLocationFunction(str(self._functiontxtbox.currentText()))
00460 
00461                                         if int(self._chk_node.isChecked()) != self.oldnode or self.nodechanged:
00462                                                 changed = True
00463                                         else:
00464                                                 changed = False
00465                                         self.deviceobj.SetNode(int(self._chk_node.isChecked()),changed)
00466         
00467                                         sysname = str(self._systemnametxtbox.text())[:-1] #Removing the last comma
00468                                         if sysname != self.oldsystem or self.systemchanged:
00469                                                 changed = True
00470                                         else:
00471                                                 changed = False
00472 
00473                                         self.deviceobj.SetSystem(sysname,changed)                                       
00474                                         self.deviceobj.SetPromMode(int(self._chk_prommode.isChecked()))
00475 
00476                                         self.deviceobjs.append(self.deviceobj) # Add device object to list (although it is only one)
00477                                         
00478                         return True # All well
00479 
00480                 except ValidationError,err:
00481                         self.main.ShowError(str(err),ERR_ERROR,True)
00482                         return False
00483                 except ValueError,err:
00484                         self.main.ShowError("A String to Integer conversion failed: " + str(err),ERR_ERROR,True)
00485                         return False
00486 
00487         ##
00488         #  We click on the Create/Modify button to create port(s) for the device(s) we create.
00489         #               They will be stored in a list in this object, and sent to the user when he/she
00490         #               asks for it.
00491         # 
00492         #               !return - True if successful, False if not.
00493         #               
00494         def OnCreatePorts(self):
00495 
00496                  # We create the same ports for all the devices 
00497                 devicename = str(self._prefixtxtbox.text())
00498                 devtype = DeviceType(str(self._systemnametxtbox.text())[:-1],str(self._devicetypetxtbox.currentText()),False)
00499                 my_createportframe = createPortsWindow(self.main,devtype,devicename,self._deviceports,self)
00500                 my_createportframe.show()
00501                 ans =  my_createportframe.exec_loop()
00502                 if ans==1:
00503                         self._deviceports = my_createportframe.GetPorts()
00504                         self._deletedports = my_createportframe.GetDeletedPorts()
00505                 return
00506                 '''# We need device type and device name for the device(s) before we assign ports to them.
00507                 if self._devicetypetxtbox.GetValue == "":
00508                         self.main.ShowError("You have to choose a device type before you assign ports.",ERR_ERROR,True)
00509                         return False
00510 
00511                 # If we create, the device name has to be set. Prefix with %d if more than one device to be created.
00512                 if self._creationmode:
00513                         if str(self._prefixtxtbox.text()) == "" or (str(self._prefixtxtbox.text()).find("%d",0,len(str(self._prefixtxtbox.text()))) == -1 and str(self._endspinbox.text()) > 1):
00514                                 self.main.ShowError("You have to set a proper device name, and include a %d if you create more than one device, before you assign ports.",ERR_ERROR,True)
00515                                 return False
00516 
00517                         # We set up the device names in a string like this: DEV_NAME_001 -> DEV_NAME_010
00518                         # for the devices that the ports will be created for.
00519                         padding = int(str(self._paddingspinbox.text()))
00520                         nrlength = len(str(str(self._startspinbox.text())))
00521                         if self._paddingchkbox.isChecked():
00522                                 padstr = "".zfill(padding-nrlength)
00523                         else:
00524                                 padstr = ""
00525         
00526                                 
00527                         devname = str(self._prefixtxtbox.text()).replace("%d",str(padstr)+str(str(self._startspinbox.text())))
00528                         if str(self._endspinbox.text()) > 1:
00529                                 nrlength = len(str(str(self._endspinbox.text())))
00530                                 if self._paddingchkbox.isChecked():
00531                                         padstr = "".zfill(padding-nrlength)
00532                                 else:
00533                                         padstr = ""
00534                                 devname += " -> " + str(self._prefixtxtbox.text()).replace("%d",str(padstr)+str(str(self._startspinbox.text())+str(self._endspinbox.text())-1))
00535                 else: # Modify
00536                         if str(self._prefixtxtbox.text()) == "":
00537                                 self.main.ShowError("You have to choose a device name before you assign ports",ERR_ERROR,True)
00538                                 return False
00539                         devname = str(self._prefixtxtbox.text())
00540 
00541                 # We also need to send the device type to the create ports window (to check that the number of created ports
00542                 # and type is correct.
00543                 devtypename = str(self._devicetypetxtbox.currentText())
00544                 devtype = DeviceType(str(self._systemnametxtbox.text())[:-1],devtypename,False) #The [:-1] removes the last comma
00545 
00546                 # Open Create Ports Window
00547                 win = CreatePortsWindow(self.main,-1,self._creationmode,devname,devtype,self._deviceports)
00548                 if win.ShowModal() == wxID_OK:
00549                         self._deviceports,deletedports = win.GetPortObjs() # Set the port objects created to the self._deviceports list
00550                         self._deletedports.extend(deletedports)'''
00551                 
00552 
00553         ##
00554         #  Enable/Disable the padding spin box depending on whether
00555         #               the paddingcheckbox is checked or not.
00556         #               
00557         def OnSelectPadding(self,checked):
00558                 
00559                 self._paddingspinbox.setEnabled(not self._paddingspinbox.isEnabled())
00560 
00561         ##
00562         #  If the user change the device type, we will have to make a new
00563         #               guess for the syntax of the device name.
00564         #               
00565         def OnDeviceTypeChanged(self,event):
00566 
00567                 devtypename = str(self._devicetypetxtbox.currentText())
00568                 sysname = str(self._systemnametxtbox.text())[:-1] #Removing the last comma
00569                 devtype = DeviceType(sysname,devtypename,False)
00570                 devs = devtype.GetDevices() # 1. We get all the devices of the device type chosen
00571                 if devs == False:
00572                         self.main.ShowError("Failed to retrieve devices of the given device type: " + str(devtype.GetErrorMessage()),ERR_ERROR,True)
00573                         self._prefixtxtbox.setEnabled(True)
00574                         self._paddingchkbox.setEnabled(True)
00575                         self._startspinbox.setEnabled(True)
00576                         self._endspinbox.setEnabled(True)
00577 
00578                         self._prefixtxtbox.setText("")
00579                         self.devs = []
00580                 elif devs == []: #no devs found, first time, let user set all
00581                         self._prefixtxtbox.setEnabled(True)
00582                         self._paddingchkbox.setEnabled(True)
00583                         self._startspinbox.setEnabled(True)
00584                         self._endspinbox.setEnabled(True)
00585                         self._prefixtxtbox.setText("")
00586                         self.devs = []
00587                 else: #found devs
00588                         # 2. We have to find the number of the last device created (highest number), we sort the devices
00589                         # and assign the list of device names to a member variable so that it is accessible for
00590                         # MakeEstimatedGuess method
00591                         devs.sort()
00592                         self.devs = devs
00593                         self._prefixtxtbox.setEnabled(True)
00594                         self._paddingchkbox.setEnabled(True)
00595                         self._startspinbox.setEnabled(True)
00596                         self._endspinbox.setEnabled(True)
00597                         self._prefixtxtbox.setText(devs[0]) # 3. Set first device in the sorted list in the prefix txt box
00598                         self.MakeEstimatedGuess(None) # 4. Continue to guess prefix
00599         
00600         ##
00601         #  Add a subsystem (shown in the combo box) to the list of subsystems that the
00602         #               the device will be added to.
00603         #               
00604         def OnAdd(self):
00605                 
00606                 val = str(self._sysnamescmbbox.currentText())
00607                 if val != "":
00608                         if string.find(str(self._systemnametxtbox.text()),val,0) != -1:
00609                                 self.main.ShowError("The subsystem is already added to the subsystem list.",ERR_ERROR)
00610                         else:
00611                                 self._systemnametxtbox.setText(str(self._systemnametxtbox.text()) + val + ",")
00612                                                                                                                                                                        
00613         ##
00614         #  Remove a subsystem (shown in the combo box) from the list of
00615         #               subsystems that the device will be added to.
00616         #               
00617         def OnRemove(self):
00618                 
00619                 val = str(self._sysnamescmbbox.currentText())
00620                 if val != "":
00621                         if string.find(str(self._systemnametxtbox.text()),val,0) == -1:
00622                                 self.main.ShowError("This subsystem is not a part of the subsystem list, and cannot be removed.",ERR_ERROR)
00623                         else:
00624                                 myvals = str(self._systemnametxtbox.text())
00625                                 myvals = myvals.replace(val + ",","")
00626                                 self._systemnametxtbox.setText(myvals)
00627 
00628         ##
00629         #  When a device type has been chosen, this method is called from the 
00630         #               OnDeviceTypeChanged(...) method, to make an estimated guess
00631         #               of what the prefix/syntax for the next device that will be added.
00632         #               We also set up padding and startnr to what we think is the correct.
00633         #               
00634         def MakeEstimatedGuess(self,event=None):
00635 
00636                 # 1. We get the value set in the prefix text box by the OnDeviceTypeChanged(...) method
00637                 # This value is the name of the first device created of the given device type.
00638                 syntax = str(self._prefixtxtbox.text())
00639                 if syntax == "":
00640                         self.main.ShowError("We didn't find any devices of the given device type, the user will have to fill out all fields him/her self",ERR_ERROR)
00641                         return
00642                 if self.devs == []:
00643                         self.main.ShowError("We didn't find any devices of the given device type, the user will have to fill out all fields him/her self",ERR_ERROR)
00644                         return
00645                 
00646                 # 2. Ok, we have enough information, we continue to guess..
00647                 i = len(syntax)-1
00648                 nrofindexint = []
00649                 prevwasint = False
00650                 while i >= 0: # a. We start looking for the ID (integer) in the device name string at the last position
00651                         try:
00652                                 nr = int(syntax[i]) # b. Check if we can convert the character in given pos to int..
00653                                 nrofindexint.insert(0,i) # c. We could convert to int, add to our list
00654                                 prevwasint = True # d. We found an integer all right
00655                         except ValueError,err:
00656                                 if prevwasint: # e. If the prev character was an integer, but not this, we found a whole
00657                                                # integer string in the device name, and can continue to work on it.
00658                                                # We don't need to look any more since we found our int, break the loop
00659                                         break
00660                         i -= 1
00661 
00662                 # 3. Did we find an integer?
00663                 if nrofindexint == []: # No..
00664                         return
00665 
00666                 # 4. Get nr substring
00667                 start = nrofindexint[0] # a. check first position in our list
00668                 end = nrofindexint[len(nrofindexint)-1] # b. check last position in our list
00669                 if start == end: # c. if both first and last position was the same index, it is only 1 number
00670                         substring = syntax[start] # get the number from the string
00671                 else: # d. more than 1 number in the number
00672                         substring = syntax[start:end+1] # get the number from the string
00673 
00674                 # 5. Split our device name string in a string before the number and a string after the number
00675                 if nrofindexint[0]-1 >= 0: # a. at least one character before the number?
00676                         startstr = syntax[0:start]
00677                 else:
00678                         startstr = ""
00679 
00680                 if len(syntax)-1 > end: # b. at least one character after the number?
00681                         endstr = syntax[end+1:len(syntax)]
00682                 else:
00683                         endstr = ""
00684 
00685                 # 6. Set prefix in prefix txt box, %d indicates where we found the number in the device name string
00686                 prefix = startstr + "%d" + endstr
00687                 self._prefixtxtbox.setText(prefix)
00688 
00689                 # 7. Set correct padding value
00690                 # Check if padding; 
00691                 # "003" -> int("003") -> 3 -> str(3) -> "3" -> "003" != "3" -> PADDING
00692                 # "3" -> int("3") -> 3 -> str(3) -> "3" -> "3" == "3" -> NO PADDING
00693                 if len(str(int(substring))) != len(str(substring)): # a. Padding was used. (substring is the number string)
00694                         nrofdigits = len(str(substring))
00695                         self._paddingchkbox.setChecked(True)
00696                         self._paddingspinbox.setValue(nrofdigits)
00697                         self._paddingspinbox.setEnabled(True)
00698 
00699                 '''# 8. Find start number of next device (need to find the highest number used for a device for this type)
00700                 nrs = []
00701                 for dev in self.devs: # self.devs is alphabetically sorted
00702                         # a. We replace string before and after number with "" (empty string), then we can easily
00703                         # extract the number, convert it to integer, and check if we have found the biggest number
00704                         # 2DO: a more efficient way to find the highest number without having to check every device name...
00705                         # (Perhaps: first check string length, then compare numbers)
00706                         if startstr != "":
00707                                 dev = dev.replace(startstr,"")
00708                         if endstr != "":
00709                                 dev = dev.replace(endstr,"")
00710 
00711                         try:
00712                                 nrs.append(int(dev))
00713                         except ValueError,err:
00714                                 self.main.ShowError("Some problem automatically finding the correct prefix, you will have to set the prefix yourself. \nStart number for new devices was not set.",ERR_ERROR,True)
00715                                 return
00716                 # 9. Sort the numbers found and pick out the last one -> the biggest one -> add 1 to get the start number
00717                 nrs.sort()
00718                 self._startspinbox.setValue(nrs[len(nrs)-1]+1)'''
00719 
00720         ##
00721         #  This method is either called when the user alters the prefix,
00722         #               or when the user wants to save the changes made. The user will
00723         #               only be informed about errors when he/she wants clicks on the
00724         #               OK button to create a device.
00725         # 
00726         #               The name of the method is a bit confusing, as the method just checks
00727         #               that you have a %d in the string if needed or not.
00728         # 
00729         #               Parameters:
00730         #               @showerror -    default (False), whether we should show an error message to the user or not
00731         #                               if it occurs.
00732         #               
00733         def GenerateNames(self,event=None,showerror=False):
00734                 
00735                 syntax = str(self._prefixtxtbox.text())
00736                 index = syntax.find("%d",0,len(syntax)) # We have maximum one %d in the prefix (do not need it if we
00737                                                         # only create one device
00738                 if index == -1 and showerror:
00739                         if self._endspinbox.value() == 1: # Do not need %d if only 1 device
00740                                 return True
00741                         else:
00742                                 self.main.ShowError("Couldn't find %d in the prefix, this HAS to be added because you are creating more than one device.",ERR_ERROR,True)
00743                                 self._startspinbox.setValue(0)
00744                                 return False
00745                 elif index == -1:
00746                         if str(self._endspinbox.text()) == 1: # Do not need %d if only 1 device
00747                                 return True
00748                         else:
00749                                 self._startspinbox.setValue(0)
00750                                 return False
00751                 else: # Did find %d
00752                         if index != 0:
00753                                 startstr = syntax[0:index]
00754                         else:
00755                                 startstr = ""
00756                         if index+1 != len(syntax)-1:
00757                                 endstr = syntax[index+2:len(syntax)]
00758                         else:
00759                                 endstr = ""
00760 
00761                 return True
00762 
00763         ##
00764         #  Easy validation before accepting user settings.
00765         #               We check that the required fields are filled out,
00766         #               that there are no device name conflicts,
00767         #               correct number of ports added to the device etc.
00768         # 
00769         #               !return - True if successful, False if not
00770         #                 
00771         def OkButtonClicked(self):
00772                 if self._systemnametxtbox.text() == "":
00773                         self.main.ShowError("No subsystems set!",ERR_ERROR,True)
00774                         return False
00775 
00776                 if self._devicetypetxtbox.currentText() == "":
00777                         self.main.ShowError("No device type set!",ERR_ERROR,True)
00778                         return False
00779 
00780                 if not self._creationmode: # Modify
00781                         if self._prefixtxtbox.text() == "":
00782                                 self.main.ShowError("No device name set!",ERR_ERROR,True)
00783                                 return False
00784                 else: # Create
00785                         if self._endspinbox.value()>1 and self._prefixtxtbox.text() == "":
00786                                 self.main.ShowError("No prefix set.",ERR_ERROR,True)
00787                                 return False
00788 
00789                 if self._creationmode:
00790                         success = self.GenerateNames(None,True) # Check that the prefix is correct
00791                         if not success:
00792                                 return False
00793 
00794                         # Check that we're creating a unique device and that we have same nr of ports created 
00795                         # as given by the device type.
00796                         # Actually...we should check if this device name is already in use, put then we
00797                         # will have to connect to the database and run such a query for each device we create
00798                         # or we will have to get a list of names of EVERY device in ConfDB (which can be 
00799                         # enourmous). We decided to do a thing that will work in most cases: check that no
00800                         # other devices of the same device type have the same name.
00801                         
00802                         devtype = DeviceType(str(self._systemnametxtbox.text())[:-1],str(self._devicetypetxtbox.currentText()),False)
00803                         # Check if correct number of ports is created.
00804                         if len(self._deviceports) > (devtype.GetPortsIn()+devtype.GetPortsOut()):
00805                                 self.main.ShowError("The number of ports that a device of the given device type has (" + str(devtype.GetPortsIn()+devtype.GetPortsOut()) + "), does not correspond to the number of ports that you have created for this device (" + str(len(self._deviceports)) + "). Click the create/modify ports button to remove ports.",ERR_ERROR,True)
00806                                 return False
00807 
00808 
00809                         # Check that the number ( and padding ) does not exceed the number of maximum digits set
00810                         # Also check that the device name is unique (for the given device type)
00811                         devs = devtype.GetDevices()
00812                         if devs == False:
00813                                 self.main.ShowError("Failed to fetch data from ConfDB: " + str(devtype.GetErrorMessage()),ERR_ERROR,True)
00814                                 return False
00815                         i = int(str(self._startspinbox.text()))
00816                         while i < (self._endspinbox.value() + self._startspinbox.value()):
00817                                 padding = int(str(self._paddingspinbox.text()))
00818                                 nrlength = len(str(i))
00819                                 if nrlength > padding and self._paddingchkbox.isChecked():
00820                                         self.main.ShowError("You have exceeded the maximum number of digits that you set as a maximum for the given device type.",ERR_ERROR,True)
00821                                         return False
00822                                 elif self._paddingchkbox.isChecked():
00823                                         padstr = "".zfill(padding-nrlength)
00824                                 else:
00825                                         padstr = ""
00826 
00827                                 devname = str(self._prefixtxtbox.text()).replace("%d",str(padstr)+str(i))
00828                                 if devname in devs:
00829                                         self.main.ShowError("The device " + str(devname) + " already exists. You have to change the range or delete the device with the same name in order to save a new one with that name.",ERR_ERROR,True)
00830                                         return False
00831                                 i += 1
00832 
00833 
00834                 # Finally, try to create the objects needed.
00835                 success = self.SaveObjects()
00836                 if not success:
00837                         return False
00838 
00839                 QDialog.accept(self) # Close window, let the calling code have access to public methods
00840 
00841         ##
00842         #  Whether we are creating or modifying a device.
00843         #               
00844         #               !return - True if creating, False if modifying
00845         #               
00846         def IsCreationMode(self):
00847                 return self._creationmode
00848 
00849         ##
00850         #  Return the objects that were created by the user in this dialog
00851         # 
00852         #               !return    -    Create: list of devices and ports 
00853         #                               Modify: the device modified, rename object if renamed, 
00854         #                                       ports if any were altered.
00855         #               
00856         def GetDeviceObjs(self):
00857                 if self._creationmode:
00858                         return self.deviceobjs,self._deviceports
00859                 else:
00860                         return self.deviceobjs[0],self.renameobj,self._deviceports,self._deletedports
00861 
00862 

Generated on Fri Aug 31 11:11:11 2007 for CDBVis by  doxygen 1.5.3