2020-11-19 14:39:32 +01:00
# include "../headers/linbostartactions.h"
2020-11-23 11:57:51 +01:00
LinboStartActions : : LinboStartActions ( LinboBackend * backend , QWidget * parent ) : QWidget ( parent )
2020-11-19 14:39:32 +01:00
{
this - > backend = backend ;
2020-11-23 11:57:51 +01:00
connect ( this - > backend , SIGNAL ( currentOsChanged ( LinboOs * ) ) , this , SLOT ( handleCurrentOsChanged ( LinboOs * ) ) ) ;
connect ( this - > backend , SIGNAL ( stateChanged ( LinboBackend : : LinboState ) ) , this , SLOT ( handleLinboStateChanged ( LinboBackend : : LinboState ) ) ) ;
2020-11-24 21:42:47 +01:00
connect ( this - > backend , SIGNAL ( autostartTimeoutProgressChanged ( ) ) , this , SLOT ( handleAutostartTimeoutProgressChanged ( ) ) ) ;
2020-11-24 20:25:39 +01:00
connect ( this - > backend - > getLogger ( ) , SIGNAL ( latestLogChanged ( const LinboLogger : : LinboLog & ) ) , this , SLOT ( handleLatestLogChanged ( const LinboLogger : : LinboLog & ) ) ) ;
2020-11-19 14:39:32 +01:00
2020-11-23 11:57:51 +01:00
this - > stackView = new QModernStackedWidget ( this ) ;
this - > inited = false ;
2020-11-19 14:39:32 +01:00
// Action Buttons
this - > buttonWidget = new QWidget ( ) ;
2020-11-23 11:57:51 +01:00
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 ) ;
2020-11-20 15:09:36 +01:00
2020-11-19 14:39:32 +01:00
// Progress bar
this - > progressBarWidget = new QWidget ( ) ;
2020-11-23 11:57:51 +01:00
this - > progressBar = new QModernProgressBar ( this - > progressBarWidget ) ;
2020-11-24 21:42:47 +01:00
this - > progressBar - > setRange ( 0 , 1000 ) ;
2020-11-23 11:57:51 +01:00
this - > progressBar - > setIndeterminate ( true ) ;
2020-11-24 20:25:39 +01:00
this - > logLabel = new QLabel ( " " , this - > progressBarWidget ) ;
this - > logLabel - > setAlignment ( Qt : : AlignCenter ) ;
this - > logFont = QFont ( " Segoe UI " ) ;
this - > logLabel - > setFont ( this - > logFont ) ;
2020-11-19 14:39:32 +01:00
this - > stackView - > addWidget ( this - > progressBarWidget ) ;
2020-11-24 21:42:47 +01:00
this - > stackView - > setCurrentWidget ( nullptr ) ;
2020-11-19 14:39:32 +01:00
2020-11-23 11:57:51 +01:00
connect ( this - > stackView , SIGNAL ( currentChanged ( int ) ) , this , SLOT ( resizeAndPositionAllItems ( ) ) ) ;
2020-11-24 21:42:47 +01:00
this - > handleLinboStateChanged ( this - > backend - > getState ( ) ) ;
2020-11-19 14:39:32 +01:00
}
void LinboStartActions : : resizeAndPositionAllItems ( ) {
// stack view
this - > stackView - > setFixedSize ( this - > size ( ) ) ;
// Action buttons
2020-11-20 15:09:36 +01:00
// bring buttons in correct order:
2020-11-23 11:57:51 +01:00
LinboOs * selectedOs = this - > backend - > getCurrentOs ( ) ;
LinboOs : : LinboOsStartAction defaultAction = LinboOs : : UnknownAction ;
if ( selectedOs ! = nullptr )
defaultAction = selectedOs - > getDefaultAction ( ) ;
2020-11-20 15:09:36 +01:00
2020-11-23 11:57:51 +01:00
int syncOsPosition = 2 ;
2020-11-20 15:09:36 +01:00
int startOsPosition = 0 ;
2020-11-23 11:57:51 +01:00
int reinstallOsPosition = 1 ;
2020-11-20 15:09:36 +01:00
switch ( defaultAction ) {
case LinboOs : : StartOs :
break ;
case LinboOs : : SyncOs :
syncOsPosition = 0 ;
startOsPosition = 1 ;
reinstallOsPosition = 2 ;
break ;
case LinboOs : : ReinstallOs :
syncOsPosition = 1 ;
startOsPosition = 2 ;
2020-11-23 11:57:51 +01:00
reinstallOsPosition = 0 ;
2020-11-20 15:09:36 +01:00
break ;
default :
break ;
}
2020-11-23 11:57:51 +01:00
while ( this - > actionButtons . length ( ) < 3 )
2020-11-20 15:09:36 +01:00
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 ;
2020-11-23 11:57:51 +01:00
while ( positionsEnabled . length ( ) < 3 )
positionsEnabled . append ( false ) ;
2020-11-20 15:09:36 +01:00
2020-11-23 11:57:51 +01:00
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 ( ) ) ;
2020-11-20 15:09:36 +01:00
// move buttons into place
2020-11-19 14:39:32 +01:00
this - > buttonWidget - > setFixedSize ( this - > size ( ) ) ;
2020-11-20 15:09:36 +01:00
2020-11-19 14:39:32 +01:00
int buttonSpacing = this - > height ( ) * 0.1 ;
int defaultButtonHeight = this - > height ( ) * 0.6 ;
2020-11-23 11:57:51 +01:00
geometries [ 0 ] = QRect ( ( this - > width ( ) - defaultButtonHeight ) / 2 , 0 , defaultButtonHeight , defaultButtonHeight ) ;
2020-11-20 15:09:36 +01:00
2020-11-19 14:39:32 +01:00
int secondaryButtonHeight = this - > height ( ) * 0.3 ;
2020-11-20 15:09:36 +01:00
if ( positionsEnabled [ 1 ] & & positionsEnabled [ 2 ] ) {
2020-11-23 11:57:51 +01:00
// place buttons besides each other
geometries [ 1 ] = QRect (
2020-11-20 15:09:36 +01:00
this - > width ( ) / 2 - secondaryButtonHeight - buttonSpacing / 2 ,
defaultButtonHeight + buttonSpacing ,
secondaryButtonHeight ,
secondaryButtonHeight
) ;
2020-11-23 11:57:51 +01:00
geometries [ 2 ] = QRect (
2020-11-20 15:09:36 +01:00
this - > width ( ) / 2 + buttonSpacing / 2 ,
defaultButtonHeight + buttonSpacing ,
secondaryButtonHeight ,
secondaryButtonHeight
) ;
}
2020-11-23 11:57:51 +01:00
else {
// place buttons on top of each other
geometries [ 1 ] = QRect (
2020-11-20 15:09:36 +01:00
this - > width ( ) / 2 - secondaryButtonHeight / 2 ,
defaultButtonHeight + buttonSpacing ,
secondaryButtonHeight ,
secondaryButtonHeight
) ;
2020-11-23 11:57:51 +01:00
geometries [ 2 ] = geometries [ 1 ] ;
2020-11-20 15:09:36 +01:00
}
2020-11-23 11:57:51 +01:00
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 ] ) ;
}
2020-11-20 15:09:36 +01:00
}
2020-11-19 14:39:32 +01:00
// Progress bar
this - > progressBarWidget - > setFixedSize ( this - > size ( ) ) ;
int progressBarHeight = this - > height ( ) * 0.1 ;
int progressBarWidth = this - > width ( ) * 0.5 ;
2020-11-24 20:25:39 +01:00
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 ) ;
2020-11-23 11:57:51 +01:00
this - > inited = true ;
2020-11-19 14:39:32 +01:00
}
void LinboStartActions : : resizeEvent ( QResizeEvent * event ) {
this - > resizeAndPositionAllItems ( ) ;
QWidget : : resizeEvent ( event ) ;
}
2020-11-23 11:57:51 +01:00
void LinboStartActions : : handleCurrentOsChanged ( LinboOs * newOs ) {
Q_UNUSED ( newOs )
this - > resizeAndPositionAllItems ( ) ;
2020-11-20 15:09:36 +01:00
}
2020-11-23 11:57:51 +01:00
void LinboStartActions : : handleLinboStateChanged ( LinboBackend : : LinboState newState ) {
2020-11-24 21:42:47 +01:00
QWidget * currentWidget = nullptr ;
2020-11-23 11:57:51 +01:00
switch ( newState ) {
2020-11-24 21:42:47 +01:00
case LinboBackend : : Autostarting :
qDebug ( ) < < " Linbo state is: autostart " ;
this - > progressBar - > setIndeterminate ( false ) ;
this - > progressBar - > setReversed ( true ) ;
this - > progressBar - > setValue ( 0 ) ;
currentWidget = this - > progressBarWidget ;
break ;
2020-11-23 11:57:51 +01:00
case LinboBackend : : Idle :
2020-11-24 21:42:47 +01:00
currentWidget = this - > buttonWidget ;
2020-11-23 11:57:51 +01:00
break ;
case LinboBackend : : Starting :
case LinboBackend : : Syncing :
case LinboBackend : : Reinstalling :
2020-11-24 21:42:47 +01:00
this - > progressBar - > setIndeterminate ( true ) ;
this - > progressBar - > setReversed ( false ) ;
currentWidget = this - > progressBarWidget ;
2020-11-23 11:57:51 +01:00
break ;
2020-11-20 15:09:36 +01:00
2020-11-23 11:57:51 +01:00
default :
break ;
}
2020-11-24 21:42:47 +01:00
if ( this - > inited )
this - > stackView - > setCurrentWidgetAnimated ( currentWidget ) ;
else
this - > stackView - > setCurrentWidget ( currentWidget ) ;
2020-11-19 14:39:32 +01:00
}
2020-11-24 20:25:39 +01:00
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 ) ;
}
2020-11-24 21:42:47 +01:00
void LinboStartActions : : handleAutostartTimeoutProgressChanged ( ) {
if ( this - > backend - > getState ( ) ! = LinboBackend : : Autostarting )
return ;
this - > progressBar - > setValue ( 1000 - this - > backend - > getAutostartTimeoutProgress ( ) * 1000 ) ;
QString actionString ;
switch ( this - > backend - > getCurrentOs ( ) - > getDefaultAction ( ) ) {
case LinboOs : : SyncOs :
actionString = " Syncing and starting " ;
break ;
case LinboOs : : ReinstallOs :
actionString = " Reinstalling and starting " ;
break ;
default :
actionString = " Starting " ;
break ;
}
this - > logLabel - > setText ( actionString + " " + this - > backend - > getCurrentOs ( ) - > getName ( ) + " in " + QString : : number ( this - > backend - > getAutostartTimeoutRemainingSeconds ( ) ) + " seconds. " ) ;
}