def __init__(self, parent=None): super(Games, self).__init__(parent) self.selectedGame = None self.onlyPending = True decorateWindow(self, i18nc('kajongg', 'Games')) self.setObjectName('Games') self.resize(700, 400) self.model = GamesModel() if Debug.modelTest: self.modelTest = ModelTest(self.model, self) self.view = MJTableView(self) self.view.setModel(self.model) self.selection = QItemSelectionModel(self.model, self.view) self.view.setSelectionModel(self.selection) self.view.setSelectionBehavior(QAbstractItemView.SelectRows) self.view.setSelectionMode(QAbstractItemView.SingleSelection) self.buttonBox = QDialogButtonBox(self) self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel) self.newButton = self.buttonBox.addButton( i18nc('start a new game', "&New"), QDialogButtonBox.ActionRole) self.newButton.setIcon(KIcon("document-new")) self.newButton.clicked.connect(self.accept) self.loadButton = self.buttonBox.addButton( i18n("&Load"), QDialogButtonBox.AcceptRole) self.loadButton.clicked.connect(self.loadGame) self.loadButton.setIcon(KIcon("document-open")) self.deleteButton = self.buttonBox.addButton( i18n("&Delete"), QDialogButtonBox.ActionRole) self.deleteButton.setIcon(KIcon("edit-delete")) self.deleteButton.clicked.connect(self.delete) chkPending = QCheckBox(i18n("Show only pending games"), self) chkPending.setChecked(True) cmdLayout = QHBoxLayout() cmdLayout.addWidget(chkPending) cmdLayout.addWidget(self.buttonBox) layout = QVBoxLayout() layout.addWidget(self.view) layout.addLayout(cmdLayout) self.setLayout(layout) StateSaver(self) self.selection.selectionChanged.connect(self.selectionChanged) self.buttonBox.accepted.connect(self.accept) self.buttonBox.rejected.connect(self.reject) self.view.doubleClicked.connect(self.loadGame) chkPending.stateChanged.connect(self.pendingOrNot)
def __init__(self, main_window): """ Initialize treeView object from Main Window. Args: main_window (QtWidgets.QMainWindow): [description] """ self.main_window = main_window self.treeView = self.main_window.treeView self.cache_files = {} tree = self.treeView # https://doc.qt.io/archives/qt-4.8/qabstractitemview.html tree.selectionMode = QAbstractItemView.ExtendedSelection tree.setEditTriggers(QAbstractItemView.NoEditTriggers) tree.clicked.connect(self.tree_clicked) tree.doubleClicked.connect(self.tree_dblclicked) tree.expanded.connect(self.on_expanded) tree.setContextMenuPolicy(Qt.CustomContextMenu) tree.customContextMenuRequested.connect(self.open_menu) self.source_model = QtGui.QStandardItemModel() tree.setModel(self.source_model) self.selection_model = QItemSelectionModel(self.source_model) tree.setSelectionModel(self.selection_model) tree.selectionModel().selectionChanged.connect( self.on_selection_changed)
def __init__(self, parent=None): super(Games, self).__init__(parent) self.selectedGame = None self.onlyPending = True decorateWindow(self, m18nc('kajongg', 'Games')) self.setObjectName('Games') self.resize(700, 400) self.model = GamesModel() if Debug.modelTest: self.modelTest = ModelTest(self.model, self) self.view = MJTableView(self) self.view.setModel(self.model) self.selection = QItemSelectionModel(self.model, self.view) self.view.setSelectionModel(self.selection) self.view.setSelectionBehavior(QAbstractItemView.SelectRows) self.view.setSelectionMode(QAbstractItemView.SingleSelection) self.buttonBox = QDialogButtonBox(self) self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel) self.newButton = self.buttonBox.addButton( m18nc('start a new game', "&New"), QDialogButtonBox.ActionRole) self.newButton.setIcon(KIcon("document-new")) self.newButton.clicked.connect(self.accept) self.loadButton = self.buttonBox.addButton( m18n("&Load"), QDialogButtonBox.AcceptRole) self.loadButton.clicked.connect(self.loadGame) self.loadButton.setIcon(KIcon("document-open")) self.deleteButton = self.buttonBox.addButton( m18n("&Delete"), QDialogButtonBox.ActionRole) self.deleteButton.setIcon(KIcon("edit-delete")) self.deleteButton.clicked.connect(self.delete) chkPending = QCheckBox(m18n("Show only pending games"), self) chkPending.setChecked(True) cmdLayout = QHBoxLayout() cmdLayout.addWidget(chkPending) cmdLayout.addWidget(self.buttonBox) layout = QVBoxLayout() layout.addWidget(self.view) layout.addLayout(cmdLayout) self.setLayout(layout) StateSaver(self) self.selection.selectionChanged.connect(self.selectionChanged) self.buttonBox.accepted.connect(self.accept) self.buttonBox.rejected.connect(self.reject) self.view.doubleClicked.connect(self.loadGame) chkPending.stateChanged.connect(self.pendingOrNot)
def loadTables(self, tables): """build and use a model around the tables. Show all new tables (no gameid given yet) and all suspended tables that also exist locally. In theory all suspended games should exist locally but there might have been bugs or somebody might have removed the local database like when reinstalling linux""" if not Internal.scene: return if Debug.table: for table in tables: if table.gameid and not table.gameExistsLocally(): logDebug('Table %s does not exist locally' % table) tables = [x for x in tables if not x.gameid or x.gameExistsLocally()] tables.sort(key=lambda x: x.tableid) preselectTableId = self.__preselectTableId(tables) self.__keepChatWindows(tables) model = TablesModel(tables) self.view.setModel(model) if Debug.modelTest: self.debugModelTest = ModelTest(model, self.view) selection = QItemSelectionModel(model, self.view) self.view.initView() self.view.setSelectionModel(selection) self.view.setSelectionBehavior(QAbstractItemView.SelectRows) self.view.setSelectionMode(QAbstractItemView.SingleSelection) selection.selectionChanged.connect(self.selectionChanged) if len(tables) == 1: self.selectTable(0) self.startButton.setFocus() elif not tables: self.newButton.setFocus() else: _ = [x for x in tables if x.tableid >= preselectTableId] self.selectTable(tables.index(_[0]) if _ else 0) self.updateButtonsForTable(self.selectedTable()) self.view.setFocus()
class Games(QDialog): """a dialog for selecting a game""" def __init__(self, parent=None): super(Games, self).__init__(parent) self.selectedGame = None self.onlyPending = True decorateWindow(self, i18nc('kajongg', 'Games')) self.setObjectName('Games') self.resize(700, 400) self.model = GamesModel() if Debug.modelTest: self.modelTest = ModelTest(self.model, self) self.view = MJTableView(self) self.view.setModel(self.model) self.selection = QItemSelectionModel(self.model, self.view) self.view.setSelectionModel(self.selection) self.view.setSelectionBehavior(QAbstractItemView.SelectRows) self.view.setSelectionMode(QAbstractItemView.SingleSelection) self.buttonBox = QDialogButtonBox(self) self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel) self.newButton = self.buttonBox.addButton( i18nc('start a new game', "&New"), QDialogButtonBox.ActionRole) self.newButton.setIcon(KIcon("document-new")) self.newButton.clicked.connect(self.accept) self.loadButton = self.buttonBox.addButton( i18n("&Load"), QDialogButtonBox.AcceptRole) self.loadButton.clicked.connect(self.loadGame) self.loadButton.setIcon(KIcon("document-open")) self.deleteButton = self.buttonBox.addButton( i18n("&Delete"), QDialogButtonBox.ActionRole) self.deleteButton.setIcon(KIcon("edit-delete")) self.deleteButton.clicked.connect(self.delete) chkPending = QCheckBox(i18n("Show only pending games"), self) chkPending.setChecked(True) cmdLayout = QHBoxLayout() cmdLayout.addWidget(chkPending) cmdLayout.addWidget(self.buttonBox) layout = QVBoxLayout() layout.addWidget(self.view) layout.addLayout(cmdLayout) self.setLayout(layout) StateSaver(self) self.selection.selectionChanged.connect(self.selectionChanged) self.buttonBox.accepted.connect(self.accept) self.buttonBox.rejected.connect(self.reject) self.view.doubleClicked.connect(self.loadGame) chkPending.stateChanged.connect(self.pendingOrNot) def showEvent(self, dummyEvent): """only now get the data set. Not doing this in__init__ would eventually make it easier to subclass from some generic TableEditor class""" self.setQuery() self.view.initView() self.selectionChanged() def keyPressEvent(self, event): """use insert/delete keys for insert/delete""" key = event.key() if key == Qt.Key_Insert: self.newEntry() return if key == Qt.Key_Delete: self.delete() event.ignore() return QDialog.keyPressEvent(self, event) def selectionChanged(self): """update button states according to selection""" selectedRows = len(self.selection.selectedRows()) self.loadButton.setEnabled(selectedRows == 1) self.deleteButton.setEnabled(selectedRows >= 1) def setQuery(self): """define the query depending on self.OnlyPending""" query = Query( "select g.id, g.starttime, " "p0.name||'///'||p1.name||'///'||p2.name||'///'||p3.name " "from game g, player p0," "player p1, player p2, player p3 " "where seed is null" " and p0.id=g.p0 and p1.id=g.p1 " " and p2.id=g.p2 and p3.id=g.p3 " "%s" "and exists(select 1 from score where game=g.id)" % ("and g.endtime is null " if self.onlyPending else "")) self.model.setResultset(query.records) self.view.hideColumn(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 self.model.data(idx, 0) == game: return idx return self.model.index(0, 0) def __getSelectedGame(self): """returns the game id of the selected game""" rows = self.selection.selectedRows() if rows: return self.model.data(rows[0], 0) else: return 0 def pendingOrNot(self, chosen): """do we want to see all games or only pending games?""" if self.onlyPending != chosen: self.onlyPending = chosen prevSelected = self.__getSelectedGame() self.setQuery() idx = self.__idxForGame(prevSelected) self.view.selectRow(idx.row()) self.view.setFocus() def loadGame(self): """load a game""" self.selectedGame = self.__getSelectedGame() self.buttonBox.accepted.emit() 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(x.data() for x in allGames) if len(deleteGames) == 0: # should never happen logException('delete: 0 rows selected') WarningYesNo( i18n( "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)
class Games(QDialog): """a dialog for selecting a game""" def __init__(self, parent=None): super(Games, self).__init__(parent) self.selectedGame = None self.onlyPending = True decorateWindow(self, m18nc('kajongg', 'Games')) self.setObjectName('Games') self.resize(700, 400) self.model = GamesModel() if Debug.modelTest: self.modelTest = ModelTest(self.model, self) self.view = MJTableView(self) self.view.setModel(self.model) self.selection = QItemSelectionModel(self.model, self.view) self.view.setSelectionModel(self.selection) self.view.setSelectionBehavior(QAbstractItemView.SelectRows) self.view.setSelectionMode(QAbstractItemView.SingleSelection) self.buttonBox = QDialogButtonBox(self) self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel) self.newButton = self.buttonBox.addButton( m18nc('start a new game', "&New"), QDialogButtonBox.ActionRole) self.newButton.setIcon(KIcon("document-new")) self.newButton.clicked.connect(self.accept) self.loadButton = self.buttonBox.addButton( m18n("&Load"), QDialogButtonBox.AcceptRole) self.loadButton.clicked.connect(self.loadGame) self.loadButton.setIcon(KIcon("document-open")) self.deleteButton = self.buttonBox.addButton( m18n("&Delete"), QDialogButtonBox.ActionRole) self.deleteButton.setIcon(KIcon("edit-delete")) self.deleteButton.clicked.connect(self.delete) chkPending = QCheckBox(m18n("Show only pending games"), self) chkPending.setChecked(True) cmdLayout = QHBoxLayout() cmdLayout.addWidget(chkPending) cmdLayout.addWidget(self.buttonBox) layout = QVBoxLayout() layout.addWidget(self.view) layout.addLayout(cmdLayout) self.setLayout(layout) StateSaver(self) self.selection.selectionChanged.connect(self.selectionChanged) self.buttonBox.accepted.connect(self.accept) self.buttonBox.rejected.connect(self.reject) self.view.doubleClicked.connect(self.loadGame) chkPending.stateChanged.connect(self.pendingOrNot) def showEvent(self, dummyEvent): """only now get the data set. Not doing this in__init__ would eventually make it easier to subclass from some generic TableEditor class""" self.setQuery() self.view.initView() self.selectionChanged() def keyPressEvent(self, event): """use insert/delete keys for insert/delete""" key = event.key() if key == Qt.Key_Insert: self.newEntry() return if key == Qt.Key_Delete: self.delete() event.ignore() return QDialog.keyPressEvent(self, event) def selectionChanged(self): """update button states according to selection""" selectedRows = len(self.selection.selectedRows()) self.loadButton.setEnabled(selectedRows == 1) self.deleteButton.setEnabled(selectedRows >= 1) def setQuery(self): """define the query depending on self.OnlyPending""" query = Query( "select g.id, g.starttime, " "p0.name||'///'||p1.name||'///'||p2.name||'///'||p3.name " "from game g, player p0," "player p1, player p2, player p3 " "where seed is null" " and p0.id=g.p0 and p1.id=g.p1 " " and p2.id=g.p2 and p3.id=g.p3 " "%s" "and exists(select 1 from score where game=g.id)" % ("and g.endtime is null " if self.onlyPending else "")) self.model.setResultset(query.records) self.view.hideColumn(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) 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 def pendingOrNot(self, chosen): """do we want to see all games or only pending games?""" if self.onlyPending != chosen: self.onlyPending = chosen prevSelected = self.__getSelectedGame() self.setQuery() idx = self.__idxForGame(prevSelected) self.view.selectRow(idx.row()) self.view.setFocus() def loadGame(self): """load a game""" self.selectedGame = self.__getSelectedGame() self.buttonBox.accepted.emit() 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)