feat(router): Implement route computation job with progress dualog
This commit is contained in:
parent
baa6785e99
commit
2c000daae1
1 changed files with 68 additions and 21 deletions
|
@ -11,10 +11,10 @@ from urllib.request import Request, urlopen
|
|||
|
||||
import _ed_lrr
|
||||
import ed_lrr_gui
|
||||
import ed_lrr_gui.config as cfg
|
||||
import requests as RQ
|
||||
from ed_lrr_gui import Preprocessor, Router
|
||||
from ed_lrr_gui import Preprocessor, Router, cfg
|
||||
from ed_lrr_gui.gui.ed_lrr import Ui_ED_LRR
|
||||
from ed_lrr_gui.gui.widget_route import Ui_Route
|
||||
from PyQt5.QtCore import QObject, Qt, QThread, QTimer, pyqtSignal
|
||||
from PyQt5.QtGui import QColor, QPalette,QIcon
|
||||
from PyQt5.QtWidgets import (
|
||||
|
@ -25,9 +25,17 @@ from PyQt5.QtWidgets import (
|
|||
QMessageBox,
|
||||
QProgressDialog,
|
||||
QTreeWidgetItem,
|
||||
QLabel,
|
||||
QDialog
|
||||
)
|
||||
|
||||
|
||||
class ProgressDialog(QProgressDialog):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.setWindowModality(Qt.WindowModal)
|
||||
|
||||
|
||||
def sizeof_fmt(num, suffix="B"):
|
||||
for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]:
|
||||
if abs(num) < 1024.0:
|
||||
|
@ -40,12 +48,6 @@ def t_round(dt):
|
|||
return dt - dt % timedelta(seconds=1)
|
||||
|
||||
|
||||
class ProgressDialog(QProgressDialog):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.setWindowModality(Qt.WindowModal)
|
||||
|
||||
|
||||
class Job(QObject):
|
||||
progress = pyqtSignal("PyQt_PyObject")
|
||||
|
||||
|
@ -59,13 +61,14 @@ class Job(QObject):
|
|||
self.timer.start(100)
|
||||
self.last_val = None
|
||||
self.progress_dialog = None
|
||||
self.handle_progess = None
|
||||
self.state = {}
|
||||
self.setup_progress(self.handle_progess)
|
||||
|
||||
def setup_progress(self, handle_progess):
|
||||
self.progress.connect(
|
||||
lambda *args, **kwargs: handle_progess(self, *args, **kwargs)
|
||||
)
|
||||
self.progress.connect(lambda *args, **kwargs: handle_progess(*args, **kwargs))
|
||||
|
||||
def handle_progess(self, *args, **kwargs):
|
||||
raise NotImplementedError
|
||||
|
||||
def start(self):
|
||||
if self.progress_dialog is None:
|
||||
|
@ -83,7 +86,7 @@ class Job(QObject):
|
|||
return (self.job.is_alive() == False) and (self.job.queue.empty())
|
||||
|
||||
def interval(self):
|
||||
while True:
|
||||
while self.job:
|
||||
try:
|
||||
res = self.job.queue.get(True, 0.1)
|
||||
except queue.Empty:
|
||||
|
@ -95,6 +98,39 @@ class Job(QObject):
|
|||
self.last_val = res
|
||||
|
||||
|
||||
class RouterJob(Job):
|
||||
def __init__(self, app, main_window, *args, **kwargs):
|
||||
super().__init__(app, main_window, Router, *args, **kwargs)
|
||||
self.progress_dialog = ProgressDialog("", "Cancel", 0, 0, self.main_window)
|
||||
self.progress_dialog.setAutoClose(False)
|
||||
self.progress_dialog.setLabelText("Loading...")
|
||||
self.progress_dialog.setWindowTitle("Loading...")
|
||||
self.progress_dialog.canceled.connect(self.cancel)
|
||||
self.progress_dialog.show()
|
||||
self.start()
|
||||
|
||||
def done(self,result):
|
||||
return
|
||||
|
||||
def handle_progess(self, state):
|
||||
if state.get('return') is not None:
|
||||
self.done(state['return'])
|
||||
self.progress_dialog.close()
|
||||
WRoute(state['return'])
|
||||
return
|
||||
msg = "Depth: {depth}\nBody: {body}\nQueued: {queue_size}\nDistance remaining: {d_rem:.2f} Ly".format(
|
||||
**state["status"]
|
||||
)
|
||||
title = "[{prc_done:.2f}%] Plotting route from [{from}] to [{to}]".format(
|
||||
**state["status"]
|
||||
)
|
||||
self.progress_dialog.setMinimum(0)
|
||||
self.progress_dialog.setMaximum(100 * 100)
|
||||
self.progress_dialog.setWindowTitle(title)
|
||||
self.progress_dialog.setLabelText(msg)
|
||||
self.progress_dialog.setValue(int(state["status"]["prc_done"] * 100))
|
||||
|
||||
|
||||
class DownloadThread(QThread):
|
||||
progress = pyqtSignal("PyQt_PyObject")
|
||||
|
||||
|
@ -311,9 +347,10 @@ class ED_LRR(Ui_ED_LRR):
|
|||
def route_progress(self, job, state):
|
||||
print("RP:", job, state)
|
||||
|
||||
def run(self):
|
||||
if not all(s["Type"] for s in self.systems):
|
||||
self.error('Not all systens have been resolved, please click "Search All"')
|
||||
def compute_route(self):
|
||||
num_resolved=self.resolve_systems()
|
||||
if num_resolved:
|
||||
if QMessageBox.question(self.main_window,"ED_LRR","Resolved {} systems, are the names correct?".format(num_resolved))==QMessageBox.No:
|
||||
return
|
||||
print(self.systems)
|
||||
systems = [str(s["id"]) for s in self.systems]
|
||||
|
@ -349,11 +386,13 @@ class ED_LRR(Ui_ED_LRR):
|
|||
path,
|
||||
precomp,
|
||||
)
|
||||
route_job = self.new_job(
|
||||
Router,
|
||||
if self.current_job is None:
|
||||
self.current_job = RouterJob(
|
||||
self.app,
|
||||
self.main_window,
|
||||
systems,
|
||||
jump_range,
|
||||
0.1,
|
||||
None,
|
||||
mode,
|
||||
primary,
|
||||
permute,
|
||||
|
@ -385,16 +424,24 @@ class ED_LRR(Ui_ED_LRR):
|
|||
def resolve_systems(self):
|
||||
# TODO: show spinner
|
||||
names = []
|
||||
nums=[]
|
||||
for n in range(self.lst_sys.topLevelItemCount()):
|
||||
sys_id = getattr(self.lst_sys.topLevelItem(n), "__id__", None)
|
||||
if sys_id is not None:
|
||||
continue
|
||||
names.append(self.sys_to_dict(n)["Name"])
|
||||
nums.append(n)
|
||||
if not names:
|
||||
return 0
|
||||
systems = self.find_sys_by_names(names)
|
||||
if systems is None:
|
||||
return
|
||||
for i, name in enumerate(names):
|
||||
for i, name in zip(nums,names):
|
||||
_, system = systems[name]
|
||||
self.lst_sys.topLevelItem(i).setData(0, 0, system["system"])
|
||||
self.lst_sys.topLevelItem(i).setData(1, 0, system["star_type"])
|
||||
self.lst_sys.topLevelItem(i).__id__ = system["id"]
|
||||
return len(names)
|
||||
# diff, item = self.find_sys_by_name(name)
|
||||
# print("Found", (diff, item))
|
||||
|
||||
|
@ -479,7 +526,7 @@ class ED_LRR(Ui_ED_LRR):
|
|||
self.rd_comp.toggled.connect(self.set_comp_mode)
|
||||
self.rd_precomp.toggled.connect(self.set_comp_mode)
|
||||
self.sld_greedyness.valueChanged.connect(self.set_greedyness)
|
||||
self.btn_go.clicked.connect(self.run)
|
||||
self.btn_go.clicked.connect(self.compute_route)
|
||||
self.btn_add.clicked.connect(self.add_system)
|
||||
self.btn_rm.clicked.connect(self.remove_system)
|
||||
self.chk_permute.stateChanged.connect(self.update_permute_chk)
|
||||
|
|
Loading…
Reference in a new issue