diff --git a/src/quick_map_services/i18n/QuickMapServices_ru.ts b/src/quick_map_services/i18n/QuickMapServices_ru.ts index 1db05c3..eedc6cb 100644 --- a/src/quick_map_services/i18n/QuickMapServices_ru.ts +++ b/src/quick_map_services/i18n/QuickMapServices_ru.ts @@ -636,6 +636,16 @@ p, li { white-space: pre-wrap; } Add Добавить + + + Service not found + Сервис не найден + + + + The service does not exist anymore. It will be removed from the recent list. + Сервис больше не существует. Он будет удалён из списка недавно использованных. + QmsServiceToolbox diff --git a/src/quick_map_services/qms_service_toolbox.py b/src/quick_map_services/qms_service_toolbox.py index f16aae8..64435e7 100644 --- a/src/quick_map_services/qms_service_toolbox.py +++ b/src/quick_map_services/qms_service_toolbox.py @@ -22,6 +22,7 @@ QThread, QTimer, pyqtSignal, + pyqtSlot, ) from qgis.PyQt.QtGui import ( QCursor, @@ -35,6 +36,7 @@ QHBoxLayout, QLabel, QListWidgetItem, + QMessageBox, QSizePolicy, QToolButton, QWidget, @@ -107,6 +109,23 @@ def add_service(self, geoservice, image_ba): self.geoservices = geoservices4store[0:5] PluginSettings.set_last_used_services(self.geoservices) + def remove_service(self, service_id: int) -> None: + """ + Remove a cached geoservice by its ID. + + :param service_id: Unique identifier of the geoservice to remove. + :type service_id: int + + :return: None + :rtype: None + """ + self.geoservices = [ + geoservice + for geoservice in self.geoservices + if geoservice.id != service_id + ] + PluginSettings.set_last_used_services(self.geoservices) + def get_cached_services(self): return [ (geoservice.attributes, geoservice.image_qByteArray) @@ -234,6 +253,18 @@ def stop_search_thread(self): self.search_threads.wait() self.search_threads = None + @pyqtSlot() + def refresh_last_used_services(self): + """ + Refresh the list of last used geoservices. + + This method clears the current search result list and adds the + last used geoservices again. It is intended to be called after + a geoservice is removed from the recent list. + """ + self.lstSearchResult.clear() + self.add_last_used_services() + def start_search(self): search_text = None geom_filter = None @@ -294,7 +325,13 @@ def start_search(self): self.search_threads = searcher searcher.start() - def add_last_used_services(self): + def add_last_used_services(self) -> None: + """ + Populate the search result list with recently used geoservices. + + :return: None + :rtype: None + """ services = CachedServices().get_cached_services() if len(services) == 0: return @@ -308,6 +345,9 @@ def add_last_used_services(self): custom_widget = QmsSearchResultItemWidget( attributes, image_qByteArray ) + custom_widget.remove_cached_service_item.connect( + self._remove_cached_service + ) new_item = QListWidgetItem(self.lstSearchResult) new_item.setSizeHint(custom_widget.sizeHint()) self.lstSearchResult.addItem(new_item) @@ -375,8 +415,24 @@ def show_error(self, error_text): self.lstSearchResult.addItem(new_item) self.lstSearchResult.setItemWidget(new_item, new_widget) + @pyqtSlot(int) + def _remove_cached_service(self, service_id: int) -> None: + """ + Slot that handles the removal of a cached geoservice. + + :param service_id: Unique identifier of the geoservice to remove. + :type service_id: int + + :return: None + :rtype: None + """ + CachedServices().remove_service(service_id) + self.refresh_last_used_services() + class QmsSearchResultItemWidget(QWidget): + remove_cached_service_item = pyqtSignal(int) + def __init__( self, geoservice, image_ba, parent=None, extent_renderer=None ): @@ -467,12 +523,29 @@ def __init__( self.geoservice = geoservice self.image_ba = image_ba - def addToMap(self): + @pyqtSlot() + def addToMap(self) -> None: + """ + Try to add the selected geoservice to the map. + """ try: QApplication.setOverrideCursor(QCursor(Qt.CursorShape.WaitCursor)) client = Client() client.set_proxy(*QGISSettings.get_qgis_proxy()) - geoservice_info = client.get_geoservice_info(self.geoservice) + try: + geoservice_info = client.get_geoservice_info(self.geoservice) + except Exception as error: + # Show a warning if the service is not found + QMessageBox.warning( + self, + self.tr("Service not found"), + self.tr( + "The service does not exist anymore." + " It will be removed from the recent list." + ), + ) + self.remove_cached_service_item.emit(self.geoservice.get("id")) + return ds = DataSourceSerializer.read_from_json(geoservice_info) add_layer_to_map(ds)