Open previous tabs/files after restart
This commit is contained in:
parent
0bc39ada3d
commit
b13a1e3eb7
@ -102,9 +102,11 @@ class AppConfigurator:
|
||||
# Save the data in the configuration dictionary.
|
||||
self.save_configuration_data()
|
||||
|
||||
# Try to find the current configuration for opening previous files.
|
||||
try:
|
||||
self.configuration_dictionary["open_previous_files"]
|
||||
|
||||
# If the configuration does not exist, set the configuration to True and save the data.
|
||||
except KeyError:
|
||||
self.configuration_dictionary["open_previous_files"] = True
|
||||
self.save_configuration_data()
|
||||
|
@ -7,7 +7,13 @@ from pygadmin.configurator import global_app_configurator
|
||||
|
||||
|
||||
class FileManager:
|
||||
""""
|
||||
Create a class for administrating the open files in the editor tabs. This class saves the paths of the open tabs in
|
||||
a yaml file.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
# Define a container for the open files.
|
||||
self.open_files = []
|
||||
# Define a path for the configuration files.
|
||||
configuration_path = os.path.join(os.path.expanduser("~"), '.pygadmin')
|
||||
@ -25,40 +31,71 @@ class FileManager:
|
||||
# Create the file as an empty file for writing in it.
|
||||
open(self.open_files_file, "a")
|
||||
|
||||
# If the current configuration is False, delete all current files, because a storage is not necessary.
|
||||
if global_app_configurator.get_single_configuration("open_previous_files") is False:
|
||||
self.delete_all_files()
|
||||
self.commit_current_files_to_yaml()
|
||||
|
||||
def add_new_file(self, file_name):
|
||||
"""
|
||||
Add a new file with its name/path to the list of open files.
|
||||
"""
|
||||
|
||||
self.open_files.append(file_name)
|
||||
|
||||
return True
|
||||
|
||||
def delete_file(self, file_name):
|
||||
"""
|
||||
Delete a file with its name/path.
|
||||
"""
|
||||
|
||||
# Check for the existence of the file.
|
||||
if file_name not in self.open_files:
|
||||
# If the file does not exist, return False for a failure.
|
||||
return False
|
||||
|
||||
# Remove the file name.
|
||||
self.open_files.remove(file_name)
|
||||
|
||||
# Return True for a success.
|
||||
return True
|
||||
|
||||
def delete_all_files(self):
|
||||
"""
|
||||
Delete all current open files with overwriting the current content of the open files list with an empty list.
|
||||
"""
|
||||
|
||||
self.open_files = []
|
||||
|
||||
def commit_current_files_to_yaml(self):
|
||||
"""
|
||||
Try to save the current list in the yaml file.
|
||||
"""
|
||||
|
||||
try:
|
||||
# Try to open the file in writing mode.
|
||||
with open(self.open_files_file, "w") as file_data:
|
||||
# Use a safe dump, because the file can be manually edited by a user.
|
||||
yaml.safe_dump(self.open_files, file_data)
|
||||
|
||||
# Return True for a success.
|
||||
return True
|
||||
|
||||
# Use the exception during the writing process for an error message.
|
||||
except Exception as file_error:
|
||||
logging.error("The file {} cannot be opened and the open files in the editor cannot be saved with the "
|
||||
"following error: {}".format(self.open_files_file, file_error), exc_info=True)
|
||||
|
||||
# Return False for a failure.
|
||||
return False
|
||||
|
||||
def load_open_file_list(self):
|
||||
"""
|
||||
Load the content of the open files out of the yaml file. Use a try statement for a potential failure of the file
|
||||
reading process.
|
||||
"""
|
||||
|
||||
# Use a try statement in case of a broken .yaml file.
|
||||
try:
|
||||
# Use the read mode for getting the content of the file.
|
||||
|
@ -394,9 +394,12 @@ class EditorWidget(QWidget, SearchReplaceParent, metaclass=MetaEditor):
|
||||
"""
|
||||
Save the current text/statement of the lexer as query editor in for further usage. The class-wide variable for
|
||||
the corresponding file is used as directory with file. If this variable contains its initialized value None,
|
||||
use the function for opening a file dialog.
|
||||
use the function for opening a file dialog. A default parameter is used for the previous file name. In this case
|
||||
the previous corresponding saved file is used. If a file name is given, the given one is used.
|
||||
"""
|
||||
|
||||
# Check for the configuration and the previous file name: if the previous file name is None, get the name of the
|
||||
# current corresponding saved file, because this one is going to be the previous one.
|
||||
if previous_file_name is None and global_app_configurator.get_single_configuration("open_previous_files") is\
|
||||
True:
|
||||
previous_file_name = self.corresponding_saved_file
|
||||
@ -416,21 +419,31 @@ class EditorWidget(QWidget, SearchReplaceParent, metaclass=MetaEditor):
|
||||
# Write the current text of the lexer as SQL editor in the file.
|
||||
file_to_save.write(current_text)
|
||||
|
||||
# Parse the error for writing in the file.
|
||||
except Exception as file_error:
|
||||
# Define an error text.
|
||||
error_text = "The file {} cannot be written with the error: {}".format(self.corresponding_saved_file,
|
||||
file_error)
|
||||
|
||||
# Show the error to the user and save it in the log.
|
||||
QMessageBox.critical(self, "File Reading Error", error_text)
|
||||
logging.critical(error_text, exc_info=True)
|
||||
|
||||
# Redefine the corresponding saved file back to the previous one.
|
||||
self.corresponding_saved_file = previous_file_name
|
||||
|
||||
# End the function after a failure, because the following part is not necessary.
|
||||
return
|
||||
|
||||
# If the corresponding file name is not the previous file and the configuration for opening the previous files
|
||||
# is True, the old file will be deleted as previous file and the new one added.
|
||||
if self.corresponding_saved_file != previous_file_name \
|
||||
and global_app_configurator.get_single_configuration("open_previous_files") is True:
|
||||
# Delete the previous one.
|
||||
global_file_manager.delete_file(previous_file_name)
|
||||
# Add the new one.
|
||||
global_file_manager.add_new_file(self.corresponding_saved_file)
|
||||
# Commit/save the change.
|
||||
global_file_manager.commit_current_files_to_yaml()
|
||||
|
||||
# Save the current text in the class-wide current editor text.
|
||||
@ -480,30 +493,42 @@ class EditorWidget(QWidget, SearchReplaceParent, metaclass=MetaEditor):
|
||||
# Get the file name out of the tuple.
|
||||
file_name = file_name_and_type[0]
|
||||
|
||||
# If the file name is false, the process has been aborted.
|
||||
if file_name is False:
|
||||
logging.info("The current file opening process was aborted by the user, so the content of this file is not "
|
||||
"loaded.")
|
||||
|
||||
# End the function with a return.
|
||||
return False
|
||||
|
||||
# Try to load the statement based on the file name.
|
||||
return self.load_statement_with_file_name(file_name)
|
||||
|
||||
def load_statement_with_file_name(self, file_name):
|
||||
"""
|
||||
Load the content of the file with its given name and path.
|
||||
"""
|
||||
|
||||
# Check for the success in form of an existing file and not an empty string.
|
||||
if file_name != "":
|
||||
# Try to open the file. This operation can fail without the correct file rights.
|
||||
try:
|
||||
# Open the file in reading mode.
|
||||
with open(file_name, "r") as file_to_load:
|
||||
# Read the whole given file and save its text.
|
||||
file_text = file_to_load.read()
|
||||
|
||||
# Show an error to the user, if the file reading process has failed.
|
||||
except Exception as file_error:
|
||||
error_text = "The file {} cannot be loaded with the error: {}".format(file_name, file_error)
|
||||
QMessageBox.critical(self, "File Reading Error", error_text)
|
||||
logging.critical(error_text, exc_info=True)
|
||||
|
||||
# Return False for the error.
|
||||
return False
|
||||
|
||||
# If the option for open the previous files is activated and if the current corresponding save file is not
|
||||
# None, delete it in the file manager, because it will be replaced by a new one.
|
||||
if global_app_configurator.get_single_configuration("open_previous_files") is True and \
|
||||
self.corresponding_saved_file is not None:
|
||||
global_file_manager.delete_file(self.corresponding_saved_file)
|
||||
@ -511,6 +536,8 @@ class EditorWidget(QWidget, SearchReplaceParent, metaclass=MetaEditor):
|
||||
# Save the name of the file in the class variable for the corresponding file.
|
||||
self.corresponding_saved_file = file_name
|
||||
|
||||
# If the configuration is set, save the new corresponding save file and commit the changes to the file
|
||||
# manager.
|
||||
if global_app_configurator.get_single_configuration("open_previous_files") is True:
|
||||
global_file_manager.add_new_file(self.corresponding_saved_file)
|
||||
global_file_manager.commit_current_files_to_yaml()
|
||||
@ -1034,8 +1061,13 @@ class EditorWidget(QWidget, SearchReplaceParent, metaclass=MetaEditor):
|
||||
file_to_save.write("\n")
|
||||
|
||||
def closeEvent(self, a0: QtGui.QCloseEvent) -> None:
|
||||
"""
|
||||
Overwrite the close event: If the configuration for opening the previous is set and the corresponding save file
|
||||
exists, delete it from the file manager, because it does not have to be opened after a restart of the program.
|
||||
"""
|
||||
|
||||
if self.corresponding_saved_file is not None \
|
||||
and global_app_configurator.get_single_configuration("open_previous_files"):
|
||||
and global_app_configurator.get_single_configuration("open_previous_files") is True:
|
||||
global_file_manager.delete_file(self.corresponding_saved_file)
|
||||
global_file_manager.commit_current_files_to_yaml()
|
||||
|
||||
|
@ -360,11 +360,14 @@ class MainWindow(QMainWindow):
|
||||
|
||||
# Check, if the current editor widget exists.
|
||||
if current_editor_widget is not None:
|
||||
if global_app_configurator.get_single_configuration("open_previous_files"):
|
||||
# Check for the configuration settings, because getting the corresponding saved file is only necessary for
|
||||
# the step for opening previous files.
|
||||
if global_app_configurator.get_single_configuration("open_previous_files") is True:
|
||||
# Get the current corresponding file name for the usage as previous file name, so an overwrite in the
|
||||
# editor for the global file manager can be realized.
|
||||
current_corresponding_file = current_editor_widget.corresponding_saved_file
|
||||
|
||||
# The corresponding file is unnecessary, if the configuration is not True.
|
||||
else:
|
||||
current_corresponding_file = None
|
||||
|
||||
|
@ -52,8 +52,11 @@ class MdiArea(QMdiArea):
|
||||
def init_open_files(self):
|
||||
"""
|
||||
Get the list of previous opened files of the editor out of the file manager and load them in new editor widgets.
|
||||
This procedure will be executed, if the configuration for opening the previous files is True.
|
||||
"""
|
||||
|
||||
# If the configuration is False, end the function with a return, so an initialization of the previous opened
|
||||
# files is not executed.
|
||||
if global_app_configurator.get_single_configuration("open_previous_files") is False:
|
||||
return
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user