-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrealcentroid_dialog.py
102 lines (90 loc) · 4.04 KB
/
realcentroid_dialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# -*- coding: utf-8 -*-
"""
/***************************************************************************
RealCentroidDialog
A QGIS plugin
Create internal point for a polygon layer
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2018-12-01
git sha : $Format:%H$
copyright : (C) 2018 by Zotan Siki
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. *
* *
***************************************************************************/
"""
import os
from PyQt5 import uic
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from qgis.core import QgsMapLayerProxyModel, QgsSettings
from qgis.gui import QgsEncodingFileDialog
FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'realcentroid_dialog_base.ui'))
class RealCentroidDialog(QtWidgets.QDialog, FORM_CLASS):
""" dialog class for realcentroid QGIS3 plugin """
def __init__(self, parent=None):
"""Constructor."""
super(RealCentroidDialog, self).__init__(parent)
self.setupUi(self)
self.encoding = None
self.layerBox.currentIndexChanged.connect(self.sel)
self.browseButton.clicked.connect(self.browse)
self.cancelBtn.clicked.connect(self.reject)
self.okBtn.clicked.connect(self.ok)
def showEvent(self, event):
""" initialize dialog widgets """
# filter polygonlayers
self.layerBox.setFilters(QgsMapLayerProxyModel.PolygonLayer)
# clear previous pointlayer
self.pointEdit.clear()
self.sel()
def sel(self):
""" check/uncheck selectBox if selected layer changed """
l = self.layerBox.currentLayer()
try:
sf = l.selectedFeatures()
except:
sf = None
if sf: # is not None and len(sf):
self.selectedBox.setEnabled(True)
self.selectedBox.setCheckState(QtCore.Qt.Checked)
else:
self.selectedBox.setEnabled(False)
self.selectedBox.setCheckState(QtCore.Qt.Unchecked)
def browse(self):
""" open save layer dialog """
settings = QgsSettings()
dirName = settings.value("/UI/lastShapefileDir")
encode = settings.value("/UI/encoding")
fileDialog = QgsEncodingFileDialog(self, "Output shape file", dirName,
"Shape file (*.shp)", encode)
fileDialog.setDefaultSuffix("shp")
fileDialog.setFileMode(QtWidgets.QFileDialog.AnyFile)
fileDialog.setAcceptMode(QtWidgets.QFileDialog.AcceptSave)
#fileDialog.setConfirmOverwrite(True)
if not fileDialog.exec_() == QtWidgets.QDialog.Accepted:
return
files = fileDialog.selectedFiles()
self.pointEdit.setText(files[0])
self.encoding = fileDialog.encoding()
def ok(self):
""" check widgets """
if len(self.layerBox.currentText()) == 0:
QtWidgets.QMessageBox.warning(self, "Realcentroid", \
QtWidgets.QApplication.translate("RealCentroid", \
"No polygon layer selected", None))
return
if len(self.pointEdit.text()) == 0:
QtWidgets.QMessageBox.warning(self, "Realcentroid", \
QtWidgets.QApplication.translate("RealCentroid", \
"No point layer given", None))
return
self.accept()