def __init__(self, parent): super(EditSourceCodeDialog, self).__init__(parent) # create UI self.main_grid_layout = QGridLayout(self) main_vertical_splitter = QSplitter() main_vertical_splitter.setOrientation(Qt.Vertical) # instructions and actions layout header_widget = QWidget() header_layout = QHBoxLayout() instructions_text_edit = QTextEdit() instructions_text_edit.setText(instructions_text) instructions_text_edit.setEnabled(False) reset_button = QPushButton() reset_button.setText('reset') reset_button.clicked.connect(self.reset) header_layout.addWidget(instructions_text_edit) header_layout.addWidget(reset_button) header_widget.setLayout(header_layout) main_vertical_splitter.addWidget(header_widget) # code text edit self.code_text_edit = CodeEditor() self.code_text_edit.static_elements = ['def update(self, input_called=-1, token=None):', 'self.data_outputs_updated()', 'def get_data(self):', 'def set_data(self, data):', ''' def __init__(self, parent_node: Node, flow, configuration=None): super(%NODE_TITLE%_NodeInstance, self).__init__(parent_node, flow, configuration)''', 'class %NODE_TITLE%_NodeInstance'] self.code_text_edit.components = ['self.main_widget', 'self.outputs', 'self.input', 'self.special_actions'] main_vertical_splitter.addWidget(self.code_text_edit) self.main_grid_layout.addWidget(main_vertical_splitter) button_box = QDialogButtonBox() button_box.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.Ok) button_box.accepted.connect(self.accept) button_box.rejected.connect(self.reject) self.main_grid_layout.addWidget(button_box) self.setWindowTitle('Edit Source Code') self.resize(1300, 950) self.reset()
def __init__(self, parent): super(EditInputWidgetDialog, self).__init__(parent) # create UI self.main_grid_layout = QGridLayout(self) main_vertical_splitter = QSplitter() main_vertical_splitter.setOrientation(Qt.Vertical) # instructions and actions layout header_widget = QWidget() header_layout = QHBoxLayout() instructions_text_edit = QTextEdit() instructions_text_edit.setText(instructions_text) instructions_text_edit.setEnabled(False) reset_button = QPushButton() reset_button.setText('reset') reset_button.clicked.connect(self.reset) header_layout.addWidget(instructions_text_edit) header_layout.addWidget(reset_button) header_widget.setLayout(header_layout) main_vertical_splitter.addWidget(header_widget) # code text edit self.code_text_edit = CodeEditor() self.code_text_edit.static_elements = [ 'def get_data(self):', 'def set_data(self, data):' ] self.code_text_edit.components = [ 'self.parent_node_instance', 'self.parent_node_instance.update_shape()', 'self.parent_port_instance' ] main_vertical_splitter.addWidget(self.code_text_edit) self.main_grid_layout.addWidget(main_vertical_splitter) button_box = QDialogButtonBox() button_box.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.Ok) button_box.accepted.connect(self.accept) button_box.rejected.connect(self.reject) self.main_grid_layout.addWidget(button_box) self.setWindowTitle('Edit Source Code') self.resize(1300, 950) self.reset()
def __init__(self, parent=None): QWidget.__init__(self, parent) self.doc_bar = DocBar() self.editor = CodeEditor() self.console = None # c, asm, coe self.select = 'c' self.code = {'c': '', 'asm': '', 'coe': '', 'debug': ''} self.init_layout() self.init_action()
def open(filename): #All of the CQGui.* calls in the Python console break after opening if we don't do this FreeCADGui.doCommand("import FreeCADGui as CQGui") # Make sure that we enforce a specific version (2.7) of the Python interpreter ver = hex(sys.hexversion) interpreter = "python%s.%s" % (ver[2], ver[4]) # => 'python2.7' # The extra version numbers won't work on Windows if sys.platform.startswith('win'): interpreter = 'python' # Set up so that we can import from our embedded packages module_base_path = module_locator.module_path() libs_dir_path = os.path.join(module_base_path, 'Libs') # Make sure we get the right libs under the FreeCAD installation fc_base_path = os.path.dirname(os.path.dirname(module_base_path)) fc_lib_path = os.path.join(fc_base_path, 'lib') #Getting the main window will allow us to find the children we need to work with mw = FreeCADGui.getMainWindow() # Grab just the file name from the path/file that's being executed docname = os.path.basename(filename) # Pull the font size from the FreeCAD-stored settings fontSize = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/cadquery-freecad-module").GetInt("fontSize") # Set up the code editor codePane = CodeEditor() codePane.setFont(QtGui.QFont('SansSerif', fontSize)) codePane.setObjectName("cqCodePane_" + os.path.splitext(os.path.basename(filename))[0]) mdi = mw.findChild(QtGui.QMdiArea) # add a code editor widget to the mdi area sub = mdi.addSubWindow(codePane) sub.setWindowTitle(docname) sub.setWindowIcon(QtGui.QIcon(':/icons/applications-python.svg')) sub.show() mw.update() #Pull the text of the CQ script file into our code pane codePane.open(filename) msg = QtGui.QApplication.translate( "cqCodeWidget", "Opened ", None) FreeCAD.Console.PrintMessage(msg + filename + "\r\n") return
class EditMainWidgetDialog(QDialog): def __init__(self, parent): super(EditMainWidgetDialog, self).__init__(parent) # create UI self.main_grid_layout = QGridLayout(self) main_vertical_splitter = QSplitter() main_vertical_splitter.setOrientation(Qt.Vertical) # instructions and actions layout header_widget = QWidget() header_layout = QHBoxLayout() instructions_text_edit = QTextEdit() instructions_text_edit.setText(instructions_text) instructions_text_edit.setEnabled(False) reset_button = QPushButton() reset_button.setText('reset') reset_button.clicked.connect(self.reset) header_layout.addWidget(instructions_text_edit) header_layout.addWidget(reset_button) header_widget.setLayout(header_layout) main_vertical_splitter.addWidget(header_widget) # code text edit self.code_text_edit = CodeEditor() self.code_text_edit.static_elements = ['def get_data(self):', 'def set_data(self, data):', 'class %NODE_TITLE%_NodeInstance_MainWidget', '''def __init__(self, parent_node_instance): super(%NODE_TITLE%_NodeInstance_MainWidget, self).__init__()''' ] self.code_text_edit.components = ['self.parent_node_instance', 'self.parent_node_instance.update_shape()'] main_vertical_splitter.addWidget(self.code_text_edit) self.main_grid_layout.addWidget(main_vertical_splitter) button_box = QDialogButtonBox() button_box.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.Ok) button_box.accepted.connect(self.accept) button_box.rejected.connect(self.reject) self.main_grid_layout.addWidget(button_box) self.setWindowTitle('Edit Source Code') self.resize(1300, 950) self.reset() def reset(self): # load template f = open('template files/main_widget_default_template.txt', 'r') self.code_text_edit.set_code(f.read()) f.close() def set_code(self, code): self.code_text_edit.setPlainText(code) def get_code(self): return self.code_text_edit.toPlainText()
class DocEditor(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) self.doc_bar = DocBar() self.editor = CodeEditor() self.console = None # c, asm, coe self.select = 'c' self.code = {'c': '', 'asm': '', 'coe': '', 'debug': ''} self.init_layout() self.init_action() def init_layout(self): layout = QVBoxLayout() layout.addWidget(self.doc_bar) layout.addWidget(self.editor) layout.setSpacing(0) layout.setContentsMargins(0, 0, 0, 0) self.setLayout(layout) self.setStyleSheet(open("qss/ToolBarStyle.qss", 'r').read()) def init_action(self): self.doc_bar.label_btn_c.clicked.connect(lambda: self.click_label('c')) self.doc_bar.label_btn_asm.clicked.connect( lambda: self.click_label('asm')) self.doc_bar.label_btn_coe.clicked.connect( lambda: self.click_label('coe')) self.doc_bar.label_btn_debug.clicked.connect( lambda: self.click_label('debug')) self.doc_bar.run_btn.clicked.connect(lambda: self.click_run()) action = QAction('run', self) action.setShortcut('Ctrl+R') action.triggered.connect(lambda: self.click_run()) self.addAction(action) def connect_console(self, console): self.console = console def click_label(self, label_name): self.code[self.select] = self.editor.get_text() self.editor.sett_text(self.code[label_name]) self.select = label_name def click_run(self): self.console.setText("") if self.select == 'c': try: self.code['c'] = self.editor.get_text() self.code['asm'] = Compiler().parse(self.code['c']) self.code['coe'] = Assembler().parse(self.code['asm']) self.code['debug'] = self.get_debug_code() if str(self.console.toPlainText()) == "": print 'successful complie c code!' else: print 'failed complie c code!!!' except: print traceback.format_exc() print 'failed complie c code!!!' elif self.select == 'asm': try: self.code['asm'] = self.editor.get_text() self.code['coe'] = Assembler().parse(self.code['asm']) self.code['debug'] = self.get_debug_code() if str(self.console.toPlainText()) == "": print 'successful complie asm code!' else: print 'failed complie asm code!!!' except: print traceback.format_exc() print 'failed complie asm code!!!' def get_debug_code(self): asm_codes = self.code['asm'].split('\n') coe_data_codes, coe_text_codes = self.code['coe'].split('=' * 32) coe_data_codes = coe_data_codes.split('\n') coe_text_codes = coe_text_codes.split('\n')[2:] # update asm_code _temp = [] for asm_code in asm_codes: if asm_code != '': _temp.append(asm_code) asm_codes = _temp res = '' # uodate coe data code len_coe_data = len(coe_data_codes) - 2 len_coe_text = len(coe_text_codes) - 2 line_asm = 0 count = 0 while count < len_coe_data: if coe_data_codes[count].find('=') != -1: res += ' ' * 6 + coe_data_codes[count] + '\n' count += 1 else: if asm_codes[line_asm].find('.') == 0: res += ' ' * 6 + '// %s\n' % asm_codes[line_asm] line_asm += 1 else: res += '%.4d: %s // %s\n' % ( (count - 2) * 4, coe_data_codes[count], asm_codes[line_asm]) line_asm += 1 count += 1 count = 0 while count < len_coe_text: if coe_text_codes[count].find('=') != -1: res += ' ' * 6 + coe_text_codes[count] + '\n' count += 1 else: if asm_codes[line_asm].find( ':') != -1 or asm_codes[line_asm].find('.') == 0: res += ' ' * 6 + '// %s\n' % asm_codes[line_asm] line_asm += 1 else: res += '%.4d: %s // %s\n' % ( (count - 2) * 4, coe_text_codes[count], asm_codes[line_asm]) line_asm += 1 count += 1 return res