C:/mesDocs/QT_confDbVis_canvas/CreateDeviceType.py

Go to the documentation of this file.
00001 import string   # Needed for string manipulation
00002 
00003 
00004 from cdbVisCore import *    # Constants
00005 from objectclasses import * # Data objects
00006 import pdb
00007 
00008 
00009 ###################################################################
00010 # CreateDeviceTypeWindow class                                  ###
00011 ###################################################################
00012 
00013 # CONSTANTS, WIDGET IDs
00014 INPUTSPINBOX = 101
00015 OUTPUTSPINBOX = 102
00016 DEVICETYPENAME = 103
00017 COLORBUTTON = 104
00018 TXTINPUTNBR = 105
00019 TXTOUTPUTNBR = 106
00020 DEVTYPE_DESCRIPTION = 107
00021 SYSNAME_CMBBOX = 108
00022 DEVTYPEID = 109
00023 
00024 
00025 ##
00026 #  This dialog window and class lets the user create or modify a device type. It is set
00027 #           to be modal.
00028 #       
00029 class CreateDeviceTypeWindow(QDialog):
00030 
00031         ##
00032         #  The constructor.
00033         #               
00034         #                   Parameters:
00035         #                   @parent - parent window to this window (usually MainWindow)
00036         #                   @id - id of this window, set by its parent
00037         #                   @selectedsystem - the subsystem to create this device type in; 
00038         #                                     the user can also add others in the dialog
00039         #                   @creationmode - if True; a device type is being created
00040         #                                   if False; a device type is being modified (already created)
00041         #                   @devicetypeobj - if we are modifying a device type (creationmode=False), 
00042         #                                    then we have to send the device type object that we want to
00043         #                                    modify; to show the settings/attributes to the user so he/she
00044         #                                    can modify them
00045         #                   @devicetypes - both when we are creating and modifying a device type we send a 
00046         #                                  list of names of currently available devicetypes; so that we can 
00047         #                                  check that the name we don't get any name conflicts when creating
00048         #                                  or renaming a device type
00049         #               
00050         def __init__(self,parent,name,selectedsystem,creationmode=True,devicetypeobj=None,devicetypes=[]):
00051                 QDialog.__init__(self,parent,name,0,0)
00052                 Form1Layout = QGridLayout(self,1,1,11,6,"Form1Layout")
00053                 spacingButtons = QSpacerItem(110,20,QSizePolicy.Expanding,QSizePolicy.Minimum)
00054                 Form1Layout.addMultiCell(spacingButtons,9,9,0,1)
00055 
00056                 self.buttonOk = QPushButton(self,"buttonOk")
00057                 self.buttonOk.setAutoDefault(1)
00058                 self.buttonOk.setDefault(1)
00059 
00060                 '''########if not self.__creationmode:
00061                         devtypeidlayout = wxBoxSizer(wxVERTICAL)
00062                         devtypeidlayout.Add(self.__devicetypeidcaption)
00063                         devtypeidlayout.Add(self.__devtypeidtxtbox)###############'''
00064                         
00065                 Form1Layout.addMultiCellWidget(self.buttonOk,9,9,2,4)
00066 
00067                 self.buttonCancel = QPushButton(self,"buttonCancel")
00068                 self.buttonCancel.setAutoDefault(1)
00069                 
00070                 Form1Layout.addMultiCellWidget(self.buttonCancel,9,9,5,6)
00071 
00072                 self.description = QTextEdit(self,"Description")
00073 
00074                 Form1Layout.addMultiCellWidget(self.description,8,8,0,6)
00075 
00076                 self.__nbrinputspinbutton = QSpinBox(self,"Inputnumber")
00077 
00078                 Form1Layout.addWidget(self.__nbrinputspinbutton,6,1)
00079                 spacinginputOutput = QSpacerItem(51,20,QSizePolicy.Expanding,QSizePolicy.Minimum)
00080                 Form1Layout.addMultiCell(spacinginputOutput,6,6,2,3)
00081 
00082                 self.outputLabel = QLabel(self,"outputLabel")
00083 
00084                 Form1Layout.addMultiCellWidget(self.outputLabel,6,6,4,5)
00085 
00086                 self.__nbroutputspinbutton = QSpinBox(self,"Outputnumber")
00087 
00088                 Form1Layout.addWidget(self.__nbroutputspinbutton,6,6)
00089 
00090                 self.addsybsystemLabel = QLabel(self,"addsybsystemLabel")
00091 
00092                 Form1Layout.addMultiCellWidget(self.addsybsystemLabel,4,4,0,1)
00093 
00094                 self.inputLabel = QLabel(self,"inputLabel")
00095 
00096                 Form1Layout.addWidget(self.inputLabel,6,0)
00097 
00098                 self.descriptionLabel = QLabel(self,"descriptionLabel")
00099 
00100                 Form1Layout.addMultiCellWidget(self.descriptionLabel,7,7,0,1)
00101                 spacingNameColor = QSpacerItem(31,20,QSizePolicy.Minimum,QSizePolicy.Minimum)
00102                 Form1Layout.addItem(spacingNameColor,1,3)
00103 
00104                 self.__systemnamecaption = QLabel(self,"__systemnamecaption")
00105 
00106                 Form1Layout.addMultiCellWidget(self.__systemnamecaption,2,2,0,1)                
00107                 
00108                 self.__devicetypetxtbox = QLineEdit(self,"devicetypenameEdit")
00109 
00110                 Form1Layout.addMultiCellWidget(self.__devicetypetxtbox,1,1,0,2)
00111 
00112                 self.__systemnametxtbox = QLineEdit(self,"__systemnametxtbox")
00113                 Form1Layout.addMultiCellWidget(self.__systemnametxtbox,3,3,0,6)                         
00114 
00115                 self.__sysnamescmbbox = QListBox(self,"__sysnamescmbbox")
00116 
00117                 Form1Layout.addMultiCellWidget(self.__sysnamescmbbox,5,5,0,6)
00118 
00119                 self.__colorbutton = QPushButton(self,"__colorbutton")
00120 
00121                 Form1Layout.addMultiCellWidget(self.__colorbutton,1,1,4,6)
00122 
00123                 self.__devicetypenameLabel = QLabel(self,"DeviceNameLabel")
00124 
00125                 Form1Layout.addMultiCellWidget(self.__devicetypenameLabel,0,0,0,1)
00126 
00127                 self.colorLabel = QLabel(self,"colorLabel")
00128 
00129                 Form1Layout.addMultiCellWidget(self.colorLabel,0,0,4,5)
00130                 self.languageChange()
00131                 
00132                 self.connect(self.buttonCancel,SIGNAL("released()"),self.reject)
00133                 self.connect(self.buttonOk,SIGNAL("released()"),self.OkButtonClicked)
00134                 self.connect(self.__colorbutton,SIGNAL("released()"),self.getColor)
00135                 self.connect(self.__sysnamescmbbox,SIGNAL("doubleClicked(QListBoxItem*)"),self.AddSubSystem)
00136                 
00137         #### END GUI PART
00138                 self.__devicetypes = devicetypes
00139                 self.__creationmode = creationmode
00140                 self.main = parent # MainWindow is _always_ the parent.
00141                                                                                                                              
00142                 if creationmode == True:
00143                         title = "Create A DeviceType" # Title bar title
00144                         selectedsystem = selectedsystem
00145                 else:
00146                         title = "Modify A DeviceType"
00147                         self.devicetypeobj = devicetypeobj # The device type object to modify
00148                         selectedsystem = self.devicetypeobj.GetSystem() # System(s) this device type is stored in
00149                         self.renameobj = None # A rename object in case we rename the device type
00150                         self.devicetypeid = self.devicetypeobj.GetDeviceTypeID() # ID the device type is given in ConfDB (or -1 if not)
00151                 self.__systemnametxtbox.setText(selectedsystem)
00152                 self.setCaption(title)
00153                 
00154                 tmpsystems = self.main.GetCdb().GetSubSystems() # All available subsystems in ConfDB, as objects
00155                 subsystems = [] # Store the names of the available subsystems here
00156                 for tmp in tmpsystems:
00157                         subsystems.append(tmp.GetName())
00158 
00159                 if self.__creationmode: # Possibility to change/add subsystems if the device type is created
00160                         self.__sysnamescmbbox.clear()
00161                         # Add subsystemname to list of subsystems where this devicetype will be stored in
00162                         for subsystem in subsystems:    self.__sysnamescmbbox.insertItem(subsystem)
00163                 else:
00164                         self.__systemnametxtbox.setEnabled(False) # Can not change if device type is being modified
00165                 
00166                 if not self.__creationmode: # Device type name and ID if being modified
00167                         selected = self.devicetypeobj.GetName()
00168                         selid = self.devicetypeobj.GetDeviceTypeID() #not visible if not stored in db yet
00169                 else:
00170                         selected = ""
00171                         style = 0
00172 
00173                 if not self.__creationmode:  ### 1 un champs en plus quand on modifie ?????
00174                         self.__devicetypeidcaption = wxStaticText(self.__panel,-1,"Device Type ID:")
00175                         self.__devtypeidtxtbox = wxTextCtrl(self.__panel,DEVTYPEID,str(selid),wxDefaultPosition,wxSize(50,-1),style=wxTE_READONLY)
00176                         self.__devtypeidtxtbox.Disable()
00177 
00178                 #2 spin wheels, select number of input and output to this device type
00179                 if creationmode:
00180                         val_in = 0
00181                         val_out = 0
00182                 else:
00183                         val_in = int(devicetypeobj.GetPortsIn())
00184                         val_out = int(devicetypeobj.GetPortsOut())
00185 
00186                 if creationmode == True:
00187                         self.__colorselected = (255,0,0) #default: red
00188                         self.__colorbutton.setPaletteBackgroundColor(Qt.red)
00189                 else:
00190                         tmpcolor = devicetypeobj.GetColour()
00191                         self.__colorbutton.setPaletteBackgroundColor(tmpcolor)
00192                         self.__colorselected = ColorToTuple(rmpcolor)
00193                 #description text area
00194                 if creationmode == False:
00195                         self.description.setText(devicetypeobj.GetDescription())
00196 
00197         
00198         def getColor(self):
00199                         newColor =  QColorDialog.getColor(Qt.red,self)
00200                         if newColor.isValid():
00201                                 self.__colorselected = ColorToTuple(newColor)
00202                                 self.__colorbutton.setPaletteBackgroundColor(newColor)
00203 
00204         def languageChange(self):
00205                 self.buttonOk.setText("&OK")
00206                 self.buttonOk.setAccel(QString.null)
00207                 self.buttonCancel.setText("&Cancel")
00208                 self.buttonCancel.setAccel(QString.null)
00209                 self.outputLabel.setText("Output ports:")
00210                 self.addsybsystemLabel.setText("Add to subsystem:")
00211                 self.inputLabel.setText("Input ports:")
00212                 self.descriptionLabel.setText("Description :")
00213                 self.__systemnamecaption.setText("Create in:")
00214                 self.__colorbutton.setText("Color")
00215                 self.__devicetypenameLabel.setText("Device Type Name:")
00216                 self.colorLabel.setText("Choose color:")
00217 
00218         ##
00219         #  If standard validation was successful, this method is called 
00220         #                   when the user clicks on the Ok button. Here we create the 
00221         #                   device type object (and a rename object if renamed), and
00222         #                   do some more excessive validation on all values set for
00223         #                   this object.
00224         # 
00225         #                   !return -  False if unsuccessful, True if successful
00226         #               
00227         def SaveObject(self):
00228 
00229                 trackValueError = 0 # Needed so that we can track down where the ValueError occured (3 possibilities)
00230 
00231                 if self.__creationmode:
00232                         create = True
00233                 else:
00234                         create = False
00235 
00236                 # We set up a try-catch to catch validation or other (value) errors that may occur.
00237                 try:
00238                         if self.__creationmode: # Create
00239                                 if str(self.__devicetypetxtbox.text()) in self.__devicetypes:
00240                                         raise ValidationError,"The devicetype cannot be created as the name you have given is already in use."
00241                         else: # Modify
00242                                 if (self.__devicetypetxtbox.text()) != self.devicetypeobj.GetName(): # Renamed
00243                                         ans = wxMessageBox("You have renamed the device type, was that your intention? Remember that you can not undo renaming, \nbut you can rename the object as many times as you want later on.","Rename?",wxYES_NO | wxICON_QUESTION)
00244                                         if ans == wxNO:
00245                                                 self.__devicetypetxtbox.SetValue(self.devicetypeobj.GetName())
00246                                                 return False # User didn't want to rename; return and let user make up his mind
00247                                         if self.__devicetypetxtbox.GetValue() in self.__devicetypes:
00248                                                 raise ValidationError,"The device type cannot be created as the name you have given is already in use."
00249                                         # Modify, and the user renamed the device; we create a rename object to put in the rename objects list
00250                                         self.renameobj = DeviceType(self.__systemnametxtbox.GetValue(),self.__devicetypetxtbox.GetValue(),True)
00251                                         self.renameobj.SetOldName(self.devicetypeobj.GetName()) # The old name before rename
00252                                         self.renameobj.SetSaveStatus(RENAME) #RENAME status
00253 
00254                         # Create or modify, we'll have to set all attributes/properties anyway in a "new" object
00255                         self.devicetypeobj = DeviceType(str(self.__systemnametxtbox.text()),str(self.__devicetypetxtbox.text()),create)
00256 
00257                         self.devicetypeobj.SetName(str(self.__devicetypetxtbox.text()))
00258                         self.devicetypeobj.SetSystem(str(self.__systemnametxtbox.text()))
00259                         self.devicetypeobj.SetPortsIn(int(str(self.__nbrinputspinbutton.text()))) # NB, Integer conversion
00260                         trackValueError = 1
00261                         self.devicetypeobj.SetPortsOut(int(str(self.__nbroutputspinbutton.text()))) # NB, Integer conversion
00262                         trackValueError = 2
00263                         self.devicetypeobj.SetDescription(str(self.description.text()))
00264                         self.devicetypeobj.SetColour(self.__colorselected)
00265                         
00266                         # Modify: Set the device type id (if already stored in db)
00267                         if not self.__creationmode:
00268                                 if str(self.devicetypeid) != "" and str(self.devicetypeid) != "-1":
00269                                         self.devicetypeobj.SetDeviceTypeID(int(self.devicetypeid)) # Convert to integer if it was string
00270                         # Everything successfully finished; we may continue.
00271                         return True
00272 
00273                 except ValidationError,err:
00274                         self.main.ShowError(str(err),ERR_ERROR,True)
00275                         return False
00276                 
00277                 except ValueError,err:
00278                         if trackValueError == 0:
00279                                 self.main.ShowError("The value given for number of ports in is not an integer.",ERR_ERROR,True)
00280                         elif trackValueError == 1:
00281                                 self.main.ShowError("The value given for number of ports out is not an integer.",ERR_ERROR,True)
00282                         else:
00283                                 self.main.ShowError("The value given for the device type ID is not an integer.",ERR_ERROR,True)
00284                         return False
00285 
00286         ##
00287         #  As a reaction to the event that the user pressed the Ok button.
00288         #                   We only do some simple validation here; f. ex. that the required fields
00289         #                   are set.
00290         #                   
00291         #                   !return - False if unsuccessful (will not close the window)
00292         #                 
00293         def OkButtonClicked(self):
00294 
00295                 if str(self.__devicetypetxtbox.text()) == "":
00296                         self.main.ShowError("No device type name chosen.",ERR_ERROR,True) #force message
00297                         return False
00298                 
00299                 success = self.SaveObject() # More validation and Create device type object (and rename object)
00300                 if not success:
00301                         return False
00302                 else:
00303                         self.accept()
00304 
00305         ##
00306         #  Add a subsystem to the systemname txtbox of subsystems that can be chosen 
00307         #                   that this device type should be added to.
00308         #               
00309         def AddSubSystem(self,item):
00310                 
00311                 if string.find(str(self.__systemnametxtbox.text()),str(item.text()),0) != -1:
00312                         self.main.ShowError("Subsystem already added!",ERR_ERROR,False) # If we try to add a subsystem that is already in the subsystem txtbox
00313                 else:
00314                         self.__systemnametxtbox.setText(str(self.__systemnametxtbox.text()) + "," + str(item.text()))
00315 
00316         ##
00317         #  Whether we are creating or modifying a device type.
00318         #                   !return - True if we are creating, False if we are modifying
00319         #               
00320         def IsCreationMode(self):
00321                 return self.__creationmode
00322                                                                                                                                                                        
00323         '''def ColorButtonClicked(self,event):
00324                 """ User clicks on the colour button and chooses a color for the current device type.
00325                 """
00326                 
00327                 colordialog = wxColourDialog(self)                                                                                                                                                       
00328                 if colordialog.ShowModal() == wxID_OK:
00329                         colorobj = colordialog.GetColourData()
00330                         color = colorobj.GetColour()
00331                         self.__colorbutton.SetBackgroundColour(color)
00332                         self.__colorselected = wxColour(red=color.Red(),green=color.Green(),blue=color.Blue()) # We create a wxColour(R,G,B) object to store the color.
00333         '''             
00334         ##
00335         #  Called from outside this class (by the caller of this window)
00336         #                   to get the created device type or the modified device type
00337         #                   (and renamed device type) to be able to take action on them.
00338         # 
00339         #                   !return - If creating device type: the device type object
00340         #                             If modifying device type: the device type object,
00341         #                             and a rename object if renamed (the rename object
00342         #                             is None if not renamed)
00343         #               
00344         def GetDeviceTypeObj(self):
00345                 
00346                 if self.__creationmode:
00347                         return self.devicetypeobj
00348                 else:
00349                         return self.devicetypeobj, self.renameobj
00350 
00351 
00352 

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