Skip to content

Commit

Permalink
Add map themes's default active layer configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Jul 21, 2024
1 parent ea77ad8 commit 9754d5e
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
83 changes: 83 additions & 0 deletions qfieldsync/gui/mapthemes_config_widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# -*- coding: utf-8 -*-
"""
/***************************************************************************
MapThemesConfigWidget
A QGIS plugin
Sync your projects to QField
-------------------
begin : 2020-10-10
git sha : $Format:%H$
copyright : (C) 2020 by OPENGIS.ch
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""

from qgis.core import Qgis
from qgis.gui import QgsMapLayerComboBox

from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtWidgets import QTableWidgetItem, QTableWidget


class MapThemesConfigWidget(QTableWidget):
def __init__(self, project, configuration, parent=None):
"""Constructor."""
super(QTableWidget, self).__init__(parent=parent)

self.project = project

self.setMinimumHeight(200)
self.setColumnCount(2)
self.setHorizontalHeaderLabels(
[self.tr("Map Theme"), self.tr("Default Active Layer")]
)

self.reload(configuration)

def reload(self, configuration):
"""
Load map themes into table.
"""

self.setRowCount(0)
self.setSortingEnabled(False)
map_themes = self.project.mapThemeCollection().mapThemes()
for map_theme in map_themes:
count = self.rowCount()
self.insertRow(count)
item = QTableWidgetItem(map_theme)
item.setData(Qt.EditRole, map_theme)
self.setItem(count, 0, item)

cmb = QgsMapLayerComboBox()
cmb.setAllowEmptyLayer(True)
cmb.setProject(self.project)
cmb.setFilters(Qgis.LayerFilter.VectorLayer)
if map_theme in configuration:
cmb.setLayer(self.project.mapLayer(configuration[map_theme]))
self.setCellWidget(count, 1, cmb)

self.setColumnWidth(0, int(self.width() * 0.2))
self.setColumnWidth(1, int(self.width() * 0.75))
self.sortByColumn(0, Qt.AscendingOrder)
self.setSortingEnabled(True)

def createConfiguration(self):
configuration = {}
for i in range(self.rowCount()):
item = self.item(i, 0)
map_theme = item.data(Qt.EditRole)
cmb = self.cellWidget(i, 1)
layer_id = cmb.currentLayer().id() if cmb.currentLayer() else ""
configuration[map_theme] = layer_id

return configuration
12 changes: 11 additions & 1 deletion qfieldsync/gui/project_configuration_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

from qfieldsync.core.preferences import Preferences
from qfieldsync.gui.layers_config_widget import LayersConfigWidget
from qfieldsync.gui.mapthemes_config_widget import MapThemesConfigWidget

WidgetUi, _ = loadUiType(
os.path.join(os.path.dirname(__file__), "../ui/project_configuration_widget.ui"),
Expand Down Expand Up @@ -152,7 +153,7 @@ def reloadProject(self):
self.cloudExportTab.layout().addWidget(infoLabel, 0, 2)
self.cableExportTab.layout().addWidget(self.cableLayersConfigWidget)

# Load Map Themes
# Map Themes configuration widgets
for theme in self.project.mapThemeCollection().mapThemes():
self.mapThemeComboBox.addItem(theme)

Expand All @@ -166,6 +167,11 @@ def reloadProject(self):

self.__project_configuration = ProjectConfiguration(self.project)

self.mapThemesConfigWidget = MapThemesConfigWidget(
self.project, self.__project_configuration.map_themes_active_layer
)
self.mapThemesGroupBox.layout().addWidget(self.mapThemesConfigWidget)

# Base map settings
self.createBaseMapGroupBox.setChecked(
self.__project_configuration.create_base_map
Expand Down Expand Up @@ -371,6 +377,10 @@ def apply(self):
keys[item.text()] = 1
self.preferences.set_value("attachmentDirs", list(keys.keys()))

self.__project_configuration.map_themes_active_layer = (
self.mapThemesConfigWidget.createConfiguration()
)

def onForceAutoPushClicked(self, checked):
self.forceAutoPushInterval.setEnabled(checked)

Expand Down
17 changes: 17 additions & 0 deletions qfieldsync/ui/project_configuration_widget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,23 @@
<bool>true</bool>
</property>
<layout class="QGridLayout" name="gridLayout_8" columnstretch="1,3">
<item row="5" column="0" colspan="2">
<widget class="QgsCollapsibleGroupBox" name="mapThemesGroupBox">
<property name="title">
<string>Map Themes Configuration</string>
</property>
<property name="checkable">
<bool>false</bool>
</property>
<property name="collapsed">
<bool>false</bool>
</property>
<property name="saveCollapsedState">
<bool>true</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout_8"/>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="attachmentDirsLabel">
<property name="toolTip">
Expand Down

0 comments on commit 9754d5e

Please sign in to comment.