def format_result(self): result = self.api_data["response"]['content'] if not self.w_format_label.isChecked(): self.w_result.setDocument(color_json(result)) else: if 'format' not in self.api_data['config']: self.w_result.setDocument(color_json(result)) else: if not self.api_data['config']['format']: self.w_result.setDocument(color_json(result)) else: pass
def tab_selected(self, arg=None): if arg is not None: if arg == 0: self.try_sync_json_table() else: if not self.is_json_parse_err: self.w_json_edit.setDocument(color_json(self.json_param))
def eventFilter(self, widget, event): if event.type() == QEvent.KeyPress and widget is self: key = event.key() if key == Qt.Key_Enter or key == Qt.Key_Return: cursor = self.textCursor() text = self.toPlainText() position = cursor.position() text_before = text[:position] html_before = color_json(text_before).toHtml() text_after = text[position:] html_after = color_json(text_after).toHtml() self.setText("") cursor.insertHtml(html_before) position = cursor.position() cursor.insertHtml(html_after) cursor.setPosition(position) cursor.insertText("\n") self.setTextCursor(cursor) return True elif key == Qt.Key_BracketLeft: cursor = self.textCursor() cursor.insertText("[]") cursor.movePosition(QTextCursor.Left, QTextCursor.MoveAnchor, 1) self.setTextCursor(cursor) return True elif key == Qt.Key_BraceLeft: cursor = self.textCursor() cursor.insertText("{}") cursor.movePosition(QTextCursor.Left, QTextCursor.MoveAnchor, 1) self.setTextCursor(cursor) return True elif key == Qt.Key_Apostrophe: cursor = self.textCursor() cursor.insertText("''") cursor.movePosition(QTextCursor.Left, QTextCursor.MoveAnchor, 1) self.setTextCursor(cursor) return True elif key == Qt.Key_QuoteDbl: cursor = self.textCursor() cursor.insertText("\"\"") cursor.movePosition(QTextCursor.Left, QTextCursor.MoveAnchor, 1) self.setTextCursor(cursor) return True return QWidget.eventFilter(self, widget, event)
def result(self, rs: dict, is_save: bool): self.api_data = rs result = color_json(rs["response"]['content']) self.w_result.setDocument(result) status = self.print_status(rs["response"]['status']) self.w_status_code.setText(status) self.w_url.setText(rs["response"]['url']) cons = color_json(dict(rs["response"]['header'])) self.w_console.setDocument(cons) if is_save: if rs["response"]['status'] >= 400: self.show_ask_save_dialog(self.print_status(rs["response"]['status']), rs) else: self.save(rs)
def try_sync_json_table(self): self.is_json_parse_err = False str_param = self.w_json_edit.toPlainText() try: if str_param == "" or str_param.isspace(): self.json_param = {} else: self.json_param = json.loads(str_param) self.w_json_edit.setDocument(color_json(self.json_param)) self.is_json_parse_err = False except Exception as ex: print(ex) self.console.emit("JSON Param", str(ex)) self.is_json_parse_err = True finally: if self.is_json_parse_err: self.w_json_edit.setDocument(color_json(str_param)) else: self.w_json_edit.setDocument(color_json(self.json_param)) self.parse_param_to_table() return self.is_json_parse_err
def component(self): self.w_json_edit.setLineWrapMode(QTextEdit.NoWrap) self.w_json_edit.setAcceptRichText(True) if 'config' in self.api_data: if 'param' in self.api_data['config']: self.json_param = self.api_data['config']['param'] self.w_json_edit.setDocument( color_json(self.api_data['config']['param'])) self.parse_param_to_table() self.w_json_edit.installEventFilter(self) self.w_widget_tab.addTab(self.w_group, "UI") self.w_widget_tab.addTab(self.w_json_edit, "JSON") self.w_widget_tab.currentChanged.connect(self.tab_selected) w_run = QPushButton() w_run.setIcon(QIcon(get_icon_link('play_arrow.svg'))) w_run.setText("Run") w_run.setToolTip("Run API call (Ctrl+R)") w_run.pressed.connect(self.run_api_without_save) w_run_save = QPushButton() w_run_save.setIcon(QIcon(get_icon_link('play_circle_outline.svg'))) w_run_save.setText("Run && Save") w_run_save.setToolTip("Run and Save API call (Ctrl++Shift+R)") w_run_save.pressed.connect(self.run_api_save) w_sync = QPushButton() w_sync.setText("Sync") w_sync.setIcon(QIcon(get_icon_link('refresh.svg'))) w_sync.setToolTip("Sync param (Ctrl+Shift+C)") w_sync.pressed.connect(self.sync_param) l_action = QHBoxLayout() l_action.addWidget(w_run, alignment=Qt.AlignLeft) l_action.addWidget(w_run_save, alignment=Qt.AlignLeft) l_action.addStretch() l_action.addWidget(w_sync, alignment=Qt.AlignRight) w_group2 = QWidget() w_group2.setLayout(l_action) w_group2.setMaximumHeight(50) self.addWidget(self.header()) self.addWidget(self.w_widget_tab) self.addWidget(w_group2) self.setStretchFactor(0, 1) self.setStretchFactor(1, 2) self.setStretchFactor(2, 1) self.setStretchFactor(3, 1)
def component(self): self.w_url.setText("Url: ") self.w_url.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)) if "response" in self.api_data: if 'url' in self.api_data["response"]: self.w_url.setText(self.api_data["response"]['url']) self.w_status_code.setText("0 - No API call") if "response" in self.api_data: if 'status' in self.api_data["response"]: self.w_status_code.setText(self.print_status(self.api_data["response"]['status'])) l_status = QHBoxLayout() l_status.addWidget(self.w_url) l_status.addWidget(self.w_status_code, alignment=Qt.AlignRight) if "response" in self.api_data: if 'header' in self.api_data["response"]: self.w_console.setDocument(color_json(self.api_data["response"]['header'])) w_gr_vbox_console = QVBoxLayout() w_gr_vbox_console.addLayout(l_status) w_gr_vbox_console.addWidget(self.w_console) w_gr_console = QGroupBox() w_gr_console.setTitle("Console") w_gr_console.setLayout(w_gr_vbox_console) w_format = QPushButton() w_format.setText("Format") w_format.setIcon(QIcon(get_icon_link('format_indent_increase.svg'))) w_format.setToolTip("Format result (Ctrl+B)") w_format.pressed.connect(self.format_result) self.w_format_field.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)) if "config" in self.api_data: if 'format' in self.api_data['config']: self.w_format_field.setText(str(self.api_data['config']['format'])) # self.w_format_field.pressed.connect(self.open_format_manager) self.w_format_field.installEventFilter(self) self.w_format_label.setText("String object: ") l_hbox = QHBoxLayout() l_hbox.addWidget(self.w_format_label, alignment=Qt.AlignLeft) l_hbox.addWidget(self.w_format_field) l_hbox.addWidget(w_format, alignment=Qt.AlignRight) w_gr_vbox_result = QVBoxLayout() w_gr_vbox_result.addLayout(l_hbox) if "response" in self.api_data: if 'content' in self.api_data["response"]: self.w_result.setDocument(color_json(self.api_data["response"]['content'])) w_gr_vbox_result.addWidget(self.w_result) w_gr_result = QGroupBox() w_gr_result.setTitle("Result") w_gr_result.setLayout(w_gr_vbox_result) self.addWidget(w_gr_console) self.addWidget(w_gr_result) self.setStretchFactor(0, 1) self.setStretchFactor(1, 2)
def console(self, src="", arg=None): if arg is not None: print(src, arg, sep=": ") s = src + ": " + arg + "\n" new = color_json(self.w_console.toPlainText() + "\n" + s) self.w_console.setDocument(new)
def sync_param(self): if self.w_widget_tab.currentIndex() == 0: self.w_json_edit.setDocument(color_json(self.json_param)) elif self.w_widget_tab.currentIndex() == 1: self.try_sync_json_table()