MII_Labs_Mochalov_PI-33/lab10/lab10.py

123 lines
5.4 KiB
Python

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QLabel, QLineEdit, QPushButton, QColorDialog, QTableWidget, QTableWidgetItem, QHeaderView
from PyQt5.QtGui import QColor
import pyqtgraph as pg
import random
class FuzzyScaleApp(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Оценка прибыли")
self.setGeometry(100, 100, 800, 600)
self.plot_widget = pg.PlotWidget()
self.table_widget = QTableWidget()
self.table_widget.setColumnCount(6)
self.table_widget.setHorizontalHeaderLabels(["Название", "Тип", "a", "b", "c", "d"])
self.table_widget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.add_btn = QPushButton("Добавить")
self.remove_btn = QPushButton("Удалить")
self.update_btn = QPushButton("Обновить график")
main_widget = QWidget()
main_layout = QVBoxLayout()
main_layout.addWidget(self.plot_widget)
main_layout.addWidget(self.table_widget)
button_layout = QVBoxLayout()
button_layout.addWidget(self.add_btn)
button_layout.addWidget(self.remove_btn)
button_layout.addWidget(self.update_btn)
main_layout.addLayout(button_layout)
main_widget.setLayout(main_layout)
self.setCentralWidget(main_widget)
self.membership_functions = [
["Низкая", "Треугольная", 10, 20, 30, None],
["Средняя", "Трапециевидная", 15, 25, 35, 45],
["Высокая", "Треугольная", 40, 50, 60, None],
]
self.update_table()
self.update_plot()
self.add_btn.clicked.connect(self.add_function)
self.remove_btn.clicked.connect(self.remove_function)
self.table_widget.cellChanged.connect(self.update_data)
self.update_btn.clicked.connect(self.update_plot)
def update_data(self):
print(self.membership_functions)
for row in range(self.table_widget.rowCount()):
for column in range(6):
index = self.table_widget.model().index(row, column)
data = self.table_widget.model().data(index)
if column == 5:
if self.membership_functions[row][1] == 'Треугольная' and data != '':
self.membership_functions[row][1] = 'Трапециевидная'
elif self.membership_functions[row][column] == 'Трапециевидная' and data == '':
self.membership_functions[row][1] = 'Треугольная'
if data == '':
self.membership_functions[row][column] = None
continue
try:
self.membership_functions[row][column] = int(data)
except ValueError:
self.membership_functions[row][column] = data
print(self.membership_functions)
def add_function(self):
print("чмо")
row_count = self.table_widget.rowCount()
self.table_widget.insertRow(row_count)
self.table_widget.setItem(row_count, 0, QTableWidgetItem("Новая функция"))
self.table_widget.setItem(row_count, 1, QTableWidgetItem("Треугольная"))
self.membership_functions.append(["Новая функция", "Треугольная", 0, 25, 50, None])
self.update_table()
self.update_plot()
def remove_function(self):
selected_rows = self.table_widget.selectionModel().selectedRows()
rows_to_remove = [index.row() for index in selected_rows]
rows_to_remove.sort(reverse=True)
for row in rows_to_remove:
self.table_widget.removeRow(row)
del self.membership_functions[row]
self.update_plot()
def update_table(self):
self.table_widget.setRowCount(len(self.membership_functions))
row = 0
for (name, mf_type, a, b, c, d) in self.membership_functions:
self.table_widget.setItem(row, 0, QTableWidgetItem(name))
self.table_widget.setItem(row, 1, QTableWidgetItem(mf_type))
self.table_widget.setItem(row, 2, QTableWidgetItem(str(a)))
self.table_widget.setItem(row, 3, QTableWidgetItem(str(b)))
self.table_widget.setItem(row, 4, QTableWidgetItem(str(c)))
self.table_widget.setItem(row, 5, QTableWidgetItem(str(d) if d is not None else ""))
row += 1
def update_plot(self):
self.plot_widget.clear()
print('лох')
x = [-10, 110]
y = [0, 0]
self.plot_widget.plot(x, y)
for (name, mf_type, a, b, c, d) in self.membership_functions:
print(name, mf_type, a, b, c, d)
if mf_type == "Треугольная":
x = [a, b, c]
y = [0, 1, 0]
self.plot_widget.plot(x, y, name=name, pen=pg.mkPen(QColor(random.randint(0,255), random.randint(0,255), random.randint(0,255)), width=2))
else:
x = [a, b, c, d]
y = [0, 1, 1, 0]
self.plot_widget.plot(x, y, name=name, pen=pg.mkPen(QColor(random.randint(0,255), random.randint(0,255), random.randint(0,255)), width=2))
if __name__ == "__main__":
app = QApplication(sys.argv)
fuzzy_scale_app = FuzzyScaleApp()
fuzzy_scale_app.show()
sys.exit(app.exec_())