From 5b1179a15a58eedcf0e604dc0410257faa0b736d Mon Sep 17 00:00:00 2001 From: Jeremy Mack Wright Date: Fri, 21 Dec 2018 17:09:31 -0500 Subject: [PATCH] Started transition to file watcher for external editor, which required change to using FreeCAD environment variables for settings. --- CQGui/Command.py | 9 ++++----- CQGui/ImportCQ.py | 35 ++++++++++++++++++++++------------- CodeEditor.py | 7 +++---- Init.py | 14 ++++++++++++-- Settings.py | 6 ------ 5 files changed, 41 insertions(+), 30 deletions(-) delete mode 100644 Settings.py diff --git a/CQGui/Command.py b/CQGui/Command.py index bd6d0b8..a10dcc8 100644 --- a/CQGui/Command.py +++ b/CQGui/Command.py @@ -13,7 +13,6 @@ try: except: from . import ImportCQ import module_locator -import Settings import Shared from random import random from contextlib import contextmanager @@ -142,7 +141,7 @@ class CadQueryExecuteScript: def GetResources(self): return {"MenuText": "Execute Script", - "Accel": Settings.execute_keybinding, + "Accel": FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/cadquery-freecad-module").GetString("executeKeybinding"), "ToolTip": "Executes the CadQuery script", "Pixmap": ":/icons/media-playback-start.svg"} @@ -193,8 +192,8 @@ class CadQueryExecuteScript: build_result = cqModel.build(build_parameters=build_parameters) - if Settings.report_execute_time: - FreeCAD.Console.PrintMessage("Script executed in " + str(build_result.buildTime) + " seconds\r\n") + # if Settings.report_execute_time: + # FreeCAD.Console.PrintMessage("Script executed in " + str(build_result.buildTime) + " seconds\r\n") # Make sure that the build was successful if build_result.success: @@ -332,7 +331,7 @@ class CadQuerySaveScript: ExportCQ.save() # Execute the script if the user has asked for it - if Settings.execute_on_save: + if FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/cadquery-freecad-module").GetBool("executeOnSave"): CadQueryExecuteScript().Activated() class CadQuerySaveAsScript: diff --git a/CQGui/ImportCQ.py b/CQGui/ImportCQ.py index 4f24540..803077c 100644 --- a/CQGui/ImportCQ.py +++ b/CQGui/ImportCQ.py @@ -6,7 +6,6 @@ import FreeCAD, FreeCADGui from PySide import QtGui import module_locator from CodeEditor import CodeEditor -import Settings #Distinguish python built-in open function from the one declared here if open.__module__ == '__builtin__': @@ -51,22 +50,32 @@ def open(filename): docname = os.path.basename(filename) # Set up the text area for our CQ code - server_path = os.path.join(module_base_path, 'cq_server.py') + # server_path = os.path.join(module_base_path, 'cq_server.py') - codePane = CodeEditor() + use_external_editor = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/cadquery-freecad-module").GetBool("useExternalEditor") - codePane.setObjectName("cqCodePane_" + os.path.splitext(os.path.basename(filename))[0]) + # If the user wants to use an external editor, don't bother with the built-in editor + if use_external_editor: + pass + else: + # Pull the font size from the FreeCAD-stored settings + fontSize = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/cadquery-freecad-module").GetInt("fontSize") - mdi = mw.findChild(QtGui.QMdiArea) - # add a widget to the mdi area - sub = mdi.addSubWindow(codePane) - sub.setWindowTitle(docname) - sub.setWindowIcon(QtGui.QIcon(':/icons/applications-python.svg')) - sub.show() - mw.update() + # Set up the code editor + codePane = CodeEditor() + codePane.setFont(QtGui.QFont('SansSerif', fontSize)) + codePane.setObjectName("cqCodePane_" + os.path.splitext(os.path.basename(filename))[0]) - #Pull the text of the CQ script file into our code pane - codePane.open(filename) + 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", diff --git a/CodeEditor.py b/CodeEditor.py index e3985aa..8d6265c 100644 --- a/CodeEditor.py +++ b/CodeEditor.py @@ -1,5 +1,6 @@ +import FreeCAD from PySide import QtCore -from PySide.QtCore import QSize, QSettings, QRect, Qt, QRegExp +from PySide.QtCore import QSize, QRect, Qt, QRegExp from PySide.QtGui import QPainter, QSyntaxHighlighter, QTextCharFormat, QFont, QColor, QTextCursor, QPlainTextEdit, QTextEdit, QWidget class LineNumberArea(QWidget): @@ -14,8 +15,6 @@ class LineNumberArea(QWidget): self.codeEditor.lineNumberAreaPaintEvent(event) class CodeEditor(QPlainTextEdit): - settings = QSettings('cqcad', 'settings') - def __init__(self): # super(CodeEditor, self).__init__() QPlainTextEdit.__init__(self) @@ -30,7 +29,7 @@ class CodeEditor(QPlainTextEdit): self.dirty = False # Determine if the line number area needs to be shown or not - lineNumbersCheckedState = self.settings.value('editor_line_numbers_visible', type=bool) + lineNumbersCheckedState = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/cadquery-freecad-module").GetBool("showLineNumbers") if lineNumbersCheckedState: self.showLineNumberArea() else: diff --git a/Init.py b/Init.py index 12f423f..0d9f7eb 100644 --- a/Init.py +++ b/Init.py @@ -32,5 +32,15 @@ if os.path.exists(fc_lib_path): if os.path.exists(fc_bin_path): sys.path.insert(1, fc_bin_path) -# Need to set this for PyQode -os.environ['QT_API'] = 'pyside' +# Set sane defaults for FreeCAD-stored settings if they haven't been set yet +has_run_before = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/cadquery-freecad-module").GetBool("runBefore") + +if not has_run_before: + FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/cadquery-freecad-module").SetBool("useExternalEditor", False) + FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/cadquery-freecad-module").SetInt("fontSize", 12) + FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/cadquery-freecad-module").SetString("executeKeybinding", "F9") + FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/cadquery-freecad-module").SetBool("executeOnSave", False) + FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/cadquery-freecad-module").SetBool("showLineNumbers", True) + + # Make sure we don't overwrite someone's existing settings + FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/cadquery-freecad-module").SetBool("runBefore", True) \ No newline at end of file diff --git a/Settings.py b/Settings.py deleted file mode 100644 index 822ffe3..0000000 --- a/Settings.py +++ /dev/null @@ -1,6 +0,0 @@ -execute_on_save = False # Automatically execute a script every time you save -use_external_editor = False # Automatically reloads and executes a file when an external change is made -max_line_length = 79 # The number of characters per line that is allowed before a warning is given -font_size = 10 # Sets the font size of the Python editor -execute_keybinding = 'F2' # The key(s) that is pressed to execute the currently active script -report_execute_time = False # Whether or not to display the time it took to execute a script (must use show_object CQGI function)