From b73a3f5d3ba5707f0bdb816452ad7f59c0da8290 Mon Sep 17 00:00:00 2001 From: MisterMLiL <99662459+MisterMLiL@users.noreply.github.com> Date: Thu, 28 Sep 2023 14:57:41 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D1=87=D1=82=D0=B5=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=B8=20=D0=B7=D0=B0=D0=BF=D0=B8=D1=81=D1=8C=20=D0=B8=D1=81?= =?UTF-8?q?=D1=85=D0=BE=D0=B4=D0=BD=D1=8B=D1=85=20=D0=B4=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=D1=85=20=D0=B2=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20.txt,?= =?UTF-8?q?=20.csv=20(#24)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #22 --------- Co-authored-by: Maxim Slipenko --- statapp/main_window.py | 85 ++++++++++++++++++++++++++++++++- statapp/models/fileslc_model.py | 33 +++++++++++++ statapp/ui/main_window.ui | 20 +++++++- statapp/ui/ui_main_window.py | 15 +++++- 4 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 statapp/models/fileslc_model.py diff --git a/statapp/main_window.py b/statapp/main_window.py index 079c397..f3ff833 100644 --- a/statapp/main_window.py +++ b/statapp/main_window.py @@ -1,29 +1,74 @@ import numpy as np -from PySide2.QtCore import Slot -from PySide2.QtWidgets import QMainWindow +from PySide2.QtCore import Slot, QLocale +from PySide2.QtWidgets import QMainWindow, QMessageBox, QApplication from statapp.generate_factor_window import GenerateFactorWindow, INDIRECT_LINK from statapp.models.data_model import DataModel from statapp.generate_window import GenerateWindow from statapp.about_window import AboutWindow +from statapp.models.fileslc_model import FileSLCModel from statapp.ui.ui_main_window import Ui_MainWindow class MainWindow(QMainWindow): def __init__(self): super().__init__() + self.ui = Ui_MainWindow() self.ui.setupUi(self) + self.isDataChanged = False self.model = DataModel() + self.fileModel = FileSLCModel() self.ui.tableView.setModel(self.model) + @Slot() + def on_openfileaction_triggered(self): + current_data = self.model.getData() + if current_data.size > 1: + file = '' + if self.fileModel.file_name: + file = '\nФайл сохранения:' + self.fileModel.file_name + + msgBox = self.createMessageBox \ + ('Сохранение данных', + "Сохранить данные?" + file, + QMessageBox.Question, + QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel, + QMessageBox.Cancel) + + reply = msgBox.exec_() + if reply == QMessageBox.StandardButton.Yes: + self.fileModel.saveFile(self.model.getData()) + elif reply == QMessageBox.StandardButton.Cancel: + return + else: + data = self.fileModel.loadFile() + if data is not None: + self.model.updateAllData(data) + self.isDataChanged = True + else: + data = self.fileModel.loadFile() + if data is not None: + self.model.updateAllData(data) + self.isDataChanged = True + + @Slot() + def on_savefileaction_triggered(self): + self.isDataChanged = not self.fileModel.saveFile(self.model.getData()) + + @Slot() + def on_closefileaction_triggered(self): + self.fileModel.closeFile() + self.isDataChanged = False + @Slot() def on_generateYaction_triggered(self): gw = GenerateWindow() if gw.exec(): y = np.random.normal(gw.mat, gw.deviation, size=(gw.count, 1)) self.model.updateAllData(y) + self.isDataChanged = True @Slot() def on_generateXaction_triggered(self): @@ -58,9 +103,45 @@ class MainWindow(QMainWindow): data = np.concatenate((data, x_arr), axis=1) # data = np.concatenate((data, dd), axis=1) self.model.updateAllData(data) + self.isDataChanged = True @Slot() def on_aboutmenuaction_triggered(self): global about_window about_window = AboutWindow() about_window.show() + + def createMessageBox(self, title, text, icon, buttons, defaultButton): + msgBox = QMessageBox() + + msgBox.setIcon(icon) + msgBox.setWindowTitle(title) + msgBox.setText(text) + msgBox.setStandardButtons(buttons) + msgBox.setDefaultButton(defaultButton) + + return msgBox + + def closeEvent(self, event): + if self.isDataChanged: + file = '' + if self.fileModel.file_name: + file = '\nФайл сохранения:' + self.fileModel.file_name + + msgBox = self.createMessageBox \ + ('Завершение работы', + "Сохранить данные?" + file, + QMessageBox.Question, + QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel, + QMessageBox.Cancel) + + reply = msgBox.exec_() + if reply == QMessageBox.StandardButton.Yes: + self.fileModel.saveFile(self.model.getData()) + event.accept() + elif reply == QMessageBox.StandardButton.No: + event.accept() + else: + event.ignore() + else: + event.accept() diff --git a/statapp/models/fileslc_model.py b/statapp/models/fileslc_model.py new file mode 100644 index 0000000..cc25980 --- /dev/null +++ b/statapp/models/fileslc_model.py @@ -0,0 +1,33 @@ +import numpy as np +from PySide2.QtWidgets import QFileDialog, QMessageBox + + +class FileSLCModel: + def __init__(self): + super().__init__() + self.file_name = None + + def saveFile(self, data): + if not self.file_name: + self.file_name, _ = QFileDialog.getSaveFileName(None, "Сохранить файл", "", "Text Files (*.txt);;CSV Files (*.csv)") + if self.file_name: + np.savetxt(self.file_name, data, delimiter=",") + return True + return False + + def loadFile(self): + self.file_name, _ = QFileDialog.getOpenFileName(None, "Загрузить файл", "", "Files (*.txt;*.csv)") + if not self.file_name: + try: + content = np.genfromtxt(self.file_name, delimiter=',', invalid_raise=True) + except ValueError as e: + QMessageBox.warning \ + (None, + 'Ошибка', + "Ошибка чтения файла!\nФайл нельзя открыть или файл неверного формата") + return None + return content + + def closeFile(self): + self.file_name = None + pass \ No newline at end of file diff --git a/statapp/ui/main_window.ui b/statapp/ui/main_window.ui index 89a3d15..47cb54f 100644 --- a/statapp/ui/main_window.ui +++ b/statapp/ui/main_window.ui @@ -40,13 +40,16 @@ 0 0 800 - 19 + 21 Файл + + + @@ -93,6 +96,21 @@ Генерация фактора + + + Открыть + + + + + Сохранить + + + + + Закрыть + + diff --git a/statapp/ui/ui_main_window.py b/statapp/ui/ui_main_window.py index dd1a94f..4b1d83b 100644 --- a/statapp/ui/ui_main_window.py +++ b/statapp/ui/ui_main_window.py @@ -24,6 +24,12 @@ class Ui_MainWindow(object): self.generateYaction.setObjectName(u"generateYaction") self.generateXaction = QAction(MainWindow) self.generateXaction.setObjectName(u"generateXaction") + self.openfileaction = QAction(MainWindow) + self.openfileaction.setObjectName(u"openfileaction") + self.savefileaction = QAction(MainWindow) + self.savefileaction.setObjectName(u"savefileaction") + self.closefileaction = QAction(MainWindow) + self.closefileaction.setObjectName(u"closefileaction") self.centralwidget = QWidget(MainWindow) self.centralwidget.setObjectName(u"centralwidget") self.gridLayout = QGridLayout(self.centralwidget) @@ -43,7 +49,7 @@ class Ui_MainWindow(object): MainWindow.setCentralWidget(self.centralwidget) self.menubar = QMenuBar(MainWindow) self.menubar.setObjectName(u"menubar") - self.menubar.setGeometry(QRect(0, 0, 800, 19)) + self.menubar.setGeometry(QRect(0, 0, 800, 21)) self.filemenu = QMenu(self.menubar) self.filemenu.setObjectName(u"filemenu") self.generatemenu = QMenu(self.menubar) @@ -64,6 +70,9 @@ class Ui_MainWindow(object): self.menubar.addAction(self.analyzemenu.menuAction()) self.menubar.addAction(self.modelmenu.menuAction()) self.menubar.addAction(self.helpmenu.menuAction()) + self.filemenu.addAction(self.openfileaction) + self.filemenu.addAction(self.savefileaction) + self.filemenu.addAction(self.closefileaction) self.generatemenu.addAction(self.generateYaction) self.generatemenu.addAction(self.generateXaction) self.helpmenu.addAction(self.aboutmenuaction) @@ -78,6 +87,9 @@ class Ui_MainWindow(object): self.aboutmenuaction.setText(QCoreApplication.translate("MainWindow", u"\u041e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435", None)) self.generateYaction.setText(QCoreApplication.translate("MainWindow", u"\u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u043e\u0442\u043a\u043b\u0438\u043a\u0430", None)) self.generateXaction.setText(QCoreApplication.translate("MainWindow", u"\u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u0444\u0430\u043a\u0442\u043e\u0440\u0430", None)) + self.openfileaction.setText(QCoreApplication.translate("MainWindow", u"\u041e\u0442\u043a\u0440\u044b\u0442\u044c", None)) + self.savefileaction.setText(QCoreApplication.translate("MainWindow", u"\u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c", None)) + self.closefileaction.setText(QCoreApplication.translate("MainWindow", u"\u0417\u0430\u043a\u0440\u044b\u0442\u044c", None)) self.label.setText(QCoreApplication.translate("MainWindow", u"\u0421\u0422\u0410\u0422\u0418\u0421\u0422\u0418\u0427\u0415\u0421\u041a\u0418\u0415 \u0414\u0410\u041d\u041d\u042b\u0415", None)) self.filemenu.setTitle(QCoreApplication.translate("MainWindow", u"\u0424\u0430\u0439\u043b", None)) self.generatemenu.setTitle(QCoreApplication.translate("MainWindow", u"\u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439", None)) @@ -85,3 +97,4 @@ class Ui_MainWindow(object): self.modelmenu.setTitle(QCoreApplication.translate("MainWindow", u"\u041c\u043e\u0434\u0435\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435", None)) self.helpmenu.setTitle(QCoreApplication.translate("MainWindow", u"\u0421\u043f\u0440\u0430\u0432\u043a\u0430", None)) # retranslateUi +