This repository has been archived on 2022-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
modern-linbo-gui/sources/linbostartactions.cpp
Dorian Zedler 06dd8c4be3
- added log text
- started to implement autostart
2020-11-24 20:25:39 +01:00

206 lines
7.2 KiB
C++

#include "../headers/linbostartactions.h"
LinboStartActions::LinboStartActions(LinboBackend* backend, QWidget *parent) : QWidget(parent)
{
this->backend = backend;
connect(this->backend, SIGNAL(currentOsChanged(LinboOs*)), this, SLOT(handleCurrentOsChanged(LinboOs*)));
connect(this->backend, SIGNAL(stateChanged(LinboBackend::LinboState)), this, SLOT(handleLinboStateChanged(LinboBackend::LinboState)));
connect(this->backend->getLogger(), SIGNAL(latestLogChanged(const LinboLogger::LinboLog&)), this, SLOT(handleLatestLogChanged(const LinboLogger::LinboLog&)));
this->stackView = new QModernStackedWidget(this);
this->inited = false;
// Action Buttons
this->buttonWidget = new QWidget();
this->startOsButton = new QModernPushButton(":/svgIcons/startAction.svg", this->buttonWidget);
connect(this->startOsButton, SIGNAL(clicked()), this->backend, SLOT(startCurrentOs()));
this->syncOsButton = new QModernPushButton(":/svgIcons/syncAction.svg", this->buttonWidget);
connect(this->syncOsButton, SIGNAL(clicked()), this->backend, SLOT(syncCurrentOs()));
this->reinstallOsButton = new QModernPushButton(":/svgIcons/resetAction.svg", this->buttonWidget);
connect(this->reinstallOsButton, SIGNAL(clicked()), this->backend, SLOT(reinstallCurrentOs()));
this->stackView->addWidget(this->buttonWidget);
// Progress bar
this->progressBarWidget = new QWidget();
this->progressBar = new QModernProgressBar(this->progressBarWidget);
this->progressBar->setIndeterminate(true);
this->logLabel = new QLabel("", this->progressBarWidget);
this->logLabel->setAlignment(Qt::AlignCenter);
this->logFont = QFont("Segoe UI");
this->logLabel->setFont(this->logFont);
this->stackView->addWidget(this->progressBarWidget);
connect(this->stackView, SIGNAL(currentChanged(int)), this, SLOT(resizeAndPositionAllItems()));
this->stackView->setCurrentWidget(this->buttonWidget);
}
void LinboStartActions::resizeAndPositionAllItems() {
// stack view
this->stackView->setFixedSize(this->size());
// Action buttons
// bring buttons in correct order:
LinboOs* selectedOs = this->backend->getCurrentOs();
LinboOs::LinboOsStartAction defaultAction = LinboOs::UnknownAction;
if(selectedOs != nullptr)
defaultAction = selectedOs->getDefaultAction();
int syncOsPosition = 2;
int startOsPosition = 0;
int reinstallOsPosition = 1;
switch (defaultAction) {
case LinboOs::StartOs:
break;
case LinboOs::SyncOs:
syncOsPosition = 0;
startOsPosition = 1;
reinstallOsPosition = 2;
break;
case LinboOs::ReinstallOs:
syncOsPosition = 1;
startOsPosition = 2;
reinstallOsPosition = 0;
break;
default:
break;
}
while (this->actionButtons.length() < 3)
this->actionButtons.append(nullptr);
this->actionButtons[startOsPosition] = this->startOsButton;
this->actionButtons[syncOsPosition] = this->syncOsButton;
this->actionButtons[reinstallOsPosition] = this->reinstallOsButton;
// check for disabled actions
QList<bool> positionsEnabled;
while(positionsEnabled.length() < 3)
positionsEnabled.append(false);
if(selectedOs != nullptr) {
positionsEnabled[startOsPosition] = selectedOs->getStartbutton();
positionsEnabled[syncOsPosition] = selectedOs->getSyncbutton();
positionsEnabled[reinstallOsPosition] = selectedOs->getNewbutton();
}
QList<QRect> geometries;
while (geometries.length() < 3)
geometries.append(QRect());
// move buttons into place
this->buttonWidget->setFixedSize(this->size());
int buttonSpacing = this->height() * 0.1;
int defaultButtonHeight = this->height() * 0.6;
geometries[0] = QRect((this->width() - defaultButtonHeight) / 2, 0,defaultButtonHeight, defaultButtonHeight);
int secondaryButtonHeight = this->height() * 0.3;
if(positionsEnabled[1] && positionsEnabled[2]) {
// place buttons besides each other
geometries[1] = QRect(
this->width() / 2 - secondaryButtonHeight - buttonSpacing / 2,
defaultButtonHeight + buttonSpacing,
secondaryButtonHeight,
secondaryButtonHeight
);
geometries[2] = QRect(
this->width() / 2 + buttonSpacing / 2,
defaultButtonHeight + buttonSpacing,
secondaryButtonHeight,
secondaryButtonHeight
);
}
else {
// place buttons on top of each other
geometries[1] = QRect(
this->width() / 2 - secondaryButtonHeight / 2,
defaultButtonHeight + buttonSpacing,
secondaryButtonHeight,
secondaryButtonHeight
);
geometries[2] = geometries[1];
}
for(int i = 0; i < this->actionButtons.length(); i++) {
if(this->inited) {
this->actionButtons[i]->setVisibleAnimated(positionsEnabled[i]);
this->actionButtons[i]->setGeometryAnimated(geometries[i]);
}
else {
// don't animate the first time
this->actionButtons[i]->setVisible(positionsEnabled[i]);
this->actionButtons[i]->setGeometry(geometries[i]);
}
}
// Progress bar
this->progressBarWidget->setFixedSize(this->size());
int progressBarHeight = this->height() * 0.1;
int progressBarWidth = this->width() * 0.5;
int logLabelHeight = progressBarHeight * 2;
int logLabelWidth = this->width() * 0.8;
progressBar->setGeometry((this->width() - progressBarWidth) / 2, (this->height() - logLabelHeight - progressBarHeight) / 2, progressBarWidth, progressBarHeight);
this->logFont.setPixelSize(logLabelHeight * 0.8);
this->logLabel->setFont(this->logFont);
this->logLabel->setGeometry((this->width() - logLabelWidth) / 2, this->height() - logLabelHeight, logLabelWidth, logLabelHeight);
this->inited = true;
}
void LinboStartActions::resizeEvent(QResizeEvent *event) {
this->resizeAndPositionAllItems();
QWidget::resizeEvent(event);
}
void LinboStartActions::handleCurrentOsChanged(LinboOs* newOs) {
Q_UNUSED(newOs)
this->resizeAndPositionAllItems();
}
void LinboStartActions::handleLinboStateChanged(LinboBackend::LinboState newState) {
switch (newState) {
case LinboBackend::Idle:
this->stackView->setCurrentWidgetAnimated(this->buttonWidget);
break;
case LinboBackend::Starting:
case LinboBackend::Syncing:
case LinboBackend::Reinstalling:
this->stackView->setCurrentWidgetAnimated(this->progressBarWidget);
break;
default:
break;
}
}
void LinboStartActions::handleLatestLogChanged(const LinboLogger::LinboLog& latestLog) {
QString logColor = "black";
switch (latestLog.type) {
case LinboLogger::StdErr:
logColor = this->backend->getConfig()->getConsoleFontcolorStderr();
break;
case LinboLogger::StdOut:
// TODO?? logColor = this->backend->getConfig()->getConsoleFontcolorStdout();
break;
default:
break;
}
this->logLabel->setStyleSheet("QLabel { color : " + logColor + "; }");
this->logLabel->setText(latestLog.message);
}