コード例 #1
0
ファイル: newgame.py プロジェクト: trawl/gamelog
 def changeGame(self, game):
     self.game = game
     self.engine = ResumeEngine(game)
     self.savedlist.clear()
     self.matches = []
     candidates = self.engine.getCandidates()
     if not candidates:
         self.savedlist.hide()
         self.resumebutton.hide()
         self.cancelbutton.hide()
         self.emptyLabel.show()
     else:
         self.emptyLabel.hide()
         for idMatch, candidate in candidates.items():
             self.matches.append(idMatch)
             savedtime = datetime.datetime.strptime(
                 candidate['started'], "%Y-%m-%d %H:%M:%S.%f")
             strtime = savedtime.strftime("%Y-%m-%d %H:%M:%S")
             hours, remainder = divmod(int(candidate['elapsed']), 3600)
             minutes, seconds = divmod(remainder, 60)
             strelapsed = "{0:02}:{1:02}:{2:02}".format(
                 hours, minutes, seconds)
             msg = i18n("ResumeBox",
                        'Saved on {}. Time played: {}').format(strtime,
                                                               strelapsed)
             item = QListWidgetItem(msg, self.savedlist)
             playerlist = ""
             for player in candidate['players']:
                 playerlist += "\n  " + player
             item.setToolTip(i18n(
                 "ResumeBox", "Players: {}").format(playerlist))
             self.savedlist.addItem(item)
         self.savedlist.show()
         self.resumebutton.show()
         self.cancelbutton.show()
コード例 #2
0
ファイル: newgame.py プロジェクト: trawl/gamelog
class ResumeBox(QGroupBox):

    restartRequested = pyqtSignal(QWidget)

    def __init__(self, parent):
        super(ResumeBox, self).__init__(parent)
        self.engine = None
        self.game = None
        self.parent = parent
        self.matches = []
        self.initUI()

    def initUI(self):
        self.widgetLayout = QVBoxLayout(self)
        self.savedlist = QListWidget(self)
        self.savedlist.setSelectionMode(
            QAbstractItemView.SingleSelection)
        self.savedlist.hide()
        self.widgetLayout.addWidget(self.savedlist)
        self.buttonLayout = QHBoxLayout()
        self.widgetLayout.addLayout(self.buttonLayout)
        self.resumebutton = QPushButton(self)
        self.resumebutton.clicked.connect(self.resumeGame)
        self.resumebutton.hide()
        self.buttonLayout.addWidget(self.resumebutton)
        self.cancelbutton = QPushButton(self)
        self.cancelbutton.clicked.connect(self.deleteGame)
        self.cancelbutton.hide()
        self.buttonLayout.addWidget(self.cancelbutton)
        self.emptyLabel = QLabel(self)
        self.widgetLayout.addWidget(self.emptyLabel)
        self.retranslateUI()

    def retranslateUI(self):
        self.setTitle(i18n(
            "ResumeBox", 'Saved Games'))
        self.resumebutton.setText(
            i18n("ResumeBox", 'Resume'))
        self.cancelbutton.setText(
            i18n("ResumeBox", 'Cancel'))
        self.emptyLabel.setText(i18n(
            "ResumeBox", 'No matches to be resumed'))

    def changeGame(self, game):
        self.game = game
        self.engine = ResumeEngine(game)
        self.savedlist.clear()
        self.matches = []
        candidates = self.engine.getCandidates()
        if not candidates:
            self.savedlist.hide()
            self.resumebutton.hide()
            self.cancelbutton.hide()
            self.emptyLabel.show()
        else:
            self.emptyLabel.hide()
            for idMatch, candidate in candidates.items():
                self.matches.append(idMatch)
                savedtime = datetime.datetime.strptime(
                    candidate['started'], "%Y-%m-%d %H:%M:%S.%f")
                strtime = savedtime.strftime("%Y-%m-%d %H:%M:%S")
                hours, remainder = divmod(int(candidate['elapsed']), 3600)
                minutes, seconds = divmod(remainder, 60)
                strelapsed = "{0:02}:{1:02}:{2:02}".format(
                    hours, minutes, seconds)
                msg = i18n("ResumeBox",
                           'Saved on {}. Time played: {}').format(strtime,
                                                                  strelapsed)
                item = QListWidgetItem(msg, self.savedlist)
                playerlist = ""
                for player in candidate['players']:
                    playerlist += "\n  " + player
                item.setToolTip(i18n(
                    "ResumeBox", "Players: {}").format(playerlist))
                self.savedlist.addItem(item)
            self.savedlist.show()
            self.resumebutton.show()
            self.cancelbutton.show()

    def resumeGame(self):
        selected = self.savedlist.selectedIndexes()
        if len(selected) > 0:
            idMatch = self.matches[selected[0].row()]
            gameengine = self.engine.resume(idMatch)
            matchTab = GameWidgetFactory.resumeGameWidget(
                self.game, gameengine, self.parent)
            if matchTab:
                matchTab.closeRequested.connect(self.parent.removeTab)
                matchTab.restartRequested.connect(self.restartGame)
                self.parent.newTab(matchTab, self.game)

    def restartGame(self, gamewidget):
        self.restartRequested.emit(gamewidget)

    def deleteGame(self):
        selected = self.savedlist.selectedIndexes()
        if len(selected) > 0:
            idMatch = self.matches[selected[0].row()]
            tit = i18n("ResumeBox", 'Cancel Saved Game')
            msg = i18n("ResumeBox",
                       "Are you sure you want to cancel saved game?")
            reply = QMessageBox.question(self, tit, msg,
                                         QMessageBox.Yes | QMessageBox.No,
                                         QMessageBox.No)

            if reply == QMessageBox.No:
                return False
            gameengine = self.engine.resume(idMatch)
            gameengine.cancelMatch()
            self.changeGame(self.game)