Started transition to file watcher for external editor, which required change to using FreeCAD environment variables for settings.

This commit is contained in:
Jeremy Mack Wright 2018-12-21 17:09:31 -05:00
parent a24759ac65
commit 5b1179a15a
5 changed files with 41 additions and 30 deletions

View File

@ -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:

View File

@ -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",

View File

@ -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:

14
Init.py
View File

@ -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)

View File

@ -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)