Пример #1
0
 def __fillLastTileComboWith(self, lastTiles, winnerTiles):
     """fill last meld combo with prepared content"""
     self.comboTilePairs = lastTiles
     idx = self.cbLastTile.currentIndex()
     if idx < 0:
         idx = 0
     indexedTile = variantValue(self.cbLastTile.itemData(idx))
     restoredIdx = None
     self.cbLastTile.clear()
     if not winnerTiles:
         return
     pmSize = winnerTiles[0].board.tileset.faceSize
     pmSize = QSize(pmSize.width() * 0.5, pmSize.height() * 0.5)
     self.cbLastTile.setIconSize(pmSize)
     QPixmapCache.clear()
     self.__tilePixMaps = []
     shownTiles = set()
     for tile in winnerTiles:
         if tile.tile in lastTiles and tile.tile not in shownTiles:
             shownTiles.add(tile.tile)
             self.cbLastTile.addItem(QIcon(tile.pixmapFromSvg(pmSize, withBorders=False)), "", toQVariant(tile.tile))
             if indexedTile is tile.tile:
                 restoredIdx = self.cbLastTile.count() - 1
     if not restoredIdx and indexedTile:
         # try again, maybe the tile changed between concealed and exposed
         indexedTile = indexedTile.exposed
         for idx in range(self.cbLastTile.count()):
             if indexedTile is variantValue(self.cbLastTile.itemData(idx)).exposed:
                 restoredIdx = idx
                 break
     if not restoredIdx:
         restoredIdx = 0
     self.cbLastTile.setCurrentIndex(restoredIdx)
     self.prevLastTile = self.computeLastTile()
Пример #2
0
 def fillLastMeldCombo(self):
     """fill the drop down list with all possible melds.
     If the drop down had content before try to preserve the
     current index. Even if the meld changed state meanwhile."""
     with BlockSignals(self.cbLastMeld):  # we only want to emit the changed signal once
         showCombo = False
         idx = self.cbLastMeld.currentIndex()
         if idx < 0:
             idx = 0
         indexedMeld = str(variantValue(self.cbLastMeld.itemData(idx)))
         self.cbLastMeld.clear()
         self.__meldPixMaps = []
         if not self.game.winner:
             return
         if self.cbLastTile.count() == 0:
             return
         lastTile = Internal.scene.computeLastTile()
         winnerMelds = [m for m in self.game.winner.hand.melds if len(m) < 4 and lastTile in m]
         assert len(winnerMelds), "lastTile %s missing in %s" % (lastTile, self.game.winner.hand.melds)
         if len(winnerMelds) == 1:
             self.cbLastMeld.addItem(QIcon(), "", toQVariant(str(winnerMelds[0])))
             self.cbLastMeld.setCurrentIndex(0)
             return
         showCombo = True
         self.__fillLastMeldComboWith(winnerMelds, indexedMeld, lastTile)
         self.lblLastMeld.setVisible(showCombo)
         self.cbLastMeld.setVisible(showCombo)
     self.cbLastMeld.currentIndexChanged.emit(0)
Пример #3
0
 def __getSelectedGame(self):
     """returns the game id of the selected game"""
     rows = self.selection.selectedRows()
     if rows:
         return variantValue(self.model.data(rows[0], 0))
     else:
         return 0
Пример #4
0
 def __idxForGame(self, game):
     """returns the model index for game"""
     for row in range(self.model.rowCount()):
         idx = self.model.index(row, 0)
         if variantValue(self.model.data(idx, 0)) == game:
             return idx
     return self.model.index(0, 0)
Пример #5
0
 def computeLastMeld(self):
     """compile hand info into a string as needed by the scoring engine"""
     if self.scoringDialog:
         # is None while ScoringGame is created
         cbLastMeld = self.scoringDialog.cbLastMeld
         idx = cbLastMeld.currentIndex()
         if idx >= 0:
             return Meld(nativeString(
                 variantValue(cbLastMeld.itemData(idx))))
     return Meld()
Пример #6
0
 def headerData(self, section, orientation, role):
     """tell the view about the wanted headers"""
     if Qt is None:
         # happens when kajongg exits unexpectedly
         return
     if role == Qt.DisplayRole and orientation == Qt.Horizontal:
         if section >= self.rootItem.columnCount():
             return toQVariant()
         result = variantValue(self.rootItem.content(section))
         if result == 'doubles':
             result = 'x2'
         return m18n(result)
     elif role == Qt.TextAlignmentRole:
         leftRight = Qt.AlignLeft if section == 0 else Qt.AlignRight
         return toQVariant(int(leftRight | Qt.AlignVCenter))
     else:
         return toQVariant()
Пример #7
0
 def _toggleWidget(self, checked):
     """user has toggled widget visibility with an action"""
     assert self.scene
     action = self.sender()
     actionData = variantValue(action.data())
     if checked:
         if isinstance(actionData, type):
             clsName = actionData.__name__
             actionData = actionData(scene=self.scene)
             action.setData(toQVariant(actionData))
             setattr(
                 self.scene,
                 clsName[0].lower() + clsName[1:],
                 actionData)
         actionData.show()
         actionData.raise_()
     else:
         assert actionData
         actionData.hide()
Пример #8
0
 def delete(self):
     """delete a game"""
     def answered(result, games):
         """question answered, result is True or False"""
         if result:
             for game in games:
                 Query("DELETE FROM score WHERE game = ?", (game, ))
                 Query("DELETE FROM game WHERE id = ?", (game, ))
             self.setQuery()  # just reload entire table
     allGames = self.view.selectionModel().selectedRows(0)
     deleteGames = list(variantValue(x.data()) for x in allGames)
     if len(deleteGames) == 0:
         # should never happen
         logException('delete: 0 rows selected')
     WarningYesNo(
         m18n(
             "Do you really want to delete <numid>%1</numid> games?<br>"
             "This will be final, you cannot cancel it with "
             "the cancel button",
             len(deleteGames))).addCallback(answered, deleteGames)
Пример #9
0
 def __fillLastMeldComboWith(self, winnerMelds, indexedMeld, lastTile):
     """fill last meld combo with prepared content"""
     winner = self.game.winner
     faceWidth = winner.handBoard.tileset.faceSize.width() * 0.5
     faceHeight = winner.handBoard.tileset.faceSize.height() * 0.5
     restoredIdx = None
     for meld in winnerMelds:
         pixMap = QPixmap(faceWidth * len(meld), faceHeight)
         pixMap.fill(Qt.transparent)
         self.__meldPixMaps.append(pixMap)
         painter = QPainter(pixMap)
         for element in meld:
             painter.drawPixmap(
                 0,
                 0,
                 winner.handBoard.tilesByElement(element)[0].pixmapFromSvg(
                     QSize(faceWidth, faceHeight), withBorders=False
                 ),
             )
             painter.translate(QPointF(faceWidth, 0.0))
         self.cbLastMeld.addItem(QIcon(pixMap), "", toQVariant(str(meld)))
         if indexedMeld == str(meld):
             restoredIdx = self.cbLastMeld.count() - 1
     if not restoredIdx and indexedMeld:
         # try again, maybe the meld changed between concealed and exposed
         indexedMeld = indexedMeld.lower()
         for idx in range(self.cbLastMeld.count()):
             meldContent = str(variantValue(self.cbLastMeld.itemData(idx)))
             if indexedMeld == meldContent.lower():
                 restoredIdx = idx
                 if lastTile not in meldContent:
                     lastTile = lastTile.swapped
                     assert lastTile in meldContent
                     with BlockSignals(self.cbLastTile):  # we want to continue right here
                         idx = self.cbLastTile.findData(toQVariant(lastTile))
                         self.cbLastTile.setCurrentIndex(idx)
                 break
     if not restoredIdx:
         restoredIdx = 0
     self.cbLastMeld.setCurrentIndex(restoredIdx)
     self.cbLastMeld.setIconSize(QSize(faceWidth * 3, faceHeight))
Пример #10
0
 def chooseVariant(self, uiTile, lowerHalf=False):
     """make the user choose from a list of possible melds for the target.
     The melds do not contain real Tiles, just the scoring strings."""
     variants = self.meldVariants(uiTile, lowerHalf)
     idx = 0
     if len(variants) > 1:
         menu = QMenu(m18n('Choose from'))
         for idx, variant in enumerate(variants):
             action = menu.addAction(variant.typeName())
             action.setData(toQVariant(idx))
         if Internal.scene.mainWindow.centralView.dragObject:
             menuPoint = QCursor.pos()
         else:
             menuPoint = uiTile.board.tileFaceRect().bottomRight()
             view = Internal.scene.mainWindow.centralView
             menuPoint = view.mapToGlobal(
                 view.mapFromScene(uiTile.mapToScene(menuPoint)))
         action = menu.exec_(menuPoint)
         if not action:
             return None
         idx = variantValue(action.data())
     return variants[idx]
Пример #11
0
 def computeLastTile(self):
     """returns the currently selected last tile"""
     idx = self.cbLastTile.currentIndex()
     if idx >= 0:
         return variantValue(self.cbLastTile.itemData(idx))