def testAdditionalLayers(self): QgsProject.instance().clear() l1 = create_layer('l1') l2 = create_layer('l2') QgsProject.instance().addMapLayers([l1, l2]) m = QgsMapLayerComboBox() self.assertEqual(m.count(), 2) l3 = create_layer('l3') l4 = create_layer('l4') m.setAdditionalLayers([l3, l4]) self.assertEqual(m.count(), 4) m.setAdditionalItems(['a', 'b']) self.assertEqual(m.count(), 6) self.assertEqual(m.itemText(0), 'l1') self.assertEqual(m.itemText(1), 'l2') self.assertEqual(m.itemText(2), 'l3') self.assertEqual(m.itemText(3), 'l4') self.assertEqual(m.itemText(4), 'a') self.assertEqual(m.itemText(5), 'b') m.setAllowEmptyLayer(True) self.assertEqual(m.count(), 7) self.assertFalse(m.itemText(0)) self.assertEqual(m.itemText(1), 'l1') self.assertEqual(m.itemText(2), 'l2') self.assertEqual(m.itemText(3), 'l3') self.assertEqual(m.itemText(4), 'l4') self.assertEqual(m.itemText(5), 'a') self.assertEqual(m.itemText(6), 'b') l3.deleteLater() QCoreApplication.sendPostedEvents(None, QEvent.DeferredDelete) self.assertEqual(m.count(), 6) self.assertFalse(m.itemText(0)) self.assertEqual(m.itemText(1), 'l1') self.assertEqual(m.itemText(2), 'l2') self.assertEqual(m.itemText(3), 'l4') self.assertEqual(m.itemText(4), 'a') self.assertEqual(m.itemText(5), 'b') l5 = create_layer('l5') l6 = create_layer('l6') m.setAdditionalLayers([l5, l6, l4]) self.assertEqual(m.count(), 8) self.assertFalse(m.itemText(0)) self.assertEqual(m.itemText(1), 'l1') self.assertEqual(m.itemText(2), 'l2') self.assertEqual(m.itemText(3), 'l4') self.assertEqual(m.itemText(4), 'l5') self.assertEqual(m.itemText(5), 'l6') self.assertEqual(m.itemText(6), 'a') self.assertEqual(m.itemText(7), 'b') m.setAdditionalLayers([l5, l4]) self.assertEqual(m.count(), 7) self.assertFalse(m.itemText(0)) self.assertEqual(m.itemText(1), 'l1') self.assertEqual(m.itemText(2), 'l2') self.assertEqual(m.itemText(3), 'l4') self.assertEqual(m.itemText(4), 'l5') self.assertEqual(m.itemText(5), 'a') self.assertEqual(m.itemText(6), 'b') QgsProject.instance().removeMapLayers([l1.id(), l2.id()]) self.assertEqual(m.count(), 5) self.assertFalse(m.itemText(0)) self.assertEqual(m.itemText(1), 'l4') self.assertEqual(m.itemText(2), 'l5') self.assertEqual(m.itemText(3), 'a') self.assertEqual(m.itemText(4), 'b')
class NewProjectDialog(Dialog): ''' dialog to select a layer and a name as inputs for creating a new project ''' def setupUi(self): ''' set up the user interface ''' self.setMinimumWidth(500) self.setWindowTitle('Neues Projekt erstellen') project_manager = ProjectManager() self.project_names = [p.name for p in project_manager.projects] layout = QVBoxLayout(self) label = QLabel('Name des Projekts') self.name_edit = QLineEdit() self.name_edit.textChanged.connect(self.validate) layout.addWidget(label) layout.addWidget(self.name_edit) self.path = os.path.join(project_manager.settings.TEMPLATE_PATH, 'projektflaechen') hlayout = QHBoxLayout(self) label = QLabel('Import der (Teil-)Flächen des Plangebiets') self.layer_combo = QgsMapLayerComboBox() self.layer_combo.setFilters(QgsMapLayerProxyModel.VectorLayer) self.source = None self.layer_combo.layerChanged.connect(self.set_layer) self.layer_combo.layerChanged.connect(self.validate) browse_button = QPushButton('...') browse_button.clicked.connect(self.browse_path) browse_button.setMaximumWidth(30) hlayout.addWidget(self.layer_combo) hlayout.addWidget(browse_button) layout.addWidget(label) layout.addLayout(hlayout) self.status_label = QLabel() layout.addWidget(self.status_label) spacer = QSpacerItem( 20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding) layout.addItem(spacer) buttons = QDialogButtonBox( QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self) self.ok_button = buttons.button(QDialogButtonBox.Ok) self.ok_button.setEnabled(False) buttons.accepted.connect(self.accept) buttons.rejected.connect(self.reject) layout.addWidget(buttons) if len(self.layer_combo) > 0: self.set_layer(self.layer_combo.currentLayer()) self.layer_combo.setCurrentIndex(0) def set_layer(self, layer: QgsVectorLayer = None): ''' set layer as user selection Parameters ---------- layer : QgsVectorLayer the selected layer ''' if not layer: path = self.layer_combo.currentText() layer = QgsVectorLayer(path, 'testlayer_shp', 'ogr') self.source = layer self.validate() def browse_path(self): ''' open dialog for user input of path to a shapefile and add it to the layer-combo ''' path, sf = QFileDialog.getOpenFileName( self, 'Datei wählen', filter="Shapefile(*.shp)", directory=self.path) if path: self.path = os.path.split(path)[0] self.layer_combo.setAdditionalItems([str(path)]) self.layer_combo.setCurrentIndex(self.layer_combo.count()-1) self.set_layer() def show(self) -> Tuple[bool, str, QgsVectorLayer]: ''' show dialog and return selections made by user ''' confirmed = self.exec_() if confirmed: return confirmed, self.name_edit.text(), self.source return False, None, None def validate(self): ''' validate current input of name and layer, set the status label according to validation result ''' name = str(self.name_edit.text()) status_text = '' regexp = re.compile('[\\\/\:*?\"\'<>|]') error = False if name and regexp.search(name): status_text = ('Der Projektname darf keines der folgenden Zeichen ' 'enthalten: \/:*?"\'<>|') error = True elif name in self.project_names: status_text = ( f'Ein Projekt mit dem Namen {name} existiert bereits!\n' 'Projektnamen müssen einzigartig sein.') error = True if self.source: if not self.source.isValid(): status_text = 'Der Layer ist ungültig.' error = True elif not self.source.geometryType() == QgsWkbTypes.PolygonGeometry: status_text = 'Der Layer hat keine Polygongeometrie.' error = True self.status_label.setText(status_text) if not error and (name and self.source): self.ok_button.setEnabled(True) else: self.ok_button.setEnabled(False)