slideshow.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
  4. ** Contact: http://www.qt-project.org/legal
  5. **
  6. ** This file is part of the demonstration applications of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL21$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and Digia. For licensing terms and
  14. ** conditions see http://qt.digia.com/licensing. For further information
  15. ** use the contact form at http://qt.digia.com/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 2.1 or version 3 as published by the Free
  20. ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
  21. ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
  22. ** following information to ensure the GNU Lesser General Public License
  23. ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
  24. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  25. **
  26. ** In addition, as a special exception, Digia gives you certain additional
  27. ** rights. These rights are described in the Digia Qt LGPL Exception
  28. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  29. **
  30. ** $QT_END_LICENSE$
  31. **
  32. ****************************************************************************/
  33. #include <QBasicTimer>
  34. #include <QList>
  35. #include <QImage>
  36. #include <QDir>
  37. #include <QPainter>
  38. #include <QPaintEvent>
  39. #include <QDebug>
  40. #include "slideshow.h"
  41. class SlideShowPrivate
  42. {
  43. public:
  44. SlideShowPrivate();
  45. int currentSlide;
  46. int slideInterval;
  47. QBasicTimer interSlideTimer;
  48. QStringList imagePaths;
  49. void showNextSlide();
  50. };
  51. SlideShowPrivate::SlideShowPrivate()
  52. {
  53. currentSlide = 0;
  54. slideInterval = 10000; // Default to 10 sec interval
  55. }
  56. void SlideShowPrivate::showNextSlide()
  57. {
  58. currentSlide++;
  59. if (currentSlide >= imagePaths.size())
  60. currentSlide = 0;
  61. }
  62. SlideShow::SlideShow(QWidget* parent) :
  63. QWidget(parent)
  64. {
  65. d = new SlideShowPrivate;
  66. setAttribute(Qt::WA_StaticContents, true);
  67. setAttribute(Qt::WA_OpaquePaintEvent, true);
  68. setAttribute(Qt::WA_NoSystemBackground, true);
  69. setMouseTracking(true);
  70. }
  71. SlideShow::~SlideShow()
  72. {
  73. delete d;
  74. }
  75. void SlideShow::addImageDir(QString dirName)
  76. {
  77. QDir dir(dirName);
  78. // lookup in directories
  79. QStringList fileNames = dir.entryList(QDir::Files | QDir::Readable, QDir::Name);
  80. for (int i=0; i<fileNames.count(); i++)
  81. d->imagePaths << dir.absoluteFilePath(fileNames[i]);
  82. // lookup in qrc
  83. dir = QDir(QString(":/fluidlauncher/" + dirName));
  84. fileNames = dir.entryList(QDir::Files | QDir::Readable, QDir::Name);
  85. for (int i=0; i<fileNames.count(); i++)
  86. d->imagePaths << dir.absoluteFilePath(fileNames[i]);
  87. }
  88. void SlideShow::addImage(QString filename)
  89. {
  90. d->imagePaths << filename;
  91. }
  92. void SlideShow::clearImages()
  93. {
  94. d->imagePaths.clear();
  95. }
  96. void SlideShow::startShow()
  97. {
  98. d->interSlideTimer.start(d->slideInterval, this);
  99. d->showNextSlide();
  100. update();
  101. }
  102. void SlideShow::stopShow()
  103. {
  104. d->interSlideTimer.stop();
  105. }
  106. int SlideShow::slideInterval()
  107. {
  108. return d->slideInterval;
  109. }
  110. void SlideShow::setSlideInterval(int val)
  111. {
  112. d->slideInterval = val;
  113. }
  114. void SlideShow::timerEvent(QTimerEvent* event)
  115. {
  116. Q_UNUSED(event);
  117. d->showNextSlide();
  118. update();
  119. }
  120. void SlideShow::paintEvent(QPaintEvent *event)
  121. {
  122. QPainter painter(this);
  123. painter.setRenderHint(QPainter::Antialiasing, false);
  124. if (d->imagePaths.size() > 0) {
  125. QPixmap slide = QPixmap(d->imagePaths[d->currentSlide]);
  126. QSize slideSize = slide.size();
  127. QSize scaledSize = QSize(qMin(slideSize.width(), size().width()),
  128. qMin(slideSize.height(), size().height()));
  129. if (slideSize != scaledSize)
  130. slide = slide.scaled(scaledSize, Qt::KeepAspectRatio);
  131. QRect pixmapRect(qMax( (size().width() - slide.width())/2, 0),
  132. qMax( (size().height() - slide.height())/2, 0),
  133. slide.width(),
  134. slide.height());
  135. if (pixmapRect.top() > 0) {
  136. // Fill in top & bottom rectangles:
  137. painter.fillRect(0, 0, size().width(), pixmapRect.top(), Qt::black);
  138. painter.fillRect(0, pixmapRect.bottom(), size().width(), size().height(), Qt::black);
  139. }
  140. if (pixmapRect.left() > 0) {
  141. // Fill in left & right rectangles:
  142. painter.fillRect(0, 0, pixmapRect.left(), size().height(), Qt::black);
  143. painter.fillRect(pixmapRect.right(), 0, size().width(), size().height(), Qt::black);
  144. }
  145. painter.drawPixmap(pixmapRect, slide);
  146. } else
  147. painter.fillRect(event->rect(), Qt::black);
  148. }
  149. void SlideShow::keyPressEvent(QKeyEvent* event)
  150. {
  151. Q_UNUSED(event);
  152. emit inputReceived();
  153. }
  154. void SlideShow::mouseMoveEvent(QMouseEvent* event)
  155. {
  156. Q_UNUSED(event);
  157. emit inputReceived();
  158. }
  159. void SlideShow::mousePressEvent(QMouseEvent* event)
  160. {
  161. Q_UNUSED(event);
  162. emit inputReceived();
  163. }
  164. void SlideShow::mouseReleaseEvent(QMouseEvent* event)
  165. {
  166. Q_UNUSED(event);
  167. emit inputReceived();
  168. }
  169. void SlideShow::showEvent(QShowEvent * event )
  170. {
  171. Q_UNUSED(event);
  172. #ifndef QT_NO_CURSOR
  173. setCursor(Qt::BlankCursor);
  174. #endif
  175. }