Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aktivoi templaatti valitsemalla se listasta #24

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 19 additions & 23 deletions arho_feature_template/core/feature_template_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from qgis.core import QgsFeature, QgsProject, QgsVectorLayer
from qgis.gui import QgsMapToolDigitizeFeature
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtCore import QItemSelectionModel
from qgis.PyQt.QtGui import QStandardItem, QStandardItemModel
from qgis.utils import iface

Expand Down Expand Up @@ -56,8 +56,6 @@ def __init__(self, template_config: FeatureTemplate) -> None:
self.config = template_config
super().__init__(template_config.name)

self.setCheckable(True)

def is_valid(self) -> bool:
"""Check if the template is valid agains current QGIS project

Expand Down Expand Up @@ -94,11 +92,27 @@ def __init__(self) -> None:
# Update template list when search text changes
self.template_dock.search_box.valueChanged.connect(self.on_template_search_text_changed)

# Activate map tool when template selection changes
self.template_model.itemChanged.connect(self.on_item_changed)
# Activate map tool when template is selected
self.template_dock.template_list.clicked.connect(self.on_template_item_clicked)

self.digitize_map_tool = TemplateGeometryDigitizeMapTool(iface.mapCanvas(), iface.cadDockWidget())
self.digitize_map_tool.digitizingCompleted.connect(self.ask_for_feature_attributes)
self.digitize_map_tool.deactivated.connect(self.template_dock.template_list.clearSelection)

def on_template_item_clicked(self, index):
item = self.template_model.itemFromIndex(index)
try:
layer = get_layer_from_project(item.config.feature.layer)
except (LayerNotFoundError, LayerNotVectorTypeError):
logger.exception("Failed to activate template")
return
self.active_template = item
self.start_digitizing_for_layer(layer)

# Reselect as a workaround for first selection visual clarity
self.template_dock.template_list.selectionModel().select(
index, QItemSelectionModel.Select | QItemSelectionModel.Rows
)

def on_template_search_text_changed(self, search_text: str):
for row in range(self.template_model.rowCount()):
Expand All @@ -111,24 +125,6 @@ def on_template_search_text_changed(self, search_text: str):
# Otherwise, hide the row
self.template_dock.template_list.setRowHidden(row, True)

def on_item_changed(self, item: TemplateItem) -> None:
if item.checkState() == Qt.Checked:
self._uncheck_others(item)

try:
layer = get_layer_from_project(item.config.feature.layer)
except (LayerNotFoundError, LayerNotVectorTypeError):
logger.exception("Failed to activate template")
return
self.active_template = item
self.start_digitizing_for_layer(layer)

def _uncheck_others(self, item: QStandardItem) -> None:
for row in range(self.template_model.rowCount()):
other_item = self.template_model.item(row)
if other_item != item and other_item.checkState() == Qt.Checked:
other_item.setCheckState(Qt.Unchecked)

def start_digitizing_for_layer(self, layer: QgsVectorLayer) -> None:
self.digitize_map_tool.clean()
self.digitize_map_tool.setLayer(layer)
Expand Down
Loading