slidewindow.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Project PhotoBunny
  3. * 2014 Gwilyn Saunders
  4. */
  5. #include "slidewindow.h"
  6. #include "common.h"
  7. #include <QtGui>
  8. #include <QDesktopWidget>
  9. #include <QAction>
  10. #include <QMessageBox>
  11. #include <QErrorMessage>
  12. SlideWindow::SlideWindow(QWidget *parent)
  13. : QMainWindow(parent) {
  14. //load settings
  15. st = new QSettings(company_str, app_str);
  16. timeout = st->value("timeout").toInt();
  17. setupScreens();
  18. }
  19. void SlideWindow::setupScreens() {
  20. ScreenDialog *diag = new ScreenDialog(this);
  21. connect(diag, SIGNAL(accepted()), this, SLOT(setupLogin()));
  22. connect(diag, SIGNAL(rejected()), qApp, SLOT(quit()));
  23. if (diag->numScreens() > 1) {
  24. diag->show();
  25. } else {
  26. setupLogin();
  27. }
  28. }
  29. void SlideWindow::setupLogin() {
  30. //load fbApi
  31. fb = new FbApi(APP_ID, this);
  32. connect(fb, SIGNAL(error(QString)), this, SLOT(handleError(QString)));
  33. //open login dialog
  34. LoginDialog *loginDiag = new LoginDialog(fb, this);
  35. loginDiag->show();
  36. connect(loginDiag, SIGNAL(accepted()), this, SLOT(setupAlbums()));
  37. connect(loginDiag, SIGNAL(rejected()), qApp, SLOT(quit()));
  38. }
  39. void SlideWindow::setupAlbums() {
  40. AlbumDialog *albumDiag = new AlbumDialog(fb, this);
  41. albumDiag->show();
  42. connect(albumDiag, SIGNAL(accepted()), this, SLOT(setupUi()));
  43. connect(albumDiag, SIGNAL(rejected()), qApp, SLOT(quit()));
  44. }
  45. void SlideWindow::setupUi() {
  46. //housekeeping
  47. setWindowTitle("PhotoBunny");
  48. setWindowIcon(QIcon(":/img/icon.png"));
  49. QDesktopWidget desktop;
  50. QRect d = desktop.screenGeometry(st->value("screen").toInt());
  51. qDebug() << "Creating slides on" << st->value("screen").toInt() << ":" << d;
  52. setFixedWidth(d.width());
  53. setFixedHeight(d.height());
  54. setGeometry(d);
  55. setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
  56. setPalette(QPalette(QColor().black()));
  57. //set default pixmap
  58. QPixmap pix(":/img/homepage.png");
  59. pix = pix.scaledToHeight(this->height(),Qt::SmoothTransformation);
  60. int tl = (pix.width() - this->width()) / 2;
  61. pix = pix.copy(tl, 0, this->width(), this->height());
  62. //set label widget
  63. img = new QLabel(this);
  64. img->setPixmap(pix);
  65. img->setMaximumHeight(this->height());
  66. img->setMaximumWidth(this->width());
  67. img->setAlignment(Qt::AlignCenter | Qt::AlignHCenter);
  68. setCentralWidget(img);
  69. //set keyboard shortcuts
  70. QAction *escAction = new QAction(this);
  71. QAction *aboutAction = new QAction(this);
  72. QAction *pauseAction = new QAction(this);
  73. QAction *blankAction = new QAction(this);
  74. escAction->setShortcut(Qt::Key_Escape);
  75. aboutAction->setShortcut(Qt::Key_F1);
  76. pauseAction->setShortcut(Qt::Key_Space);
  77. blankAction->setShortcut(Qt::Key_Return);
  78. addAction(escAction);
  79. addAction(aboutAction);
  80. addAction(pauseAction);
  81. addAction(blankAction);
  82. //events
  83. connect(escAction, SIGNAL(triggered()), qApp, SLOT(quit()));
  84. connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
  85. connect(pauseAction, SIGNAL(triggered()), this, SLOT(pause()));
  86. connect(blankAction, SIGNAL(triggered()), this, SLOT(blank()));
  87. show();
  88. setupFinal();
  89. }
  90. void SlideWindow::setupFinal() {
  91. if (!fb->loggedIn()) {
  92. QMessageBox::critical(this,
  93. "Login Error",
  94. "Not logged in!");
  95. return;
  96. }
  97. //final stuff
  98. qApp->setOverrideCursor(Qt::BlankCursor);
  99. timer = new QTimer();
  100. qDebug() << "-- slideshow started with a" << timeout << "ms timeout --";
  101. if (fb->caching) {
  102. qDebug() << "-- caching enabled --";
  103. }
  104. connect(timer, SIGNAL(timeout()), this, SLOT(photoChange()));
  105. connect(fb, SIGNAL(picsLoaded()), this, SLOT(photoChange()));
  106. connect(fb, SIGNAL(picLoaded(QString)), this, SLOT(setImage(QString)));
  107. fb->loadPictures();
  108. }
  109. void SlideWindow::about() {
  110. QMessageBox::about(this,
  111. "PhotoBunny",
  112. "A fullscreen app that displays a slideshow of photos from selected facebook albums. \n"
  113. "Made for Lincoln College, North Adelaide SA, Australia \n"
  114. "www.lincoln.edu.au \n"
  115. "Licenced under GPL 3 \n"
  116. "(c) Gwilyn Saunders 2014");
  117. }
  118. void SlideWindow::pause() {
  119. if (timer->isActive()) {
  120. qDebug() << "paused";
  121. timer->stop();
  122. } else {
  123. qDebug() << "unpaused";
  124. photoChange();
  125. }
  126. }
  127. void SlideWindow::blank() {
  128. pause();
  129. img->setPixmap(QPixmap(":/img/black.png"));
  130. }
  131. void SlideWindow::handleError(QString msg) {
  132. // QButton but = QMessageBox::critical(this,
  133. // "PhotoBunny Error",
  134. // msg);
  135. QErrorMessage *diag = new QErrorMessage(this);
  136. diag->showMessage(msg);
  137. connect(diag, SIGNAL(destroyed()), qApp, SLOT(quit()));
  138. }
  139. void SlideWindow::photoChange() {
  140. timer->stop();
  141. fb->nextPicture();
  142. timer->start(timeout);
  143. }
  144. void SlideWindow::setImage(QString path) {
  145. QPixmap pix(path);
  146. pix = pix.scaled(this->width(), this->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
  147. img->setPixmap(pix);
  148. }