示例#1
0
    def testSignals(self):
        QgsProject.instance().clear()
        m = QgsMapLayerComboBox()
        l1 = QgsVectorLayer(
            "Point?crs=EPSG:3111&field=fldtxt:string&field=fldint:integer",
            'layer 1', "memory")
        QgsProject.instance().addMapLayer(l1)
        l2 = QgsVectorLayer(
            "Polygon?crs=EPSG:3111&field=fldtxt:string&field=fldint:integer",
            'lAyEr 2', "memory")
        QgsProject.instance().addMapLayer(l2)
        spy = QSignalSpy(m.layerChanged)

        m.setLayer(l2)
        self.assertEqual(len(spy), 1)
        self.assertEqual(m.currentLayer(), l2)
        m.setLayer(l1)
        self.assertEqual(len(spy), 2)
        self.assertEqual(m.currentLayer(), l1)
        # no signal if same layer
        m.setLayer(l1)
        self.assertEqual(len(spy), 2)

        m.setAllowEmptyLayer(True)
        m.setLayer(None)
        self.assertEqual(len(spy), 3)
        self.assertIsNone(m.currentLayer())
        self.assertFalse(m.currentText())
        m.setLayer(None)
        self.assertEqual(len(spy), 3)
        self.assertIsNone(m.currentLayer())

        m.setEditable(True)
        m.setCurrentText('aaa')
        self.assertIsNone(m.currentLayer())

        m.setLayer(l1)
        self.assertEqual(len(spy), 4)
        self.assertEqual(m.currentLayer(), l1)
class SettingsDialog(QDialog):
    def __init__(self, parent=None):
        QDialog.__init__(self, parent)

        self.resize(500, 100)
        self.setWindowTitle("Settings")

        layout = QGridLayout(self)

        csTargetLayerName = getCSLayerName()
        bufferTargetLayerName = getRZLayerName()

        csLable = QLabel("Compressor staitions layer:")
        csLable.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        layout.addWidget(csLable, 0, 0)
        self.__csLayerName = QgsMapLayerComboBox()
        self.__csLayerName.setEditable(True)
        self.__csLayerName.setFilters(QgsMapLayerProxyModel.PointLayer)
        self.__csLayerName.setEditText(csTargetLayerName)
        self.__csLayerName.layerChanged.connect(self.csLayerChooze)
        self.__csLayerName.editTextChanged.connect(self.csLayernameSave)
        self.__csLayerName.setSizePolicy(QSizePolicy.Preferred,
                                         QSizePolicy.Fixed)
        layout.addWidget(self.__csLayerName, 0, 1)

        self.__csIdField = QgsFieldComboBox()
        self.__csIdField.setEditable(True)
        self.__csIdField.fieldChanged.connect(self.csIdFiledChooze)
        self.__csIdField.editTextChanged.connect(self.csIdFieldSave)
        self.__csIdField.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        self.csIdFieldFill()
        layout.addWidget(self.__csIdField, 0, 2)

        bufferLable = QLabel("Buffer layer:")
        bufferLable.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        layout.addWidget(bufferLable, 1, 0)
        # self.__bufferLayerName = QLineEdit(bufferTargetLayerName, self)
        # self.__bufferLayerName.editingFinished.connect(self.bufferTargetLayernameSave)
        self.__bufferLayerName = QgsMapLayerComboBox()
        self.__bufferLayerName.setEditable(True)
        self.__bufferLayerName.setFilters(QgsMapLayerProxyModel.PolygonLayer)
        self.__bufferLayerName.setEditText(bufferTargetLayerName)
        self.__bufferLayerName.layerChanged.connect(self.bufferLayerChooze)
        self.__bufferLayerName.editTextChanged.connect(
            self.bufferLayernameSave)
        layout.addWidget(self.__bufferLayerName, 1, 1)

    def csLayerChooze(self, qgsMapLayer):
        self.__csLayerName.setEditText(qgsMapLayer.name())

    def csLayernameSave(self, csTargetLayerName):
        if csTargetLayerName == u"":
            return
        setCSLayerName(csTargetLayerName)
        self.csIdFieldFill()

    def csIdFieldFill(self):
        csIdField = getCSIdField()
        csTargetLayerName = getCSLayerName()
        layers = QgsMapLayerRegistry.instance().mapLayersByName(
            csTargetLayerName)
        if len(layers) > 0:
            self.__csIdField.setLayer(layers[0])
        else:
            self.__csIdField.setLayer(None)
        self.__csIdField.setEditText(csIdField)

    def csIdFiledChooze(self, fieldName):
        self.__csIdField.setEditText(fieldName)

    def csIdFieldSave(self, fieldName):
        settings = QSettings()
        if fieldName == u"":
            return
        setCSIdField(fieldName)

    def bufferLayerChooze(self, qgsMapLayer):
        self.__bufferLayerName.setEditText(qgsMapLayer.name())

    def bufferLayernameSave(self, bufferTargetLayerName):
        settings = QSettings()
        if bufferTargetLayerName == u"":
            return
        setRZLayerName(bufferTargetLayerName)
class SettingsDialog(QDialog):
    def __init__(self, parent=None):
        QDialog.__init__(self, parent)

        self.resize(500, 100)
        self.setWindowTitle("Settings")

        layout = QGridLayout(self)

        csTargetLayerName = getCSLayerName()
        bufferTargetLayerName = getRZLayerName()

        csLable = QLabel("Compressor staitions layer:")
        csLable.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        layout.addWidget(csLable, 0, 0)
        self.__csLayerName = QgsMapLayerComboBox()
        self.__csLayerName.setEditable(True)
        self.__csLayerName.setFilters(QgsMapLayerProxyModel.PointLayer)
        self.__csLayerName.setEditText(csTargetLayerName)
        self.__csLayerName.layerChanged.connect(self.csLayerChooze)
        self.__csLayerName.editTextChanged.connect(self.csLayernameSave)
        self.__csLayerName.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed)
        layout.addWidget(self.__csLayerName, 0, 1)

        self.__csIdField = QgsFieldComboBox()
        self.__csIdField.setEditable(True)
        self.__csIdField.fieldChanged.connect(self.csIdFiledChooze)
        self.__csIdField.editTextChanged.connect(self.csIdFieldSave)
        self.__csIdField.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        self.csIdFieldFill()
        layout.addWidget(self.__csIdField, 0, 2)

        bufferLable = QLabel("Buffer layer:")
        bufferLable.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        layout.addWidget(bufferLable, 1, 0)
        # self.__bufferLayerName = QLineEdit(bufferTargetLayerName, self)
        # self.__bufferLayerName.editingFinished.connect(self.bufferTargetLayernameSave)
        self.__bufferLayerName = QgsMapLayerComboBox()
        self.__bufferLayerName.setEditable(True)
        self.__bufferLayerName.setFilters(QgsMapLayerProxyModel.PolygonLayer)
        self.__bufferLayerName.setEditText(bufferTargetLayerName)
        self.__bufferLayerName.layerChanged.connect(self.bufferLayerChooze)
        self.__bufferLayerName.editTextChanged.connect(self.bufferLayernameSave)
        layout.addWidget(self.__bufferLayerName, 1, 1)

    def csLayerChooze(self, qgsMapLayer):
        self.__csLayerName.setEditText(qgsMapLayer.name())

    def csLayernameSave(self, csTargetLayerName):
        if csTargetLayerName == u"":
            return
        setCSLayerName(csTargetLayerName)
        self.csIdFieldFill()

    def csIdFieldFill(self):
        csIdField = getCSIdField()
        csTargetLayerName = getCSLayerName()
        layers = QgsMapLayerRegistry.instance().mapLayersByName(csTargetLayerName)
        if len(layers) > 0:
            self.__csIdField.setLayer(layers[0])
        else:
            self.__csIdField.setLayer(None)
        self.__csIdField.setEditText(csIdField)

    def csIdFiledChooze(self, fieldName):
        self.__csIdField.setEditText(fieldName)

    def csIdFieldSave(self, fieldName):
        settings = QSettings()
        if fieldName == u"":
            return
        setCSIdField(fieldName)

    def bufferLayerChooze(self, qgsMapLayer):
        self.__bufferLayerName.setEditText(qgsMapLayer.name())

    def bufferLayernameSave(self, bufferTargetLayerName):
        settings = QSettings()
        if bufferTargetLayerName == u"":
            return
        setRZLayerName(bufferTargetLayerName)
示例#4
0
class Dialog(QtGui.QDialog):
    def __init__(self, curPointLayerName, curPolygonLayerName, curFiledName, parent=None):
        QtGui.QDialog.__init__(self, parent)

        self.resize(300, 100)
        Plugin().plPrint("curPointLayerName:: %s" % curPointLayerName)
        Plugin().plPrint("curPolygonLayerName:: %s" % curPolygonLayerName)
        Plugin().plPrint("curFiledName:: %s" % curFiledName)
        self.setWindowTitle(Plugin().getPluginName())
        self.__mainLayout = QtGui.QVBoxLayout(self)
        self.__layout = QtGui.QGridLayout(self)

        # self.__layout.addWidget(QtGui.QLabel(self.tr("Point layer name") + ":"), 0, 0)
        l1 = QtGui.QLabel(u"Имя точечного слоя" + ":")
        l1.setSizePolicy(
            QtGui.QSizePolicy.Preferred,
            QtGui.QSizePolicy.Fixed
        )
        self.__layout.addWidget(l1, 0, 0)
        self.pointsLayersComboBox = QgsMapLayerComboBox()
        self.pointsLayersComboBox.setSizePolicy(
            QtGui.QSizePolicy.Expanding,
            QtGui.QSizePolicy.Fixed
        )
        self.pointsLayersComboBox.setFilters(QgsMapLayerProxyModel.PointLayer)
        self.pointsLayersComboBox.setEditable(True)
        self.pointsLayersComboBox.setEditText(curPointLayerName)
        self.pointsLayersComboBox.layerChanged.connect(self.layerChooze1)
        self.__layout.addWidget(self.pointsLayersComboBox, 0, 1)


        # self.__layout.addWidget(QtGui.QLabel(self.tr("Field name") + ":"), 2, 0)
        self.__layout.addWidget(QtGui.QLabel(u"Имя поля" + ":"), 2, 0)
        self.fieldName = QgsFieldComboBox()
        self.fieldName.setFilters(QgsFieldProxyModel.Int)
        self.fieldName.setEditable(True)
        self.fieldName.setEditText(curFiledName)
        self.fieldName.fieldChanged.connect(self.filedChooze)
        self.__layout.addWidget(self.fieldName, 2, 1)

        # self.__layout.addWidget(QtGui.QLabel(self.tr("Polypon layer name") + ":"), 1, 0)
        self.__layout.addWidget(QtGui.QLabel(u"Имя полигонального слоя" + ":"), 1, 0)
        self.polygonsLayersComboBox = QgsMapLayerComboBox()
        self.polygonsLayersComboBox.setFilters(QgsMapLayerProxyModel.PolygonLayer)
        self.polygonsLayersComboBox.setEditable(True)
        self.polygonsLayersComboBox.setEditText(curPolygonLayerName)
        self.polygonsLayersComboBox.layerChanged.connect(self.layerChooze2)
        self.polygonsLayersComboBox.layerChanged.connect(self.fieldName.setLayer)
        self.__layout.addWidget(self.polygonsLayersComboBox, 1, 1)

        # self.startButton = QtGui.QPushButton(self.tr("Start"))
        # self.startButton.clicked.connect(self.startCalculation)
        # self.__layout.addWidget(self.startButton, 3, 1)

        self.__mainLayout.addLayout(self.__layout)
        # self.progress = QtGui.QLabel()
        # self.__mainLayout.addWidget(self.progress)
        self.__bbox = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok)
        self.__bbox.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Fixed)
        self.__bbox.accepted.connect(self.accept)
        self.__mainLayout.addWidget(self.__bbox)

    def layerChooze1(self, qgsMapLayer):
        self.pointsLayersComboBox.setEditText(qgsMapLayer.name())

    def layerChooze2(self, qgsMapLayer):
        self.polygonsLayersComboBox.setEditText(qgsMapLayer.name())

    def filedChooze(self, fieldName):
        self.sender().setEditText(fieldName)

    # def layernameSave(self, csTargetLayerName):
    #     if csTargetLayerName == u"":
    #         return
    #     setCSLayerName(csTargetLayerName)

    def getSettings(self):
        return [
            self.pointsLayersComboBox.currentText(),
            self.polygonsLayersComboBox.currentText(),
            self.fieldName.currentText()
        ]
示例#5
0
class Dialog(QtGui.QDialog):
    def __init__(
            self,
            curPointsLayerFrom,
            curPointsLayerTo,
            curFNIdFrom,
            curFNLink,
            curFNIdTo,
            curResultLayerName,
            parent=None):
        QtGui.QDialog.__init__(self, parent)

        self.resize(500, 200)

        self.setWindowTitle(QgisPlugin().pluginName)
        self.__mainLayout = QtGui.QVBoxLayout(self)
        self.__layout = QtGui.QGridLayout(self)

        l1 = QtGui.QLabel(self.tr(u"Point layer 'FROM'") + ":")
        l1.setSizePolicy(
            QtGui.QSizePolicy.Preferred,
            QtGui.QSizePolicy.Fixed
        )
        self.__layout.addWidget(l1, 0, 0)
        self.pointsLayerFrom = QgsMapLayerComboBox()
        self.pointsLayerFrom.setSizePolicy(
            QtGui.QSizePolicy.Expanding,
            QtGui.QSizePolicy.Fixed
        )
        self.pointsLayerFrom.setFilters(QgsMapLayerProxyModel.PointLayer)
        self.pointsLayerFrom.setEditable(True)
        # self.pointsLayerFrom.setEditText(curPointsLayerFrom)
        self.pointsLayerFrom.layerChanged.connect(self.choozeLayerFrom)
        self.__layout.addWidget(self.pointsLayerFrom, 0, 1)

        l2 = QtGui.QLabel(self.tr(u"Point layer 'TO'") + ":")
        l2.setSizePolicy(
            QtGui.QSizePolicy.Preferred,
            QtGui.QSizePolicy.Fixed
        )
        self.__layout.addWidget(l2, 1, 0)
        self.pointsLayerTo = QgsMapLayerComboBox()
        self.pointsLayerTo.setSizePolicy(
            QtGui.QSizePolicy.Expanding,
            QtGui.QSizePolicy.Fixed
        )
        self.pointsLayerTo.setFilters(QgsMapLayerProxyModel.PointLayer)
        self.pointsLayerTo.setEditable(True)
        # self.pointsLayerTo.setEditText(curPointsLayerTo)
        self.pointsLayerTo.layerChanged.connect(self.choozeLayerTo)
        self.__layout.addWidget(self.pointsLayerTo, 1, 1)

        self.__layout.addWidget(
            QtGui.QLabel(self.tr(u"Point 'FROM' id field") + ":"),
            2, 0
        )
        self.fnIdFrom = QgsFieldComboBox()
        self.fnIdFrom.setFilters(QgsFieldProxyModel.Int | QgsFieldProxyModel.LongLong)
        self.fnIdFrom.setEditable(True)
        # self.fnIdFrom.setEditText(curFNIdFrom)
        self.fnIdFrom.fieldChanged.connect(self.filedChooze)
        self.__layout.addWidget(self.fnIdFrom, 2, 1)

        self.__layout.addWidget(
            QtGui.QLabel(self.tr(u"Link field") + ":"),
            3, 0
        )
        self.fnLink = QgsFieldComboBox()
        self.fnLink.setFilters(QgsFieldProxyModel.Int | QgsFieldProxyModel.LongLong)
        self.fnLink.setEditable(True)
        # self.fnLink.setEditText(curFNLink)
        self.fnLink.fieldChanged.connect(self.filedChooze)
        self.__layout.addWidget(self.fnLink, 3, 1)

        self.__layout.addWidget(
            QtGui.QLabel(self.tr(u"Point 'TO' id field") + ":"), 4, 0)
        self.fnIdTo = QgsFieldComboBox()
        self.fnIdTo.setFilters(QgsFieldProxyModel.Int | QgsFieldProxyModel.LongLong)
        self.fnIdTo.setEditable(True)
        # self.fnIdTo.setEditText(curFNIdTo)
        self.fnIdTo.fieldChanged.connect(self.filedChooze)
        self.__layout.addWidget(self.fnIdTo, 4, 1)

        self.__layout.addWidget(
            QtGui.QLabel(self.tr(u"Save result in layer") + ":"),
            5, 0
        )
        self.linesLayer = QgsMapLayerComboBox()
        self.linesLayer.setSizePolicy(
            QtGui.QSizePolicy.Expanding,
            QtGui.QSizePolicy.Fixed
        )
        self.linesLayer.setFilters(QgsMapLayerProxyModel.LineLayer)
        self.linesLayer.setEditable(True)
        self.linesLayer.layerChanged.connect(self.choozeResultLayer)
        self.__layout.addWidget(self.linesLayer, 5, 1)

        # self.__layout4resultFileChoose = QtGui.QHBoxLayout()
        # self.leResultFilename = QtGui.QLineEdit(curResultFilename)
        # self.__layout4resultFileChoose.addWidget(self.leResultFilename)
        # self.pbChooseResultFilename = QtGui.QPushButton(u"Выбрать")
        # self.pbChooseResultFilename.released.connect(self.chooseResultFilename)
        # self.__layout4resultFileChoose.addWidget(self.pbChooseResultFilename)
        # self.__layout.addLayout(self.__layout4resultFileChoose, 6, 0, 2, 0)

        self.pointsLayerFrom.layerChanged.connect(self.fnIdFrom.setLayer)
        self.pointsLayerFrom.layerChanged.connect(self.fnLink.setLayer)

        self.pointsLayerTo.layerChanged.connect(self.fnIdTo.setLayer)

        self.__mainLayout.addLayout(self.__layout)

        self.__bbox = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok)
        self.__bbox.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Fixed)
        self.__bbox.accepted.connect(self.accept)
        self.__mainLayout.addWidget(self.__bbox)

        self.fillControls(
            curPointsLayerFrom,
            curPointsLayerTo,
            curFNIdFrom,
            curFNLink,
            curFNIdTo,
            curResultLayerName
        )

    def fillControls(
        self,
        curPointsLayerFrom,
        curPointsLayerTo,
        curFNIdFrom,
        curFNLink,
        curFNIdTo,
        curResultLayerName
    ):
        QgisPlugin().plPrint("curPointsLayerFrom: " + curPointsLayerFrom)
        QgisPlugin().plPrint("curPointsLayerTo: " + curPointsLayerTo)
        QgisPlugin().plPrint("curFNIdFrom: " + curPointsLayerFrom)
        QgisPlugin().plPrint("curFNLink: " + curFNIdFrom)
        QgisPlugin().plPrint("curPointsLayerFrom: " + curFNLink)
        QgisPlugin().plPrint("curFNIdTo: " + curFNIdTo)
        QgisPlugin().plPrint("curResultLayerName: " + curResultLayerName)

        layerFrom = self.getQGISLayer(curPointsLayerFrom)
        layerTo = self.getQGISLayer(curPointsLayerTo)
        layerResult = self.getQGISLayer(curResultLayerName, True)

        if layerFrom is None:
            self.pointsLayerFrom.setCurrentIndex(-1)
            self.pointsLayerFrom.setEditText(curPointsLayerFrom)
        else:
            self.pointsLayerFrom.setLayer(layerFrom)

        if layerTo is None:
            self.pointsLayerTo.setCurrentIndex(-1)
            self.pointsLayerTo.setEditText(curPointsLayerTo)
        else:
            self.pointsLayerTo.setLayer(layerTo)

        if layerResult is None:
            self.linesLayer.setCurrentIndex(-1)
            self.linesLayer.setEditText(curResultLayerName)
        else:
            self.linesLayer.setLayer(layerResult)

        # self.fnIdFrom.clear()
        # self.fnLink.clear()
        # self.fnIdTo.clear()

        # self.fnIdFrom.setField(curFNIdFrom)
        self.fnIdFrom.setCurrentIndex(-1)
        self.fnIdFrom.setEditText(curFNIdFrom)
        # self.fnLink.setField(curFNLink)
        self.fnLink.setCurrentIndex(-1)
        self.fnLink.setEditText(curFNLink)
        # self.fnIdTo.setField(curFNIdTo)
        self.fnIdTo.setCurrentIndex(-1)
        self.fnIdTo.setEditText(curFNIdTo)

    def getQGISLayer(self, layerName, silent=False):
        if layerName is None:
            return
        if layerName == "":
            return

        layers = QgsMapLayerRegistry.instance().mapLayersByName(layerName)
        if len(layers) == 0:
            if silent is False:
                QgisPlugin().showMessageForUser(
                    self.tr(u"Layer with name '%s' not found!") % layerName,
                    QgsMessageBar.CRITICAL,
                    0
                )
            return None
        return layers[0]

    def choozeLayerFrom(self, qgsMapLayer):
        self.pointsLayerFrom.setEditText(qgsMapLayer.name())

    def choozeLayerTo(self, qgsMapLayer):
        self.pointsLayerTo.setEditText(qgsMapLayer.name())

    def choozeResultLayer(self, qgsMapLayer):
        self.linesLayer.setEditText(qgsMapLayer.name())

    def filedChooze(self, fieldName):
        self.sender().setEditText(fieldName)

    def chooseResultFilename(self):
        filename = QtGui.QFileDialog.getSaveFileName(
            self,
            self.tr(u"Choose file for save result"),
            self.curResultFilename
            # QtCore.QFileInfo(self.curResultFilename).absolutePath()
        )
        self.leResultFilename.setText(filename)

    def getSettings(self):
        return [
            self.pointsLayerFrom.currentText(),
            self.pointsLayerTo.currentText(),
            self.fnIdFrom.currentText(),
            self.fnLink.currentText(),
            self.fnIdTo.currentText(),
            self.linesLayer.currentText(),
        ]