class AnnotationManager: def __init__(self, iface): locale = QSettings().value('locale/userLocale')[0:2] locale_path = os.path.join(os.path.dirname(__file__), 'i18n', 'annotationManager_{}.qm'.format(locale)) self.translator = None if os.path.exists(locale_path): self.translator = QTranslator() self.translator.load(locale_path) if qVersion() > '4.3.3': QCoreApplication.installTranslator(self.translator) self.iface = iface self.iface.projectRead.connect(self.projectOpen) self.dock = QDockWidget(self.tr('Annotations')) self.manager = QWidget() toolbar = QToolBar() self.annotationList = QListWidget() self.annotationList.setSelectionMode( QAbstractItemView.ExtendedSelection) self.annotationList.itemSelectionChanged.connect(self.selectAnnotation) self.annotationList.itemChanged.connect(self.checkItem) action_refresh = QAction( QIcon(':/plugins/annotationManager/resources/mActionDraw.png'), self.tr('Refresh the annotations list'), self.manager) action_refresh.triggered.connect(self.refreshAnnotations) action_remove = QAction( QIcon( ':/plugins/annotationManager/resources/mActionRemoveAnnotation.png' ), self.tr('Remove the selected annotation'), self.manager) action_remove.triggered.connect(self.removeAnnotation) viewMenu = QMenu() action_showAll = QAction( QIcon(':/plugins/annotationManager/resources/mActionShowAll.png'), self.tr('Show all annotations'), self.manager) action_showAll.triggered.connect(self.showAll) action_hideAll = QAction( QIcon(':/plugins/annotationManager/resources/mActionHideAll.png'), self.tr('Hide all annotations'), self.manager) action_hideAll.triggered.connect(self.hideAll) action_showAllSelected = QAction( QIcon(':/plugins/annotationManager/resources/mActionShowAll.png'), self.tr('Show all selected annotations'), self.manager) action_showAllSelected.triggered.connect(self.showAllSelected) action_hideAllSelected = QAction( QIcon(':/plugins/annotationManager/resources/mActionHideAll.png'), self.tr('Hide all selected annotations'), self.manager) action_hideAllSelected.triggered.connect(self.hideAllSelected) viewMenu.addAction(action_showAll) viewMenu.addAction(action_hideAll) viewMenu.addAction(action_showAllSelected) viewMenu.addAction(action_hideAllSelected) viewButton = QToolButton() viewButton.setIcon( QIcon(':/plugins/annotationManager/resources/mActionShowAll.png')) viewButton.setPopupMode(2) viewButton.setMenu(viewMenu) toolbar.addAction(action_refresh) toolbar.addAction(action_remove) toolbar.addWidget(viewButton) toolbar.setIconSize(QSize(16, 16)) p1_vertical = QVBoxLayout() p1_vertical.setContentsMargins(0, 0, 0, 0) p1_vertical.addWidget(toolbar) p1_vertical.addWidget(self.annotationList) self.manager.setLayout(p1_vertical) self.dock.setWidget(self.manager) self.dock.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea) self.iface.addDockWidget(Qt.LeftDockWidgetArea, self.dock) self.rb = QgsRubberBand(self.iface.mapCanvas(), QgsWkbTypes.PolygonGeometry) self.project = QgsProject.instance() self.annotationManager = self.project.annotationManager() self.annotationManager.annotationAdded.connect(self.refreshAnnotations) self.annotationManager.annotationRemoved.connect( self.refreshAnnotations) def checkItem(self, item): index = self.annotationList.row(item) if item.checkState() == Qt.Checked: self.annotationManager.annotations()[index].setVisible(True) else: self.annotationManager.annotations()[index].setVisible(False) if item.isSelected(): item.setSelected(False) self.rb.reset(QgsWkbTypes.PolygonGeometry) def selectAnnotation(self): self.rb.reset(QgsWkbTypes.PolygonGeometry) self.rb.setColor(QColor(0, 0, 255, 128)) for item in self.annotationList.selectedItems(): index = self.annotationList.row(item) mapTool = QgsMapTool(self.iface.mapCanvas()) point = mapTool.toCanvasCoordinates( self.annotationManager.annotations()[index].mapPosition()) pt1 = mapTool.toMapCoordinates( QPoint(point.x() - 10, point.y() - 10)) pt2 = mapTool.toMapCoordinates( QPoint(point.x() + 10, point.y() + 10)) rect = QgsRectangle(pt1, pt2) poly = QgsGeometry().fromRect(rect) self.rb.addGeometry(poly, None) def showAll(self): count = self.annotationList.count() for i in range(count): self.annotationList.item(i).setCheckState(Qt.Checked) def hideAll(self): count = self.annotationList.count() for i in range(count): self.annotationList.item(i).setCheckState(Qt.Unchecked) def showAllSelected(self): for item in self.annotationList.selectedItems(): item.setCheckState(Qt.Checked) def hideAllSelected(self): for item in self.annotationList.selectedItems(): item.setCheckState(Qt.Unchecked) def unload(self): del self.dock def tr(self, message): return QCoreApplication.translate('AnnotationManager', message) def refreshAnnotationTitle(self, annotation=None): if annotation is None: annotation = self.project.annotationManager().sender() item = self.annotationList.item( self.annotationManager.annotations().index(annotation)) title = 'Annotation' if isinstance(annotation, QgsTextAnnotation): title = annotation.document().toPlainText().split('\n')[0] if len(title) > 40: title = title[:40] + '(...)' item.setText(title) def refreshAnnotations(self): self.annotationList.clearSelection() self.annotationList.clear() for annotation in self.annotationManager.annotations(): item = QListWidgetItem() annotation.appearanceChanged.connect(self.refreshAnnotationTitle) if annotation.isVisible(): item.setCheckState(Qt.Checked) else: item.setCheckState(Qt.Unchecked) item.setFlags(item.flags()) self.annotationList.addItem(item) self.refreshAnnotationTitle(annotation) def removeAnnotation(self): if len(self.annotationList.selectedItems()) > 0: self.annotationManager.annotationRemoved.disconnect() trash = [] for item in self.annotationList.selectedItems(): index = self.annotationList.row(item) trash.append(self.annotationManager.annotations()[index]) while trash: self.annotationManager.removeAnnotation(trash.pop()) self.refreshAnnotations() self.annotationManager.annotationRemoved.connect( self.refreshAnnotations) def projectOpen(self): self.refreshAnnotations() def initGui(self): self.refreshAnnotations()
class DistrictSelectionDialog(QDialog): """ A dialog used for selecting from available districts :param district_registry: associated registry of available districts to show """ def __init__(self, district_registry, parent=None): super().__init__(parent) self.district_registry = district_registry self.setWindowTitle(self.tr('Select New {}').format( district_registry.type_string_title())) layout = QVBoxLayout() self.recent_label = QLabel(self.tr('Recently used {}').format( district_registry.type_string_sentence_plural())) layout.addWidget(self.recent_label) self.recent_list = QListWidget() self.recent_list.setMaximumHeight(100) for d in district_registry.recent_districts_list(): item = QListWidgetItem(self.district_registry.get_district_title(d)) item.setData(Qt.UserRole, d) self.recent_list.addItem(item) layout.addWidget(self.recent_list, 0) self.available_label = QLabel(self.tr('Available {}').format( district_registry.type_string_sentence_plural() )) layout.addWidget(self.available_label) self.search = QgsFilterLineEdit() self.search.setShowSearchIcon(True) self.search.setPlaceholderText(self.tr('Search for {}').format( district_registry.type_string_sentence())) self.search.textChanged.connect(self.filter_changed) layout.addWidget(self.search) self.list = QListWidget() for title, code in district_registry.district_titles().items(): item = QListWidgetItem(title) item.setData(Qt.UserRole, code) self.list.addItem(item) layout.addWidget(self.list, 10) button_box = QDialogButtonBox( QDialogButtonBox.Ok | QDialogButtonBox.Cancel) layout.addWidget(button_box) button_box.rejected.connect(self.reject) button_box.accepted.connect(self.accept) if self.district_registry.flags() & DistrictRegistry.FLAG_ALLOWS_SPATIAL_SELECT: self.select_from_map_button = button_box.addButton( self.tr("Select from Map"), QDialogButtonBox.ActionRole) self.select_from_map_button.clicked.connect(self.pick_from_map) else: self.select_from_map_button = None self.chose_pick_from_map = False self.setLayout(layout) self.recent_list.itemSelectionChanged.connect( self.recent_list_item_selected) self.list.itemSelectionChanged.connect( self.list_item_selected) self.recent_list.itemDoubleClicked.connect( self.accept) self.list.itemDoubleClicked.connect( self.accept) # select most recently used district by default if self.recent_list.count() > 0: self.recent_list.item(0).setSelected(True) def recent_list_item_selected(self): """ Handles a selection made in the recent district list """ if self.recent_list.selectedItems(): self.list.clearSelection() def list_item_selected(self): """ Handles a selection made in the complete district list """ if self.list.selectedItems(): self.recent_list.clearSelection() def set_selected_district(self, district): """ Sets the district selected in the dialog :param district: district to select """ for i in range(self.list.count()): if self.list.item(i).data(Qt.UserRole) == district: self.list.item(i).setSelected(True) return def selected_district(self): """ Returns the district selected in the dialog """ if self.recent_list.selectedItems(): return self.recent_list.selectedItems()[0].data(Qt.UserRole) elif self.list.selectedItems(): return self.list.selectedItems()[0].data(Qt.UserRole) return None def accept(self): # pylint: disable=missing-docstring self.district_registry.push_recent_district(self.selected_district()) super().accept() def filter_changed(self, filter_text): """ Handles search filter changes """ for i in range(self.list.count()): item = self.list.item(i) item.setHidden(filter_text.upper() not in item.text().upper()) def pick_from_map(self): """ Triggered when a user selects the "Select from Map" option """ self.chose_pick_from_map = True self.reject()