2323 get_prefs_dir ,
2424 get_relative_path ,
2525)
26+ from .group_tab_widget .one_tab_widget import OneTabWidget
2627
2728logger = logging .getLogger (__name__ )
2829
@@ -92,7 +93,9 @@ def __init__(
9293 self ._show_blank = False
9394 self ._tempdir = None
9495
95- self ._dialogShown = False
96+ # As event-driven dialogs are shown, add the tuple of (title, message)
97+ # to this list, to prevent multiple dialogs showing for same reason.
98+ self .shownDialogs = []
9699
97100 self .core_name = core_name
98101
@@ -141,7 +144,10 @@ def __set_last_saved_text__(self, text):
141144 text (str): The text to define as last_saved_text
142145 """
143146 self ._last_saved_text = text
144- self .__tab_widget__ ().tabBar ().update ()
147+
148+ tab_widget = self .__tab_widget__ ()
149+ if tab_widget is not None :
150+ tab_widget .tabBar ().update ()
145151
146152 def __last_saved_text__ (self ):
147153 """Returns the last_saved_text on this workbox
@@ -176,7 +182,14 @@ def __tab_widget__(self):
176182 Returns:
177183 GroupedTabWidget: The tab widget which contains this workbox
178184 """
179- return self ._tab_widget
185+ tab_widget = None
186+ parent = self .parent ()
187+ while parent is not None :
188+ if issubclass (parent .__class__ , OneTabWidget ):
189+ tab_widget = parent
190+ break
191+ parent = parent .parent ()
192+ return tab_widget
180193
181194 def __set_tab_widget__ (self , tab_widget ):
182195 """Set this workbox's _tab_widget to the provided tab_widget"""
@@ -363,8 +376,11 @@ def __workbox_name__(self, workbox=None):
363376 group_name = None
364377 workbox_name = None
365378
379+ grouped_tab_widget = workbox .__tab_widget__ ()
380+ if grouped_tab_widget is None :
381+ return WorkboxName ("" , "" )
382+
366383 if workbox :
367- grouped_tab_widget = workbox .__tab_widget__ ()
368384 for group_idx in range (workboxTAB .count ()):
369385 # If a previous iteration determine workbox_name, bust out
370386 if workbox_name :
@@ -381,20 +397,19 @@ def __workbox_name__(self, workbox=None):
381397 workbox_name = cur_group_widget .tabText (workbox_idx )
382398 break
383399 else :
384- grouped = workbox .__tab_widget__ ()
385- groupedTabBar = grouped .tabBar ()
400+ groupedTabBar = grouped_tab_widget .tabBar ()
386401
387402 idx = - 1
388- for idx in range (grouped .count ()):
389- if grouped .widget (idx ) == workbox :
403+ for idx in range (grouped_tab_widget .count ()):
404+ if grouped_tab_widget .widget (idx ) == workbox :
390405 break
391406 workbox_name = groupedTabBar .tabText (idx )
392407
393- group = grouped .tab_widget ()
394- groupTabBar = group .tabBar ()
408+ group_tab_widget = grouped_tab_widget .tab_widget ()
409+ groupTabBar = group_tab_widget .tabBar ()
395410 idx = - 1
396- for idx in range (group .count ()):
397- if group .widget (idx ) == grouped :
411+ for idx in range (group_tab_widget .count ()):
412+ if group_tab_widget .widget (idx ) == grouped_tab_widget :
398413 break
399414 group_name = groupTabBar .tabText (idx )
400415
@@ -503,19 +518,36 @@ def __marker_add__(self, line):
503518 def __marker_clear_all__ (self ):
504519 raise NotImplementedError ("Mixin method not overridden." )
505520
506- def __reload_file__ (self ):
521+ def __set_workbox_title__ (self , title ):
522+ """Set the tab-text on the grouped widget tab for this workbox.
523+
524+ Args:
525+ title (str): The text to put on the grouped tab's tabText.
526+ """
527+ _group_idx , editor_idx = self .__group_tab_index__ ()
528+
529+ tab_widget = self .__tab_widget__ ()
530+ if tab_widget is not None :
531+ tab_widget .tabBar ().setTabText (editor_idx , title )
532+
533+ def __maybe_reload_file__ (self ):
507534 """Reload this workbox's linked file."""
508535 # Loading the file too quickly misses any changes
509536 time .sleep (0.1 )
510537 font = self .__font__ ()
511538
512- choice = self .__show_reload_dialog__ ()
539+ choice = self .__linked_file_changed__ ()
513540 if choice is True :
541+ # First save unsaved changes, so user can get it from a previous
542+ # version is desired.
543+ self .__save_prefs__ (saveLinkedFile = False , resetLastInfos = False )
544+
545+ # Load the file
514546 self .__load__ (self .__filename__ ())
515547
516- self .__set_last_saved_text__ (self .__text__ ())
517- self .__set_last_workbox_name__ (self .__workbox_name__ ())
548+ # Reset the font
518549 self .__set_font__ (font )
550+ return choice
519551
520552 def __single_messagebox__ (self , title , message ):
521553 """Display a messagebox, but only once, in case this is triggered by a
@@ -529,42 +561,17 @@ def __single_messagebox__(self, title, message):
529561 Returns:
530562 choice (bool): Whether the user accepted the dialog or not.
531563 """
532- choice = False
533- buttons = QMessageBox .StandardButton .Yes | QMessageBox .StandardButton .No
534564
535- if not self ._dialogShown :
536- self ._dialogShown = True
537- result = QMessageBox .question (self .window (), title , message , buttons )
538- choice = result == QMessageBox .StandardButton .Yes
539- self ._dialogShown = False
540- return choice
565+ tup = (title , message )
566+ if tup in self .shownDialogs :
567+ return None
568+ self .shownDialogs .append (tup )
541569
542- def __show_reload_dialog__ (self ):
543- """Show a messagebox asking if user wants to reload the linked-file,
544- which has changed externally.
545-
546- Returns:
547- choice (bool): Whether user chose to reload the link file (or
548- auto-reload setting is True)
549- """
550- choice = None
551- if self .__auto_reload_on_change__ ():
552- choice = True
553- else :
554- name = Path (self .__filename__ ()).name
555- title = 'Reload File...'
556- msg = f'Are you sure you want to reload { name } ?'
557- choice = self .__single_messagebox__ (title , msg )
558- return choice
559-
560- def __set_workbox_title__ (self , title ):
561- """Set the tab-text on the grouped widget tab for this workbox.
570+ buttons = QMessageBox .StandardButton .Yes | QMessageBox .StandardButton .No
571+ result = QMessageBox .question (self .window (), title , message , buttons )
572+ self .shownDialogs .remove (tup )
562573
563- Args:
564- title (str): The text to put on the grouped tab's tabText.
565- """
566- _group_idx , editor_idx = self .__group_tab_index__ ()
567- self .__tab_widget__ ().tabBar ().setTabText (editor_idx , title )
574+ return result == QMessageBox .StandardButton .Yes
568575
569576 def __linked_file_changed__ (self ):
570577 """If a file was modified or deleted this method
@@ -588,25 +595,28 @@ def __linked_file_changed__(self):
588595 'The file was deleted, removing document from editor' ,
589596 )
590597 group_idx , editor_idx = self .__group_tab_index__ ()
591- self .__tab_widget__ ().close_tab (editor_idx )
598+
599+ tab_widget = self .__tab_widget__ ()
600+ if tab_widget is not None :
601+ tab_widget .close_tab (editor_idx )
602+
592603 return False
593604 elif choice :
594605 self .__set_filename__ ("" )
595- title = self .__tab_widget__ ().get_next_available_tab_name ()
596- self .__set_workbox_title__ (title )
597606
598- # TODO: The file no longer exists, and the document should be marked as
599- # changed.
607+ tab_widget = self .__tab_widget__ ()
608+ if tab_widget is not None :
609+ title = tab_widget .get_next_available_tab_name ()
610+ self .__set_workbox_title__ (title )
600611
601- if self .autoReloadOnChange () or not self .isModified ():
612+ if self .__auto_reload_on_change__ () or not self .__is_dirty__ ():
602613 choice = True
603614 else :
604615 title = 'Reload File...'
605616 msg = f'File: { filename } has been changed.\n Reload from disk?'
606617 choice = self .__single_messagebox__ (title , msg )
607618
608- if choice is True :
609- self .__load__ (self .__filename__ ())
619+ return choice
610620
611621 def __remove_selected_text__ (self ):
612622 raise NotImplementedError ("Mixin method not overridden." )
@@ -769,7 +779,13 @@ def __unix_end_lines__(cls, txt):
769779 """Replaces all windows and then mac line endings with unix line endings."""
770780 return txt .replace ('\r \n ' , '\n ' ).replace ('\r ' , '\n ' )
771781
772- def __save_prefs__ (self , current = None , saveLinkedFile = True , force = False ):
782+ def __save_prefs__ (
783+ self ,
784+ current = None ,
785+ force = False ,
786+ saveLinkedFile = True ,
787+ resetLastInfos = True ,
788+ ):
773789 ret = {}
774790
775791 # Hopefully the alphabetical sorting of this dict is preserved in py3
@@ -829,8 +845,9 @@ def __save_prefs__(self, current=None, saveLinkedFile=True, force=False):
829845 self .__save__ ()
830846 ret ['workbox_id' ] = workbox_id
831847
832- self .__set_last_workbox_name__ (self .__workbox_name__ ())
833- self .__set_last_saved_text__ (self .__text__ ())
848+ if resetLastInfos :
849+ self .__set_last_workbox_name__ (self .__workbox_name__ ())
850+ self .__set_last_saved_text__ (self .__text__ ())
834851
835852 return ret
836853
@@ -985,7 +1002,10 @@ def __load_workbox_version_text__(self, versionType):
9851002 self ._backup_file = str (filepath )
9861003
9871004 self .__set_text__ (txt )
988- self .__tab_widget__ ().tabBar ().update ()
1005+
1006+ tab_widget = self .__tab_widget__ ()
1007+ if tab_widget is not None :
1008+ tab_widget .tabBar ().update ()
9891009
9901010 filename = Path (filepath ).name
9911011 return filename , idx , count
0 commit comments