def on_log_widget(self, show=None): if show is None: groupbox = QGroupBox('运行日志') groupbox.setStyleSheet(self.boxqss) layout = QVBoxLayout() self.logWidget = LogWidget(self.ros_client) layout.addWidget(self.logWidget) groupbox.setLayout(layout) groupbox.hide() self.middle_splitter.insertWidget(1, groupbox) elif show: self.middle_splitter.widget(1).show() else: self.middle_splitter.widget(1).hide() self.adjustSize()
def createLayout(self): self.ui = uic.loadUi("GameWidget.ui", self) self.scene = GraphicsScene( self.nrRows, self.nrCols, QtGui.QColor(self.player.color[0], self.player.color[1], self.player.color[2]), self, ) self.scene.block(False) self.ui.graphicsView.setScene(self.scene) self.logList = LogWidget(self) self.logList.setMaximumSize(250, 200) self.freezeButton = QtGui.QPushButton("Freeze", self) QtCore.QObject.connect(self.freezeButton, QtCore.SIGNAL("clicked()"), self.freezeGame) self.freezeButton.setDisabled(True) self.ui.verticalLayout.addWidget(self.logList) self.ui.verticalLayout.addWidget(self.freezeButton) self.ui.chatEdit.returnPressed.connect(self.sendMessage) self.ui.aiCheckBox.stateChanged.connect(self.aiToggled) self.ui.show() self.layoutCreated = True
def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(850, 700) icon = QtGui.QIcon.fromTheme(":/icon/color") MainWindow.setWindowIcon(icon) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget) self.verticalLayout.setObjectName("verticalLayout") self.filelist_label = QtWidgets.QLabel(self.centralwidget) self.filelist_label.setObjectName("filelist_label") self.verticalLayout.addWidget(self.filelist_label) self.splitter_horizontal = QtWidgets.QSplitter(self.centralwidget) self.splitter_horizontal.setOrientation(QtCore.Qt.Vertical) self.splitter_horizontal.setObjectName("splitter_horizontal") self.tableView = MyTableView(self.splitter_horizontal) self.tableView.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu) self.tableView.setObjectName("tableView") self.listView = MyListView(self.splitter_horizontal) self.listView.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu) self.listView.setObjectName("listView") self.splitter_vertical = QtWidgets.QSplitter(self.splitter_horizontal) self.splitter_vertical.setOrientation(QtCore.Qt.Horizontal) self.splitter_vertical.setObjectName("splitter_vertical") self.layoutWidget = QtWidgets.QWidget(self.splitter_vertical) self.layoutWidget.setObjectName("layoutWidget") self.verticalLayout_1 = QtWidgets.QVBoxLayout(self.layoutWidget) self.verticalLayout_1.setContentsMargins(0, 0, 0, 0) self.verticalLayout_1.setObjectName("verticalLayout_1") self.log_label = QtWidgets.QLabel(self.layoutWidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Maximum) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.log_label.sizePolicy().hasHeightForWidth()) self.log_label.setSizePolicy(sizePolicy) self.log_label.setObjectName("log_label") self.verticalLayout_1.addWidget(self.log_label) self.searchLog = QtWidgets.QLineEdit(self.layoutWidget) self.searchLog.setObjectName("searchLog") self.verticalLayout_1.addWidget(self.searchLog) self.logWidget = LogWidget(self.layoutWidget) self.logWidget.setObjectName("logWidget") self.verticalLayout_1.addWidget(self.logWidget) self.layoutWidget1 = QtWidgets.QWidget(self.splitter_vertical) self.layoutWidget1.setObjectName("layoutWidget1") self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.layoutWidget1) self.verticalLayout_2.setContentsMargins(0, 0, 0, 0) self.verticalLayout_2.setObjectName("verticalLayout_2") self.pktReceive_label = QtWidgets.QLabel(self.layoutWidget1) self.pktReceive_label.setObjectName("pktReceive_label") self.verticalLayout_2.addWidget(self.pktReceive_label) self.dataPktReceive = QtWidgets.QTreeWidget(self.layoutWidget1) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.dataPktReceive.sizePolicy().hasHeightForWidth()) self.dataPktReceive.setSizePolicy(sizePolicy) self.dataPktReceive.setObjectName("dataPktReceive") self.dataPktReceive.headerItem().setText(0, "文件/包") self.dataPktReceive.header().setStretchLastSection(False) self.verticalLayout_2.addWidget(self.dataPktReceive) self.speed_label = QtWidgets.QLabel(self.layoutWidget1) self.speed_label.setObjectName("speed_label") self.verticalLayout_2.addWidget(self.speed_label) self.speedGraph = PlotWidget(self.layoutWidget1) self.speedGraph.setObjectName("speedGraph") self.verticalLayout_2.addWidget(self.speedGraph) self.verticalLayout.addWidget(self.splitter_horizontal) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 850, 23)) self.menubar.setObjectName("menubar") self.menu = QtWidgets.QMenu(self.menubar) self.menu.setObjectName("menu") self.menu_2 = QtWidgets.QMenu(self.menubar) self.menu_2.setObjectName("menu_2") self.menu_3 = QtWidgets.QMenu(self.menubar) self.menu_3.setObjectName("menu_3") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.toolBar = QtWidgets.QToolBar(MainWindow) self.toolBar.setMinimumSize(QtCore.QSize(16, 16)) self.toolBar.setObjectName("toolBar") MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar) self.action_reg = QtWidgets.QAction(MainWindow) self.action_reg.setObjectName("action_reg") self.action_undoReg = QtWidgets.QAction(MainWindow) self.action_undoReg.setObjectName("action_undoReg") self.action_dow = QtWidgets.QAction(MainWindow) self.action_dow.setObjectName("action_dow") self.action_del = QtWidgets.QAction(MainWindow) self.action_del.setObjectName("action_del") self.action_hub = QtWidgets.QAction(MainWindow) self.action_hub.setObjectName("action_hub") self.action_add = QtWidgets.QAction(MainWindow) self.action_add.setObjectName("action_add") self.action_import = QtWidgets.QAction(MainWindow) self.action_import.setObjectName("action_import") self.action_openDir = QtWidgets.QAction(MainWindow) self.action_openDir.setObjectName("action_openDir") self.action_swi = QtWidgets.QAction(MainWindow) self.action_swi.setObjectName("action_swi") self.action_reset = QtWidgets.QAction(MainWindow) self.action_reset.setObjectName("action_reset") self.actionWindows = QtWidgets.QAction(MainWindow) self.actionWindows.setObjectName("actionWindows") self.actionFusion = QtWidgets.QAction(MainWindow) self.actionFusion.setObjectName("actionFusion") self.actionQdarkstyle = QtWidgets.QAction(MainWindow) self.actionQdarkstyle.setObjectName("actionQdarkstyle") self.actionwindowsvista = QtWidgets.QAction(MainWindow) self.actionwindowsvista.setObjectName("actionwindowsvista") self.action_advancedreg = QtWidgets.QAction(MainWindow) self.action_advancedreg.setObjectName("action_advancedreg") self.action_video = QtWidgets.QAction(MainWindow) self.action_video.setObjectName("action_video") self.action_cmdline = QtWidgets.QAction(MainWindow) self.action_cmdline.setObjectName("action_cmdline") self.menu.addAction(self.action_import) self.menu.addAction(self.action_swi) self.menu.addAction(self.action_reset) self.menu.addAction(self.action_hub) self.menu.addAction(self.action_video) self.menu.addAction(self.action_cmdline) self.menu_2.addAction(self.action_add) self.menu_2.addAction(self.action_reg) self.menu_2.addAction(self.action_advancedreg) self.menu_2.addAction(self.action_undoReg) self.menu_2.addAction(self.action_dow) self.menu_2.addAction(self.action_del) self.menu_2.addAction(self.action_openDir) self.menu_3.addAction(self.actionWindows) self.menu_3.addAction(self.actionwindowsvista) self.menu_3.addAction(self.actionFusion) self.menu_3.addAction(self.actionQdarkstyle) self.menubar.addAction(self.menu.menuAction()) self.menubar.addAction(self.menu_2.menuAction()) self.menubar.addAction(self.menu_3.menuAction()) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow)
class GameWidget(QtGui.QWidget): # GameWidget provides a window in which a game can be played. It displays the gameboard(GraphicsScene), # active players, log messages and chat messages CREATE_NEW_GAME, JOIN_GAME = range(2) def __init__(self, type, info, player, service, win_parent=None): # If type is CREATE_NEW_GAME, info contains the number of rows and colums, name and comment. # If type is JOIN_GAME, info contains the UUID of the game the user wishes to join. QtGui.QWidget.__init__(self, win_parent) self.scene = None self.aiActive = False self.manyInARowService = service self.player = player self.nrCols = info["cols"] self.nrRows = info["rows"] self.gameName = str(info["name"]) self.comment = str(info["comment"]) self.waitTime = info["waitTime"] self.startTime = None self.players = {} self.manyInARow = None self.layoutCreated = False self.aiTimer = QtCore.QTimer() self.aiTimer.timeout.connect(self.aiMakeMove) self.aiTimer.setInterval(1000) self.errorBox = QtGui.QMessageBox(QtGui.QMessageBox.Information, "0", "0", QtGui.QMessageBox.Ok, self) # Custom event types: self.MAKE_MOVE_EVENT = QtCore.QEvent.User + 1 self.PLAYER_WON_EVENT = QtCore.QEvent.User + 2 self.GAME_FINISHED_EVENT = QtCore.QEvent.User + 3 # ensure threading safety: self.lock = threading.Condition() if type == self.CREATE_NEW_GAME: self.manyInARow = ManyInARowGame.hostGame( service, player, self.gameName, self.comment, self.nrRows, self.nrCols, self.waitTime, self.chatCallBack, self.gameJoinedCallBack, self.playerJoinedCallBack, self.playerLeftCallBack, self.moveCallBack, self.enableClicks, self.disableClicks, self.playerWonCallBack, self.gameFinishedCallBack, self.freezeCallback, self.unfreezeCallback, ) if not self.layoutCreated: self.createLayout() QtCore.QObject.connect(self.scene, QtCore.SIGNAL("playerClicked(int)"), self.makeMove) QtCore.QObject.connect(self.scene, QtCore.SIGNAL("mouseHoverColumn(int)"), self.makeHoverMove) self.manyInARow.start() if type == self.JOIN_GAME: self.gameUUID = info["UUID"] self.manyInARow = ManyInARowGame.joinGame( service, player, self.gameUUID, self.gameName, self.comment, self.nrRows, self.nrCols, self.waitTime, self.chatCallBack, self.gameJoinedCallBack, self.playerJoinedCallBack, self.playerLeftCallBack, self.moveCallBack, self.enableClicks, self.disableClicks, self.playerWonCallBack, self.gameFinishedCallBack, self.freezeCallback, self.unfreezeCallback, ) self.gameUUID = self.manyInARow.UUID self.startTime = self.manyInARow.startTime self.manyInARow.start() # Instead of accessing the GUI directly from other threads, we need to provide a clean way to change the GUI. # By using custom events, other trhead can signal the gui that it has to change. The changes will be made in # the main GUI thread def customEvent(self, event): if event.type() == self.MAKE_MOVE_EVENT: with self.lock: playerUUID = event.userData["playerUUID"] row = event.userData["row"] col = event.userData["col"] player = self.getPlayer(playerUUID) self.logList.addMessage(player, "placed: (column, row) - (" + str(col) + ", " + str(row) + ")") self.scene.makeMove(col, row, QtGui.QColor(player.color[0], player.color[1], player.color[2])) elif event.type() == self.PLAYER_WON_EVENT: with self.lock: winners = event.userData["winners"] currentGoal = event.userData["currentGoal"] winnerUUID = winners[currentGoal] player = self.getPlayer(winnerUUID) self.logList.addMessage(player, "has won round " + str(currentGoal)) # winnerBox = QtGui.QMessageBox(QtGui.QMessageBox.Information, "Round finished in game: " + "\'"+self.gameName+"\'", player.name + " has won this round", QtGui.QMessageBox.Ok, self) # winnerBox.exec_() elif event.type() == self.GAME_FINISHED_EVENT: with self.lock: self.scene.block(False) winners = event.userData["winners"] self.logList.addMessage(self.player, "The game has finished") winnerStr = "" self.aiTimer.stop() for winner in winners.items(): player = self.getPlayer(winner[1]) winnerStr = winnerStr + str(winner[0]) + " in a row: " + player.name + "\n" winnerBox = QtGui.QMessageBox( QtGui.QMessageBox.Information, "Game " + "'" + self.gameName + "' " + "finished", "The game has finished, the winners are: \n" + winnerStr, QtGui.QMessageBox.Ok, self, ) winnerBox.exec_() else: return QtCore.QObject.customEvent(event) def getGameUUID(self): return self.gameUUID def getPlayer(self, playerUUID): player = None if playerUUID in self.players.keys(): player = self.players[playerUUID] else: player = Player("Disconnected player", playerUUID) return player def aiToggled(self, state): if state == QtCore.Qt.Checked: self.aiActive = True self.aiTimer.start() else: self.aiActive = False self.aiTimer.stop() # Functions the user can trigger: def sendMessage(self): message = self.ui.chatEdit.text() self.ui.chatEdit.clear() self.manyInARow.sendChatMessage(message) def makeMove(self, column): # Passes the move to the class coordinating the game (ManyInARowGame) with self.lock: if self.aiActive: self.errorBox.setWindowTitle("AI is playing") self.errorBox.setText("The A.I. is currently playing, uncheck the checkbox to enable normal game mode.") else: self.scene.block(False) self.manyInARow.makeMove(column) def freezeGame(self): with self.lock: self.freezeButton.setDisabled(True) self.freezeButton.setText("Unfreeze") self.manyInARow.freezeGame() def unfreezeGame(self): with self.lock: self.freezeButton.setDisabled(True) self.freezeButton.setText("Freeze") self.manyInARow.unfreezeGame() # Callbacks: def aiMakeMove(self): if not self.scene.rejectClicks: self.scene.block(False) self.manyInARow._makeAiMove(self.players) def gameJoinedCallBack(self, UUID, name, description, numRows, numCols, waitTime, startTime): # We know the number of rows and colums, build the GUI board. with self.lock: self.gameUUID = UUID self.nrCols = numCols self.nrRows = numRows self.gameName = name self.comment = description self.waitTime = waitTime self.startTime = startTime if not self.layoutCreated: self.createLayout() QtCore.QObject.connect(self.scene, QtCore.SIGNAL("playerClicked(int)"), self.makeMove) QtCore.QObject.connect(self.scene, QtCore.SIGNAL("mouseHoverColumn(int)"), self.makeHoverMove) def enableClicks(self): with self.lock: self.freezeButton.setEnabled(True) if self.scene != None: self.scene.unblock(False) def disableClicks(self): with self.lock: self.freezeButton.setDisabled(True) if self.scene != None: self.scene.block(False) def playerJoinedCallBack(self, playerUUID, newPlayer): with self.lock: if not self.layoutCreated: self.createLayout() if playerUUID not in self.players.keys(): self.logList.addMessage(newPlayer, "successfully joined") self.players[playerUUID] = newPlayer self.ui.playerList.clear() for currentPlayer in self.players.values(): currentPlayer newItem = QtGui.QListWidgetItem(currentPlayer.name, self.ui.playerList) newItem.setBackgroundColor( QtGui.QColor(currentPlayer.color[0], currentPlayer.color[1], currentPlayer.color[2]) ) self.ui.playerList.addItem(newItem) def playerLeftCallBack(self, playerUUID): with self.lock: self.logList.addMessage(self.players[playerUUID], "has left") del self.players[playerUUID] self.ui.playerList.clear() for currentPlayer in self.players.values(): newItem = QtGui.QListWidgetItem(currentPlayer.name, self.ui.playerList) newItem.setBackgroundColor( QtGui.QColor(currentPlayer.color[0], currentPlayer.color[1], currentPlayer.color[2]) ) self.ui.playerList.addItem(newItem) def chatCallBack(self, playerUUID, message): with self.lock: player = self.getPlayer(playerUUID) self.logList.addMessage(player, "said: " + message) color = QtGui.QColor(player.color[0], player.color[1], player.color[2]) playerName = player.name newItem = QtGui.QListWidgetItem(playerName + ": " + message, self.ui.messageList) newItem.setBackgroundColor(color) self.ui.messageList.addItem(newItem) self.ui.messageList.scrollToItem(newItem) def moveCallBack(self, playerUUID, col, row): customEvent = CustomEvent(self.MAKE_MOVE_EVENT, {"playerUUID": playerUUID, "col": col, "row": row}) QtCore.QCoreApplication.postEvent(self, customEvent) def playerWonCallBack(self, winners, currentGoal): customEvent = CustomEvent(self.PLAYER_WON_EVENT, {"winners": winners, "currentGoal": currentGoal}) QtCore.QCoreApplication.postEvent(self, customEvent) def gameFinishedCallBack(self, winners): customEvent = CustomEvent(self.GAME_FINISHED_EVENT, {"winners": winners}) QtCore.QCoreApplication.postEvent(self, customEvent) def freezeCallback(self): with self.lock: self.freezeButton.setText("Unfreeze") QtCore.QObject.disconnect(self.freezeButton, QtCore.SIGNAL("clicked()"), self.freezeGame) # self.freezeButton.clicked.disconnect(self.freezeGame) QtCore.QObject.connect(self.freezeButton, QtCore.SIGNAL("clicked()"), self.unfreezeGame) self.freezeButton.setEnabled(True) self.scene.block(True) def unfreezeCallback(self): with self.lock: self.freezeButton.setEnabled(True) self.freezeButton.setText("Freeze") QtCore.QObject.disconnect(self.freezeButton, QtCore.SIGNAL("clicked()"), self.unfreezeGame) # self.freezeButton.clicked.disconnect(self.unfreezeGame) QtCore.QObject.connect(self.freezeButton, QtCore.SIGNAL("clicked()"), self.freezeGame) self.scene.unblock(True) def makeHoverMove(self, column): with self.lock: if not self.aiActive: row = self.manyInARow._makeDummyMove(column) if row != -1: # update the gui color = QtGui.QColor(self.player.color[0], self.player.color[1], self.player.color[2]) self.scene.makeDummyMove(column, row, color) def createLayout(self): self.ui = uic.loadUi("GameWidget.ui", self) self.scene = GraphicsScene( self.nrRows, self.nrCols, QtGui.QColor(self.player.color[0], self.player.color[1], self.player.color[2]), self, ) self.scene.block(False) self.ui.graphicsView.setScene(self.scene) self.logList = LogWidget(self) self.logList.setMaximumSize(250, 200) self.freezeButton = QtGui.QPushButton("Freeze", self) QtCore.QObject.connect(self.freezeButton, QtCore.SIGNAL("clicked()"), self.freezeGame) self.freezeButton.setDisabled(True) self.ui.verticalLayout.addWidget(self.logList) self.ui.verticalLayout.addWidget(self.freezeButton) self.ui.chatEdit.returnPressed.connect(self.sendMessage) self.ui.aiCheckBox.stateChanged.connect(self.aiToggled) self.ui.show() self.layoutCreated = True def closeEvent(self, event): if self.manyInARow != None: self.manyInARow.kill()