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/linboosselectionrow.cpp
Dorian Zedler c01d1b884d
- added some fancy animations to the start action buttons
- Made everything depend on the LinboBackend state
2020-11-23 11:57:51 +01:00

94 lines
3.2 KiB
C++

#include "../headers/linboosselectionrow.h"
LinboOsSelectionRow::LinboOsSelectionRow(LinboBackend* backend, QWidget *parent) : QWidget(parent)
{
this->backend = backend;
connect(this->backend, SIGNAL(stateChanged(LinboBackend::LinboState)), this, SLOT(handleLinboStateChanged(LinboBackend::LinboState)));
this->showOnlySelectedButton = false;
this->osButtonGroup = new QButtonGroup();
this->osButtonGroup->setExclusive(true);
for(LinboOs* os : backend->getOperatingSystems()) {
LinboOsSelectButton* osButton = new LinboOsSelectButton("/icons/" + os->getIconName(), os, this->osButtonGroup, this);
connect(osButton->button, SIGNAL(toggled(bool)), this, SLOT(handleButtonToggled(bool)));
// auto select current OS
if(this->backend->getCurrentOs() == os)
osButton->button->setChecked(true);
this->osButtons.append(osButton);
}
}
void LinboOsSelectionRow::resizeAndPositionAllButtons() {
int buttonWidth = this->width() / this->osButtonGroup->buttons().length();
if(this->showOnlySelectedButton) {
for(int i = 0; i < this->osButtons.length(); i++) {
if(!this->osButtons[i]->button->isChecked())
this->osButtons[i]->hide();
else {
QPropertyAnimation* moveAnimation = new QPropertyAnimation(this->osButtons[i], "geometry");
moveAnimation->setEasingCurve(QEasingCurve::InOutQuad);
moveAnimation->setDuration(400);
moveAnimation->setStartValue(this->osButtons[i]->geometry());
moveAnimation->setEndValue(QRect((this->width() - buttonWidth) / 2, 0, buttonWidth, this->height()));
moveAnimation->start();
}
}
}
else {
for(int i = 0; i < this->osButtons.length(); i++) {
this->osButtons[i]->setGeometry(buttonWidth * i, 0, buttonWidth, this->height());
}
}
}
void LinboOsSelectionRow::handleButtonToggled(bool checked) {
if(checked)
this->backend->setCurrentOs(this->getSelectedOs());
}
LinboOs* LinboOsSelectionRow::getSelectedOs() {
for(LinboOsSelectButton* button : this->osButtons) {
if(button->button->isChecked())
return button->getOs();
}
return nullptr;
}
void LinboOsSelectionRow::setShowOnlySelectedButton(bool value) {
// find selected button
// set its x so it is in the middle (animated)
// set Opacity of all other buttons to 0 (animated)
if(value == this->showOnlySelectedButton)
return;
this->showOnlySelectedButton = value;
this->resizeAndPositionAllButtons();
}
void LinboOsSelectionRow::resizeEvent(QResizeEvent *event) {
QWidget::resizeEvent(event);
//qDebug() << "RESIZE EVENT: width: " << width() << " height: " << height();
this->resizeAndPositionAllButtons();
}
void LinboOsSelectionRow::handleLinboStateChanged(LinboBackend::LinboState newState) {
switch (newState) {
case LinboBackend::Idle:
this->setShowOnlySelectedButton(false);
break;
case LinboBackend::Starting:
case LinboBackend::Syncing:
case LinboBackend::Reinstalling:
this->setShowOnlySelectedButton(true);
break;
default:
break;
}
}