class CheckList(QWidget): def __init__(self, model, label="", help_link="", custom_filter_button=None): """ :param custom_filter_button: if needed, add a button that opens a custom filter menu. Useful when search alone isn't enough to filter the list. :type custom_filter_button: QToolButton """ QWidget.__init__(self) self._model = model if help_link != "": addHelpToWidget(self, help_link) layout = QVBoxLayout() self._createCheckButtons() self._list = QListWidget() self._list.setContextMenuPolicy(Qt.CustomContextMenu) self._list.setSelectionMode(QAbstractItemView.ExtendedSelection) self._search_box = SearchBox() check_button_layout = QHBoxLayout() check_button_layout.setMargin(0) check_button_layout.setSpacing(0) check_button_layout.addWidget(QLabel(label)) check_button_layout.addStretch(1) check_button_layout.addWidget(self._checkAllButton) check_button_layout.addWidget(self._uncheckAllButton) layout.addLayout(check_button_layout) layout.addWidget(self._list) """ Inserts the custom filter button, if provided. The caller is responsible for all related actions. """ if custom_filter_button is not None: search_bar_layout = QHBoxLayout() search_bar_layout.addWidget(self._search_box) search_bar_layout.addWidget(custom_filter_button) layout.addLayout(search_bar_layout) else: layout.addWidget(self._search_box) self.setLayout(layout) self._checkAllButton.clicked.connect(self.checkAll) self._uncheckAllButton.clicked.connect(self.uncheckAll) self._list.itemChanged.connect(self.itemChanged) self._search_box.filterChanged.connect(self.filterList) self._list.customContextMenuRequested.connect(self.showContextMenu) self._model.selectionChanged.connect(self.modelChanged) self._model.modelChanged.connect(self.modelChanged) self.modelChanged() def _createCheckButtons(self): self._checkAllButton = QToolButton() self._checkAllButton.setIcon(resourceIcon("checked")) self._checkAllButton.setIconSize(QSize(16, 16)) self._checkAllButton.setToolButtonStyle(Qt.ToolButtonIconOnly) self._checkAllButton.setAutoRaise(True) self._checkAllButton.setToolTip("Select all") self._uncheckAllButton = QToolButton() self._uncheckAllButton.setIcon(resourceIcon("notchecked")) self._uncheckAllButton.setIconSize(QSize(16, 16)) self._uncheckAllButton.setToolButtonStyle(Qt.ToolButtonIconOnly) self._uncheckAllButton.setAutoRaise(True) self._uncheckAllButton.setToolTip("Unselect all") def itemChanged(self, item): """@type item: QListWidgetItem""" if item.checkState() == Qt.Checked: self._model.selectValue(str(item.text())) elif item.checkState() == Qt.Unchecked: self._model.unselectValue(str(item.text())) else: raise AssertionError("Unhandled checkstate!") def modelChanged(self): self._list.clear() items = self._model.getList() for item in items: list_item = QListWidgetItem(item) list_item.setFlags(list_item.flags() | Qt.ItemIsUserCheckable) if self._model.isValueSelected(item): list_item.setCheckState(Qt.Checked) else: list_item.setCheckState(Qt.Unchecked) self._list.addItem(list_item) self.filterList(self._search_box.filter()) def setSelectionEnabled(self, enabled): self.setEnabled(enabled) self._checkAllButton.setEnabled(enabled) self._uncheckAllButton.setEnabled(enabled) def filterList(self, filter): filter = filter.lower() for index in range(0, self._list.count()): item = self._list.item(index) text = str(item.text()).lower() if filter == "": item.setHidden(False) elif filter in text: item.setHidden(False) else: item.setHidden(True) def checkAll(self): """ Checks all visible items in the list. """ for index in range(0, self._list.count()): item = self._list.item(index) if not item.isHidden(): self._model.selectValue(str(item.text())) def uncheckAll(self): """ Unchecks all items in the list, visible or not """ self._model.unselectAll() def checkSelected(self): items = [] for item in self._list.selectedItems(): items.append(str(item.text())) for item in items: self._model.selectValue(item) def uncheckSelected(self): items = [] for item in self._list.selectedItems(): items.append(str(item.text())) for item in items: self._model.unselectValue(item) def showContextMenu(self, point): p = self._list.mapToGlobal(point) menu = QMenu() check_selected = menu.addAction("Check selected") uncheck_selected = menu.addAction("Uncheck selected") menu.addSeparator() clear_selection = menu.addAction("Clear selection") selected_item = menu.exec_(p) if selected_item == check_selected: self.checkSelected() elif selected_item == uncheck_selected: self.uncheckSelected() elif selected_item == clear_selection: self._list.clearSelection()
class CheckList(HelpedWidget): def __init__(self, model, label="", help_link=""): HelpedWidget.__init__(self, "", help_link) layout = QVBoxLayout() widget = QWidget() widget.setLayout(layout) self.checkAllButton = QToolButton() self.checkAllButton.setIcon(resourceIcon("checked")) self.checkAllButton.setIconSize(QSize(16, 16)) self.checkAllButton.setToolButtonStyle(Qt.ToolButtonIconOnly) self.checkAllButton.setAutoRaise(True) self.checkAllButton.setToolTip("Select all") self.uncheckAllButton = QToolButton() self.uncheckAllButton.setIcon(resourceIcon("notchecked")) self.uncheckAllButton.setIconSize(QSize(16, 16)) self.uncheckAllButton.setToolButtonStyle(Qt.ToolButtonIconOnly) self.uncheckAllButton.setAutoRaise(True) self.uncheckAllButton.setToolTip("Unselect all") self.list = QListWidget() self.list.setContextMenuPolicy(Qt.CustomContextMenu) self.list.setSelectionMode(QAbstractItemView.ExtendedSelection) self.search_box = SearchBox() check_button_layout = QHBoxLayout() check_button_layout.setMargin(0) check_button_layout.setSpacing(0) check_button_layout.addWidget(QLabel(label)) check_button_layout.addStretch(1) check_button_layout.addWidget(self.checkAllButton) check_button_layout.addWidget(self.uncheckAllButton) layout.addLayout(check_button_layout) layout.addWidget(self.list) layout.addWidget(self.search_box) self.addWidget(widget) self.connect(self.checkAllButton, SIGNAL('clicked()'), self.checkAll) self.connect(self.uncheckAllButton, SIGNAL('clicked()'), self.uncheckAll) self.connect(self.list, SIGNAL('itemChanged(QListWidgetItem*)'), self.itemChanged) self.search_box.filterChanged.connect(self.filterList) # self.connect(self.search_box, SIGNAL('filterChanged(str)'), self.filterList) self.connect(self.list, SIGNAL('customContextMenuRequested(QPoint)'), self.showContextMenu) assert isinstance(model, (SelectableModelMixin, ListModelMixin)) self.model = model self.model.observable().attach( SelectableModelMixin.SELECTION_CHANGED_EVENT, self.modelChanged) self.model.observable().attach(ListModelMixin.LIST_CHANGED_EVENT, self.modelChanged) self.modelChanged() def itemChanged(self, item): """@type item: QListWidgetItem""" if item.checkState() == Qt.Checked: self.model.selectValue(str(item.text())) elif item.checkState() == Qt.Unchecked: self.model.unselectValue(str(item.text())) else: raise AssertionError("Unhandled checkstate!") def modelChanged(self): self.list.clear() items = self.model.getList() for item in items: list_item = QListWidgetItem(item) list_item.setFlags(list_item.flags() | Qt.ItemIsUserCheckable) if self.model.isValueSelected(item): list_item.setCheckState(Qt.Checked) else: list_item.setCheckState(Qt.Unchecked) self.list.addItem(list_item) self.filterList(self.search_box.filter()) def setSelectionEnabled(self, enabled): self.setEnabled(enabled) self.checkAllButton.setEnabled(enabled) self.uncheckAllButton.setEnabled(enabled) def filterList(self, filter): filter = filter.lower() for index in range(0, self.list.count()): item = self.list.item(index) text = str(item.text()).lower() if filter == "": item.setHidden(False) elif filter in text: item.setHidden(False) else: item.setHidden(True) def checkAll(self): self.model.selectAll() def uncheckAll(self): self.model.unselectAll() def checkSelected(self): items = [] for item in self.list.selectedItems(): items.append(str(item.text())) for item in items: self.model.selectValue(item) def uncheckSelected(self): items = [] for item in self.list.selectedItems(): items.append(str(item.text())) for item in items: self.model.unselectValue(item) def showContextMenu(self, point): p = self.list.mapToGlobal(point) menu = QMenu() check_selected = menu.addAction("Check selected") uncheck_selected = menu.addAction("Uncheck selected") menu.addSeparator() clear_selection = menu.addAction("Clear selection") selected_item = menu.exec_(p) if selected_item == check_selected: self.checkSelected() elif selected_item == uncheck_selected: self.uncheckSelected() elif selected_item == clear_selection: self.list.clearSelection()
class CheckList(HelpedWidget): def __init__(self, model, label="", help_link=""): HelpedWidget.__init__(self, "", help_link) layout = QVBoxLayout() widget = QWidget() widget.setLayout(layout) self.checkAllButton = QToolButton() self.checkAllButton.setIcon(resourceIcon("checked")) self.checkAllButton.setIconSize(QSize(16, 16)) self.checkAllButton.setToolButtonStyle(Qt.ToolButtonIconOnly) self.checkAllButton.setAutoRaise(True) self.checkAllButton.setToolTip("Select all") self.uncheckAllButton = QToolButton() self.uncheckAllButton.setIcon(resourceIcon("notchecked")) self.uncheckAllButton.setIconSize(QSize(16, 16)) self.uncheckAllButton.setToolButtonStyle(Qt.ToolButtonIconOnly) self.uncheckAllButton.setAutoRaise(True) self.uncheckAllButton.setToolTip("Unselect all") self.list = QListWidget() self.list.setContextMenuPolicy(Qt.CustomContextMenu) self.list.setSelectionMode(QAbstractItemView.ExtendedSelection) self.search_box = SearchBox() check_button_layout = QHBoxLayout() check_button_layout.setMargin(0) check_button_layout.setSpacing(0) check_button_layout.addWidget(QLabel(label)) check_button_layout.addStretch(1) check_button_layout.addWidget(self.checkAllButton) check_button_layout.addWidget(self.uncheckAllButton) layout.addLayout(check_button_layout) layout.addWidget(self.list) layout.addWidget(self.search_box) self.addWidget(widget) self.connect(self.checkAllButton, SIGNAL('clicked()'), self.checkAll) self.connect(self.uncheckAllButton, SIGNAL('clicked()'), self.uncheckAll) self.connect(self.list, SIGNAL('itemChanged(QListWidgetItem*)'), self.itemChanged) self.search_box.filterChanged.connect(self.filterList) # self.connect(self.search_box, SIGNAL('filterChanged(str)'), self.filterList) self.connect(self.list, SIGNAL('customContextMenuRequested(QPoint)'), self.showContextMenu) assert isinstance(model, (SelectableModelMixin, ListModelMixin)) self.model = model self.model.observable().attach(SelectableModelMixin.SELECTION_CHANGED_EVENT, self.modelChanged) self.model.observable().attach(ListModelMixin.LIST_CHANGED_EVENT, self.modelChanged) self.modelChanged() def itemChanged(self, item): """@type item: QListWidgetItem""" if item.checkState() == Qt.Checked: self.model.selectValue(str(item.text())) elif item.checkState() == Qt.Unchecked: self.model.unselectValue(str(item.text())) else: raise AssertionError("Unhandled checkstate!") def modelChanged(self): self.list.clear() items = self.model.getList() for item in items: list_item = QListWidgetItem(item) list_item.setFlags(list_item.flags() | Qt.ItemIsUserCheckable) if self.model.isValueSelected(item): list_item.setCheckState(Qt.Checked) else: list_item.setCheckState(Qt.Unchecked) self.list.addItem(list_item) self.filterList(self.search_box.filter()) def setSelectionEnabled(self, enabled): self.setEnabled(enabled) self.checkAllButton.setEnabled(enabled) self.uncheckAllButton.setEnabled(enabled) def filterList(self, filter): filter = filter.lower() for index in range(0, self.list.count()): item = self.list.item(index) text = str(item.text()).lower() if filter == "": item.setHidden(False) elif filter in text: item.setHidden(False) else: item.setHidden(True) def checkAll(self): self.model.selectAll() def uncheckAll(self): self.model.unselectAll() def checkSelected(self): items = [] for item in self.list.selectedItems(): items.append(str(item.text())) for item in items: self.model.selectValue(item) def uncheckSelected(self): items = [] for item in self.list.selectedItems(): items.append(str(item.text())) for item in items: self.model.unselectValue(item) def showContextMenu(self, point): p = self.list.mapToGlobal(point) menu = QMenu() check_selected = menu.addAction("Check selected") uncheck_selected = menu.addAction("Uncheck selected") menu.addSeparator() clear_selection = menu.addAction("Clear selection") selected_item = menu.exec_(p) if selected_item == check_selected: self.checkSelected() elif selected_item == uncheck_selected: self.uncheckSelected() elif selected_item == clear_selection: self.list.clearSelection()
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) QCoreApplication.installTranslator(self.translator) self.iface = iface self.iface.projectRead.connect(self.projectOpen) self.dock = QDockWidget(self.tr('Annotations manager')) self.manager = QWidget() toolbar = QToolBar() self.annotationList = QListWidget() self.annotationList.itemClicked.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) toolbar.addAction(action_refresh) toolbar.addAction(action_remove) 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(), QGis.Polygon) self.annotations = [] self.annotationsName = [] def checkItem(self, item): row = self.annotationList.row(item) self.annotationsName[row] = item.text() if item.checkState() == Qt.Checked: self.annotations[row].show() else: self.annotations[row].hide() if item.isSelected(): item.setSelected(False) self.rb.reset() def selectAnnotation(self): index = self.annotationList.currentRow() self.rb.reset() self.rb.setColor(QColor(0, 0, 255, 128)) mapTool = QgsMapTool(self.iface.mapCanvas()) point = self.annotations[index].pos().toPoint() 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.setToGeometry(poly, None) def unload(self): del self.dock def tr(self, message): return QCoreApplication.translate('AnnotationManager', message) def getAnnotations(self): annotations = [] items = self.iface.mapCanvas().items() for item in items: if item.data(0) == 'AnnotationItem': annotations.append(item) return annotations def refreshAnnotations(self): for annotation in self.getAnnotations(): if annotation not in self.annotations: self.annotations.append(annotation) self.annotationsName.append('Annotation') i = 0 to_del = [] for annotation in self.annotations: if annotation not in self.getAnnotations(): to_del.append(i) i += 1 i = 0 for index in to_del: self.annotations.pop(index) self.annotationsName.pop(index) self.annotationList.clearSelection() self.annotationList.clear() # argh for annotation in self.annotations: item = QListWidgetItem(self.annotationsName[i]) if annotation.isVisible(): item.setCheckState(Qt.Checked) else: item.setCheckState(Qt.Unchecked) item.setFlags(item.flags() | Qt.ItemIsEditable) self.annotationList.addItem(item) i += 1 # fin argh def removeAnnotation(self): if len(self.annotationList.selectedItems()) > 0: index = self.annotationList.currentRow() self.annotationList.takeItem(index) self.annotationList.clearSelection() self.annotationList.clearFocus() self.iface.mapCanvas().scene().removeItem(self.annotations[index]) self.annotations.pop(index) self.annotationsName.pop(index) self.rb.reset() def projectOpen(self): del self.annotations[:] del self.annotationsName[:] self.refreshAnnotations() def initGui(self): self.refreshAnnotations()