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 .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.utils import read_json, DiagramParser
|
||||||
from src.uml import Request, UMLCreator
|
from src.uml import Request, UMLCreator
|
||||||
from src.OptAlgorithm import OptAlgorithm
|
from src.OptAlgorithm import OptAlgorithm
|
||||||
from src.gui import PlotWindow, settingsWindow
|
from src.gui import Plotter, settingsWindow
|
||||||
|
|
||||||
|
|
||||||
class app:
|
class graphWindow:
|
||||||
def __init__(self, path: str):
|
def __init__(self, path = None):
|
||||||
self.path = path
|
self.path = path
|
||||||
self._load_preset()
|
self._load_preset()
|
||||||
|
|
||||||
|
def get_widget(self):
|
||||||
|
return self.plotter.widget
|
||||||
|
|
||||||
def _get_ideal_timings(self, opt: OptAlgorithm) -> list[float]:
|
def _get_ideal_timings(self, opt: OptAlgorithm) -> list[float]:
|
||||||
data = opt.Ts
|
data = opt.Ts
|
||||||
ideal_time = [data['tclose'], data['tgrow'], opt.getMarkOpen(), data["tmovement"]]
|
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.operator_params = read_json("params/operator_params.json")
|
||||||
self.system_params = read_json("params/system_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/')
|
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.operSettings = settingsWindow("params\operator_params.json", 'Operator', self._update_settings)
|
||||||
self.sysSettings = settingsWindow("params\system_params.json", 'System', 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.bool_dict = self.parser.getBoolDict()
|
||||||
self.float_dict = self.parser.getFloatDict()
|
self.float_dict = self.parser.getFloatDict()
|
||||||
self.timings_dict = self.parser.getRealTimings()
|
self.timings_dict = self.parser.getRealTimings()
|
||||||
self.mode = self.parser.getMode()
|
self.mode = self.parser.getMode()
|
||||||
|
self._update_settings()
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
def _update_settings(self, _):
|
def _update_settings(self, _ = None):
|
||||||
self.operator_params = self.operSettings.getParams()
|
self.operator_params = self.operSettings.getParams()
|
||||||
self.system_params = self.sysSettings.getParams()
|
self.system_params = self.sysSettings.getParams()
|
||||||
|
|
||||||
self.opt_algorithm = OptAlgorithm(operator_config=self.operator_params, system_config=self.system_params)
|
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.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,
|
ideal_time=self.ideal_times,
|
||||||
bool_dict=self.bool_dict,
|
bool_dict=self.bool_dict,
|
||||||
float_dict=self.float_dict,
|
float_dict=self.float_dict,
|
||||||
@ -89,5 +71,5 @@ class app:
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
pg.mkQApp("Plotting")
|
pg.mkQApp("Plotting")
|
||||||
temp = app()
|
temp = graphWindow("trace_samples/2024_11_08-19_30_49.csv")
|
||||||
pg.exec()
|
pg.exec()
|
@ -7,11 +7,10 @@ from src.gui import qt_settings as qts
|
|||||||
from src.OptAlgorithm import OptAlgorithm
|
from src.OptAlgorithm import OptAlgorithm
|
||||||
|
|
||||||
|
|
||||||
class PlotWindow:
|
class Plotter:
|
||||||
def __init__(self, opt: OptAlgorithm, show_settings_func):
|
def __init__(self, show_settings_func):
|
||||||
pg.setConfigOptions(antialias=True)
|
pg.setConfigOptions(antialias=True)
|
||||||
self.alpha = 100 #[0-255 прозрачность фона]
|
self.alpha = 100 #[0-255 прозрачность фона]
|
||||||
self.opt = opt
|
|
||||||
self._init_ui()
|
self._init_ui()
|
||||||
self.settings_button.clicked.connect(show_settings_func)
|
self.settings_button.clicked.connect(show_settings_func)
|
||||||
|
|
||||||
@ -70,7 +69,6 @@ class PlotWindow:
|
|||||||
self.p12.setAutoVisible(x=False, y=True)
|
self.p12.setAutoVisible(x=False, y=True)
|
||||||
self.p13.setAutoVisible(x=False, y=True)
|
self.p13.setAutoVisible(x=False, y=True)
|
||||||
self.widget.setStyleSheet(qts.dark_style)
|
self.widget.setStyleSheet(qts.dark_style)
|
||||||
self.widget.show()
|
|
||||||
|
|
||||||
def _init_graph(self, title, Yname, Yunits, Xname, Xunits):
|
def _init_graph(self, title, Yname, Yunits, Xname, Xunits):
|
||||||
plot = self.win.addPlot(title = title)
|
plot = self.win.addPlot(title = title)
|
||||||
|
147
src/main.py
147
src/main.py
@ -1,80 +1,105 @@
|
|||||||
import pyqtgraph as pg
|
import sys
|
||||||
|
|
||||||
from src.gui import app
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
from PyQt5.QtCore import QThread, pyqtSignal, QObject
|
from PyQt5.QtWidgets import (
|
||||||
from watchdog.observers import Observer
|
QApplication,
|
||||||
from watchdog.events import FileSystemEventHandler
|
QMainWindow,
|
||||||
|
QTabWidget,
|
||||||
|
QWidget,
|
||||||
|
QVBoxLayout,
|
||||||
|
QLabel,
|
||||||
|
)
|
||||||
|
from PyQt5.QtCore import Qt, QThread, pyqtSignal, QObject, QFileSystemWatcher
|
||||||
|
from PyQt5.QtGui import QIcon
|
||||||
|
|
||||||
def main():
|
# Импортируйте ваш класс `app` здесь
|
||||||
pg.mkQApp()
|
# Предполагается, что класс `app` предоставляет интерфейс для отображения данных
|
||||||
apps = appHandler
|
# Если `app` не предоставляет виджет, возможно, потребуется его модифицировать
|
||||||
directory_path = "D:/downloads/a22"
|
from src.gui.app import graphWindow
|
||||||
|
|
||||||
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))
|
|
||||||
|
|
||||||
|
|
||||||
class FileWatcherWorker(QObject):
|
class DirectoryWatcher(QObject):
|
||||||
file_detected = pyqtSignal(str) # Сигнал для передачи пути к новому файлу
|
|
||||||
|
file_created = pyqtSignal(str)
|
||||||
|
|
||||||
def __init__(self, directory_path):
|
def __init__(self, directory_path):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.directory_path = directory_path
|
self.directory_path = directory_path
|
||||||
self.observer = Observer()
|
self.watcher = QFileSystemWatcher()
|
||||||
self._running = True
|
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):
|
def on_directory_changed(self, _):
|
||||||
# Настраиваем наблюдателя и запускаем его
|
try:
|
||||||
event_handler = CSVFileHandler(self.file_detected)
|
current_files = set(os.listdir(self.directory_path))
|
||||||
self.observer.schedule(event_handler, self.directory_path, recursive=False)
|
new_files = current_files - self.existing_files
|
||||||
self.observer.start()
|
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):
|
class MainWindow(QMainWindow):
|
||||||
def __init__(self, file_detected_signal):
|
def __init__(self, directory_to_watch):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.file_detected_signal = file_detected_signal
|
self.setWindowTitle("Мониторинг CSV-файлов")
|
||||||
|
self.setGeometry(100, 100, 800, 600)
|
||||||
|
|
||||||
def on_created(self, event):
|
self.tabs = QTabWidget()
|
||||||
# Проверяем, что это файл и что он имеет расширение .csv
|
self.setCentralWidget(self.tabs)
|
||||||
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) # Испускаем сигнал с путем к файлу
|
|
||||||
|
|
||||||
class FileWatcherThread(QThread):
|
# self.setWindowIcon(QIcon("path_to_icon.png"))
|
||||||
def __init__(self, directory_path):
|
self.start_directory_watcher(directory_to_watch)
|
||||||
super().__init__()
|
|
||||||
self.worker = FileWatcherWorker(directory_path)
|
|
||||||
|
|
||||||
def run(self):
|
def start_directory_watcher(self, directory_path):
|
||||||
self.worker.start_watching() # Запуск наблюдателя
|
self.watcher_thread = QThread()
|
||||||
self.exec_() # Запускаем событийный цикл в потоке
|
self.watcher = DirectoryWatcher(directory_path)
|
||||||
|
self.watcher.moveToThread(self.watcher_thread)
|
||||||
|
|
||||||
def stop(self):
|
self.watcher.file_created.connect(self.handle_new_file)
|
||||||
self.worker.stop_watching() # Остановка наблюдателя
|
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()
|
main()
|
||||||
|
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user