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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
| import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QMenu, QPushButton, QLabel
from PyQt5.QtGui import QIcon
# from PyQt5.QtCore import pyqtSlot
class QWindow(QMainWindow):
def __init__(self):
super().__init__()
self.createMenusHorizontaux()
self.initUI()
self.createStatusBar()
def initUI(self):
self.modificationParametresWindow(200, 300, 400, 250, 'titre')
self.modificationParametresToolTips()
# création d'un bouton
btn1 = self.createButton("sortie", self.width-125, self.height-30, "tooltip btn1", self.on_click)
btn1.setToolTip('lg=120, haut=15, x=100, y=70, désactivé')
btn1.resize(120,20)
# btn1.clicked.connect(self.on_click)
self.creationLabel ("<font color=#4400FF size='3'><b>Commune à rechercher:</b>", 10, 50, 300, 15)
self.creationTextBox (150, 50, 150, 20)
# self.getText()
self.show()
def modificationParametresWindow(self, x, y, l, h, titre):
self.title = titre
self.left = x
self.top = y
self.width = l
self.height = h
self.setWindowTitle(titre)
self.setGeometry(self.left, self.top, self.width, self.height)
def modificationParametresToolTips(self):
# modification des paramètres des tooltips
QToolTip.setFont(QFont('SansSerif', 9)) # Cette méthode statique définit une police utilisée pour rendre les info-bulles.
couleurFondToolTips = QToolTip.palette();
couleurFondToolTips.setColor(QPalette.ToolTipBase,QColor("#FFdddd")); #
couleurFondToolTips.setColor(QPalette.ToolTipText,QColor("#000000")); #
QToolTip.setPalette(couleurFondToolTips)
def createStatusBar(self):
sb = QStatusBar()
sb.setFixedHeight(18)
self.setStatusBar(sb)
self.statusBar().showMessage(self.tr("Ready"))
def createMenusHorizontaux(self):
# <a href="https://www.tutorialspoint.com/pyqt/qmenubar_qmenu_qaction_widgets.htm" target="_blank">https://www.tutorialspoint.com/pyqt/...on_widgets.htm</a>
# la classe QMenuBar possède les méthodes suivantes : addMenu(), addAction(), setEnabled(), addSeperator(), Clear(), setShortcut(), setText(), setTitle(),
bar = self.menuBar() # création d'un menu en haut d'écran; menuBar() est une méthode de la classe QMenuBar
file = bar.addMenu("&File")
file.addAction("New")
save = QAction("Save",self)
save.setShortcut("Ctrl+S")
file.addAction(save)
edit = file.addMenu("Edit")
edit.addAction("copy")
edit.addAction("paste")
quit = QAction("Quit",self)
file.addAction(quit)
file.triggered[QAction].connect(self.processtrigger)
self.helpMenu = self.menuBar().addMenu(self.tr("&Help"))
# self.helpMenu.addAction(self.aboutAct)
# self.helpMenu.addAction(self.aboutQtAct)
bar.setStyleSheet("QMenuBar::item { color: rgb(0, 0, 0); }")
def createButton(self, text, x, y, toolTip, member=None):
bouton = QPushButton(text, self)
bouton.setToolTip(text)
bouton.move(x,y)
if (member != None) : bouton.clicked.connect(member);
return bouton
def creationLabel (self, text, x, y, l, h):
self.libelle = QLabel(self)
self.libelle.setText(text)
#self.libelle.setAlignment(Qt.AlignCenter)
self.libelle.move(x,y)
#self.libelle.adjustSize()
self.libelle.resize(300, 15)
def getText(self):
text, okPressed = QInputDialog.getText(self, "Get text","Your name:", QLineEdit.Normal, "")
if okPressed and text != '':
print(text)
def creationTextBox (self, x, y, l, h):
self.textbox = QLineEdit(self)
self.textbox.move(x, y)
self.textbox.resize(l,h)
def processtrigger(self,q):
print (q.text()+" is triggered")
if (q.text() == "Quit"): self.close();
@pyqtSlot()
def on_click(self):
print('PyQt5 button click')
textboxValue = self.textbox.text()
QMessageBox.question(self, 'Message - pythonspot.com', "You typed: " + textboxValue, QMessageBox.Ok, QMessageBox.Ok)
# self.textbox.setText("")
self.close()
# fin class App(QWidget):
class RechercheCommune():
def __init__(self):
self.nomFichierCommune = "D:/_Informatique/011_QGis/_QGis_donnees/_QGis_couches/_Administratif/communes.shp"
self.aliasFichierCommune = "communes"
self.chargementCouche()
self.rechercheCommune("Ponteilla")
def chargementCouche(self):
# la couche est-elle déjà chargée?
bCoucheCommunesDejaChargee = False
for l in QgsProject.instance().mapLayers().values(): #iface.mapCanvas().layers():
# print ("l.name()=", l.name(), l.source())
# print(l.source())
if (l.source().find("communes") >=0):
bCoucheCommunesDejaChargee = True
if (bCoucheCommunesDejaChargee == False):
# la couche n'est pas encore chargée
# self.layerCommunes = iface.addVectorLayer(self.nomFichierCommune, self.aliasFichierCommune, "ogr")
iface.addVectorLayer(self.nomFichierCommune, self.aliasFichierCommune, "ogr")
print("couche communes créée")
else:
print("couche déjà présente")
# self.layerCommunes = iface.setActiveLayer(QgsProject.instance().mapLayersByName('communes')[0])
# la couche 'communes' est rendue active
iface.setActiveLayer(QgsProject.instance().mapLayersByName('communes')[0])
self.layerCommunes = iface.activeLayer()
print("la couche 'communes' est rendue active")
# type de symbologie de la couche
renderer = self.layerCommunes.renderer()
# print("Type:", renderer.type()) # ['nullSymbol', 'singleSymbol', 'categorizedSymbol', 'graduatedSymbol', 'RuleRenderer', ...]
# on change le remplissage des communes (jaune pâle, avec transparence)
#symbols = self.layerCommunes.renderer().symbols(QgsRenderContext()); # print(symbols)
#symbols[0].setColor(QColor.fromRgb(0,255,0, 80))
layerRenderer = self.layerCommunes.renderer() # modif fond, et bordure
mySymbol1 = QgsFillSymbol.createSimple({'color':'#00ff00', 'color_border':'#ff0000', 'width_border':'0.5'})
layerRenderer.setSymbol (mySymbol1)
self.layerCommunes.triggerRepaint()
def rechercheCommune(self, nomCommune):
# on parcours les 'features' de la couche pour y trouver la commune souhaitée
print("...rechercheCommune...self.layerCommunes.name()=", self.layerCommunes.name())
self.layerCommunes.selectByExpression("nom ILIKE \'" + nomCommune + "\'", QgsVectorLayer.SetSelection)
iface.mapCanvas().zoomToSelected()
scale = iface.mapCanvas().scale()
scale = 120000 # int(int(scale)*2000)/1000
iface.mapCanvas().zoomScale(scale)
if __name__ == '__main__':
app = QApplication(sys.argv)
mw = QWindow()
sys.exit(app.exec_())
else:
mw = QWindow()
print("après mw = QWindow()")
communeRecherchee = mw.textbox.text()
rc = RechercheCommune()
# rc.rechercheCommune("Arles")
rc.rechercheCommune(communeRecherchee) |
Partager