def init_nominatim_autofill(self): """Open the nominatim file and start setting up the auto-completion.""" # Useful to avoid duplicate if we add a new completer. for line_edit in self.dialog.places_edits.values(): line_edit.setCompleter(None) user_file = nominatim_file() with io.open(user_file, 'r', encoding='utf8') as f: for line in f: self.last_places.append(line.rstrip('\n')) nominatim_completer = QCompleter(self.last_places) for line_edit in self.dialog.places_edits.values(): line_edit.setCompleter(nominatim_completer) line_edit.completer().setCompletionMode( QCompleter.PopupCompletion)
def write_nominatim_file(self, panel: Panels): """Write new nominatim value in the file. :param panel: The panel to use so as to fetch the nominatim value. :type panel: Panels """ value = self.dialog.places_edits[panel].text() new_list = self.sort_nominatim_places(self.last_places, value) user_file = nominatim_file() try: with io.open(user_file, 'w', encoding='utf8') as file: for item in new_list: if item: file.write('{}\n'.format(item)) except UnicodeDecodeError: # The file is corrupted ? # Remove all old places with io.open(user_file, 'w', encoding='utf8') as file: file.write('\n') self.init_nominatim_autofill()
def nominatim_value(self, panel): """Edit the new nominatim file from a given panel. :param panel: Name of the panel. :type panel: Panels """ value = self.places_edits[panel].text() new_list = self.sort_nominatim_places(self.last_places, value) user_file = nominatim_file() try: with io.open(user_file, 'w', encoding='utf8') as f: for item in new_list: if item: f.write('{}\n'.format(item)) except UnicodeDecodeError: # The file is corrupted ? # Remove all old places with io.open(user_file, 'w', encoding='utf8') as f: f.write('\n') self.init_nominatim_autofill() return value