def _initChildren(self): self.customNameLe = QtGui.QLineEdit(self._model.CustomName()) self.isPrimaryDetachmentCb = QtGui.QCheckBox() if self._model.IsPrimary(): self.isPrimaryDetachmentCb.setChecked(True) self.unitTable = UnitTable(self._model, self.ctrl) self.pointsLbl = QtGui.QLabel("<b>0</b>") self.addUnitPb = QtGui.QPushButton( QtGui.QIcon( os.path.join(globals.BASEDIR, "data", "icons", "plus.png")), "&Add") self.duplicateUnitPb = QtGui.QPushButton( QtGui.QIcon( os.path.join(globals.BASEDIR, "data", "icons", "copy.png")), "Dupli&cate") self.duplicateUnitPb.setEnabled(False) self.deleteUnitPb = QtGui.QPushButton( QtGui.QIcon( os.path.join(globals.BASEDIR, "data", "icons", "delete.png")), "&Delete") self.deleteUnitPb.setEnabled(False) #========================================================================= # group boxes #========================================================================= self.generalGb = QtGui.QGroupBox("General") self.detailsGb = QtGui.QGroupBox("Details") self.detailsGb.setMinimumWidth(400) self.unitGb = QtGui.QGroupBox("Units")
class DetachmentView(View): """ Widget showing a single kow.Detachment instance and allowing to edit it. Usually used within an ArmyListView, which will show each of its DetachmentViews in a TabWidget. """ siModified = QtCore.Signal(bool) siNameChanged = QtCore.Signal(str) def __init__(self, model, ctrl, parent=None): View.__init__(self, ctrl, parent) # ctrl is an ArmyListCtrl self._model = model # Detachment self._initChildren() self._initLayout() self._initConnections() self.UpdateContent() def _initChildren(self): self.customNameLe = QtGui.QLineEdit(self._model.CustomName()) self.isPrimaryDetachmentCb = QtGui.QCheckBox() if self._model.IsPrimary(): self.isPrimaryDetachmentCb.setChecked(True) self.unitTable = UnitTable(self._model, self.ctrl) self.pointsLbl = QtGui.QLabel("<b>0</b>") self.addUnitPb = QtGui.QPushButton( QtGui.QIcon( os.path.join(globals.BASEDIR, "data", "icons", "plus.png")), "&Add") self.duplicateUnitPb = QtGui.QPushButton( QtGui.QIcon( os.path.join(globals.BASEDIR, "data", "icons", "copy.png")), "Dupli&cate") self.duplicateUnitPb.setEnabled(False) self.deleteUnitPb = QtGui.QPushButton( QtGui.QIcon( os.path.join(globals.BASEDIR, "data", "icons", "delete.png")), "&Delete") self.deleteUnitPb.setEnabled(False) #========================================================================= # group boxes #========================================================================= self.generalGb = QtGui.QGroupBox("General") self.detailsGb = QtGui.QGroupBox("Details") self.detailsGb.setMinimumWidth(400) self.unitGb = QtGui.QGroupBox("Units") def _initLayout(self): #self.setWindowTitle(("*" + name)) # general gb genlay = QtGui.QGridLayout() row = 0 genlay.addWidget(QtGui.QLabel("Detachment name:"), row, 0) genlay.addWidget(self.customNameLe, row, 1) row += 1 genlay.addWidget(QtGui.QLabel("Primary detachment:"), row, 0) genlay.addWidget(self.isPrimaryDetachmentCb, row, 1) row += 1 self.generalGb.setLayout(genlay) # units gb self.unitGb.setLayout(QtGui.QVBoxLayout()) unitButtonsLay = QtGui.QHBoxLayout() unitButtonsLay.addWidget(self.addUnitPb) unitButtonsLay.addWidget(self.duplicateUnitPb) unitButtonsLay.addWidget(self.deleteUnitPb) #unitButtonsLay.addWidget(self.optionsPb) unitButtonsLay.addStretch(1) self.unitGb.layout().addLayout(unitButtonsLay) self.unitGb.layout().addWidget(self.unitTable) # details gb detlay = QtGui.QGridLayout() self.detailsGb.setLayout(detlay) row = 0 detlay.addWidget(QtGui.QLabel("Force:"), row, 0) detlay.addWidget( QtGui.QLabel("<b>%s</b>" % self._model.Choices().Name()), row, 1) row += 1 detlay.addWidget(QtGui.QLabel("Alignment:"), row, 0) detlay.addWidget( QtGui.QLabel("<b>%s</b>" % self._model.Choices().AlignmentName()), row, 1) row += 1 detlay.addWidget(QtGui.QLabel("Total points:"), row, 0) detlay.addWidget(self.pointsLbl) row += 1 mainlay = QtGui.QGridLayout() mainlay.addWidget(self.generalGb, 0, 0) mainlay.addWidget(self.detailsGb, 0, 1) mainlay.addWidget(self.unitGb, 1, 0, 1, 2) self.generalGb.setFixedHeight(150) self.detailsGb.setFixedHeight(150) mainlay.setRowStretch(0, 0) mainlay.setRowStretch(1, 1) mainlay.setRowMinimumHeight(1, 400) self.setLayout(mainlay) def _initConnections(self): self.customNameLe.editingFinished.connect(self.RenameDetachment) self.addUnitPb.clicked.connect(self.AddUnit) self.deleteUnitPb.clicked.connect(self.DeleteUnit) self.duplicateUnitPb.clicked.connect(self.DuplicateUnit) self.unitTable.itemSelectionChanged.connect(self.UnitSelectionChanged) self.unitTable.siModified.connect(self.SetModified) self.unitTable.siAddUnitRequested.connect(self.AddUnitDragAndDrop) self.isPrimaryDetachmentCb.clicked.connect(self.TogglePrimary) def AddUnit(self): cmd = AddDefaultUnitCmd(self.ctrl.model, self._model) self.ctrl.AddAndExecute(cmd) def AddUnitDragAndDrop(self, unitname): cmd = AddSpecificUnitCmd(self.ctrl.model, self._model, unitname) self.ctrl.AddAndExecute(cmd) def DeleteUnit(self): delUnits = self.unitTable.SelectedUnits() if len(delUnits) == 1: confirm = "This will delete the current unit.<br />Are you sure?" elif len(delUnits) > 1: confirm = "This will delete %d units.<br />Are you sure?" % len( delUnits) if QtGui.QMessageBox.Yes != QtGui.QMessageBox.warning( self, "Delete unit(s)", confirm, QtGui.QMessageBox.Yes | QtGui.QMessageBox.Cancel): return cmd = DeleteUnitCmd(self.ctrl.model, self._model, delUnits) self.ctrl.AddAndExecute(cmd) self.unitTable.clearSelection() def DuplicateUnit(self): cmd = DuplicateUnitCmd(self.ctrl.model, self._model, self.unitTable.SelectedUnits()) self.ctrl.AddAndExecute(cmd) def RenameDetachment(self): newName = self.customNameLe.text() if newName != self._model.CustomName(): cmd = RenameDetachmentCmd(self.ctrl.model, self._model, newName) self.ctrl.AddAndExecute(cmd) def SetModified(self, modified=True): self.siModified.emit(modified) def TogglePrimary(self): det = self._model makePrimary = self.isPrimaryDetachmentCb.isChecked() cmd = SetPrimaryDetachmentCmd(self.ctrl.model, det, makePrimary) self.ctrl.AddAndExecute(cmd) def UnitSelectionChanged(self): rows = self.unitTable.SelectedRows() if len(rows) > 0: self.duplicateUnitPb.setEnabled(True) self.deleteUnitPb.setEnabled(True) else: self.duplicateUnitPb.setEnabled(False) self.deleteUnitPb.setEnabled(False) def UpdateContent(self, hints=None): self.isPrimaryDetachmentCb.setChecked(self._model.IsPrimary()) self._UpdatePoints() # check if unit table must be updated if hints is None or len(hints) == 0: updateUnitTable = True # no hints ^= global update else: updateUnitTable = False for hint in hints: if (hint.IsType(ALH.ModifyDetachmentHint) and hint.which is self._model) \ or (hint.IsType(ALH.ModifyUnitHint) and hint.which in self._model.ListUnits()): updateUnitTable = True break if updateUnitTable: if self.unitTable.rowCount() > self._model.NumUnits(): self.unitTable.setRowCount(self._model.NumUnits()) for i in range(self._model.NumUnits()): self._UpdateUnit(i) # "private" functions def _UpdatePoints(self): pts = self._model.PointsTotal() self.pointsLbl.setText("<b>%d</b>" % pts) def _UpdateUnit(self, index): self.unitTable.SetRow(index, self._model.ListUnits()[index])