fix: создание новой вкладки для MainWindow при появлении CSV файла теперь работает
This commit is contained in:
parent
8f5a65405d
commit
844aa4a78a
Binary file not shown.
@ -1,3 +1,3 @@
|
||||
from .plot_window import PlotWindow
|
||||
from .plot_window import Plotter
|
||||
from .settings_window import settingsWindow
|
||||
from .app import app
|
||||
from .app import graphWindow
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -3,15 +3,17 @@ import pyqtgraph as pg
|
||||
from src.utils import read_json, DiagramParser
|
||||
from src.uml import Request, UMLCreator
|
||||
from src.OptAlgorithm import OptAlgorithm
|
||||
from src.gui import PlotWindow, settingsWindow
|
||||
from src.gui import Plotter, settingsWindow
|
||||
|
||||
|
||||
class app:
|
||||
def __init__(self, path: str):
|
||||
class graphWindow:
|
||||
def __init__(self, path = None):
|
||||
self.path = path
|
||||
self._load_preset()
|
||||
|
||||
|
||||
def get_widget(self):
|
||||
return self.plotter.widget
|
||||
|
||||
def _get_ideal_timings(self, opt: OptAlgorithm) -> list[float]:
|
||||
data = opt.Ts
|
||||
ideal_time = [data['tclose'], data['tgrow'], opt.getMarkOpen(), data["tmovement"]]
|
||||
@ -21,52 +23,32 @@ class app:
|
||||
self.operator_params = read_json("params/operator_params.json")
|
||||
self.system_params = read_json("params/system_params.json")
|
||||
|
||||
self.parser = DiagramParser(system_config=self.system_params)
|
||||
self.parser.setData(self.path)
|
||||
#trace_samples/2024_11_08-19_30_52.csv
|
||||
|
||||
|
||||
self.opt_algorithm = OptAlgorithm(operator_config=self.operator_params, system_config=self.system_params)
|
||||
generator = Request(server_url='http://www.plantuml.com/plantuml/svg/')
|
||||
self.uml_creator = UMLCreator(request_generator=generator)
|
||||
self.operSettings = settingsWindow("params\operator_params.json", 'Operator', self._update_settings)
|
||||
self.sysSettings = settingsWindow("params\system_params.json", 'System', self._update_settings)
|
||||
self.plotter = PlotWindow(opt =self.opt_algorithm,
|
||||
show_settings_func=self._show_settings)
|
||||
self.uml_creator = UMLCreator(request_generator=generator)
|
||||
|
||||
self.ideal_times = self._get_ideal_timings(self.opt_algorithm)
|
||||
|
||||
self.plotter = Plotter(show_settings_func=self._show_settings)
|
||||
|
||||
self.parser = DiagramParser(system_config=self.system_params)
|
||||
self.parser.setData(self.path)
|
||||
|
||||
self.bool_dict = self.parser.getBoolDict()
|
||||
self.float_dict = self.parser.getFloatDict()
|
||||
self.timings_dict = self.parser.getRealTimings()
|
||||
self.mode = self.parser.getMode()
|
||||
|
||||
self.uml_creator.update_uml(operator_config=self.operator_params,
|
||||
system_config=self.system_params,
|
||||
ideal_time=self.ideal_times,
|
||||
bool_dict=self.bool_dict,
|
||||
float_dict=self.float_dict,
|
||||
mode = self.mode,
|
||||
timings_dict=self.timings_dict)
|
||||
|
||||
self.plotter.update_data(operator_config=self.operator_params,
|
||||
system_config=self.system_params,
|
||||
opt=self.opt_algorithm,
|
||||
ideal_time=self.ideal_times,
|
||||
bool_dict=self.bool_dict,
|
||||
float_dict=self.float_dict,
|
||||
mode = self.mode,
|
||||
timings_dict=self.timings_dict)
|
||||
self._update_settings()
|
||||
|
||||
|
||||
def _update_settings(self, _):
|
||||
def _update_settings(self, _ = None):
|
||||
self.operator_params = self.operSettings.getParams()
|
||||
self.system_params = self.sysSettings.getParams()
|
||||
|
||||
self.opt_algorithm = OptAlgorithm(operator_config=self.operator_params, system_config=self.system_params)
|
||||
self.ideal_times = self._get_ideal_timings(self.opt_algorithm)
|
||||
|
||||
self.uml_creator.update_uml(operator_config=self.system_params,
|
||||
self.uml_creator.update_uml(operator_config=self.operator_params,
|
||||
system_config=self.system_params,
|
||||
ideal_time=self.ideal_times,
|
||||
bool_dict=self.bool_dict,
|
||||
float_dict=self.float_dict,
|
||||
@ -89,5 +71,5 @@ class app:
|
||||
|
||||
if __name__ == '__main__':
|
||||
pg.mkQApp("Plotting")
|
||||
temp = app()
|
||||
temp = graphWindow("trace_samples/2024_11_08-19_30_49.csv")
|
||||
pg.exec()
|
@ -7,11 +7,10 @@ from src.gui import qt_settings as qts
|
||||
from src.OptAlgorithm import OptAlgorithm
|
||||
|
||||
|
||||
class PlotWindow:
|
||||
def __init__(self, opt: OptAlgorithm, show_settings_func):
|
||||
class Plotter:
|
||||
def __init__(self, show_settings_func):
|
||||
pg.setConfigOptions(antialias=True)
|
||||
self.alpha = 100 #[0-255 прозрачность фона]
|
||||
self.opt = opt
|
||||
self._init_ui()
|
||||
self.settings_button.clicked.connect(show_settings_func)
|
||||
|
||||
@ -70,7 +69,6 @@ class PlotWindow:
|
||||
self.p12.setAutoVisible(x=False, y=True)
|
||||
self.p13.setAutoVisible(x=False, y=True)
|
||||
self.widget.setStyleSheet(qts.dark_style)
|
||||
self.widget.show()
|
||||
|
||||
def _init_graph(self, title, Yname, Yunits, Xname, Xunits):
|
||||
plot = self.win.addPlot(title = title)
|
||||
|
147
src/main.py
147
src/main.py
@ -1,80 +1,105 @@
|
||||
import pyqtgraph as pg
|
||||
|
||||
from src.gui import app
|
||||
|
||||
import sys
|
||||
import os
|
||||
import time
|
||||
from PyQt5.QtCore import QThread, pyqtSignal, QObject
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
from PyQt5.QtWidgets import (
|
||||
QApplication,
|
||||
QMainWindow,
|
||||
QTabWidget,
|
||||
QWidget,
|
||||
QVBoxLayout,
|
||||
QLabel,
|
||||
)
|
||||
from PyQt5.QtCore import Qt, QThread, pyqtSignal, QObject, QFileSystemWatcher
|
||||
from PyQt5.QtGui import QIcon
|
||||
|
||||
def main():
|
||||
pg.mkQApp()
|
||||
apps = appHandler
|
||||
directory_path = "D:/downloads/a22"
|
||||
|
||||
file_watcher_thread = FileWatcherThread(directory_path)
|
||||
file_watcher_thread.worker.file_detected.connect(get)
|
||||
file_watcher_thread.start()
|
||||
|
||||
pg.exec()
|
||||
#temp = app()
|
||||
|
||||
def get(path):
|
||||
time.sleep(1)
|
||||
temp = app(path)
|
||||
|
||||
class appHandler:
|
||||
def __init__(self):
|
||||
self.appTemp = []
|
||||
|
||||
def makeApp(self, result):
|
||||
self.appTemp.append(app(result))
|
||||
# Импортируйте ваш класс `app` здесь
|
||||
# Предполагается, что класс `app` предоставляет интерфейс для отображения данных
|
||||
# Если `app` не предоставляет виджет, возможно, потребуется его модифицировать
|
||||
from src.gui.app import graphWindow
|
||||
|
||||
|
||||
class FileWatcherWorker(QObject):
|
||||
file_detected = pyqtSignal(str) # Сигнал для передачи пути к новому файлу
|
||||
class DirectoryWatcher(QObject):
|
||||
|
||||
file_created = pyqtSignal(str)
|
||||
|
||||
def __init__(self, directory_path):
|
||||
super().__init__()
|
||||
self.directory_path = directory_path
|
||||
self.observer = Observer()
|
||||
self._running = True
|
||||
self.watcher = QFileSystemWatcher()
|
||||
self.watcher.addPath(self.directory_path)
|
||||
self.existing_files = set(os.listdir(self.directory_path))
|
||||
self.watcher.directoryChanged.connect(self.on_directory_changed)
|
||||
|
||||
def start_watching(self):
|
||||
# Настраиваем наблюдателя и запускаем его
|
||||
event_handler = CSVFileHandler(self.file_detected)
|
||||
self.observer.schedule(event_handler, self.directory_path, recursive=False)
|
||||
self.observer.start()
|
||||
def on_directory_changed(self, _):
|
||||
try:
|
||||
current_files = set(os.listdir(self.directory_path))
|
||||
new_files = current_files - self.existing_files
|
||||
self.existing_files = current_files
|
||||
for file in new_files:
|
||||
if file.lower().endswith(".csv"):
|
||||
full_path = os.path.join(self.directory_path, file)
|
||||
self.file_created.emit(full_path)
|
||||
except Exception as e:
|
||||
print(f"Ошибка при обработке изменений директории: {e}")
|
||||
|
||||
def stop_watching(self):
|
||||
self._running = False
|
||||
self.observer.stop()
|
||||
self.observer.join()
|
||||
|
||||
class CSVFileHandler(FileSystemEventHandler):
|
||||
def __init__(self, file_detected_signal):
|
||||
class MainWindow(QMainWindow):
|
||||
def __init__(self, directory_to_watch):
|
||||
super().__init__()
|
||||
self.file_detected_signal = file_detected_signal
|
||||
self.setWindowTitle("Мониторинг CSV-файлов")
|
||||
self.setGeometry(100, 100, 800, 600)
|
||||
|
||||
def on_created(self, event):
|
||||
# Проверяем, что это файл и что он имеет расширение .csv
|
||||
if not event.is_directory and event.src_path.endswith(".csv"):
|
||||
abs_path = os.path.abspath(event.src_path)
|
||||
print(f"Найден новый CSV файл: {abs_path}")
|
||||
self.file_detected_signal.emit(abs_path) # Испускаем сигнал с путем к файлу
|
||||
self.tabs = QTabWidget()
|
||||
self.setCentralWidget(self.tabs)
|
||||
|
||||
class FileWatcherThread(QThread):
|
||||
def __init__(self, directory_path):
|
||||
super().__init__()
|
||||
self.worker = FileWatcherWorker(directory_path)
|
||||
# self.setWindowIcon(QIcon("path_to_icon.png"))
|
||||
self.start_directory_watcher(directory_to_watch)
|
||||
|
||||
def run(self):
|
||||
self.worker.start_watching() # Запуск наблюдателя
|
||||
self.exec_() # Запускаем событийный цикл в потоке
|
||||
def start_directory_watcher(self, directory_path):
|
||||
self.watcher_thread = QThread()
|
||||
self.watcher = DirectoryWatcher(directory_path)
|
||||
self.watcher.moveToThread(self.watcher_thread)
|
||||
|
||||
def stop(self):
|
||||
self.worker.stop_watching() # Остановка наблюдателя
|
||||
self.watcher.file_created.connect(self.handle_new_file)
|
||||
self.watcher_thread.started.connect(self.watcher.on_directory_changed)
|
||||
|
||||
if __name__ == '__main__':
|
||||
self.watcher_thread.start()
|
||||
|
||||
def handle_new_file(self, file_path):
|
||||
file_name = os.path.basename(file_path)
|
||||
|
||||
app_instance = graphWindow(path=file_path)
|
||||
|
||||
tab = QWidget()
|
||||
layout = QVBoxLayout()
|
||||
app_widget = app_instance.get_widget()
|
||||
layout.addWidget(app_widget)
|
||||
|
||||
label = QLabel(f"{file_name}")
|
||||
label.setAlignment(Qt.AlignCenter)
|
||||
layout.addWidget(label)
|
||||
tab.setLayout(layout)
|
||||
|
||||
self.tabs.addTab(tab, file_name)
|
||||
|
||||
def closeEvent(self, event):
|
||||
self.watcher_thread.quit()
|
||||
self.watcher_thread.wait()
|
||||
event.accept()
|
||||
|
||||
|
||||
def main():
|
||||
directory_to_watch = "D:/downloads/a22"
|
||||
|
||||
if not os.path.isdir(directory_to_watch):
|
||||
print(f"Директория не найдена: {directory_to_watch}")
|
||||
sys.exit(1)
|
||||
|
||||
app_instance = QApplication(sys.argv)
|
||||
window = MainWindow(directory_to_watch)
|
||||
window.show()
|
||||
sys.exit(app_instance.exec_())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user