Skip to content

Commit

Permalink
#258 Improve SettingsManager quicksearches handling
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSAMPERE committed Mar 28, 2024
1 parent 3835c00 commit 40faedf
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 106 deletions.
17 changes: 7 additions & 10 deletions isogeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,7 @@ def search(self, show: bool = False, page_change: int = 0):
name = self.tr("Last search")
self.form_mng.qs_mng.write_params(name, "Last")
# update quick searches combobox
saved_searches = list(self.form_mng.qs_mng.load_file())
self.form_mng.pop_qs_cbbs(items_list=saved_searches)
self.form_mng.pop_qs_cbbs(items_list=self.form_mng.qs_mng.get_quicksearches_names())
self.store = False
else:
pass
Expand Down Expand Up @@ -471,7 +470,7 @@ def search(self, show: bool = False, page_change: int = 0):

def search_slot(self, result: dict, tags: dict):
"""Slot connected to ApiRequester.search_sig signal. It updates widgets, using
SearchFormManager appropiate methods to fill them from 'tags' parameter and put
SearchFormManager appropriate methods to fill them from 'tags' parameter and put
them in the right status. It also display the results contained in 'result'
parameter by calling ResultManager.show_results method if necessary.
Expand Down Expand Up @@ -511,8 +510,7 @@ def search_slot(self, result: dict, tags: dict):
# Filling Advanced search comboboxes from tags
self.form_mng.pop_as_cbbs(tags)
# Filling quick searches comboboxes from json file (also the one in settings tab)
qs_list = list(self.form_mng.qs_mng.load_file().keys())
self.form_mng.pop_qs_cbbs(qs_list)
self.form_mng.pop_qs_cbbs(self.form_mng.qs_mng.get_quicksearches_names())
# Sorting Advanced search comboboxes
for cbb in self.cbbs_search_advanced:
cbb.model().sort(0)
Expand All @@ -535,7 +533,7 @@ def search_slot(self, result: dict, tags: dict):
# Putting all the comboboxes selected index according to params found in the json file
logger.debug("Quicksearch case: {}".format(self.savedSearch))
# Opening the json to get quick search's params
params = self.form_mng.qs_mng.load_file().get(self.savedSearch)
params = self.form_mng.qs_mng.get_quicksearches().get(self.savedSearch)
quicksearch = self.savedSearch
self.savedSearch = ""
selected_keywords = [v for k, v in params.items() if k.startswith("keyword")]
Expand Down Expand Up @@ -584,7 +582,7 @@ def search_slot(self, result: dict, tags: dict):
# Re enable all user input fields now the search function is
# finished.
self.form_mng.switch_widgets_on_and_off(1)
# Reseting attributes values
# Resetting attributes values
self.hardReset = False
self.showResult = False

Expand All @@ -593,16 +591,15 @@ def set_widget_status(self):
selected_search = self.form_mng.cbb_quicksearch_use.currentText()
logger.debug("Quicksearch selected: {}".format(selected_search))
# load quicksearches
saved_searches = self.form_mng.qs_mng.load_file()
saved_searches = self.form_mng.qs_mng.get_quicksearches()
if selected_search != self.tr("Quicksearches"):
self.form_mng.switch_widgets_on_and_off(0) # disable search form
# check if selected search can be found
if selected_search in saved_searches:
self.savedSearch = selected_search
search_params = saved_searches.get(selected_search)
logger.debug(
"Quicksearch found in saved searches and"
" related search params have just been loaded from."
"Quicksearch found in saved searches and related search params have just been loaded from it."
)
elif selected_search not in saved_searches and "_default" in saved_searches:
logger.warning("Selected search ({}) not found." "'_default' will be used instead.")
Expand Down
76 changes: 32 additions & 44 deletions modules/quick_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def api_base_url_setter(self, value: str):
def customize_settings_manager(self):
settings_mng.api_base_url = self.api_base_url
settings_mng.tr = self.tr
settings_mng.load_quicksearches()
logger.debug("SettingsManager initialized for quicksearches handling.")

def fetch_params(self):
# Write the current parameters in a dict
Expand All @@ -103,28 +105,26 @@ def fetch_params(self):
def write_params(self, search_name: str = "_default", search_kind: str = "Default"):
"""Write a new element in the json file when a search is saved."""

saved_searches = settings_mng.load_quicksearch()
# If the name already exists, ask for a new one. ================ TO DO
if search_kind == "Last":
params = saved_searches.get(
"_current", "{}/resources/search?&_limit=0".format(self.api_base_url)
)
if search_name == "Last search" and "Dernière recherche" in saved_searches:
del saved_searches["Dernière recherche"]
elif search_name == "Dernière recherche" and "Last search" in saved_searches:
del saved_searches["Last search"]
if search_name == "Last search" and "Dernière recherche" in self.get_quicksearches_names():
settings_mng.remove_quicksearch("Dernière recherche")
elif search_name == "Dernière recherche" and "Last search" in self.get_quicksearches_names():
settings_mng.remove_quicksearch("Last search")
else:
pass
# writing file
settings_mng.save_quicksearch(
search_name,
settings_mng.quicksearches_content.get("_current", "{}/resources/search?&_limit=0".format(self.api_base_url))
)
else:
params = self.fetch_params()

saved_searches[search_name] = params
# writing file
settings_mng.update_quicksearch(saved_searches)
# writing file
settings_mng.save_quicksearch(search_name, self.fetch_params())

# Log and messages
logger.info(
"{} search stored as '{}'. Parameters: {}".format(search_kind, search_name, params)
"{} search stored as '{}'.".format(search_kind, search_name)
)
if search_kind != "Current" and search_kind != "Last":
msgBar.pushMessage(
Expand All @@ -139,7 +139,6 @@ def write_params(self, search_name: str = "_default", search_kind: str = "Defaul

def check_already_exist(self, rename: bool = 0):

saved_searches = settings_mng.load_quicksearch()
if rename:
search_name = self.dlg_rename.txt_quicksearch_rename.text()
popup_title = self.tr("Isogeo - Rename quicksearch", __class__.__name__,)
Expand All @@ -149,7 +148,7 @@ def check_already_exist(self, rename: bool = 0):
popup_title = self.tr("Isogeo - New quicksearch", __class__.__name__,)
slot_func = self.save

if search_name in saved_searches:
if search_name in self.get_quicksearches_names():
popup = QMessageBox()
popup.setWindowIcon(ico_bolt)
popup.setWindowTitle(popup_title)
Expand All @@ -175,11 +174,8 @@ def save(self, i):
search_name = self.dlg_new.txt_quicksearch_name.text()
self.dlg_new.txt_quicksearch_name.setText("")
self.write_params(search_name, search_kind="Quicksearch")
# load all saved quicksearches and populate drop-down (combobox)
saved_searches = settings_mng.load_quicksearch()
search_list = list(saved_searches.keys())
# updating quick search widgets
self.form_mng.pop_qs_cbbs(items_list=search_list)
self.form_mng.pop_qs_cbbs(items_list=self.get_quicksearches_names())
# method ending
return
else:
Expand All @@ -193,14 +189,9 @@ def rename(self, i):
new_name = self.dlg_rename.txt_quicksearch_rename.text()
self.dlg_rename.txt_quicksearch_rename.setText("")

saved_searches = settings_mng.load_quicksearch()
saved_searches[new_name] = saved_searches[old_name]
saved_searches.pop(old_name)
search_list = list(saved_searches.keys())
settings_mng.rename_quicksearch(old_name, new_name)
# Update quick search widgets
self.form_mng.pop_qs_cbbs(items_list=search_list)
# Update JSON file
settings_mng.update_quicksearch(saved_searches)
self.form_mng.pop_qs_cbbs(items_list=self.get_quicksearches_names())

# inform user
msgBar.pushMessage(
Expand All @@ -221,13 +212,9 @@ def remove(self):
"""Modify the json file in order to delete a search."""

to_remove = self.form_mng.cbb_quicksearch_edit.currentText()
saved_searches = settings_mng.load_quicksearch()
saved_searches.pop(to_remove)
search_list = list(saved_searches.keys())
settings_mng.remove_quicksearch(to_remove)
# Update quick search widgets
self.form_mng.pop_qs_cbbs(items_list=search_list)
# Update JSON file
settings_mng.update_quicksearch(saved_searches)
self.form_mng.pop_qs_cbbs(items_list=self.get_quicksearches_names())

# inform user
msgBar.pushMessage(
Expand All @@ -243,13 +230,10 @@ def remove(self):
def reset_default_search(self):

search_name = "_default"
# fetch current JSON file content
saved_searches = settings_mng.load_quicksearch()
# fetch default search default params
params = settings_mng.get_default_quicksearch_content().get(search_name)
# update JSON file content
saved_searches[search_name] = params
settings_mng.update_quicksearch(saved_searches)

settings_mng.save_quicksearch(
search_name, settings_mng.get_default_quicksearch_content().get(search_name)
)

# Log and messages
logger.info("Default search successfully reset.")
Expand All @@ -258,14 +242,18 @@ def reset_default_search(self):
duration=3,
)

def load_file(self):
def get_quicksearches(self):

return settings_mng.quicksearches_content

def get_quicksearches_names(self):

saved_searches = settings_mng.load_quicksearch()
saved_searches_names = list(self.get_quicksearches().keys())

logger.debug(
"{} quicksearch(es) found : {}".format(
len(saved_searches), list(saved_searches.keys())
len(saved_searches_names), saved_searches_names
)
)

return saved_searches
return saved_searches_names
4 changes: 2 additions & 2 deletions modules/search_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def pop_qs_cbbs(self, items_list: list = None):
logger.debug("Filling quick searches comboboxes")
# building the list of widgets' items'content
if items_list is None:
qs_list = list(self.qs_mng.load_file().keys())
qs_list = self.qs_mng.get_quicksearches_names()
else:
qs_list = items_list
qs_list.pop(qs_list.index("_default"))
Expand Down Expand Up @@ -521,7 +521,7 @@ def get_coords(self, filter: str):
"""Get the extent's coordinates of a layer or canvas in the right format
and SRS (WGS84).
:param str filter: the name of the element wich we want to get extent's
:param str filter: the name of the element which we want to get extent's
coordinates.
:returns: the x and y coordinates of the canvas' Southwestern and
Expand Down
Loading

0 comments on commit 40faedf

Please sign in to comment.