2 @package libtextworker.interface.base.dirctrl
3 @brief The base of DirCtrl - directory tree widget
13 from typing
import Callable, Literal, Any
14 from .
import DC_FLAGS, WidgetBase
27 DC_ONEROOT = DC_FLAGS.DC_ONEROOT
28 DC_EDIT = DC_FLAGS.DC_EDIT
29 DC_HIDEROOT = DC_FLAGS.DC_HIDEROOT
30 DC_MULTIPLE = DC_FLAGS.DC_MULTIPLE
31 DC_DIRONLY = DC_FLAGS.DC_DIRONLY
32 DC_RIGHTCL = DC_FLAGS.DC_RIGHTCL
33 DC_USEICON = DC_FLAGS.DC_USEICON
39 Styles are defined in ..DC_FLAGS class and called in this module.
42 * DC_EDIT (toolkit-specific)
43 * DC_USEICON (toolkit-specific)
45 Features can be different on different platforms - this depends on the developer.
46 The actual tree, also scrollbars if enabled - must be placed in a frame (named Frame).
47 Custom flags (styles above) should be checked & implemented manually.
51 Styles = DC_EDIT | DC_USEICON
52 History: dict[Any, list[str]]
57 Make DirCtrl to show a directory tree.
58 If the directory is already used, will find that one and redraw.
59 @param path (str): Target folder
60 @param newroot (bool): Multiple root? Depends on DC_ONEROOT flag not to be included.
63 path = os.path.normpath(path)
64 if not os.path.isdir(path):
65 raise NotADirectoryError(path)
69 if DC_ONEROOT
in this.Styles
and newroot ==
True:
71 "DirCtrl.SetFolder: Attemping to create a new root node but"
72 "DC_ONEROOT style is enabled, which blocks that behaviour."
73 "Report this to the developer."
77 this, item: str | Callable |
None =
None, event: Callable |
None =
None
80 Get the full path of an item if specified, else the path of the curernt selection.
81 @param event (Callable | None): GUI toolkit's event. Optional.
82 @param item (str | Callable | None). Item to use.
88 if this.History
and this.HistoryIdx < len(this.History):
89 this.SetFolder(this.History[this.HistoryIdx + 1])
90 this.HistoryIdx = this.HistoryIdx + 1
93 this.PostSetDir(this.History[this.HistoryIdx],
"forward")
97 if this.History
and this.HistoryIdx > 0:
98 this.SetFolder(this.History[this.HistoryIdx - 1])
99 this.HistoryIdx = this.HistoryIdx - 1
102 this.PostSetDir(this.History[this.HistoryIdx],
"back")
104 def PostSetDir(this, path, mode: Literal[
"forward",
"back",
"go"]) -> bool:
105 if not this.History
or this.HistoryIdx:
107 if mode ==
"forward":
108 if this.HistoryIdx == len(this.History):
111 this.HistoryIdx = this.HistoryIdx + 1
113 if this.HistoryIdx == 0:
116 this.HistoryIdx = this.HistoryIdx - 1
118 if this.HistoryIdx
not in [0, len(this.History)]:
119 for i
in range(this.HistoryIdx, len(this.History)):
121 this.History.append(path)
122 this.HistoryIdx = len(this.History)
127 def sizeof_fmt(this, num, suffix="B"):
128 for unit
in (
"",
"Ki",
"Mi",
"Gi",
"Ti",
"Pi",
"Ei",
"Zi"):
129 if abs(num) < 1024.0:
130 return f
"{num:3.1f}{unit}{suffix}"
132 return f
"{num:.1f}Yi{suffix}"
str GetFullPath(this, str|Callable|None item=None, Callable|None event=None)
def SetFolder(this, str path, bool newroot)