Skip to content

Commit

Permalink
Ask whether to link file to project if it's not
Browse files Browse the repository at this point in the history
  • Loading branch information
octimot committed Feb 3, 2024
1 parent 3de3fa3 commit b4b2065
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
2 changes: 1 addition & 1 deletion storytoolkitai/core/toolkit_ops/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ def is_linked_to_project(self, object_type, file_path):
return file_path in self._documents

else:
logger.debug('Cannot unlink file {} from project {} - unknown object type "{}".'
logger.debug('Cannot find link for file {} to project {} - unknown object type "{}".'
.format(file_path, self._project_path, object_type))
return False

Expand Down
71 changes: 70 additions & 1 deletion storytoolkitai/ui/toolkit_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -3434,6 +3434,25 @@ def update_project_last_target_dir(project, dir_path):

project.set('last_target_dir', dir_path, save_soon=True)

def ask_to_link_unlinked(self, project, object_type, file_path, parent_window=None):

if not project or not isinstance(project, Project):
return

if not project.is_linked_to_project(object_type, file_path):

if messagebox.askyesno(
'Add to project',
message='This {} is not linked to the current project.\n\n'
'Would you like to link it now?'.format(object_type),
parent=parent_window
):
project.link_to_project(
object_type=object_type, file_path=file_path, save_soon=True
)

self.update_main_window()

def _project_context_menu(self, event, project_name):

main_window = self.windows['main']
Expand Down Expand Up @@ -4437,7 +4456,23 @@ def open_text_file(self, file_path: str = None, window_id: str = None, tag_text=

# now open the text window if it doesn't exist
if not self.get_window_by_id(window_id):
self.open_text_window(initial_text=file_content, window_id=window_id, can_find=True, **kwargs)

# open the window
window_id = self.open_text_window(initial_text=file_content, window_id=window_id, can_find=True, **kwargs)

# get the window object
window = self.get_window_by_id(window_id)

# find out if this is linked to the project and if we should
window.after(
500,
lambda: self.ask_to_link_unlinked(
project=self.current_project,
object_type='document',
file_path=file_path,
parent_window=window
)
)

def tag_passed_text(search_text, start_index, end_index):

Expand Down Expand Up @@ -12091,6 +12126,13 @@ def open_new_transcription_window(self, transcription_segments=None, transcripti

transcription.save_soon(backup=False, force=True, sec=0)

# also add the transcription to the project if we're in one
if self.current_project:
self.current_project.link_to_project(
object_type='transcription', file_path=transcription_file_path, save_soon=True
)
self.update_main_window()

time.sleep(0.2)

# open the story editor window and return it
Expand Down Expand Up @@ -12559,6 +12601,17 @@ def open_transcription_window(self, title=None, transcription_file_path=None,
# and attach it to the window
t_window.transcript_groups_module = transcript_groups_module

# find out if this is linked to the project and ask if we should
t_window.after(
500,
lambda: self.ask_to_link_unlinked(
project=self.current_project,
object_type='transcription',
file_path=transcription_file_path,
parent_window=t_window
)
)

# if the transcription window already exists
else:

Expand Down Expand Up @@ -15903,6 +15956,11 @@ def open_new_story_editor_window(self):
story.set('name', os.path.basename(story_file_path).split('.')[0])
story.save_soon(backup=False, force=True, sec=0)

# also add the story to the project if we're in one
if self.current_project:
self.current_project.link_to_project(object_type='story', file_path=story_file_path, save_soon=True)
self.update_main_window()

time.sleep(0.1)

# open the story editor window and return it
Expand Down Expand Up @@ -16102,6 +16160,17 @@ def open_story_editor_window(self, title=None, story_file_path=None):
'<Button-2>', lambda e: self.StoryEdit.story_editor_context_menu(
e, window_id=window_id, toolkit_UI_obj=self))

# find out if this is linked to the project and if we should
window.after(
500,
lambda: self.ask_to_link_unlinked(
project=self.current_project,
object_type='story',
file_path=story_file_path,
parent_window=window
)
)

return window

def destroy_story_editor_window(self, window_id):
Expand Down

0 comments on commit b4b2065

Please sign in to comment.