fix: исправил ошибки и замечания Maks1m_s, добавил проверку на открытие файла при непустой таблице

This commit is contained in:
Почти Демон 2023-09-28 14:12:20 +03:00
parent 64a8d9bc4c
commit 3552e93dde
2 changed files with 70 additions and 32 deletions

View File

@ -1,5 +1,5 @@
import numpy as np import numpy as np
from PySide2.QtCore import Slot from PySide2.QtCore import Slot, QLocale
from PySide2.QtWidgets import QMainWindow, QMessageBox, QApplication from PySide2.QtWidgets import QMainWindow, QMessageBox, QApplication
from statapp.generate_factor_window import GenerateFactorWindow, INDIRECT_LINK from statapp.generate_factor_window import GenerateFactorWindow, INDIRECT_LINK
@ -13,6 +13,7 @@ from statapp.ui.ui_main_window import Ui_MainWindow
class MainWindow(QMainWindow): class MainWindow(QMainWindow):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.ui = Ui_MainWindow() self.ui = Ui_MainWindow()
self.ui.setupUi(self) self.ui.setupUi(self)
@ -23,9 +24,34 @@ class MainWindow(QMainWindow):
@Slot() @Slot()
def on_openfileaction_triggered(self): def on_openfileaction_triggered(self):
data = self.fileModel.loadFile() current_data = self.model.getData()
self.model.updateAllData(data) if current_data.size > 1:
self.isDataChanged = True 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() @Slot()
def on_savefileaction_triggered(self): def on_savefileaction_triggered(self):
@ -85,33 +111,42 @@ class MainWindow(QMainWindow):
about_window = AboutWindow() about_window = AboutWindow()
about_window.show() 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)
if msgBox.button(QMessageBox.Yes) is not None:
msgBox.button(QMessageBox.Yes).setText("Да")
if msgBox.button(QMessageBox.No) is not None:
msgBox.button(QMessageBox.No).setText("Нет")
if msgBox.button(QMessageBox.Ok) is not None:
msgBox.button(QMessageBox.Ok).setText("Ок")
if msgBox.button(QMessageBox.Cancel) is not None:
msgBox.button(QMessageBox.Cancel).setText("Отмена")
return msgBox
def closeEvent(self, event): def closeEvent(self, event):
if self.isDataChanged: if self.isDataChanged:
msgBox = QMessageBox()
msgBox.setIcon(QMessageBox.Question)
msgBox.setWindowTitle('Завершение работы')
file = '' file = ''
if self.fileModel.file_name: if self.fileModel.file_name:
file = '\nФайл сохранения:' + self.fileModel.file_name file = '\nФайл сохранения:' + self.fileModel.file_name
msgBox.setText("Сохранить данные?" + file) msgBox = self.createMessageBox \
('Завершение работы',
"Сохранить данные?" + file,
QMessageBox.Question,
QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel,
QMessageBox.Cancel)
msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel)
msgBox.setDefaultButton(QMessageBox.Cancel)
yesButton = msgBox.button(QMessageBox.Yes)
yesButton.setText("Да")
noButton = msgBox.button(QMessageBox.No)
noButton.setText("Нет")
cancelButton = msgBox.button(QMessageBox.Cancel)
cancelButton.setText("Отмена")
QApplication.beep()
reply = msgBox.exec_() reply = msgBox.exec_()
if reply == QMessageBox.StandardButton.Yes: if reply == QMessageBox.StandardButton.Yes:
self.fileModel.saveFile(self.model.getData()) self.fileModel.saveFile(self.model.getData())

View File

@ -1,16 +1,14 @@
import numpy as np import numpy as np
from PySide2.QtWidgets import QFileDialog from PySide2.QtWidgets import QFileDialog, QMessageBox
from PySide2 import QtCore
class FileSLCModel(QtCore.QAbstractTableModel):
class FileSLCModel:
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.file_name = None self.file_name = None
def saveFile(self, data): def saveFile(self, data):
if self.file_name is None: if self.file_name is None:
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
self.file_name, _ = QFileDialog.getSaveFileName(None, "Сохранить файл", "", "Text Files (*.txt);;CSV Files (*.csv)") self.file_name, _ = QFileDialog.getSaveFileName(None, "Сохранить файл", "", "Text Files (*.txt);;CSV Files (*.csv)")
if self.file_name: if self.file_name:
np.savetxt(self.file_name, data, delimiter=",") np.savetxt(self.file_name, data, delimiter=",")
@ -18,11 +16,16 @@ class FileSLCModel(QtCore.QAbstractTableModel):
return False return False
def loadFile(self): def loadFile(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
self.file_name, _ = QFileDialog.getOpenFileName(None, "Загрузить файл", "", "Files (*.txt;*.csv)") self.file_name, _ = QFileDialog.getOpenFileName(None, "Загрузить файл", "", "Files (*.txt;*.csv)")
if self.file_name: if self.file_name:
content = np.genfromtxt(self.file_name, delimiter=',') try:
content = np.genfromtxt(self.file_name, delimiter=',', invalid_raise=True)
except ValueError as e:
QMessageBox.warning \
(None,
'Ошибка',
"Ошибка чтения файла!\nФайл нельзя открыть или файл неверного формата")
return None
return content return content
def closeFile(self): def closeFile(self):