Newer
Older
Import / applications / Photoframe / src / manager.cpp
@John Ryland John Ryland on 11 Jan 2021 4 KB save wip
#include <QApplication>
#include <QGraphicsOpacityEffect>
#include <QPropertyAnimation>
#include <QParallelAnimationGroup>
#include <QMetaProperty>
#include <QSound>
#include "intro.h"
#include "manager.h"


Manager *g_Manager = 0;


Manager::Manager(QWidget *parent) : QWidget(parent)
{
	g_Manager = this;
	resize(800,480);

	Intro *intro = new Intro(this);
	intro->raise();
	connect(intro, SIGNAL(setStage(int)), this, SLOT(setStage(int)));

	mainmenu = new MainMenu(this);
	connect(mainmenu, SIGNAL(setStage(int)), this, SLOT(setStage(int)));
	mainmenu->hide();

	slideshow = 0;
	photos = 0;
	videos = 0;
	music = 0;
	info = 0;
	settings = 0;
}


Manager::~Manager()
{

}


void Manager::fadeInWidget(QWidget* w)
{
	w->show();
	w->setFocus();
	w->setWindowOpacity(0.0);

	QGraphicsOpacityEffect* eff = new QGraphicsOpacityEffect(this);
	w->setGraphicsEffect(eff);

	QParallelAnimationGroup* group = new QParallelAnimationGroup();

	QPropertyAnimation* anim = new QPropertyAnimation(eff, "opacity");
	anim->setDuration(1200);
	anim->setStartValue(0);
	anim->setEndValue(1);
	anim->setEasingCurve(QEasingCurve::OutBack);
	group->addAnimation(anim);

	QLabel* icon = w->findChild<QLabel*>("icon");
	if (icon)
	{
		icon->show();
		icon->raise();
		icon->setWindowOpacity(1.0);

		QPropertyAnimation* anim2 = new QPropertyAnimation(icon, "geometry");
		anim2->setDuration(170);
		anim2->setStartValue(QRect(20, 80, 360, 360));
		anim2->setEndValue(QRect(10, 10, 72, 72));
		anim2->setEasingCurve(QEasingCurve::InExpo);
		group->addAnimation(anim2);
	}

	//connect(group, &QAnimationGroup::finished, [w]() { w->setGraphicsEffect(nullptr); });
	group->start(QAbstractAnimation::DeleteWhenStopped);
}


void Manager::fadeOutWidget(QWidget* w)
{
	if (!w->isVisible())
		return;

	QGraphicsOpacityEffect* eff = new QGraphicsOpacityEffect(this);
	w->setGraphicsEffect(eff);

	QParallelAnimationGroup* group = new QParallelAnimationGroup();

	QPropertyAnimation* anim = new QPropertyAnimation(eff, "opacity");
	anim->setDuration(700);
	anim->setStartValue(1);
	anim->setEndValue(0);
	anim->setEasingCurve(QEasingCurve::OutBack);
	group->addAnimation(anim);

	QLabel* icon = w->findChild<QLabel*>("icon");
	if (icon)
	{
		icon->show();
		icon->raise();
		icon->setWindowOpacity(1.0);

		QPropertyAnimation* anim2 = new QPropertyAnimation(icon, "geometry");
		anim2->setDuration(120);
		anim2->setStartValue(QRect(10, 10, 72, 72));
		anim2->setEndValue(QRect(75, 145, 256, 256));
		anim2->setEasingCurve(QEasingCurve::OutExpo);
		group->addAnimation(anim2);
	}

	connect(group, &QAnimationGroup::finished, [w, icon]() { /* w->setGraphicsEffect(nullptr); */ w->hide(); if (icon) icon->hide(); });
	group->start(QAbstractAnimation::DeleteWhenStopped);
}


void Manager::setStage(int stage)
{
	switch (stage) {
		case -1:
			QSound::play("sounds/Delete.wav");
		case -2:
			if (slideshow)
				fadeOutWidget(slideshow);
			if (photos)
				fadeOutWidget(photos);
			if (music)
				fadeOutWidget(music);
			if (videos)
				fadeOutWidget(videos);
			if (info)
				fadeOutWidget(info);
			if (settings)
				fadeOutWidget(settings);
			mainmenu->show();
			mainmenu->setFocus();
			break;
		case 0:
			//delete slideshow; slideshow = 0;
			if (!slideshow) {
				slideshow = new SlideShow(this);
				connect(slideshow, SIGNAL(setStage(int)), this, SLOT(setStage(int)));
			}
			fadeInWidget(slideshow);
			break;
		case 1:
			//delete photos; photos = 0;
			if (!photos)
			{
				photos = new PhotoList(this);
				connect(photos, SIGNAL(setStage(int)), this, SLOT(setStage(int)));
			}
			fadeInWidget(photos);
			break;
		case 2:
			//delete music; music = 0;
			if (!music) {
				music = new MusicList(this);
				connect(music, SIGNAL(setStage(int)), this, SLOT(setStage(int)));
			}
			fadeInWidget(music);
			break;
		case 3:
			//delete videos; videos = 0;
			if (!videos) {
				videos = new VideoList(this);
				connect(videos, SIGNAL(setStage(int)), this, SLOT(setStage(int)));
			}
			fadeInWidget(videos);
			break;
		case 4:
			//delete info; info = 0;
			if (!info) {
				info = new Information(this);
				connect(info, SIGNAL(setStage(int)), this, SLOT(setStage(int)));
			}
			fadeInWidget(info);
			break;
		case 5:
			//delete settings; settings = 0;
			if (!settings) {
				settings = new Settings(this);
				connect(settings, SIGNAL(setStage(int)), this, SLOT(setStage(int)));
			}
			fadeInWidget(settings);
			break;
		case 6:
			// Turn Off
			// system("shutdown -t now");
			qApp->quit();
			break;
		default:
			return;
	}
}