def testGetIconSvg(self):
     """
     Tests get_icon svg path
     """
     self.assertTrue(GuiUtils.get_icon_svg('plugin.svg'))
     self.assertIn('plugin.svg', GuiUtils.get_icon_svg('plugin.svg'))
     self.assertFalse(GuiUtils.get_icon_svg('not_an_icon.svg'))
Exemplo n.º 2
0
    def __init__(self, show_marker_count=False, show_orientation=False, show_placement=False, parent=None):
        super(MarkerSettingsWidget, self).__init__(parent)
        self.setupUi(self)

        self.code_combo.setEditable(True)
        self.code_combo.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Preferred)
        self.code_combo.setMinimumWidth(QFontMetrics(self.font()).width('X') * 40)

        self.field_code_combo.setAllowEmptyFieldName(True)

        self.field_rotation_combo.setFilters(QgsFieldProxyModel.Numeric)
        self.field_rotation_combo.setAllowEmptyFieldName(True)

        self.setFocusProxy(self.field_code_combo)

        self.field_code_combo.fieldChanged.connect(self.field_code_changed)
        self.field_rotation_combo.fieldChanged.connect(self.field_rotation_changed)
        self.code_combo.currentTextChanged.connect(self.on_code_changed)
        self.marker_count_spin.valueChanged.connect(self.marker_count_changed)
        self.marker_distance_spin.valueChanged.connect(self.marker_distance_changed)

        if not show_marker_count:
            self.marker_count_label.setVisible(False)
            self.marker_count_spin.setVisible(False)
            self.spacing_label.setVisible(False)
            self.spacing_combo.setVisible(False)
            self.spacing_stacked_widget.setVisible(False)
        if not show_orientation:
            self.orientation_label.setVisible(False)
            self.orientation_combo.setVisible(False)
        if not show_placement:
            self.placement_label.setVisible(False)
            self.placement_combo.setVisible(False)

        self.orientation_combo.addItem("0°", 0.0)
        self.orientation_combo.addItem("90°", 90.0)
        self.orientation_combo.addItem("180°", 180.0)
        self.orientation_combo.addItem("270°", 270.0)

        self.placement_combo.addItem(GuiUtils.get_icon('include_endpoints.svg'), self.tr('Include Endpoints'), True)
        self.placement_combo.addItem(GuiUtils.get_icon('exclude_endpoints.svg'), self.tr('Exclude Endpoints'), False)

        self.spacing_combo.addItem(self.tr('Via Count'), 0)
        self.spacing_combo.addItem(self.tr('Via Distance'), 1)

        self.orientation_combo.currentIndexChanged.connect(self.on_orientation_changed)
        self.placement_combo.currentIndexChanged.connect(self.on_placement_changed)
        self.spacing_combo.currentIndexChanged.connect(self.on_spacing_changed)

        self.marker_count_spin.setMinimum(2)

        self.layer = None
    def designer_opened(self, designer: QgsLayoutDesignerInterface):
        """
        Called whenever a new layout designer window is opened
        """
        toggle_unplaced_labels_action = QAction(
            self.tr('Show Unplaced Labels on Maps'), parent=designer)
        toggle_unplaced_labels_action.setCheckable(True)
        toggle_unplaced_labels_action.setIcon(
            GuiUtils.get_icon('show_unplaced_labels.svg'))

        # determine initial check state
        layout = designer.layout()
        maps = [
            item for item in layout.items()
            if isinstance(item, QgsLayoutItemMap)
        ]
        initial_checked = bool(maps) and all(
            m.mapFlags() & QgsLayoutItemMap.ShowUnplacedLabels for m in maps)
        toggle_unplaced_labels_action.setChecked(initial_checked)
        toggle_unplaced_labels_action.toggled.connect(
            partial(self.toggle_unplaced_labels, designer))
        toggle_unplaced_labels_action.setShortcut(QKeySequence('Ctrl+Shift+U'))

        tb = designer.actionsToolbar()
        tb.addSeparator()
        tb.addAction(toggle_unplaced_labels_action)
        designer.viewMenu().addSeparator()
        designer.viewMenu().addAction(toggle_unplaced_labels_action)
Exemplo n.º 4
0
    def update_for_renderer(self):
        if not self.layer:
            return

        icon_size = GuiUtils.scale_icon_size(16)

        renderer = self.layer.renderer()
        if isinstance(renderer, QgsCategorizedSymbolRenderer):
            prev_value = self.code_combo.currentText()
            self.code_combo.clear()

            prev_index = -1

            for category in renderer.categories():
                if category.value() is not None and category.value() != NULL:
                    if category.value() == prev_value:
                        prev_index = self.code_combo.count()
                    icon = QgsSymbolLayerUtils.symbolPreviewIcon(category.symbol(), QSize(icon_size, icon_size))

                    item_label = f'{category.label()} - ({category.value()})' if category.label() != category.value() else category.value()
                    self.code_combo.addItem(icon, item_label)
                    self.code_combo.setItemData(self.code_combo.count() - 1, category.value())

            if prev_index >= 0:
                self.code_combo.setCurrentIndex(prev_index)
            else:
                self.code_combo.setCurrentText(prev_value)
Exemplo n.º 5
0
    def create_rotation_item(self, map_point: QgsPointXY):
        f = self.create_feature(point=self.initial_point, rotation=0)

        # find symbol for feature
        renderer = self.current_layer().renderer().clone()
        context = QgsRenderContext.fromMapSettings(self.canvas().mapSettings())
        context.expressionContext().appendScope(
            QgsExpressionContextUtils.layerScope(self.current_layer()))
        context.expressionContext().setFeature(f)

        renderer.startRender(context, self.current_layer().fields())
        symbol = renderer.originalSymbolForFeature(f, context)
        if symbol is None:
            # e.g. code which doesn't match existing category
            if len(renderer.symbols(context)):
                symbol = renderer.symbols(context)[0]
            else:
                symbol = QgsSymbol.defaultSymbol(
                    self.current_layer().geometryType())

        renderer.stopRender(context)

        # clear existing data defined rotation
        symbol.setDataDefinedAngle(QgsProperty())

        # render symbol to image
        symbol_image = GuiUtils.big_marker_preview_image(
            symbol, context.expressionContext())

        self.rotation_item = PointRotationItem(self.canvas())
        self.rotation_item.set_symbol(symbol_image)
        self.rotation_item.set_point_location(map_point)
        self.rotation_item.set_symbol_rotation(0)
        self.rotation_item.update()
Exemplo n.º 6
0
    def create_tools(self):
        """
        Creates all map tools ands add them to the QGIS interface
        """
        action_single_point_templated_marker = QAction(
            GuiUtils.get_icon('single_point_templated_marker.svg'),
            self.tr('Single Point Templated Marker'))
        action_single_point_templated_marker.setCheckable(True)
        self.tools[SinglePointTemplatedMarkerTool.
                   ID] = SinglePointTemplatedMarkerTool(
                       self.iface.mapCanvas(), self.iface.cadDockWidget(),
                       self.iface, action_single_point_templated_marker)
        self.tools[SinglePointTemplatedMarkerTool.ID].setAction(
            action_single_point_templated_marker)
        action_single_point_templated_marker.triggered.connect(
            partial(self.switch_tool, SinglePointTemplatedMarkerTool.ID))
        action_single_point_templated_marker.setData(
            SinglePointTemplatedMarkerTool.ID)
        self.toolbar.addAction(action_single_point_templated_marker)
        self.actions.append(action_single_point_templated_marker)

        self.get_map_tool_action_group().addAction(
            action_single_point_templated_marker)

        # single point at center of line tool

        action_single_point_at_center_of_line = QAction(
            GuiUtils.get_icon('marker_at_center_of_line.svg'),
            self.tr('Single Point Templated Marker Via Two Points'))
        action_single_point_at_center_of_line.setCheckable(True)
        self.tools[
            TwoPointTemplatedMarkerTool.ID] = TwoPointTemplatedMarkerTool(
                self.iface.mapCanvas(), self.iface.cadDockWidget(), self.iface,
                action_single_point_at_center_of_line)
        self.tools[TwoPointTemplatedMarkerTool.ID].setAction(
            action_single_point_at_center_of_line)
        action_single_point_at_center_of_line.triggered.connect(
            partial(self.switch_tool, TwoPointTemplatedMarkerTool.ID))
        action_single_point_at_center_of_line.setData(
            TwoPointTemplatedMarkerTool.ID)
        self.toolbar.addAction(action_single_point_at_center_of_line)
        self.actions.append(action_single_point_at_center_of_line)

        self.get_map_tool_action_group().addAction(
            action_single_point_at_center_of_line)

        # multi point tool

        action_multi_point_templated_marker = QAction(
            GuiUtils.get_icon('multi_point_templated_marker.svg'),
            self.tr('Multiple Point Templated Marker Along LineString'))
        action_multi_point_templated_marker.setCheckable(True)
        self.tools[
            MultiPointTemplatedMarkerTool.ID] = MultiPointTemplatedMarkerTool(
                self.iface.mapCanvas(), self.iface.cadDockWidget(), self.iface,
                action_multi_point_templated_marker)
        self.tools[MultiPointTemplatedMarkerTool.ID].setAction(
            action_multi_point_templated_marker)
        action_multi_point_templated_marker.triggered.connect(
            partial(self.switch_tool, MultiPointTemplatedMarkerTool.ID))
        action_multi_point_templated_marker.setData(
            MultiPointTemplatedMarkerTool.ID)
        self.toolbar.addAction(action_multi_point_templated_marker)
        self.actions.append(action_multi_point_templated_marker)

        self.get_map_tool_action_group().addAction(
            action_single_point_templated_marker)

        # multi at center of segment point tool

        action_multi_point_center_segment_templated_marker = QAction(
            GuiUtils.get_icon('multi_point_templated_marker_at_center.svg'),
            self.tr('Multiple Point Templated Marker At Center Of Segments'))
        action_multi_point_center_segment_templated_marker.setCheckable(True)
        self.tools[MultiPointSegmentCenterTemplatedMarkerTool.
                   ID] = MultiPointSegmentCenterTemplatedMarkerTool(
                       self.iface.mapCanvas(), self.iface.cadDockWidget(),
                       self.iface,
                       action_multi_point_center_segment_templated_marker)
        self.tools[MultiPointSegmentCenterTemplatedMarkerTool.ID].setAction(
            action_multi_point_center_segment_templated_marker)
        action_multi_point_center_segment_templated_marker.triggered.connect(
            partial(self.switch_tool,
                    MultiPointSegmentCenterTemplatedMarkerTool.ID))
        action_multi_point_center_segment_templated_marker.setData(
            MultiPointSegmentCenterTemplatedMarkerTool.ID)
        self.toolbar.addAction(
            action_multi_point_center_segment_templated_marker)
        self.actions.append(action_multi_point_center_segment_templated_marker)

        self.get_map_tool_action_group().addAction(
            action_single_point_templated_marker)

        self.enable_actions_for_layer(self.iface.activeLayer())
 def testGetIcon(self):
     """
     Tests get_icon
     """
     self.assertFalse(GuiUtils.get_icon('plugin.svg').isNull())
     self.assertTrue(GuiUtils.get_icon('not_an_icon.svg').isNull())
Exemplo n.º 8
0
    def paint(self, painter, option, widget):
        if not painter:
            return

        painter.save()
        painter.setRenderHint(QPainter.Antialiasing, True)

        # do a bit of trigonometry to find out how to transform a rotated item such
        # that the center point is at the point feature
        x = 0.0
        y = 0.0

        if self.pixmap.width() > 0 and self.pixmap.height() > 0:
            half_item_diagonal = math.sqrt(
                self.pixmap.width() * self.pixmap.width() +
                self.pixmap.height() * self.pixmap.height()) / 2
            diagonal_angle = math.acos(
                self.pixmap.width() / (half_item_diagonal * 2)) * 180 / math.pi
            x = half_item_diagonal * math.cos(
                (self.rotation - diagonal_angle) * math.pi / 180)
            y = half_item_diagonal * math.sin(
                (self.rotation - diagonal_angle) * math.pi / 180)

        painter.rotate(self.rotation)
        painter.translate(x - self.pixmap.width() / 2.0,
                          -y - self.pixmap.height() / 2.0)
        painter.drawPixmap(0, 0, self.pixmap)

        # draw arrow, using a red line over a thicker white line so that the arrow is visible
        # against a range of backgrounds
        pen = QPen()
        pen.setWidth(GuiUtils.scale_icon_size(4))
        pen.setColor(QColor(Qt.white))
        painter.setPen(pen)
        painter.drawPath(self.arrow_path)
        pen.setWidth(GuiUtils.scale_icon_size(1))
        pen.setColor(QColor(Qt.red))
        painter.setPen(pen)
        painter.drawPath(self.arrow_path)
        painter.restore()

        # draw numeric value beside the symbol
        painter.save()
        painter.setRenderHint(QPainter.Antialiasing, True)

        buffer_pen = QPen()
        buffer_pen.setColor(Qt.white)
        buffer_pen.setWidthF(GuiUtils.scale_icon_size(4))
        fm = QFontMetricsF(self.marker_font)
        label = QPainterPath()
        label.addText(self.pixmap.width(),
                      self.pixmap.height() / 2.0 + fm.height() / 2.0,
                      self.marker_font, str(round(self.rotation, 1)))
        painter.setPen(buffer_pen)
        painter.setBrush(Qt.NoBrush)
        painter.drawPath(label)
        painter.setPen(Qt.NoPen)
        painter.setBrush(QBrush(Qt.black))
        painter.drawPath(label)

        painter.restore()
Exemplo n.º 9
0
 def svgIconPath(self):
     """
     Returns a path to the provider's icon as a SVG file
     """
     return GuiUtils.get_icon_svg("plugin.svg")
Exemplo n.º 10
0
 def icon(self):
     """
     Returns the provider's icon
     """
     return GuiUtils.get_icon("plugin.svg")
Exemplo n.º 11
0
from qgis.PyQt import uic
from qgis.PyQt.QtCore import QSize, pyqtSignal
from qgis.PyQt.QtGui import QFontMetrics
from qgis.PyQt.QtWidgets import QSizePolicy
from qgis.core import (
    QgsFieldProxyModel,
    QgsVectorLayer,
    QgsCategorizedSymbolRenderer,
    QgsSymbolLayerUtils,
    NULL
)

from cartography_tools.gui.gui_utils import GuiUtils

WIDGET, BASE = uic.loadUiType(
    GuiUtils.get_ui_file_path('marker_settings.ui'))


class MarkerSettingsWidget(BASE, WIDGET):
    count_changed = pyqtSignal(int)
    distance_changed = pyqtSignal(float)
    code_changed = pyqtSignal()
    orientation_changed = pyqtSignal()
    placement_changed = pyqtSignal()

    def __init__(self, show_marker_count=False, show_orientation=False, show_placement=False, parent=None):
        super(MarkerSettingsWidget, self).__init__(parent)
        self.setupUi(self)

        self.code_combo.setEditable(True)
        self.code_combo.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Preferred)