aboutsummaryrefslogtreecommitdiff
path: root/src-qt5/core/lumina-desktop/src-screensaver/animations/Text.h
blob: bdde5ba242112bb2b74ded64c7d337470a584959 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//===========================================
//  Lumina-DE source code
//  Copyright (c) 2015, Ken Moore
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================
#ifndef _LUMINA_DESKTOP_SCREEN_SAVER_TEXT_ANIMATION_H
#define _LUMINA_DESKTOP_SCREEN_SAVER_TEXT_ANIMATION_H

#include "global-includes.h"
#include "BaseAnimGroup.h"
#include <QParallelAnimationGroup>
#include <QtMath>

#include <unistd.h>

class Text: public QParallelAnimationGroup{
	Q_OBJECT
private:
	QLabel *text;
	QPropertyAnimation *movement;
	QSize range;
	QPoint v;
	bool bounce;

private slots:
	void LoopChanged(){
	  movement->setStartValue(movement->endValue());
	  QPoint currLoc = movement->startValue().toPoint();
	  bounce = !(currLoc.y() < 100 or currLoc.y() > range.height()-100 or currLoc.x() > range.width()-100 or currLoc.x() < 100);
	  if((currLoc.y() < 10 or currLoc.y() > range.height()-40) and !bounce) {
	    v.setY((v.y() * -1) + (qrand() % 20 - 10));
	  }else if((currLoc.x() > range.width()-10 or currLoc.x() < 10) and !bounce) {
	    v.setX((v.x() * -1) + (qrand() % 20 - 10));
	  }
	  currLoc.setX(currLoc.x() + v.x());
	  currLoc.setY(currLoc.y() + v.y());
	  movement->setEndValue(currLoc);
	}
	void stopped(){ qDebug() << "Text Stopped"; text->hide();}

public:
	Text(QWidget *parent, QString display) : QParallelAnimationGroup(parent){
	  text = new QLabel(parent);
	  range = parent->size();
	  QPoint center = QRect( QPoint(0,0), parent->size()).center();

	  QString color = "rgba(" + QString::number(qrand() % 206 + 50) + ", " + QString::number(qrand() % 206 + 50) + ", " + QString::number(qrand() % 206 + 50);
	  text->setStyleSheet("QLabel {background-color: transparent; color: " + color + "); }");
	  text->setFont(QFont("Courier", 24, QFont::Bold));
	  text->setText(display);
          QFontMetrics metrics(text->font());
	  text->setMinimumSize(QSize( metrics.horizontalAdvance(text->text())+10, metrics.height()*text->text().count("\n") +10));

	  movement = new QPropertyAnimation(text);
	  movement->setPropertyName("pos");
	  movement->setTargetObject(text);

	  this->addAnimation(movement);
	  text->show();
	  v.setX((qrand() % 100 + 50) * qPow(-1, qrand() % 2));
	  v.setY((qrand() % 100 + 50) * qPow(-1, qrand() % 2));
	  movement->setStartValue(center);
	  //Ensures the screensaver will not stop until the user wishes to login or it times out
	  this->setLoopCount(200); //number of wall bounces
	  movement->setDuration(200);
	  movement->setEndValue(QPoint(qrand() % (int)range.height(), qrand() % range.width()));
	  LoopChanged();  //load initial values

	  connect(this, SIGNAL(currentLoopChanged(int)), this, SLOT(LoopChanged()) );
	  connect(this, SIGNAL(finished()), this, SLOT(stopped()) );
	}
	~Text(){}

};

class TextAnimation : public BaseAnimGroup{
	Q_OBJECT
public:
	TextAnimation(QWidget *parent) : BaseAnimGroup(parent){}
	~TextAnimation(){
	  this->stop();
	}

	void LoadAnimations(){
	  canvas->setStyleSheet("background: black;");
	  //Read off the text that needs to be displayed
	  QString textToShow = readSetting("text", "").toString();
	  if(textToShow.isEmpty()){
 	    char hname[300];
            gethostname(hname, 300);
            textToShow = QString::fromLocal8Bit(hname);
	  }
	  // Now create the animation
	  Text *tmp = new Text(canvas, textToShow);
	  this->addAnimation(tmp);
	}

};
#endif
bgstack15