1 //=========================================== 2 // Lumina-DE source code 3 // Copyright (c) 2015, Ken Moore 4 // Available under the 3-clause BSD license 5 // See the LICENSE file for full details 6 //=========================================== 7 #ifndef _LUMINA_DESKTOP_SCREEN_SAVER_TEXT_ANIMATION_H 8 #define _LUMINA_DESKTOP_SCREEN_SAVER_TEXT_ANIMATION_H 9 10 #include "global-includes.h" 11 #include "BaseAnimGroup.h" 12 #include <QParallelAnimationGroup> 13 #include <QtMath> 14 15 #include <unistd.h> 16 17 class Text: public QParallelAnimationGroup{ 18 Q_OBJECT 19 private: 20 QLabel *text; 21 QPropertyAnimation *movement; 22 QSize range; 23 QPoint v; 24 bool bounce; 25 26 private slots: LoopChanged()27 void LoopChanged(){ 28 movement->setStartValue(movement->endValue()); 29 QPoint currLoc = movement->startValue().toPoint(); 30 bounce = !(currLoc.y() < 100 or currLoc.y() > range.height()-100 or currLoc.x() > range.width()-100 or currLoc.x() < 100); 31 if((currLoc.y() < 10 or currLoc.y() > range.height()-40) and !bounce) { 32 v.setY((v.y() * -1) + (qrand() % 20 - 10)); 33 }else if((currLoc.x() > range.width()-10 or currLoc.x() < 10) and !bounce) { 34 v.setX((v.x() * -1) + (qrand() % 20 - 10)); 35 } 36 currLoc.setX(currLoc.x() + v.x()); 37 currLoc.setY(currLoc.y() + v.y()); 38 movement->setEndValue(currLoc); 39 } stopped()40 void stopped(){ qDebug() << "Text Stopped"; text->hide();} 41 42 public: Text(QWidget * parent,QString display)43 Text(QWidget *parent, QString display) : QParallelAnimationGroup(parent){ 44 text = new QLabel(parent); 45 range = parent->size(); 46 QPoint center = QRect( QPoint(0,0), parent->size()).center(); 47 48 QString color = "rgba(" + QString::number(qrand() % 206 + 50) + ", " + QString::number(qrand() % 206 + 50) + ", " + QString::number(qrand() % 206 + 50); 49 text->setStyleSheet("QLabel {background-color: transparent; color: " + color + "); }"); 50 text->setFont(QFont("Courier", 24, QFont::Bold)); 51 text->setText(display); 52 QFontMetrics metrics(text->font()); 53 text->setMinimumSize(QSize( metrics.horizontalAdvance(text->text())+10, metrics.height()*text->text().count("\n") +10)); 54 55 movement = new QPropertyAnimation(text); 56 movement->setPropertyName("pos"); 57 movement->setTargetObject(text); 58 59 this->addAnimation(movement); 60 text->show(); 61 v.setX((qrand() % 100 + 50) * qPow(-1, qrand() % 2)); 62 v.setY((qrand() % 100 + 50) * qPow(-1, qrand() % 2)); 63 movement->setStartValue(center); 64 //Ensures the screensaver will not stop until the user wishes to login or it times out 65 this->setLoopCount(200); //number of wall bounces 66 movement->setDuration(200); 67 movement->setEndValue(QPoint(qrand() % (int)range.height(), qrand() % range.width())); 68 LoopChanged(); //load initial values 69 70 connect(this, SIGNAL(currentLoopChanged(int)), this, SLOT(LoopChanged()) ); 71 connect(this, SIGNAL(finished()), this, SLOT(stopped()) ); 72 } ~Text()73 ~Text(){} 74 75 }; 76 77 class TextAnimation : public BaseAnimGroup{ 78 Q_OBJECT 79 public: TextAnimation(QWidget * parent)80 TextAnimation(QWidget *parent) : BaseAnimGroup(parent){} ~TextAnimation()81 ~TextAnimation(){ 82 this->stop(); 83 } 84 LoadAnimations()85 void LoadAnimations(){ 86 canvas->setStyleSheet("background: black;"); 87 //Read off the text that needs to be displayed 88 QString textToShow = readSetting("text", "").toString(); 89 if(textToShow.isEmpty()){ 90 char hname[300]; 91 gethostname(hname, 300); 92 textToShow = QString::fromLocal8Bit(hname); 93 } 94 // Now create the animation 95 Text *tmp = new Text(canvas, textToShow); 96 this->addAnimation(tmp); 97 } 98 99 }; 100 #endif 101