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
|
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
import os
from PyQt5 import QtCore
from PyQt5 import QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtSql import (QSqlTableModel,QSqlDatabase, QSqlQuery,
QSqlRelationalTableModel,
QSqlRelation,QSqlRelationalDelegate)
from PyQt5.QtWidgets import (QApplication, QTableView, QDataWidgetMapper, QComboBox, QDialog, QPushButton,
QFormLayout, QLabel, QStackedLayout, QMainWindow, QMessageBox, QAbstractItemView)
from SiF_ui import Ui_MenuSiF
from Nouvelagent_ui import Ui_NouvelAgentDialog
CONFIG_DATABASE_PATH = "./Data"
CONFIG_DATABASE_NAME = "Formation.db"
class Fenetre(QMainWindow, Ui_MenuSiF):
def __init__(self, parent=None):
super(Fenetre, self).__init__(parent)
self.ui = Ui_MenuSiF()
self.ui.setupUi(self)
self.ouvrecnx()
self.ui.pushButtonSortir.clicked.connect(self.Sortir)
self.ui.bntNouveauAgent.clicked.connect(self.Nouveau)
################################################################################
## Affichage dans la fenetre principale
################################################################################
self.modellisteemloyes = QSqlRelationalTableModel(self)
self.modellisteemloyes.setTable("Agents")
self.modellisteemloyes.setEditStrategy(QSqlRelationalTableModel.OnManualSubmit)
self.modellisteemloyes.select()
self.modellisteemloyes.setSort(0, Qt.AscendingOrder)
self.ui.tableViewEmployes.setSortingEnabled(True)
self.ui.tableViewEmployes.resizeColumnsToContents()
self.ui.tableViewEmployes.setModel(self.modellisteemloyes)
def Sortir(self):
reponse = QMessageBox.question(self,"Sortir ... ",
"Vous voulez quitter l'application ?",
QMessageBox.Yes,QMessageBox.No)
if reponse == QMessageBox.Yes:
self.close()
def Nouveau(self):
NouvelAgent(self)
################################################################################
##Connexion à la Base de Données
################################################################################
def ouvrecnx(self):
basedonnee = os.path.join(CONFIG_DATABASE_PATH, CONFIG_DATABASE_NAME)
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName(basedonnee)
if not db.open():
QMessageBox.critical(None, "Problème d'accès à*la base de données",
"Impossible d'établir une connexion à la base de données.\n"
"Cliquer Cancel pour sortir.",
QMessageBox.Cancel)
return False
################################################################################
##Fermeture de la connexion
################################################################################
def closeEvent(self, event):
reponse = QMessageBox.question(self,"Confirmation",
"Vous voulez vraiment quitter l'application ?",
QMessageBox.Yes,QMessageBox.No)
if reponse == QMessageBox.Yes:
if self.ouvrecnx !=None:
try:
self.ouvrecnx.close()
self.ouvrecnx = None
self.close()
except:
pass
event.accept()
else:
event.ignore()
################################################################################
## Boite de dialogue
################################################################################
class NouvelAgent(QDialog):
def __init__(self, parent=None):
super(NouvelAgent, self).__init__()
NouvelAgentDialog = QtWidgets.QDialog()
self.ui = Ui_NouvelAgentDialog()
self.ui.setupUi(NouvelAgentDialog)
NouvelAgentDialog.show()
NouvelAgentDialog.exec_()
self.ui.btnannulagent.clicked.connect(self.Annul_Saisie)
#Affichage liste des fonctions dans comboBoxFonction
self.listeFonction = QSqlTableModel(self)
self.listeFonction.setTable('Fonction')
self.listeFonction.select()
self.ui.comboBoxFonction.setModel(self.listeFonction)
self.ui.comboBoxFonction.setModelColumn(self.listeFonction.fieldIndex('FonctionLibelle'))
#Affichage liste des structures dans comboBoxStructure
self.listeStructure = QSqlTableModel(self)
self.listeStructure.setTable('Structure')
self.listeStructure.select()
self.ui.comboBoxStructure.setModel(self.listeStructure)
self.ui.comboBoxStructure.setModelColumn(self.listeStructure.fieldIndex('StructureLibelle'))
###############################################################################
def Annul_Saisie(self):
reponse = QMessageBox.question(self,"Confirmation",
"Vous voulez annuler la saisie ?",
QMessageBox.Yes,QMessageBox.No)
if reponse == QMessageBox.Yes:
self.close()
################################################################################
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
locale = QtCore.QLocale.system().name()
translator = QtCore.QTranslator()
reptrad = QtCore.QLibraryInfo.location(QtCore.QLibraryInfo.TranslationsPath)
translator.load("qtbase_" + locale, reptrad) # qtbase_fr.qm
app.installTranslator(translator)
fenetre = Fenetre()
fenetre.show()
sys.exit(app.exec_()) |
Partager