Skip to content

Commit 1403f70

Browse files
committed
Refactor to support WorkboxTextEdit and overall cleaner structure
1 parent ac42377 commit 1403f70

File tree

9 files changed

+569
-345
lines changed

9 files changed

+569
-345
lines changed

preditor/gui/console.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def errorHyperlink(self):
287287
cmdTempl = window.textEditorCmdTempl
288288

289289
# Bail if not setup properly
290-
if workboxName is None:
290+
if not workboxName:
291291
msg = (
292292
"Cannot use traceback hyperlink (Correct the path with Options "
293293
"> Set Preferred Text Editor Path).\n"
@@ -334,7 +334,7 @@ def errorHyperlink(self):
334334
)
335335
subprocess.Popen(command)
336336
except (ValueError, OSError):
337-
msg = "The provided text editor command template is not valid:\n {}"
337+
msg = "The provided text editor command is not valid:\n {}"
338338
msg = msg.format(cmdTempl)
339339
print(msg)
340340
elif workboxName is not None:
@@ -427,9 +427,11 @@ def getWorkboxLine(self, name, lineNum):
427427
workbox = self.window().workbox_for_name(name)
428428
if not workbox:
429429
return None
430-
if lineNum > workbox.lines():
430+
431+
num_lines = workbox.__num_lines__()
432+
if lineNum > num_lines:
431433
return None
432-
txt = workbox.text(lineNum).strip() + "\n"
434+
txt = workbox.__text_line__(lineNum=lineNum).strip() + "\n"
433435
return txt
434436

435437
def executeString(

preditor/gui/drag_tab_bar.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ def get_color_and_tooltip(self, index):
107107
widget = self.parent().widget(index)
108108

109109
filename = None
110-
if hasattr(widget, "text"):
111-
filename = widget.__filename__() or widget._filename_pref
110+
if hasattr(widget, "__filename__"):
111+
filename = widget.__filename__()
112112

113113
if widget.__changed_by_instance__():
114114
if filename:
@@ -145,13 +145,12 @@ def get_color_and_tooltip(self, index):
145145
else:
146146
state = TabStates.Dirty
147147
toolTip = "Workbox has unsaved changes, or it's name has changed."
148-
elif filename:
149-
if Path(filename).is_file():
150-
state = TabStates.Linked
151-
toolTip = "Linked to file on disk"
152-
else:
153-
state = TabStates.MissingLinked
154-
toolTip = "Linked file is missing"
148+
elif widget.__is_missing_linked_file__():
149+
state = TabStates.MissingLinked
150+
toolTip = "Linked file is missing"
151+
elif hasattr(widget, "__filename__") and widget.__filename__():
152+
state = TabStates.Linked
153+
toolTip = "Linked to file on disk"
155154

156155
if hasattr(widget, "__workbox_id__"):
157156
workbox_id = widget.__workbox_id__()
@@ -347,8 +346,8 @@ def tab_menu(self, pos, popup=True):
347346

348347
# Show File-related actions depending if filename already set. Don't include
349348
# Rename if the workbox is linked to a file.
350-
if hasattr(workbox, 'filename'):
351-
if not workbox.filename():
349+
if hasattr(workbox, '__filename__'):
350+
if not workbox.__filename__():
352351
act = menu.addAction('Rename')
353352
act.triggered.connect(self.rename_tab)
354353

@@ -358,7 +357,7 @@ def tab_menu(self, pos, popup=True):
358357
act = menu.addAction('Save and Link File')
359358
act.triggered.connect(partial(self.save_and_link_file, workbox))
360359
else:
361-
if Path(workbox.filename()).is_file():
360+
if Path(workbox.__filename__()).is_file():
362361
act = menu.addAction('Explore File')
363362
act.triggered.connect(partial(self.explore_file, workbox))
364363

@@ -397,14 +396,21 @@ def link_file(self, workbox):
397396
Args:
398397
workbox (WorkboxMixin): The workbox contained in the clicked tab
399398
"""
400-
filename = workbox.filename()
399+
filename = workbox.__filename__()
400+
workbox.__set_file_monitoring_enabled__(False)
401+
workbox.__set_filename__("")
401402
filename, _other = QFileDialog.getOpenFileName(directory=filename)
402403
if filename and Path(filename).is_file():
404+
405+
# First, save any unsaved text
406+
workbox.__save_prefs__()
407+
408+
# Now, load file
403409
workbox.__load__(filename)
404-
workbox._filename_pref = filename
405-
workbox._filename = filename
406-
name = Path(filename).name
410+
workbox.__set_filename__(filename)
411+
workbox.__set_file_monitoring_enabled__(True)
407412

413+
name = Path(filename).name
408414
self.setTabText(self._context_menu_tab, name)
409415
self.update()
410416
self.window().setWorkboxFontBasedOnConsole(workbox=workbox)
@@ -417,29 +423,32 @@ def save_and_link_file(self, workbox):
417423
Args:
418424
workbox (WorkboxMixin): The workbox contained in the clicked tab
419425
"""
420-
filename = workbox.filename()
426+
filename = workbox.__filename__()
427+
if filename and Path(filename).is_file():
428+
workbox.__set_file_monitoring_enabled__(False)
421429
directory = six.text_type(Path(filename).parent) if filename else ""
422-
success = workbox.saveAs(directory=directory)
430+
success = workbox.__save_as__(directory=directory)
423431
if not success:
424432
return
425433

426-
filename = workbox.filename()
427-
workbox._filename_pref = filename
428-
workbox._filename = filename
429-
workbox.__set_last_workbox_name__(workbox.__workbox_name__())
434+
# Workbox
435+
filename = workbox.__filename__()
436+
workbox.__set_last_saved_text__(workbox.__text__())
437+
workbox.__set_file_monitoring_enabled__(True)
430438
name = Path(filename).name
431439

432440
self.setTabText(self._context_menu_tab, name)
433441
self.update()
434442
self.window().setWorkboxFontBasedOnConsole(workbox=workbox)
443+
workbox.__set_last_workbox_name__(workbox.__workbox_name__())
435444

436445
def explore_file(self, workbox):
437446
"""Open a system file explorer at the path of the linked file.
438447
439448
Args:
440449
workbox (WorkboxMixin): The workbox contained in the clicked tab
441450
"""
442-
path = Path(workbox._filename_pref)
451+
path = Path(workbox.__filename__())
443452
if path.exists():
444453
osystem.explore(str(path))
445454
elif path.parent.exists():
@@ -451,9 +460,8 @@ def unlink_file(self, workbox):
451460
Args:
452461
workbox (WorkboxMixin): The workbox contained in the clicked tab
453462
"""
454-
workbox.updateFilename("")
455-
workbox._filename_pref = ""
456-
463+
workbox.__set_file_monitoring_enabled__(False)
464+
workbox.__set_filename__("")
457465
name = self.parent().default_title
458466
self.setTabText(self._context_menu_tab, name)
459467

preditor/gui/group_tab_widget/__init__.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def add_new_tab(self, group, title=None, prefs=None):
9797
WorkboxMixin: The new text editor.
9898
"""
9999
if not group:
100-
group = self.get_next_available_tab_name(self.default_title)
100+
group = self.get_next_available_tab_name()
101101
elif group is True:
102102
group = self.currentIndex()
103103

@@ -172,6 +172,20 @@ def default_tab(self, title=None, prefs=None):
172172
)
173173
return widget, title
174174

175+
def get_next_available_tab_name(self, name=None):
176+
"""Get the next available tab name, providing a default if needed.
177+
178+
Args:
179+
name (str, optional): The name for which to get the next available
180+
name.
181+
182+
Returns:
183+
str: The determined next available tab name
184+
"""
185+
if name is None:
186+
name = self.default_title
187+
return super().get_next_available_tab_name(name)
188+
175189
def append_orphan_workboxes_to_prefs(self, prefs, existing_by_group):
176190
"""If prefs are saved in a different PrEditor instance (in this same core)
177191
there may be a workbox which is either:
@@ -343,9 +357,9 @@ def restore_prefs(self, prefs):
343357
# Support legacy arg for emergency backwards compatibility
344358
tempfile = tab.get('tempfile', None)
345359
# Get various possible saved filepaths.
346-
filename_pref = tab.get('filename', "")
347-
if filename_pref:
348-
if Path(filename_pref).is_file():
360+
filename = tab.get('filename', "")
361+
if filename:
362+
if Path(filename).is_file():
349363
loadable = True
350364

351365
# See if there are any workbox backups available
@@ -361,7 +375,7 @@ def restore_prefs(self, prefs):
361375
# tab if it hasn't already been created.
362376
prefs = dict(
363377
workbox_id=workbox_id,
364-
filename=filename_pref,
378+
filename=filename,
365379
backup_file=backup_file,
366380
existing_editor_info=existing_by_id.pop(workbox_id, None),
367381
orphaned_by_instance=orphaned_by_instance,

preditor/gui/group_tab_widget/grouped_tab_widget.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ def __is_dirty__(self):
7979
break
8080
return is_dirty
8181

82+
def __is_missing_linked_file__(self):
83+
"""Determine if any of the workboxes are linked to file which is missing
84+
on disk.
85+
86+
Returns:
87+
bool: Whether any of this group's workboxes define a linked file
88+
which is missing on disk.
89+
"""
90+
is_missing_linked_file = False
91+
for workbox_idx in range(self.count()):
92+
workbox = self.widget(workbox_idx)
93+
if workbox.__is_missing_linked_file__():
94+
is_missing_linked_file = True
95+
break
96+
return is_missing_linked_file
97+
8298
def add_new_editor(self, title=None, prefs=None):
8399
title = title or self.default_title
84100

@@ -120,6 +136,7 @@ def close_tab(self, index):
120136
# Keep track of deleted tabs, make re-openable
121137
# Maybe also move workbox dir to a 'removed workboxes' dir
122138

139+
_editor.__set_file_monitoring_enabled__(False)
123140
super(GroupedTabWidget, self).close_tab(index)
124141

125142
def default_tab(self, title=None, prefs=None):
@@ -138,7 +155,6 @@ def default_tab(self, title=None, prefs=None):
138155
if editor:
139156
editor.__load_workbox_version_text__(VersionTypes.Last)
140157

141-
editor.__set_tab_widget__(self)
142158
editor.__set_last_saved_text__(editor.text())
143159
editor.__set_last_workbox_name__(editor.__workbox_name__())
144160

@@ -154,12 +170,27 @@ def default_tab(self, title=None, prefs=None):
154170
editor.__set_orphaned_by_instance__(orphaned_by_instance)
155171
return editor, title
156172

173+
def get_next_available_tab_name(self, name=None):
174+
"""Get the next available tab name, providing a default if needed.
175+
176+
Args:
177+
name (str, optional): The name for which to get the next available
178+
name.
179+
180+
Returns:
181+
str: The determined next available tab name
182+
"""
183+
if name is None:
184+
name = self.default_title
185+
return super().get_next_available_tab_name(name)
186+
157187
def showEvent(self, event): # noqa: N802
158188
super(GroupedTabWidget, self).showEvent(event)
159189
self.tab_shown(self.currentIndex())
160190

161191
def tab_shown(self, index):
162192
editor = self.widget(index)
193+
editor.__set_tab_widget__(self)
163194
if editor and editor.isVisible():
164195
editor.__show__()
165196

preditor/gui/loggerwindow.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ def apply_options(self):
372372
self.editor_cls_name = editor_cls_name
373373
self.uiWorkboxTAB.editor_cls = editor_cls
374374
# We need to change the editor, save all prefs
375-
self.recordPrefs(manual=True)
375+
self.recordPrefs(manual=True, disableFileMonitoring=True)
376376
# Clear the uiWorkboxTAB
377377
self.uiWorkboxTAB.clear()
378378
# Restore prefs to populate the tabs
@@ -1103,13 +1103,15 @@ def linkedFileChanged(self, filename):
11031103
self.restorePrefs(skip_geom=True)
11041104
else:
11051105
for info in self.uiWorkboxTAB.all_widgets():
1106-
editor, _, _, group_idx, editor_idx = info
1107-
if not editor.filename():
1106+
editor, _, _, _, _ = info
1107+
if not editor or not editor.__filename__():
11081108
continue
1109-
if Path(editor.filename()).as_posix() == Path(filename).as_posix():
1109+
if Path(editor.__filename__()) == Path(filename):
1110+
editor.__set_file_monitoring_enabled__(False)
11101111
editor.__save_prefs__(saveLinkedFile=False)
11111112
editor.__reload_file__()
11121113
editor.__save_prefs__(saveLinkedFile=False, force=True)
1114+
editor.__set_file_monitoring_enabled__(True)
11131115

11141116
def closeEvent(self, event):
11151117
self.recordPrefs()
@@ -1166,10 +1168,15 @@ def reportExecutionTime(self, seconds):
11661168
self.uiStatusLBL.showSeconds(seconds)
11671169
self.uiMenuBar.adjustSize()
11681170

1169-
def recordPrefs(self, manual=False):
1171+
def recordPrefs(self, manual=False, disableFileMonitoring=False):
11701172
if not manual and not self.autoSaveEnabled():
11711173
return
11721174

1175+
if disableFileMonitoring:
1176+
for editor_info in self.uiWorkboxTAB.all_widgets():
1177+
editor = editor_info[0]
1178+
editor.__set_file_monitoring_enabled__(False)
1179+
11731180
origPref = self.load_prefs()
11741181
pref = copy.deepcopy(origPref)
11751182
geo = self.geometry()

0 commit comments

Comments
 (0)