def initUI(self): self.setWindowTitle('TickTackToe 2000') self.resize(350, 350) self.center() self.main_layout = QVBoxLayout() self.form_layout = QGridLayout() self.main_layout.addLayout(self.form_layout) splitter = QSplitter(QtCore.Qt.Horizontal) self.main_layout.addWidget(splitter) self.game_layout = QGridLayout() self.main_layout.addLayout(self.game_layout) self.server_label = QLabel("Server: ") self.server_input = QLineEdit("127.0.0.1") self.form_layout.addWidget(self.server_label, 0, 0) self.form_layout.addWidget(self.server_input, 0, 1) self.port_label = QLabel("Port: ") self.port_input = QLineEdit("1632") self.form_layout.addWidget(self.port_label, 1, 0) self.form_layout.addWidget(self.port_input, 1, 1) self.connect_button = QPushButton("Connect") self.connect_button.setMinimumHeight(60) self.connect_button.pressed.connect(self.reconnect) self.form_layout.addWidget(self.connect_button, 0, 2, 2, 1) self.game_fields = {} tile_font = QFont() tile_font.setPointSize(30) for x in range(0, GAME_SIZE): for y in range(0, GAME_SIZE): f = QPushButton(" ") f.setMinimumHeight(90) f.setDisabled(True) f.setFont(tile_font) f.clicked.connect(self.onGameButtonClicked) if x in self.game_fields: self.game_fields[x][y] = f else: self.game_fields[x] = { 0: f } self.game_layout.addWidget(f, y, x) central_widget = QtGui.QWidget() central_widget.setLayout(self.main_layout) self.setCentralWidget(central_widget) self.statusBar().showMessage("") self.show()
class LandmarkLocationWidget(QWidget): # Signals activated = Signal(int, bool) deleted = Signal(int) def __init__(self): super(LandmarkLocationWidget, self).__init__() self._active = False self._font = QFont() self._font.setPointSize(10) self.indexLabel = QLabel() self.indexLabel.setMaximumWidth(10) self.indexLabel.setMinimumWidth(10) self.doneButton = QPushButton("Done") self.doneButton.setMaximumWidth(50) self.doneButton.setFont(self._font) self.doneButton.clicked.connect(self.doneButtonClicked) self.deleteButton = QPushButton("X") self.deleteButton.setMaximumWidth(15) self.deleteButton.setMinimumWidth(15) self.deleteButton.setMaximumHeight(15) self.deleteButton.setMinimumHeight(15) self.deleteButton.setFont(self._font) self.deleteButton.setVisible(False) self.deleteButton.clicked.connect(self.deleteButtonClicked) self.fixedButton = SpecialButton() self.fixedButton.setFont(self._font) self.movingButton = SpecialButton() self.movingButton.setFont(self._font) layout = QGridLayout() layout.setContentsMargins(0, 0, 0, 0) layout.setVerticalSpacing(0) layout.addWidget(self.deleteButton, 0, 0) layout.addWidget(self.indexLabel, 0, 1) layout.addWidget(self.fixedButton, 0, 2) layout.addWidget(self.movingButton, 0, 3) layout.addWidget(self.doneButton, 0, 4) self.setLayout(layout) self._updateState() def setIndex(self, index): self.index = index self.indexLabel.setText(str(index+1)) @property def active(self): return self._active @active.setter def active(self, value): self._active = value self._updateState() def setLandmarkSet(self, points): self.setFixedLandmark(points[0]) self.setMovingLandmark(points[1]) def setFixedLandmark(self, landmark): if not landmark: return labelX = "%2.0f" % landmark[0] labelY = "%2.0f" % landmark[1] labelZ = "%2.0f" % landmark[2] self.fixedButton.setText(labelX + ", " + labelY + ", " + labelZ) def setMovingLandmark(self, landmark): if not landmark: return labelX = "%2.0f" % landmark[0] labelY = "%2.0f" % landmark[1] labelZ = "%2.0f" % landmark[2] self.movingButton.setText(labelX + ", " + labelY + ", " + labelZ) @Slot() def doneButtonClicked(self): self._active = not self._active self.activated.emit(self.index, self._active) self._updateState() @Slot() def deleteButtonClicked(self): self.deleted.emit(self.index) def _updateState(self): text = "Done" if self._active else "Edit" self.doneButton.setText(text) self.deleteButton.setVisible(self._active) self.indexLabel.setVisible(not self._active) self.fixedButton.setEnabled(self._active) self.movingButton.setEnabled(self._active)
class TTClient(QtGui.QMainWindow): def __init__(self): super(TTClient, self).__init__() self.initUI() self.initNetworking() self.figure = None def initNetworking(self): self.tcpSocket = QtNetwork.QTcpSocket(self) self.destroyed.connect(self.disconnect) def reconnect(self): self.tcpSocket.abort() self.statusBar().showMessage("Connecting to %s:%d..." % (self.server_input.text(), int(self.port_input.text()))) self.tcpSocket.connectToHost(self.server_input.text(), int(self.port_input.text())) self.tcpSocket.connected.connect(self.sayHello) self.tcpSocket.readyRead.connect(self.onMessage) self.tcpSocket.disconnected.connect(self.onDisconnect) def onMessage(self): while self.tcpSocket.bytesAvailable() > 0: line = str(self.tcpSocket.readLine()).strip() print("Received: %s" % line.strip()) if ':' in line: parts = line.split(':', 1) #print(parts) if parts[0] == "START": self.statusBar().showMessage("Peer connected, you're: %s" % parts[1]) self.disableKeypad(True) self.cleanKeypad() self.figure = parts[1] elif parts[0] == "ERROR": alert = QMessageBox(QMessageBox.Critical, "Error!", "Error! %s" % parts[1]) self.tcpSocket.abort() alert.exec_() elif parts[0] == "WINNER": alert = QMessageBox(QMessageBox.Information, parts[1], parts[1]) self.tcpSocket.abort() alert.exec_() elif parts[0] == "YOUR MOVE": board = parts[1] for x in range(0, GAME_SIZE): for y in range(0, GAME_SIZE): if board[y*GAME_SIZE+x] == 'O': self.game_fields[x][y].setText("O") self.game_fields[x][y].setDisabled(True) elif board[y*GAME_SIZE+x] == 'X': self.game_fields[x][y].setText("X") self.game_fields[x][y].setDisabled(True) else: self.game_fields[x][y].setText(" ") self.game_fields[x][y].setDisabled(False) def onDisconnect(self): self.disableKeypad(False) self.statusBar().showMessage("Disconnected!") def sayHello(self): self.disableKeypad(True) self.statusBar().showMessage("Connected, waiting for second player...") def onGameButtonClicked(self): button_x = button_y = 0 for x in range(0, GAME_SIZE): for y in range(0, GAME_SIZE): if self.sender() == self.game_fields[x][y]: button_x = x button_y = y break if self.tcpSocket.isOpen(): s = "SELECT:%dx%d\n" % (button_x, button_y) print("Sending : %s" % s.strip()) self.tcpSocket.write(s) self.tcpSocket.flush() self.game_fields[button_x][button_y].setText(self.figure) self.disableKeypad(True) def cleanKeypad(self, b=True): for x in range(0, GAME_SIZE): for y in range(0, GAME_SIZE): self.game_fields[x][y].setText(" ") def disableKeypad(self, b=True): for x in range(0, GAME_SIZE): for y in range(0, GAME_SIZE): self.game_fields[x][y].setDisabled(b) def initUI(self): self.setWindowTitle('TickTackToe 2000') self.resize(350, 350) self.center() self.main_layout = QVBoxLayout() self.form_layout = QGridLayout() self.main_layout.addLayout(self.form_layout) splitter = QSplitter(QtCore.Qt.Horizontal) self.main_layout.addWidget(splitter) self.game_layout = QGridLayout() self.main_layout.addLayout(self.game_layout) self.server_label = QLabel("Server: ") self.server_input = QLineEdit("127.0.0.1") self.form_layout.addWidget(self.server_label, 0, 0) self.form_layout.addWidget(self.server_input, 0, 1) self.port_label = QLabel("Port: ") self.port_input = QLineEdit("1632") self.form_layout.addWidget(self.port_label, 1, 0) self.form_layout.addWidget(self.port_input, 1, 1) self.connect_button = QPushButton("Connect") self.connect_button.setMinimumHeight(60) self.connect_button.pressed.connect(self.reconnect) self.form_layout.addWidget(self.connect_button, 0, 2, 2, 1) self.game_fields = {} tile_font = QFont() tile_font.setPointSize(30) for x in range(0, GAME_SIZE): for y in range(0, GAME_SIZE): f = QPushButton(" ") f.setMinimumHeight(90) f.setDisabled(True) f.setFont(tile_font) f.clicked.connect(self.onGameButtonClicked) if x in self.game_fields: self.game_fields[x][y] = f else: self.game_fields[x] = { 0: f } self.game_layout.addWidget(f, y, x) central_widget = QtGui.QWidget() central_widget.setLayout(self.main_layout) self.setCentralWidget(central_widget) self.statusBar().showMessage("") self.show() def center(self): qr = self.frameGeometry() cp = QtGui.QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft())
class MainWindow(QDialog): """monkey_linux UI""" def __init__(self,parent=None): super(MainWindow, self).__init__() self.init_conf() self.setParent(parent) common.Log.info("set up ui") self.setup_ui() self.findDeviceAction.triggered.connect(self.get_device_status) self.deleteDeviceAction.triggered.connect(self.del_device) # self.storeButton.clicked.connect(self.set_conf) self.startButton.clicked.connect(self.start) self.stopButton.clicked.connect(self.stop) self.checkLogButton.clicked.connect(self.check) self.monkeyButton.clicked.connect(self.checkMonkeyLog) self.exportButton.clicked.connect(self.exportConf) self.importButton.clicked.connect(self.importConf) self.setAbout.triggered.connect(self.about) self.startTime = datetime.datetime.now() self.secsTime = float(1) * 60 * 60 def setup_ui(self): # main window width hand height # self.setMinimumWidth(600) self.setMaximumWidth(800) self.setMinimumHeight(600) # main window title self.setWindowTitle(static.title) # file menu bar self.menuBar = QMenuBar() self.menuBar.setMaximumHeight(23) # self.menuFile = self.menuBar.addMenu(static.menuFile) # self.importAction = QAction(QIcon(static.importPNG), static.importFile, self) # self.exportAction = QAction(QIcon(static.exportPNG), static.exportFile, self) # self.menuFile.addAction(self.importAction) # self.menuFile.addAction(self.exportAction) self.setEnvActioin = QAction(QIcon(static.setPNG), static.pcSet, self) self.menuSet = self.menuBar.addMenu(static.menuSet) self.menuSet.addAction(self.setEnvActioin) self.setAbout = QAction(QIcon(static.setPNG), static.menuAbout, self) self.setAbout.setStatusTip('About') # 状态栏提示 self.menuHelp = self.menuBar.addMenu(static.menuHelp) self.menuHelp.addAction(self.setAbout) # set all layout self.hbox = QHBoxLayout(self) # device ======== self.topLeft = QFrame(self) self.topLeft.setMaximumSize(218, 300) self.topLeft.setMinimumSize(218, 200) self.topLeft.setFrameShape(QFrame.StyledPanel) self.topLeftLayout = QVBoxLayout(self.topLeft) self.toolBar = QToolBar() # self.androidDeviceAction = QRadioButton('Android', self) # self.androidDeviceAction.setFocusPolicy(Qt.NoFocus) # self.ipDeviceAction = QRadioButton('IP', self) # self.ipDeviceAction.setFocusPolicy(Qt.NoFocus) # self.ipDeviceAction.move(10, 10) # self.ipDeviceAction.toggle() self.findDeviceAction = QAction(QIcon(static.findDevice), static.findDeviceButton, self) self.deleteDeviceAction = QAction(QIcon(static.deleteDevice), static.deleteDeviceButton, self) # self.toolBar.addWidget(self.androidDeviceAction) # self.toolBar.addWidget(self.ipDeviceAction) self.toolBar.addAction(self.findDeviceAction) self.toolBar.addAction(self.deleteDeviceAction) self.deviceLab = QLabel(static.deviceName, self) self.device = QTableWidget(1, 2) self.device.setHorizontalHeaderLabels(['name', 'status']) self.device.setColumnWidth(0, 100) self.device.setColumnWidth(1, 80) self.topLeftLayout.addWidget(self.deviceLab) self.topLeftLayout.addWidget(self.toolBar) self.topLeftLayout.addWidget(self.device) # set button or other for running monkey or not and status of device and log ======== self.topRight = QFrame(self) self.topRight.setFrameShape(QFrame.StyledPanel) self.topRight.setMaximumHeight(40) self.startButton = QPushButton(QIcon(static.startPNG), "") self.stopButton = QPushButton(QIcon(static.stopPNG), "") self.status = QLabel(static.status) self.statusEdit = QLineEdit(self) self.statusEdit.setReadOnly(True) self.statusEdit.setMaximumWidth(80) self.statusEdit.setMinimumWidth(80) self.statusEdit.setText("") # check log self.checkLogButton = QPushButton(static.checkLog) self.checkLogButton.setMaximumHeight(20) self.checkLogButton.setMinimumHeight(20) self.checkLogButton.setMaximumWidth(60) self.selectLog = QLabel(static.selectlog) self.logfile = QComboBox() self.dirlist = os.listdir(os.path.join(DIR, "Result")) for d in self.dirlist: if d != "AutoMonkey.log": self.logfile.insertItem(0, d) self.logfile.setMaximumWidth(150) self.logfile.setMaximumHeight(20) self.logfile.setMinimumHeight(20) self.topLayout = QHBoxLayout(self.topRight) self.topLayout.addWidget(self.startButton) self.topLayout.addWidget(self.stopButton) self.topLayout.addWidget(self.status) self.topLayout.addWidget(self.statusEdit) self.topLayout.addWidget(self.selectLog) self.topLayout.addWidget(self.logfile) self.topLayout.addWidget(self.checkLogButton) # set parameter for monkey ======= self.midRight = QFrame(self) self.midRight.setMaximumSize(555, 200) self.midRight.setMinimumSize(555, 200) self.midRight.setFrameShape(QFrame.StyledPanel) self.midRightLayout = QVBoxLayout(self.midRight) self.subLayout0 = QVBoxLayout() self.subLayout1 = QVBoxLayout() self.subLayout2 = QHBoxLayout() self.subLayout3 = QVBoxLayout() self.subLayout4 = QVBoxLayout() self.subLayout5 = QHBoxLayout() self.subLayout6 = QHBoxLayout() self.toolBar = QToolBar() # self.storeAction = QAction(QIcon(static.storePNG), static.storeButton, self) self.startAction = QAction(QIcon(static.startPNG), static.startButton, self) self.stopAction = QAction(QIcon(static.stopPNG), static.stopButton, self) # self.toolBar.addAction(self.storeAction) self.toolBar.addAction(self.startAction) self.toolBar.addAction(self.stopAction) self.timeLongLbl = QLabel(static.timeString, self) self.timeLong = QLineEdit(self) self.timeLong.setMaximumWidth(100) self.timeLong.setMinimumWidth(100) self.timeLong.setPlaceholderText(static.timeLong) self.timeLongUnit = QLabel("H") self.etSetLbl = QLabel(static.eventTypeSet) self.etSet = QTableWidget(2, 2) self.etSet.setMaximumHeight(150) self.etSet.setHorizontalHeaderLabels(['option', 'value']) self.etSet.horizontalHeader().setStretchLastSection(True) self.etSet.setItem(0, 0, QTableWidgetItem("--throttle")) self.etSet.setItem(0, 1, QTableWidgetItem(str(static.eventType["--throttle"]))) # set event type percent self.etPercentLbl = QLabel(static.eventTpyePercent, self) self.etPercent = QTableWidget(2, 2) self.etPercent.setMaximumHeight(150) self.etPercent.setHorizontalHeaderLabels(['option', 'value']) self.etPercent.horizontalHeader().setStretchLastSection(True) self.etPercent.setItem(0, 0, QTableWidgetItem("--pct-touch")) self.etPercent.setItem(0, 1, QTableWidgetItem(str(static.eventPercent["--pct-touch"]))) self.etPercent.setItem(1, 0, QTableWidgetItem("--pct-motion")) self.etPercent.setItem(1, 1, QTableWidgetItem(str(static.eventPercent["--pct-motion"]))) # self.storeButton = QPushButton(QIcon(static.storePNG), static.storeButton) # self.storeButton.setToolTip(static.storeButton) self.exportButton = QPushButton(QIcon(static.exportPNG), static.exportFile) self.exportButton.setToolTip(static.exportFile) self.importButton = QPushButton(QIcon(static.importPNG), static.importFile) self.importButton.setToolTip(static.importFile) self.subLayout2.addWidget(self.timeLongLbl) self.subLayout2.addWidget(self.timeLong) self.subLayout2.addWidget(self.timeLongUnit) self.subLayout2.addWidget(QLabel(" " * 300)) # self.subLayout2.addWidget(self.storeButton) self.subLayout2.addWidget(self.exportButton) self.subLayout2.addWidget(self.importButton) self.subLayout0.addLayout(self.subLayout2) self.subLayout3.addWidget(self.etSetLbl) self.subLayout3.addWidget(self.etSet) self.subLayout4.addWidget(self.etPercentLbl) self.subLayout4.addWidget(self.etPercent) self.subLayout5.addLayout(self.subLayout0) self.subLayout6.addLayout(self.subLayout3) self.subLayout6.addLayout(self.subLayout4) self.midRightLayout.addLayout(self.subLayout5) self.midRightLayout.addLayout(self.subLayout6) # log ======== self.bottom = QFrame(self) self.bottom.setFrameShape(QFrame.StyledPanel) # log information self.logInfo = QLabel(static.logInfo) # information filter self.logFilter = QLabel(static.logFilter) self.monkeyButton = QPushButton(static.openMonkeyLog) self.combo = QComboBox() for i in range(len(static.logLevel)): self.combo.addItem(static.logLevel[i]) self.combo.setMaximumWidth(55) self.combo.setMaximumHeight(20) self.combo.setMinimumHeight(20) # information details self.bottomLayout = QVBoxLayout(self.bottom) self.subLayout = QHBoxLayout() self.subLayout.addWidget(self.logInfo) for i in range(10): self.subLayout.addWidget(QLabel("")) self.subLayout.addWidget(self.monkeyButton) self.subLayout.addWidget(self.logFilter) self.subLayout.addWidget(self.combo) self.bottomLayout.addLayout(self.subLayout) self.tabwidget = TabWidget() self.tabwidget.setMinimumHeight(100) self.bottomLayout.addWidget(self.tabwidget) # splitter mainWindow ++++++++++++++++++++++++++++++++++++ self.splitter2 = QSplitter(Qt.Vertical) self.splitter2.addWidget(self.topRight) self.splitter2.addWidget(self.midRight) self.splitter0 = QSplitter(Qt.Horizontal) self.splitter0.addWidget(self.topLeft) self.splitter0.addWidget(self.splitter2) self.splitter1 = QSplitter(Qt.Vertical) self.splitter1.addWidget(self.menuBar) self.splitter1.addWidget(self.splitter0) self.splitter1.addWidget(self.bottom) self.hbox.addWidget(self.splitter1) self.setLayout(self.hbox) self.show() def about(self): common.showDialog(static.menuAbout, static.dialogAbout) def init_conf(self): common.Log.info("init monkey conf") with open(monkeyConfFile, "w") as f: f.write("deviceList = []" + "\n") f.write("times = " + static.times + "\n") f.write("--throttle = " + static.eventType["--throttle"] + "\n") f.write("--pct-touch = " + static.eventPercent["--pct-touch"] + "\n") f.write("--pct-motion = " + static.eventPercent["--pct-motion"] + "\n") def setup_env(self): pass def _set_eventType(self, confFile): try: option = self.etSet.item(0, 0).text() value = self.etSet.item(0, 1).text() if value > "0": with open(confFile, 'a+') as f: f.write(option + " = " + value + "\n") except AttributeError, e: pass