Skip to content

Commit

Permalink
add loading window
Browse files Browse the repository at this point in the history
  • Loading branch information
LoSk-p committed Mar 18, 2024
1 parent db86755 commit 97992f2
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
## Run
```bash
pyuic5 app/gui/mainwindow.ui -o app/gui/mainwindow.py
python3 app/libelium-calibration.py
python3 app/main.py
```
Binary file added app/assets/loading.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 77 additions & 0 deletions app/loading_window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from PyQt5.QtWidgets import QLabel, QWidget, QVBoxLayout, QSpacerItem, QSizePolicy, QMainWindow
from PyQt5.QtGui import QCloseEvent, QMovie
from PyQt5.QtCore import Qt, QSize

from logger import get_logger

_LOGGER = get_logger(__name__)

class LoadingWindowManager:
def __init__(self, main_window):
self.loading_window = None
self.main_window = main_window

def show_usb_window(self):
message = "Connecting to the board..."
self._show_window(message)

def show_calibration_window(self):
message = "Wait for the board response..."
self._show_window(message)

def _show_window(self, message: str):
self.loading_window = LoadingWindow(self.main_window, message)
self.loading_window.show()

def close_window(self):
if self.loading_window is not None:
_LOGGER.debug("Close loading window")
self.loading_window.close()
self.loading_window = None

def follow_parent(self):
if self.loading_window is not None:
self.loading_window.place_on_parent()
self.raise_on_top()

def raise_on_top(self):
if self.loading_window is not None:
self.loading_window.raise_()


class LoadingWindow(QWidget):
def __init__(self, parent, message: str):
super().__init__()
self.parent_window = parent
self.setAttribute(Qt.WA_StyledBackground, True)
self.setStyleSheet("background-color: lightgrey;")
self.setFixedSize(200, 100)

layout = QVBoxLayout()
self.setLayout(layout)

label_text = QLabel(message)
label_text.setAlignment(Qt.AlignCenter)
layout.addWidget(label_text)

spacer = QSpacerItem(40, 10, QSizePolicy.Expanding, QSizePolicy.Minimum)
layout.addItem(spacer)

self.movie = QMovie("app/assets/loading.gif")
self.movie.setScaledSize(QSize(50, 50))
self.label = QLabel()
self.label.setMovie(self.movie)
self.label.setAlignment(Qt.AlignCenter)
layout.addWidget(self.label)

self.movie.start()

def showEvent(self, event):
super().showEvent(event)
_LOGGER.debug(f"Parent pos: {self.parent_window.pos()}, current pos: {self.rect().center()}, move: {self.parent_window.pos() - self.rect().center()}")
self.place_on_parent()

def place_on_parent(self):
parent_center = self.parent_window.pos() + self.parent_window.rect().center()
self.move(parent_center - self.rect().center())

20 changes: 20 additions & 0 deletions app/libelium-calibration.py → app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from workers import BoardSerial, PortDetectThread
from logger import get_logger
from gui.mainwindow import Ui_MainWindow
from loading_window import LoadingWindowManager


_LOGGER = get_logger(__name__)
Expand All @@ -34,6 +35,7 @@ def __init__(self, main_window: QtWidgets.QMainWindow):
self.button.setEnabled(False)

def _start_calibration(self):
self.main_window.loading_window_manager.show_calibration_window()
sensor_name = self.main_window.boxSensors.currentText()
solution = self.main_window.boxCalibrationSolution.currentText()
self._setup_graphics()
Expand All @@ -48,6 +50,7 @@ def _setup_graphics(self):
self.values = []

def _progress_update(self, data):
self.main_window.loading_window_manager.close_window()
_LOGGER.debug(f"Progress update: {data['step']}")
self.progress_bar.setValue(data["step"] + 1)
self._draw_graphics(data)
Expand All @@ -72,6 +75,8 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None, app=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.w = None
self.loading_window_manager = LoadingWindowManager(self)
self.board_serial = None
self.board_status = BoardStatus.Disconnected
self.current_board_type: str = SW_BOARD_TYPE
Expand Down Expand Up @@ -248,6 +253,15 @@ def _update_calibration_coeffs(self) -> None:
def _update_board_status(self, board_status: str):
self.board_status = board_status
self.dataStatus.setText(board_status)
if self.board_status == BoardStatus.Connection:
self.loading_window_manager.show_usb_window()
# self.w = AnotherWindow(self)
# self.w.show()
else:
self.loading_window_manager.close_window()
# if self.w is not None:
# self.w.close()
# self.w = None

def populate_boards(self, ports: tp.List[str]):
self.boxUSBPorts.clear()
Expand Down Expand Up @@ -285,6 +299,12 @@ def choose_port(self, port):
else:
self.radioButtonSW.setEnabled(True)
self.radioButtonSWIons.setEnabled(True)

def moveEvent(self, event):
self.loading_window_manager.follow_parent()

def mousePressEvent(self, event):
self.loading_window_manager.raise_on_top()


app = QtWidgets.QApplication(sys.argv)
Expand Down
4 changes: 2 additions & 2 deletions firmware/smart_water_with_calibration.pde
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ void setup()
auxFVMajor = Utils.readEEPROM(addressFVMajor);
auxFVMinor = Utils.readEEPROM(addressFVMajor);

delay(5000);
delay(500);

if (debug == 1) {
USB.println(cal_point_10);
Expand Down Expand Up @@ -749,7 +749,7 @@ void SesorData() {
USB.println(F("$measure"));
Water.ON();
Turbidity.ON();
delay(2000);
delay(500);

///////////////////////////////////////////
// 2. Read sensors
Expand Down

0 comments on commit 97992f2

Please sign in to comment.