@@ -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
0 commit comments