Example #1
0
class Phase10PlayerWidget(GamePlayerWidget):

    roundWinnerSet = QtCore.pyqtSignal(str)

    def __init__(self, nick, engine, bgroup=None, parent=None):
        self.engine = engine
        self.current_phase = min(
            self.engine.getRemainingPhasesFromPlayer(nick))
        self.phases_in_order = self.engine.getPhasesInOrderFlag()
        self.bgroup = bgroup
        super(Phase10PlayerWidget, self).__init__(
            nick, PlayerColours[self.engine.getListPlayers().index(nick)],
            parent)

    def initUI(self):
        css = ("QGroupBox {{ font-size: 28px;"
               "font-weight: bold; color:rgb({},{},{});}}")
        self.setStyleSheet(css.format(self.pcolour.red(),
                                      self.pcolour.green(),
                                      self.pcolour.blue()))
        self.setTitle(self.player)
        super(Phase10PlayerWidget, self).initUI()

        trashWidget = QWidget()
        trashWidget.setLayout(self.mainLayout)

        self.mainLayout = QVBoxLayout(self)
        self.mainLayout.addStretch()
        self.upperLayout = QHBoxLayout()
        self.mainLayout.addLayout(self.upperLayout)
        self.upperLayout.addStretch()
        self.phaseNameLabel = QLabel(self)
        self.phaseNameLabel.setStyleSheet(
            "font-weight: bold; font-size: 24px;")
        self.updatePhaseName()
        self.upperLayout.addWidget(self.phaseNameLabel)
        self.upperLayout.addStretch()
        self.lowerLayout = QHBoxLayout()
        self.mainLayout.addLayout(self.lowerLayout)
        self.mainLayout.addStretch()

        self.phaseLabelsLayout = QGridLayout()
        self.phaseLabelsLayout.setSpacing(5)

        self.checkboxLayout = QVBoxLayout()

        self.scoreLCD = QLCDNumber(self)
        self.scoreLCD.setSegmentStyle(QLCDNumber.Flat)
        self.mainLayout.addWidget(self.scoreLCD)
        self.scoreLCD.setNumDigits(3)
        self.scoreLCD.setMinimumWidth(100)
        css = "QLCDNumber {{ color:rgb({},{},{});}}"
        self.scoreLCD.setStyleSheet(css.format(self.pcolour.red(),
                                               self.pcolour.green(),
                                               self.pcolour.blue()))

        # Left part - score
        self.lowerLayout.addWidget(self.iconlabel)
        self.lowerLayout.addWidget(self.scoreLCD)
        self.lowerLayout.addLayout(self.phaseLabelsLayout)
        self.lowerLayout.addLayout(self.checkboxLayout)

        self.iconlabel.setMinimumSize(60, 60)

#         self.scoreLCD.setMinimumWidth(100)
#         self.scoreLCD.setMaximumWidth(200)
#         self.scoreLCD.setMinimumHeight(60)
#         self.scoreLCD.setMaximumHeight(80)

        self.scoreLCD.display(self.engine.getScoreFromPlayer(self.player))

        # Middle part - Phase list
        self.phaseLabels = list()
        for phase in range(1, 11):
            label = Phase10Label(phase, self)
            if phase == self.current_phase:
                label.setCurrent()
            elif self.engine.hasPhaseCompleted(self.player, phase):
                label.setPassed()
            self.phaseLabels.append(label)
            self.phaseLabelsLayout.addWidget(
                label, (phase-1)/5, (phase-1) % 5, 1, 1)

        # Middle part - Inputs
        self.roundWinnerRadioButton = QRadioButton()
        self.bgroup.addButton(self.roundWinnerRadioButton)
        self.checkboxLayout.addWidget(self.roundWinnerRadioButton)

        self.roundPhaseClearedCheckbox = QCheckBox(self)
        self.checkboxLayout.addWidget(self.roundPhaseClearedCheckbox)

        self.roundScore = Phase10ScoreSpinBox(self)
        self.roundScore.setMaximumWidth(90)
        self.roundScore.valueChanged.connect(self.updateRoundPhaseCleared)
        self.lowerLayout.addWidget(self.roundScore)

        self.roundWinnerRadioButton.toggled.connect(
            self.roundScore.setDisabled)
        self.roundWinnerRadioButton.toggled.connect(
            self.roundPhaseClearedCheckbox.setDisabled)
        self.roundWinnerRadioButton.toggled.connect(
            self.roundPhaseClearedCheckbox.setChecked)
        self.roundWinnerRadioButton.toggled.connect(self.roundWinnerSetAction)

        self.retranslateUI()

    def retranslateUI(self):
        self.roundWinnerRadioButton.setText(
            i18n("Phase10PlayerWidget", "Winner"))
        self.roundPhaseClearedCheckbox.setText(
            i18n("Phase10PlayerWidget", "Completed"))
        self.updatePhaseName()

    def updateDisplay(self, points, completed_phases, remaining_phases):
        if len(remaining_phases) == 0:
            self.current_phase = 0
        else:
            self.current_phase = min(remaining_phases)

        self.roundWinnerRadioButton.setDown(True)
        if points >= 1000:
            self.scoreLCD.setNumDigits(4)
        self.scoreLCD.display(points)
        self.roundScore.setValue(5)
        self.roundScore.clear()
        self.roundPhaseClearedCheckbox.setChecked(False)

        for phase, label in enumerate(self.phaseLabels, start=1):
            if phase == self.current_phase and not self.engine.getWinner():
                if not label.isCurrent():
                    label.setCurrent()
            elif phase in completed_phases:
                if not label.isPassed():
                    label.setPassed()
            else:
                if not label.isRemaining():
                    label.setRemaining()
        self.updatePhaseName()

    def getScore(self):
        if self.isRoundWinner():
            return 0
        try:
            return int(self.roundScore.value())
        except Exception:
            return -1

    def switchPhasesInOrder(self, in_order):
        self.phases_in_order = in_order
        if not self.phases_in_order:
            return
        self.phaseLabels[self.current_phase-1].setRemaining()
        for label in self.phaseLabels:
            if label.isRemaining():
                label.setCurrent()
                self.current_phase = label.getNumber()
                break

    def updatePhaseSelected(self, phaselabel):
        if phaselabel.isRemaining():
            self.current_phase = phaselabel.getNumber()
            for label in self.phaseLabels:
                if label.isCurrent():
                    label.setRemaining()
            phaselabel.setCurrent()
        self.updatePhaseName()

    def updatePhaseName(self):
        phasenames = getPhaseNames(self.engine.getPhases())
        self.phaseNameLabel.setText(phasenames[self.current_phase-1])

    def updateRoundPhaseCleared(self, value):
        try:
            score = int(self.roundScore.text())
        except Exception:
            self.roundPhaseClearedCheckbox.setChecked(False)
            return

        if score < 0:
            if not self.roundWinnerRadioButton.isChecked():
                self.roundPhaseClearedCheckbox.setChecked(False)
            return

        if (score % 5 != 0):
            return

        if (score >= 50):
            self.roundWinnerRadioButton.setChecked(False)
            self.roundPhaseClearedCheckbox.setChecked(False)
        elif (score == 0):
            self.roundWinnerRadioButton.setChecked(True)
            self.roundPhaseClearedCheckbox.setChecked(True)
        else:
            self.roundWinnerRadioButton.setChecked(False)
            self.roundPhaseClearedCheckbox.setChecked(True)

    def mousePressEvent(self, event):
        child = self.childAt(event.pos())
        if child is None:
            self.roundScore.setFocus()
        elif type(child) == Phase10Label and not self.phases_in_order:
            self.updatePhaseSelected(child)
        return QGroupBox.mousePressEvent(self, event)
#

    def isRoundWinner(self): return self.roundWinnerRadioButton.isChecked()

    def getRoundPhase(self): return self.current_phase

    def isRoundCleared(self): return self.roundPhaseClearedCheckbox.isChecked()

    def roundWinnerSetAction(self, isset):
        if isset:
            self.roundWinnerSet.emit(self.player)

    def reset(self): pass

    def finish(self):
        self.roundWinnerRadioButton.toggled.disconnect(
            self.roundScore.setDisabled)
        self.roundWinnerRadioButton.toggled.disconnect(
            self.roundPhaseClearedCheckbox.setDisabled)
        self.roundWinnerRadioButton.toggled.disconnect(
            self.roundPhaseClearedCheckbox.setChecked)
        self.roundWinnerRadioButton.toggled.disconnect(
            self.roundWinnerSetAction)
        self.roundWinnerRadioButton.setDisabled(True)
        self.roundPhaseClearedCheckbox.setDisabled(True)
        self.roundScore.setDisabled(True)

    def setColour(self, colour):
        self.pcolour = colour
        css = ("QGroupBox {{ font-size: 28px; font-weight: bold;"
               "color:rgb({},{},{});}}")
        self.setStyleSheet(css.format(self.pcolour.red(),
                                      self.pcolour.green(),
                                      self.pcolour.blue()))