._.
This commit is contained in:
parent
70b534283d
commit
37481d4fa6
33
Desktop/employee_card_widget.py
Normal file
33
Desktop/employee_card_widget.py
Normal file
@ -0,0 +1,33 @@
|
||||
from PyQt6 import uic
|
||||
from PyQt6.QtWidgets import QWidget
|
||||
|
||||
from data.connect import connect, Department, Post
|
||||
|
||||
|
||||
class EmployeeCardWidget(QWidget):
|
||||
def __init__(self, user):
|
||||
super().__init__()
|
||||
|
||||
uic.loadUi('ui/card_widget.ui', self)
|
||||
|
||||
self.setStyleSheet('''
|
||||
QWidget {
|
||||
background-color: rgb(228, 244, 204);
|
||||
}
|
||||
''')
|
||||
|
||||
with connect() as session:
|
||||
department = session.query(Department).filter(Department.id == user.department_id).first()
|
||||
post = session.query(Post).filter(Post.id == user.post_id).first()
|
||||
|
||||
if department is not None and post is not None:
|
||||
self.label.setText(f'{department.title} - {post.title}')
|
||||
|
||||
if user.last_name is not None and user.first_name is not None and user.patronymic is not None:
|
||||
self.label_2.setText(f'{user.last_name} {user.first_name} {user.patronymic}')
|
||||
|
||||
if user.work_phone is not None and user.email is not None:
|
||||
self.label_3.setText(f'{user.work_phone} {user.email}')
|
||||
|
||||
if user.office is not None:
|
||||
self.label_4.setText(f'{user.office}')
|
||||
@ -2,10 +2,19 @@ import sys
|
||||
|
||||
from PyQt6.QtWidgets import QApplication
|
||||
|
||||
from data.connect import init_db
|
||||
from main_window import MainWindow
|
||||
|
||||
|
||||
def hook(a, b, c):
|
||||
sys.__excepthook__(a, b, c)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
init_db()
|
||||
|
||||
app = QApplication(sys.argv)
|
||||
window = MainWindow()
|
||||
window.show()
|
||||
sys.excepthook = hook
|
||||
sys.exit(app.exec())
|
||||
|
||||
@ -1,9 +1,39 @@
|
||||
from PyQt6 import uic
|
||||
from PyQt6.QtWidgets import QMainWindow
|
||||
|
||||
from employee_card_widget import EmployeeCardWidget
|
||||
from data.connect import connect, User, Department
|
||||
from structure_widget import DepartmentGraph
|
||||
|
||||
|
||||
class MainWindow(QMainWindow):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
uic.loadUi('ui/main.ui', self)
|
||||
|
||||
self.department_employees = []
|
||||
|
||||
self.structer_company_widget = DepartmentGraph(self)
|
||||
self.gridLayout_2.addWidget(self.structer_company_widget)
|
||||
|
||||
def select_department(self, department_id):
|
||||
self.department_employees = []
|
||||
self.search_employees(department_id)
|
||||
|
||||
for i in range(self.verticalLayout.count()):
|
||||
self.verticalLayout.itemAt(i).widget().close()
|
||||
|
||||
for employee in self.department_employees:
|
||||
employee_card = EmployeeCardWidget(employee)
|
||||
self.verticalLayout.addWidget(employee_card)
|
||||
|
||||
def search_employees(self, department_id):
|
||||
with connect() as session:
|
||||
employees = session.query(User).filter(User.department_id == department_id).all()
|
||||
self.department_employees += employees
|
||||
|
||||
child_departments = session.query(Department).filter(Department.parent_id == department_id).all()
|
||||
|
||||
for department in child_departments:
|
||||
self.search_employees(department.id)
|
||||
|
||||
@ -1,19 +1,20 @@
|
||||
import sys
|
||||
from PyQt6.QtCore import Qt
|
||||
from PyQt6.QtGui import QPen
|
||||
from PyQt6.QtWidgets import QGraphicsView, QGraphicsScene, QPushButton, QGraphicsProxyWidget
|
||||
|
||||
from PyQt5.QtWidgets import QApplication, QGraphicsView, QGraphicsScene, QPushButton, QGraphicsProxyWidget
|
||||
from PyQt5.QtGui import QPen
|
||||
from PyQt5.QtCore import Qt
|
||||
|
||||
from Desktop.data.connect import connect, Department, init_db
|
||||
from data.connect import connect, Department
|
||||
|
||||
|
||||
class DepartmentGraph(QGraphicsView):
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
def __init__(self, parent):
|
||||
super().__init__()
|
||||
self.scene = QGraphicsScene(self)
|
||||
self.setScene(self.scene)
|
||||
with connect() as session:
|
||||
self.departments = session.query(Department).all() # Список департаментов с их иерархией
|
||||
self.departments = session.query(Department).all()
|
||||
|
||||
self.pushed_button = None
|
||||
self.parent = parent
|
||||
|
||||
self.nodes = {}
|
||||
self.render_graph()
|
||||
@ -21,50 +22,61 @@ class DepartmentGraph(QGraphicsView):
|
||||
def render_graph(self):
|
||||
root_departments = [d for d in self.departments if d.parent_id is None]
|
||||
|
||||
|
||||
x = 50 # Начальная координата X
|
||||
y = 50 # Начальная координата Y
|
||||
x = 50
|
||||
y = 50
|
||||
|
||||
for root in root_departments:
|
||||
self.add_department_node(root, x, y)
|
||||
x += 900
|
||||
|
||||
def button_clicked(self):
|
||||
if self.pushed_button is not None:
|
||||
self.pushed_button.setStyleSheet('''
|
||||
QPushButton {
|
||||
background-color: rgb(228, 244, 204);
|
||||
}
|
||||
''')
|
||||
|
||||
button = self.sender()
|
||||
button.setStyleSheet('''
|
||||
QPushButton {
|
||||
background-color: #2f9836;
|
||||
}
|
||||
''')
|
||||
self.pushed_button = button
|
||||
|
||||
self.parent.select_department(button.department_id)
|
||||
|
||||
def add_department_node(self, department, x, y, parent_item=None, level=0):
|
||||
# Создаем кнопку
|
||||
button = QPushButton(department.title)
|
||||
button.department_id = department.id
|
||||
|
||||
button.clicked.connect(self.button_clicked)
|
||||
|
||||
button.setStyleSheet('''
|
||||
QPushButton {
|
||||
background-color: rgb(228, 244, 204);
|
||||
}
|
||||
''')
|
||||
|
||||
proxy = QGraphicsProxyWidget()
|
||||
proxy.setWidget(button)
|
||||
proxy.setPos(x, y)
|
||||
self.scene.addItem(proxy)
|
||||
|
||||
# Сохраняем узел
|
||||
self.nodes[department.id] = proxy
|
||||
|
||||
# Если есть родитель, добавляем линию
|
||||
if parent_item:
|
||||
parent_center = parent_item.sceneBoundingRect().center()
|
||||
current_center = proxy.sceneBoundingRect().center()
|
||||
line = self.scene.addLine(parent_center.x(), parent_center.y(), current_center.x(), current_center.y(),
|
||||
QPen(Qt.black))
|
||||
QPen(Qt.GlobalColor.black))
|
||||
|
||||
# Рекурсивно добавляем подразделы
|
||||
child_departments = [d for d in self.departments if d.parent_id == department.id]
|
||||
child_x = x - len(child_departments) * 100 // 2
|
||||
for index, child in enumerate(child_departments):
|
||||
# Чередуем высоту (шашечное расположение)
|
||||
offset_y = (index % 2) * 50 # Смещение по оси Y для чередования
|
||||
offset_y = (index % 2) * 50
|
||||
but_width = self.add_department_node(child, child_x, y + 50, proxy, level + 1)
|
||||
child_x += 250
|
||||
|
||||
return button.width()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
init_db()
|
||||
|
||||
app = QApplication(sys.argv)
|
||||
window = DepartmentGraph()
|
||||
window.setWindowTitle("Иерархия департаментов")
|
||||
window.resize(800, 600)
|
||||
window.show()
|
||||
|
||||
sys.exit(app.exec_())
|
||||
Loading…
x
Reference in New Issue
Block a user