1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the demonstration applications of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
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 The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include <QBasicTimer> |
52 | #include <QList> |
53 | #include <QImage> |
54 | #include <QDir> |
55 | #include <QPainter> |
56 | #include <QPaintEvent> |
57 | |
58 | #include <QDebug> |
59 | |
60 | |
61 | #include "slideshow.h" |
62 | |
63 | |
64 | class SlideShowPrivate |
65 | { |
66 | public: |
67 | SlideShowPrivate(); |
68 | |
69 | int currentSlide; |
70 | int slideInterval; |
71 | QBasicTimer interSlideTimer; |
72 | QStringList imagePaths; |
73 | |
74 | void showNextSlide(); |
75 | }; |
76 | |
77 | |
78 | |
79 | SlideShowPrivate::SlideShowPrivate() |
80 | { |
81 | currentSlide = 0; |
82 | slideInterval = 10000; // Default to 10 sec interval |
83 | } |
84 | |
85 | |
86 | void SlideShowPrivate::showNextSlide() |
87 | { |
88 | currentSlide++; |
89 | if (currentSlide >= imagePaths.size()) |
90 | currentSlide = 0; |
91 | } |
92 | |
93 | |
94 | |
95 | SlideShow::SlideShow(QWidget* parent) : |
96 | QWidget(parent) |
97 | { |
98 | d = new SlideShowPrivate; |
99 | |
100 | setAttribute(Qt::WA_StaticContents, on: true); |
101 | setAttribute(Qt::WA_OpaquePaintEvent, on: true); |
102 | setAttribute(Qt::WA_NoSystemBackground, on: true); |
103 | |
104 | setMouseTracking(true); |
105 | } |
106 | |
107 | |
108 | SlideShow::~SlideShow() |
109 | { |
110 | delete d; |
111 | } |
112 | |
113 | |
114 | void SlideShow::addImageDir(QString dirName) |
115 | { |
116 | QDir dir(dirName); |
117 | |
118 | // lookup in directories |
119 | QStringList fileNames = dir.entryList(filters: QDir::Files | QDir::Readable, sort: QDir::Name); |
120 | for (int i=0; i<fileNames.count(); i++) |
121 | d->imagePaths << dir.absoluteFilePath(fileName: fileNames[i]); |
122 | |
123 | // lookup in qrc |
124 | dir = QDir(QString(":/fluidlauncher/" + dirName)); |
125 | fileNames = dir.entryList(filters: QDir::Files | QDir::Readable, sort: QDir::Name); |
126 | for (int i=0; i<fileNames.count(); i++) |
127 | d->imagePaths << dir.absoluteFilePath(fileName: fileNames[i]); |
128 | } |
129 | |
130 | void SlideShow::addImage(QString filename) |
131 | { |
132 | d->imagePaths << filename; |
133 | } |
134 | |
135 | |
136 | void SlideShow::clearImages() |
137 | { |
138 | d->imagePaths.clear(); |
139 | } |
140 | |
141 | |
142 | void SlideShow::startShow() |
143 | { |
144 | d->interSlideTimer.start(msec: d->slideInterval, obj: this); |
145 | d->showNextSlide(); |
146 | update(); |
147 | } |
148 | |
149 | |
150 | void SlideShow::stopShow() |
151 | { |
152 | d->interSlideTimer.stop(); |
153 | } |
154 | |
155 | |
156 | int SlideShow::slideInterval() |
157 | { |
158 | return d->slideInterval; |
159 | } |
160 | |
161 | void SlideShow::setSlideInterval(int val) |
162 | { |
163 | d->slideInterval = val; |
164 | } |
165 | |
166 | |
167 | void SlideShow::timerEvent(QTimerEvent* event) |
168 | { |
169 | Q_UNUSED(event); |
170 | d->showNextSlide(); |
171 | update(); |
172 | } |
173 | |
174 | |
175 | void SlideShow::paintEvent(QPaintEvent *event) |
176 | { |
177 | QPainter painter(this); |
178 | painter.setRenderHint(hint: QPainter::Antialiasing, on: false); |
179 | |
180 | if (d->imagePaths.size() > 0) { |
181 | QPixmap slide = QPixmap(d->imagePaths[d->currentSlide]); |
182 | QSize slideSize = slide.size(); |
183 | QSize scaledSize = QSize(qMin(a: slideSize.width(), b: size().width()), |
184 | qMin(a: slideSize.height(), b: size().height())); |
185 | if (slideSize != scaledSize) |
186 | slide = slide.scaled(s: scaledSize, aspectMode: Qt::KeepAspectRatio); |
187 | |
188 | QRect pixmapRect(qMax( a: (size().width() - slide.width())/2, b: 0), |
189 | qMax( a: (size().height() - slide.height())/2, b: 0), |
190 | slide.width(), |
191 | slide.height()); |
192 | |
193 | if (pixmapRect.top() > 0) { |
194 | // Fill in top & bottom rectangles: |
195 | painter.fillRect(x: 0, y: 0, w: size().width(), h: pixmapRect.top(), c: Qt::black); |
196 | painter.fillRect(x: 0, y: pixmapRect.bottom(), w: size().width(), h: size().height(), c: Qt::black); |
197 | } |
198 | |
199 | if (pixmapRect.left() > 0) { |
200 | // Fill in left & right rectangles: |
201 | painter.fillRect(x: 0, y: 0, w: pixmapRect.left(), h: size().height(), c: Qt::black); |
202 | painter.fillRect(x: pixmapRect.right(), y: 0, w: size().width(), h: size().height(), c: Qt::black); |
203 | } |
204 | |
205 | painter.drawPixmap(r: pixmapRect, pm: slide); |
206 | |
207 | } else |
208 | painter.fillRect(r: event->rect(), c: Qt::black); |
209 | } |
210 | |
211 | |
212 | void SlideShow::keyPressEvent(QKeyEvent* event) |
213 | { |
214 | Q_UNUSED(event); |
215 | emit inputReceived(); |
216 | } |
217 | |
218 | |
219 | void SlideShow::mouseMoveEvent(QMouseEvent* event) |
220 | { |
221 | Q_UNUSED(event); |
222 | emit inputReceived(); |
223 | } |
224 | |
225 | |
226 | void SlideShow::mousePressEvent(QMouseEvent* event) |
227 | { |
228 | Q_UNUSED(event); |
229 | emit inputReceived(); |
230 | } |
231 | |
232 | |
233 | void SlideShow::mouseReleaseEvent(QMouseEvent* event) |
234 | { |
235 | Q_UNUSED(event); |
236 | emit inputReceived(); |
237 | } |
238 | |
239 | |
240 | void SlideShow::showEvent(QShowEvent * event ) |
241 | { |
242 | Q_UNUSED(event); |
243 | #ifndef QT_NO_CURSOR |
244 | setCursor(Qt::BlankCursor); |
245 | #endif |
246 | } |
247 | |
248 | |