Skip to content

Commit

Permalink
Draft: small improvements to the todo module
Browse files Browse the repository at this point in the history
Small ammeds to the docstrings and the use of `App` and `Gui`
namespaces, and the explicit use of the `ToDo` class
in `PascalCase`.
  • Loading branch information
vocx-fc authored and yorikvanhavre committed May 4, 2020
1 parent 14423df commit da413bd
Showing 1 changed file with 41 additions and 38 deletions.
79 changes: 41 additions & 38 deletions src/Mod/Draft/draftutils/todo.py
Expand Up @@ -23,10 +23,11 @@
# ***************************************************************************
"""Provides the ToDo class for the Draft Workbench.
The ToDo class is used to delay the commit of commands for later execution.
The `ToDo` class is used to delay the commit of commands for later execution.
This is necessary when a GUI command needs to manipulate the 3D view
in such a way that a callback would crash Coin.
The ToDo class essentially calls `QtCore.QTimer.singleShot`
in such a way that a callback would crash `Coin`.
The `ToDo` class essentially calls `QtCore.QTimer.singleShot`
to execute the instructions stored in internal lists.
"""
## @package todo
Expand All @@ -38,8 +39,8 @@
import traceback
from PySide import QtCore

import FreeCAD
import FreeCADGui
import FreeCAD as App
import FreeCADGui as Gui
from draftutils.messages import _msg, _wrn, _err, _log

__title__ = "FreeCAD Draft Workbench, Todo class"
Expand All @@ -59,7 +60,7 @@ class ToDo:
Attributes
----------
itinerary : list of tuples
itinerary: list of tuples
Each tuple is of the form `(name, arg)`.
The `name` is a reference (pointer) to a function,
and `arg` is the corresponding argument that is passed
Expand All @@ -70,7 +71,7 @@ class ToDo:
name(arg)
name()
commitlist : list of tuples
commitlist: list of tuples
Each tuple is of the form `(name, command_list)`.
The `name` is a string identifier or description of the commands
that will be run, and `command_list` is a list of strings
Expand All @@ -82,21 +83,21 @@ class ToDo:
and finally commits the transaction.
::
command_list = ["command1", "command2", "..."]
FreeCAD.ActiveDocument.openTransaction(name)
FreeCADGui.doCommand("command1")
FreeCADGui.doCommand("command2")
FreeCADGui.doCommand("...")
FreeCAD.ActiveDocument.commitTransaction()
App.activeDocument().openTransaction(name)
Gui.doCommand("command1")
Gui.doCommand("command2")
Gui.doCommand("...")
App.activeDocument().commitTransaction()
If `command_list` is a reference to a function
the function is executed directly.
::
command_list = function
FreeCAD.ActiveDocument.openTransaction(name)
App.activeDocument().openTransaction(name)
function()
FreeCAD.ActiveDocument.commitTransaction()
App.activeDocument().commitTransaction()
afteritinerary : list of tuples
afteritinerary: list of tuples
Each tuple is of the form `(name, arg)`.
This list is used just like `itinerary`.
Expand Down Expand Up @@ -125,7 +126,7 @@ def doTasks():
todo.commitlist,
todo.afteritinerary))
try:
for f, arg in todo.itinerary:
for f, arg in ToDo.itinerary:
try:
if _DEBUG_inner:
_msg("Debug: executing.\n"
Expand All @@ -144,10 +145,10 @@ def doTasks():
except ReferenceError:
_wrn("Debug: ToDo.doTasks: "
"queue contains a deleted object, skipping")
todo.itinerary = []
ToDo.itinerary = []

if todo.commitlist:
for name, func in todo.commitlist:
if ToDo.commitlist:
for name, func in ToDo.commitlist:
if six.PY2:
if isinstance(name, six.text_type):
name = name.encode("utf8")
Expand All @@ -156,13 +157,13 @@ def doTasks():
"name: {}\n".format(name))
try:
name = str(name)
FreeCAD.ActiveDocument.openTransaction(name)
App.activeDocument().openTransaction(name)
if isinstance(func, list):
for string in func:
FreeCADGui.doCommand(string)
Gui.doCommand(string)
else:
func()
FreeCAD.ActiveDocument.commitTransaction()
App.activeDocument().commitTransaction()
except Exception:
_log(traceback.format_exc())
_err(traceback.format_exc())
Expand All @@ -171,11 +172,11 @@ def doTasks():
"in {1}".format(sys.exc_info()[0], func))
_wrn(wrn)
# Restack Draft screen widgets after creation
if hasattr(FreeCADGui, "Snapper"):
FreeCADGui.Snapper.restack()
todo.commitlist = []
if hasattr(Gui, "Snapper"):
Gui.Snapper.restack()
ToDo.commitlist = []

for f, arg in todo.afteritinerary:
for f, arg in ToDo.afteritinerary:
try:
if _DEBUG_inner:
_msg("Debug: executing after.\n"
Expand All @@ -191,7 +192,7 @@ def doTasks():
"{0}\n"
"in {1}({2})".format(sys.exc_info()[0], f, arg))
_wrn(wrn)
todo.afteritinerary = []
ToDo.afteritinerary = []

@staticmethod
def delay(f, arg):
Expand All @@ -209,23 +210,23 @@ def delay(f, arg):
Parameters
----------
f : function reference
f: function reference
A reference (pointer) to a Python command
which can be executed directly.
::
f()
arg : argument reference
arg: argument reference
A reference (pointer) to the argument to the `f` function.
::
f(arg)
"""
if _DEBUG:
_msg("Debug: delaying.\n"
"function: {}\n".format(f))
if todo.itinerary == []:
QtCore.QTimer.singleShot(0, todo.doTasks)
todo.itinerary.append((f, arg))
if ToDo.itinerary == []:
QtCore.QTimer.singleShot(0, ToDo.doTasks)
ToDo.itinerary.append((f, arg))

@staticmethod
def delayCommit(cl):
Expand All @@ -242,7 +243,7 @@ def delayCommit(cl):
Parameters
----------
cl : list of tuples
cl: list of tuples
Each tuple is of the form `(name, command_list)`.
The `name` is a string identifier or description of the commands
that will be run, and `command_list` is a list of strings
Expand All @@ -253,8 +254,8 @@ def delayCommit(cl):
if _DEBUG:
_msg("Debug: delaying commit.\n"
"commitlist: {}\n".format(cl))
QtCore.QTimer.singleShot(0, todo.doTasks)
todo.commitlist = cl
QtCore.QTimer.singleShot(0, ToDo.doTasks)
ToDo.commitlist = cl

@staticmethod
def delayAfter(f, arg):
Expand All @@ -275,9 +276,11 @@ def delayAfter(f, arg):
if _DEBUG:
_msg("Debug: delaying after.\n"
"function: {}\n".format(f))
if todo.afteritinerary == []:
QtCore.QTimer.singleShot(0, todo.doTasks)
todo.afteritinerary.append((f, arg))
if ToDo.afteritinerary == []:
QtCore.QTimer.singleShot(0, ToDo.doTasks)
ToDo.afteritinerary.append((f, arg))


# In the past, the class was in lowercase, so we provide a reference
# to it in lowercase, to satisfy the usage of older modules.
todo = ToDo

0 comments on commit da413bd

Please sign in to comment.