File indexing completed on 2024-05-12 04:41:12

0001 /* AtCore KDE Libary for 3D Printers
0002     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0003     SPDX-FileCopyrightText: 2018, 2020 Chris Rizzitello <rizzitello@kde.org>
0004     SPDX-FileCopyrightText: 2018 Patrick José Pereira <patrickjp@kde.org>
0005 */
0006 
0007 #include <QElapsedTimer>
0008 #include <QHBoxLayout>
0009 #include <QLabel>
0010 #include <QProgressBar>
0011 #include <QPushButton>
0012 #include <QStyle>
0013 #include <QTime>
0014 #include <QTimer>
0015 
0016 #include "statuswidget.h"
0017 
0018 StatusWidget::StatusWidget(bool showStop, QWidget *parent)
0019     : QWidget(parent)
0020 {
0021     // first create the item for the print Progress.
0022     auto hBoxLayout = new QHBoxLayout;
0023 
0024     printingProgress = new QProgressBar(this);
0025     printingProgress->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
0026     hBoxLayout->addWidget(printingProgress);
0027 
0028     if (showStop) {
0029         auto newButton = new QPushButton(style()->standardIcon(QStyle::SP_BrowserStop), QString(), this);
0030         connect(newButton, &QPushButton::clicked, this, [this] { Q_EMIT stopPressed(); });
0031         hBoxLayout->addWidget(newButton);
0032     }
0033 
0034     lblTime = new QLabel(QStringLiteral("00:00:00"), this);
0035     lblTime->setAlignment(Qt::AlignHCenter);
0036     auto newLabel = new QLabel(QStringLiteral(" / "), this);
0037     lblTimeLeft = new QLabel(QStringLiteral("??:??:??"), this);
0038     lblTimeLeft->setAlignment(Qt::AlignHCenter);
0039 
0040     hBoxLayout->addWidget(lblTime);
0041     hBoxLayout->addWidget(newLabel);
0042     hBoxLayout->addWidget(lblTimeLeft);
0043     printProgressWidget = new QWidget(this);
0044     printProgressWidget->setLayout(hBoxLayout);
0045 
0046     // Then Create the full bar.
0047     newLabel = new QLabel(tr("AtCore State:"), this);
0048     lblState = new QLabel(tr("Not Connected"), this);
0049     lblSd = new QLabel(this);
0050 
0051     spacer = new QSpacerItem(10, 20, QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
0052 
0053     hBoxLayout = new QHBoxLayout;
0054     hBoxLayout->addWidget(newLabel);
0055     hBoxLayout->addWidget(lblState);
0056     hBoxLayout->addSpacerItem(new QSpacerItem(5, 20, QSizePolicy::Fixed));
0057     hBoxLayout->addWidget(lblSd);
0058     hBoxLayout->addSpacerItem(spacer);
0059     hBoxLayout->addWidget(printProgressWidget);
0060 
0061     setLayout(hBoxLayout);
0062 
0063     printTime = new QElapsedTimer();
0064     printTimer = new QTimer(this);
0065     printTimer->setInterval(1000);
0066     printTimer->setSingleShot(false);
0067     connect(printTimer, &QTimer::timeout, this, &StatusWidget::updatePrintTime);
0068 }
0069 
0070 void StatusWidget::setSD(bool hasSd)
0071 {
0072     QString labelText = hasSd ? tr("SD") : QString();
0073     lblSd->setText(labelText);
0074 }
0075 void StatusWidget::setState(const QString &state)
0076 {
0077     lblState->setText(state);
0078 }
0079 
0080 void StatusWidget::showPrintArea(bool visible)
0081 {
0082     printProgressWidget->setVisible(visible);
0083     if (visible) {
0084         spacer->changeSize(10, 20, QSizePolicy::Fixed, QSizePolicy::Fixed);
0085         printTime->start();
0086         printTimer->start();
0087     } else {
0088         if (printTimer->isActive()) {
0089             printTimer->stop();
0090         }
0091         spacer->changeSize(10, 20, QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
0092     }
0093 }
0094 
0095 void StatusWidget::updatePrintTime()
0096 {
0097     QTime temp(0, 0, 0, 0);
0098     lblTime->setText(temp.addMSecs(printTime->elapsed()).toString(QStringLiteral("hh:mm:ss")));
0099 }
0100 
0101 void StatusWidget::updatePrintProgress(const int progress)
0102 {
0103     printingProgress->setValue(progress);
0104     if (progress >= 1) {
0105         QTime temp(0, 0, 0, 0);
0106         lblTimeLeft->setText(temp.addMSecs( (100 - progress) * (printTime->elapsed() / progress)).toString(QStringLiteral("hh:mm:ss")));
0107     } else {
0108         lblTimeLeft->setText(QStringLiteral("??:??:??"));
0109     }
0110 }