| 1 |
/* |
| 2 |
* Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd. |
| 3 |
* |
| 4 |
* Author: ZhangYong <[email protected]> |
| 5 |
* |
| 6 |
* Maintainer: ZhangYong <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
#include "imageanimation.h" |
| 22 |
#include "unionimage.h" |
| 23 |
#include "application.h" |
| 24 |
|
| 25 |
#include <QDebug> |
| 26 |
#include <QVBoxLayout> |
| 27 |
#include <QPainter> |
| 28 |
#include <QPointer> |
| 29 |
#include <QSharedPointer> |
| 30 |
#include <QTime> |
| 31 |
#include <QTimer> |
| 32 |
#include <QScreen> |
| 33 |
#include <QObject> |
| 34 |
#include <QDesktopWidget> |
| 35 |
#include <QPalette> |
| 36 |
|
| 37 |
#include <cmath> |
| 38 |
|
| 39 |
float GaussFunction(double max, float mu, float sigma, float x) |
| 40 |
{ |
| 41 |
return static_cast<float>(max * std::exp(static_cast<double>(-(x - mu) * (x - mu) / 2 * sigma * sigma))); |
| 42 |
} |
| 43 |
|
| 44 |
class LoopQueue: public QObject |
| 45 |
{ |
| 46 |
Q_OBJECT |
| 47 |
public: |
| 48 |
LoopQueue(QObject *parent = nullptr); |
| 49 |
~LoopQueue() override; |
| 50 |
void setBeginPathAndPath(const QString &beginPath, const QStringList &list); |
| 51 |
const QString first()const; |
| 52 |
const QString second()const; |
| 53 |
const QString last()const; |
| 54 |
int index() const; |
| 55 |
const QString next(); |
| 56 |
// inline const QString pre(); |
| 57 |
const QString jumpTonext(); |
| 58 |
const QString jumpTopre(); |
| 59 |
const QString current()const; |
| 60 |
void changeOrder(bool order); |
| 61 |
private: |
| 62 |
void AddIndex(); |
| 63 |
private: |
| 64 |
QVector<QString> loop_paths; |
| 65 |
QMutex queueMutex; |
| 66 |
bool loop_order = true; |
| 67 |
char m_padding[3] = {'0', '0', '0'}; //填充占位,使数据结构内存对齐 |
| 68 |
int loop_pindex = 0; |
| 69 |
}; |
| 70 |
|
| 71 |
class ImageAnimationPrivate: public QWidget |
| 72 |
{ |
| 73 |
Q_OBJECT |
| 74 |
|
| 75 |
public: |
| 76 |
enum AnimationType { |
| 77 |
FadeEffect = 0, //图像1渐渐变淡,图像2渐渐显现 |
| 78 |
BlindsEffect = 1, //百叶窗效果 |
| 79 |
FlipRightToLeft = 2, //图像从右向左翻转 |
| 80 |
OutsideToInside = 3, //从外到内水平分割 |
| 81 |
MoveLeftToRightEffect = 4, //图像1从左至右退出可视区域,同时图像2从左至右进入可视区域 |
| 82 |
MoveRightToLeftEffect = 5, //图像1从左至右退出可视区域,同时图像2从左至右进入可视区域 |
| 83 |
MoveBottomToUpEffect = 6, //图像1从下至上退出可视区域,同时图像2从下至上进入可视区域 |
| 84 |
MoveUpToBottomEffect = 7, //图像1从上至下退出可视区域,同时图像2从上至下进入可视区域 |
| 85 |
MoveBottomToLeftUpEffect = 8//图像1不动,同时图像2从右下到左上 |
| 86 |
}; |
| 87 |
|
| 88 |
protected: |
| 89 |
explicit ImageAnimationPrivate(ImageAnimation *qq = nullptr); |
| 90 |
~ImageAnimationPrivate(); |
| 91 |
void effectPainter(QPainter *painter, const QRect &rect); |
| 92 |
void forwardPainter(QPainter *painter, const QRect &rect); |
| 93 |
// void retreatPainter(QPainter *painter, const QRect &rect); |
| 94 |
void keepStaticPainter(QPainter *painter, const QRect &rect); |
| 95 |
/** |
| 96 |
**************************************************************************************************************** |
| 97 |
Effects |
| 98 |
**************************************************************************************************************** |
| 99 |
*/ |
| 100 |
void fadeEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2); |
| 101 |
void blindsEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2); |
| 102 |
void flipRightToLeft(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2); |
| 103 |
void outsideToInside(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2); |
| 104 |
void moveLeftToRightEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2); |
| 105 |
// void moveRightToLeftEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2); |
| 106 |
// void moveBottomToUpEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2); |
| 107 |
// void moveUpToBottomEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2); |
| 108 |
// void moveBottomToLeftUpEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2); |
| 109 |
|
| 110 |
/** |
| 111 |
**************************************************************************************************************** |
| 112 |
*/ |
| 113 |
private: |
| 114 |
float m_factor; //动画因子(0 - 1.0之间变化) |
| 115 |
float m_funval; |
| 116 |
QString m_imageName1; //图片1路径名称 |
| 117 |
QString m_imageName2; //图片2路径名称 |
| 118 |
QPixmap m_pixmap1; //图片1 |
| 119 |
QPixmap m_pixmap2; //图片2 |
| 120 |
AnimationType m_animationType; //动画效果类型 |
| 121 |
bool m_isAnimationIng = false; //正在播动画 |
| 122 |
bool m_isRandom = true; //设置是否随机播放效果 |
| 123 |
|
| 124 |
public: |
| 125 |
// float getFactor() const |
| 126 |
// { |
| 127 |
// return m_factor; |
| 128 |
// } |
| 129 |
// QString getImageName1() const |
| 130 |
// { |
| 131 |
// return m_imageName1; |
| 132 |
// } |
| 133 |
// QString getImageName2() const |
| 134 |
// { |
| 135 |
// return m_imageName2; |
| 136 |
// } |
| 137 |
// QPixmap getPixmap1() const |
| 138 |
// { |
| 139 |
// return m_pixmap1; |
| 140 |
// } |
| 141 |
// QPixmap getPixmap2() const |
| 142 |
// { |
| 143 |
// return m_pixmap2; |
| 144 |
// } |
| 145 |
// AnimationType getAnimationType()const |
| 146 |
// { |
| 147 |
// return m_animationType; |
| 148 |
// } |
| 149 |
void setSlideModel(ImageAnimation::SlideModel model) |
| 150 |
{ |
| 151 |
m_SliderModel = model; |
| 152 |
} |
| 153 |
// ImageAnimation::SlideModel getSlideModel() |
| 154 |
// { |
| 155 |
// return m_SliderModel; |
| 156 |
// } |
| 157 |
void setPlayOrStatue(ImageAnimation::PlayOrStatue statue) |
| 158 |
{ |
| 159 |
m_PlayOrStatue = statue; |
| 160 |
} |
| 161 |
// ImageAnimation::PlayOrStatue getPlayOrStatue() |
| 162 |
// { |
| 163 |
// return m_PlayOrStatue; |
| 164 |
// } |
| 165 |
|
| 166 |
void setPathList(const QString &first, const QStringList &list); |
| 167 |
void startAnimation(); |
| 168 |
void startSingleNextAnimation(); |
| 169 |
void startSinglePreAnimation(); |
| 170 |
void startStatic(); |
| 171 |
void endSlide() |
| 172 |
{ |
| 173 |
if (m_staticTimer) { |
| 174 |
m_staticTimer->stop(); |
| 175 |
} |
| 176 |
if (m_continuousanimationTimer) { |
| 177 |
m_continuousanimationTimer->stop(); |
| 178 |
} |
| 179 |
} |
| 180 |
//设置动画效果是否随机 |
| 181 |
void setIsRandom(bool isRandom) |
| 182 |
{ |
| 183 |
m_isRandom = isRandom; |
| 184 |
} |
| 185 |
|
| 186 |
public slots: |
| 187 |
void onContinuousAnimationTimer(); |
| 188 |
void onStaticTimer(); |
| 189 |
|
| 190 |
private: |
| 191 |
//设置动画因子 |
| 192 |
// inline void setFactor(float factor_bar) |
| 193 |
// { |
| 194 |
// m_factor = factor_bar; |
| 195 |
// } |
| 196 |
|
| 197 |
//设置图片1+图片2路径名称并适应widget |
| 198 |
void setImage1(const QString &imageName1_bar); |
| 199 |
void setImage2(const QString &imageName2_bar); |
| 200 |
|
| 201 |
//设置图片1+图片2 |
| 202 |
// void setPixmap1(const QPixmap &pixmap1_bar) |
| 203 |
// { |
| 204 |
// m_pixmap1 = pixmap1_bar; |
| 205 |
// } |
| 206 |
// void setPixmap2(const QPixmap &pixmap2_bar) |
| 207 |
// { |
| 208 |
// m_pixmap2 = pixmap2_bar; |
| 209 |
// } |
| 210 |
//设置动画类型 |
| 211 |
// void setAnimationType(const AnimationType &animationType_bar) |
| 212 |
// { |
| 213 |
// m_animationType = animationType_bar; |
| 214 |
// } |
| 215 |
|
| 216 |
private: |
| 217 |
char m_padding2[2] = {'0', '0'}; //填充占位,使数据结构内存对齐 |
| 218 |
LoopQueue *queue = nullptr; |
| 219 |
QPointer<QTimer> m_continuousanimationTimer; |
| 220 |
QPointer<QTimer> m_staticTimer; |
| 221 |
QPoint centrePoint; |
| 222 |
// int beginX; |
| 223 |
// int beginY; |
| 224 |
// int finalX; |
| 225 |
// int finalY; |
| 226 |
|
| 227 |
ImageAnimation *const q_ptr; |
| 228 |
ImageAnimation::SlideModel m_SliderModel = ImageAnimation::AutoPlayModel; |
| 229 |
ImageAnimation::PlayOrStatue m_PlayOrStatue = ImageAnimation::PlayStatue; |
| 230 |
Q_DECLARE_PUBLIC(ImageAnimation) |
| 231 |
}; |
| 232 |
|
| 233 |
/** |
| 234 |
**************************************************************************************************************** |
| 235 |
* LoopQueue |
| 236 |
**************************************************************************************************************** |
| 237 |
*/ |
| 238 |
|
| 239 |
LoopQueue::LoopQueue(QObject *parent): QObject(parent) |
| 240 |
{ |
| 241 |
} |
| 242 |
|
| 243 |
LoopQueue::~LoopQueue() |
| 244 |
{ |
| 245 |
} |
| 246 |
|
| 247 |
void LoopQueue::setBeginPathAndPath(const QString &beginPath, const QStringList &list) |
| 248 |
{ |
| 249 |
loop_paths.clear(); |
| 250 |
int newfirst = list.indexOf(beginPath); |
| 251 |
QVector<QString> temp; |
| 252 |
QList<QString>::const_iterator i = list.begin(); |
| 253 |
for (int j = 0; i != list.end() && j < newfirst; j++) { |
| 254 |
temp.append(*i); |
| 255 |
i.operator++(); |
| 256 |
} |
| 257 |
for (; i != list.end();) { |
| 258 |
loop_paths.append(*i); |
| 259 |
i.operator++(); |
| 260 |
} |
| 261 |
loop_paths.append(temp); |
| 262 |
} |
| 263 |
|
| 264 |
const QString LoopQueue::first() const |
| 265 |
{ |
| 266 |
return loop_paths.first(); |
| 267 |
} |
| 268 |
|
| 269 |
const QString LoopQueue::second() const |
| 270 |
{ |
| 271 |
return loop_paths[1]; |
| 272 |
} |
| 273 |
|
| 274 |
const QString LoopQueue::last() const |
| 275 |
{ |
| 276 |
return loop_paths.last(); |
| 277 |
} |
| 278 |
|
| 279 |
int LoopQueue::index() const |
| 280 |
{ |
| 281 |
return loop_pindex; |
| 282 |
} |
| 283 |
|
| 284 |
const QString LoopQueue::next() |
| 285 |
{ |
| 286 |
if (loop_pindex + 1 >= loop_paths.size()) { |
| 287 |
return loop_paths.first(); |
| 288 |
} else { |
| 289 |
return loop_paths[loop_pindex + 1]; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
//const QString LoopQueue::pre() |
| 294 |
//{ |
| 295 |
// if (loop_pindex - 1 < 0) { |
| 296 |
// return loop_paths.last(); |
| 297 |
// } else { |
| 298 |
// return loop_paths[loop_pindex - 1]; |
| 299 |
// } |
| 300 |
//} |
| 301 |
|
| 302 |
const QString LoopQueue::jumpTonext() |
| 303 |
{ |
| 304 |
changeOrder(true); |
| 305 |
AddIndex(); |
| 306 |
if (loop_paths.size() > loop_pindex) |
| 307 |
return loop_paths[loop_pindex]; |
| 308 |
else |
| 309 |
return QString(); |
| 310 |
} |
| 311 |
|
| 312 |
const QString LoopQueue::jumpTopre() |
| 313 |
{ |
| 314 |
changeOrder(false); |
| 315 |
AddIndex(); |
| 316 |
return loop_paths[loop_pindex]; |
| 317 |
} |
| 318 |
|
| 319 |
const QString LoopQueue::current() const |
| 320 |
{ |
| 321 |
return loop_paths[loop_pindex]; |
| 322 |
} |
| 323 |
|
| 324 |
void LoopQueue::changeOrder(bool order) |
| 325 |
{ |
| 326 |
loop_order = order; |
| 327 |
} |
| 328 |
|
| 329 |
void LoopQueue::AddIndex() |
| 330 |
{ |
| 331 |
QMutexLocker locker(&queueMutex); |
| 332 |
if (loop_order) { |
| 333 |
loop_pindex++; |
| 334 |
if (loop_pindex >= loop_paths.size()) { |
| 335 |
loop_pindex = 0; |
| 336 |
} |
| 337 |
} else { |
| 338 |
loop_pindex--; |
| 339 |
if (loop_pindex < 0) { |
| 340 |
loop_pindex = loop_paths.size() - 1; |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
**************************************************************************************************************** |
| 347 |
* ImageAnimationPrivate |
| 348 |
**************************************************************************************************************** |
| 349 |
*/ |
| 350 |
|
| 351 |
ImageAnimationPrivate::ImageAnimationPrivate(ImageAnimation *qq) : |
| 352 |
m_factor(0.0f), m_funval(0.0f), m_animationType(AnimationType::BlindsEffect), queue(nullptr), |
| 353 |
m_continuousanimationTimer(nullptr), m_staticTimer(nullptr), q_ptr(qq) |
| 354 |
{ |
| 355 |
// Q_UNUSED(m_padding2); |
| 356 |
} |
| 357 |
|
| 358 |
ImageAnimationPrivate::~ImageAnimationPrivate() |
| 359 |
{ |
| 360 |
|
| 361 |
} |
| 362 |
|
| 363 |
void ImageAnimationPrivate::effectPainter(QPainter *painter, const QRect &rect) |
| 364 |
{ |
| 365 |
if (m_pixmap1.isNull() || m_pixmap2.isNull()) { |
| 366 |
return; |
| 367 |
} else if (!m_isAnimationIng) { |
| 368 |
painter->drawPixmap(0, 0, m_pixmap2); |
| 369 |
return; |
| 370 |
} |
| 371 |
centrePoint = rect.center(); |
| 372 |
switch (m_animationType) { |
| 373 |
case 0: |
| 374 |
fadeEffect(painter, rect, m_factor, m_pixmap1, m_pixmap2); |
| 375 |
break; |
| 376 |
case 1: |
| 377 |
blindsEffect(painter, rect, m_factor, m_pixmap1, m_pixmap2); |
| 378 |
break; |
| 379 |
case 2: |
| 380 |
flipRightToLeft(painter, rect, m_factor, m_pixmap1, m_pixmap2); |
| 381 |
break; |
| 382 |
case 3: |
| 383 |
outsideToInside(painter, rect, m_factor, m_pixmap1, m_pixmap2); |
| 384 |
break; |
| 385 |
case 4: |
| 386 |
moveLeftToRightEffect(painter, rect, m_factor, m_pixmap1, m_pixmap2); |
| 387 |
break; |
| 388 |
// case 5: |
| 389 |
// moveRightToLeftEffect(painter, rect, m_factor, m_pixmap1, m_pixmap2); |
| 390 |
// break; |
| 391 |
// case 6: |
| 392 |
// moveBottomToUpEffect(painter, rect, m_factor, m_pixmap1, m_pixmap2); |
| 393 |
// break; |
| 394 |
// case 7: |
| 395 |
// moveUpToBottomEffect(painter, rect, m_factor, m_pixmap1, m_pixmap2); |
| 396 |
// break; |
| 397 |
// case 8: |
| 398 |
// moveBottomToLeftUpEffect(painter, rect, m_factor, m_pixmap1, m_pixmap2); |
| 399 |
// break; |
| 400 |
} |
| 401 |
painter->end(); |
| 402 |
} |
| 403 |
|
| 404 |
void ImageAnimationPrivate::forwardPainter(QPainter *painter, const QRect &rect) |
| 405 |
{ |
| 406 |
Q_UNUSED(rect); |
| 407 |
if (m_pixmap1.isNull() || m_pixmap2.isNull()) { |
| 408 |
return; |
| 409 |
} |
| 410 |
Q_Q(ImageAnimation); |
| 411 |
if (!m_continuousanimationTimer && !m_staticTimer) { |
| 412 |
setImage1(m_imageName2); |
| 413 |
setImage2(queue->jumpTonext()); |
| 414 |
painter->drawPixmap(0, 0, m_pixmap1); |
| 415 |
q->setPaintTarget(ImageAnimation::KeepStatic); |
| 416 |
return; |
| 417 |
} |
| 418 |
if (m_continuousanimationTimer) { |
| 419 |
m_continuousanimationTimer->stop(); |
| 420 |
m_continuousanimationTimer->setInterval(0); |
| 421 |
m_factor = 0.0f; |
| 422 |
painter->drawPixmap(0, 0, m_pixmap2); |
| 423 |
q->setPaintTarget(ImageAnimation::KeepStatic); |
| 424 |
m_continuousanimationTimer->deleteLater(); |
| 425 |
} |
| 426 |
if (m_staticTimer) { |
| 427 |
if (m_continuousanimationTimer) |
| 428 |
if (m_continuousanimationTimer->timerId() >= 0) |
| 429 |
killTimer(m_continuousanimationTimer->timerId()); |
| 430 |
} |
| 431 |
q->update(); |
| 432 |
} |
| 433 |
|
| 434 |
//void ImageAnimationPrivate::retreatPainter(QPainter *painter, const QRect &rect) |
| 435 |
//{ |
| 436 |
// Q_UNUSED(rect); |
| 437 |
// if (m_pixmap1.isNull() || m_pixmap2.isNull()) { |
| 438 |
// return; |
| 439 |
// } |
| 440 |
// Q_Q(ImageAnimation); |
| 441 |
// if (!m_continuousanimationTimer && !m_staticTimer) { |
| 442 |
// setImage1(m_imageName2); |
| 443 |
// setImage2(queue->jumpTopre()); |
| 444 |
// painter->drawPixmap(0, 0, m_pixmap1); |
| 445 |
// q->setPaintTarget(ImageAnimation::KeepStatic); |
| 446 |
// return; |
| 447 |
// } |
| 448 |
|
| 449 |
// //动画播放中 |
| 450 |
// if (m_continuousanimationTimer) { |
| 451 |
// m_continuousanimationTimer->stop(); |
| 452 |
// m_continuousanimationTimer->setInterval(0); |
| 453 |
// m_factor = 0.0f; |
| 454 |
// setImage2(queue->jumpTopre()); |
| 455 |
// painter->drawPixmap(0, 0, m_pixmap2); |
| 456 |
// q->setPaintTarget(ImageAnimation::KeepStatic); |
| 457 |
// m_continuousanimationTimer->deleteLater(); |
| 458 |
// } |
| 459 |
// if (m_staticTimer) { |
| 460 |
// if (m_continuousanimationTimer->timerId() >= 0) |
| 461 |
// killTimer(m_continuousanimationTimer->timerId()); |
| 462 |
// if (m_staticTimer->timerId() >= 0) |
| 463 |
// killTimer(m_staticTimer->timerId()); |
| 464 |
// } |
| 465 |
//} |
| 466 |
|
| 467 |
void ImageAnimationPrivate::keepStaticPainter(QPainter *painter, const QRect &rect) |
| 468 |
{ |
| 469 |
Q_UNUSED(rect); |
| 470 |
painter->drawPixmap(0, 0, m_pixmap2); |
| 471 |
} |
| 472 |
|
| 473 |
void ImageAnimationPrivate::fadeEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2) |
| 474 |
{ |
| 475 |
factor = factor + FACTOR_STEP > 1.0f ? 1.0f : factor; |
| 476 |
int alpha = static_cast<int>(255 * (1.0f - factor)); |
| 477 |
QPixmap alphaPixmap(rect.size()); |
| 478 |
alphaPixmap.fill(Qt::transparent); |
| 479 |
|
| 480 |
|
| 481 |
QPainter p1(&alphaPixmap); |
| 482 |
p1.setCompositionMode(QPainter::CompositionMode_Source); |
| 483 |
p1.drawPixmap(0, 0, pixmap1); |
| 484 |
p1.setCompositionMode(QPainter::CompositionMode_DestinationIn); |
| 485 |
p1.fillRect(alphaPixmap.rect(), QColor(0, 0, 0, alpha)); |
| 486 |
p1.end(); |
| 487 |
|
| 488 |
painter->drawPixmap(0, 0, alphaPixmap); |
| 489 |
alpha = 255 - alpha; |
| 490 |
alphaPixmap.fill(Qt::transparent); |
| 491 |
QPainter p2(&alphaPixmap); |
| 492 |
p2.setCompositionMode(QPainter::CompositionMode_Source); |
| 493 |
p2.drawPixmap(0, 0, pixmap2); |
| 494 |
p2.setCompositionMode(QPainter::CompositionMode_DestinationIn); |
| 495 |
p2.fillRect(alphaPixmap.rect(), QColor(0, 0, 0, alpha)); |
| 496 |
p2.end(); |
| 497 |
painter->drawPixmap(0, 0, alphaPixmap); |
| 498 |
} |
| 499 |
|
| 500 |
void ImageAnimationPrivate::blindsEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2) |
| 501 |
{ |
| 502 |
Q_UNUSED(rect); |
| 503 |
factor = factor + FACTOR_STEP > 1.0f ? 1.0f : factor; |
| 504 |
int i, n, dh, ddh; |
| 505 |
painter->drawPixmap(0, 0, pixmap1); |
| 506 |
n = 10; |
| 507 |
dh = pixmap2.height() / n; |
| 508 |
ddh = static_cast<int>(factor * dh); |
| 509 |
if (ddh < 1) { |
| 510 |
ddh = 1; |
| 511 |
} |
| 512 |
for (i = 0; i < n; i++) { |
| 513 |
painter->drawPixmap(0, 0 + i * dh, pixmap2, 0, i * dh, pixmap2.width(), ddh); |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
void ImageAnimationPrivate::flipRightToLeft(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2) |
| 518 |
{ |
| 519 |
int w, h; |
| 520 |
float rot; |
| 521 |
QTransform trans; |
| 522 |
|
| 523 |
w = rect.width(); |
| 524 |
h = rect.height(); |
| 525 |
|
| 526 |
rot = factor * 90.0f; |
| 527 |
trans.translate(static_cast<qreal>(w * (1 - factor)), h / 2); |
| 528 |
trans.rotate(static_cast<qreal>(rot), Qt::YAxis); |
| 529 |
trans.translate(-w, -h / 2); |
| 530 |
|
| 531 |
painter->setTransform(trans); |
| 532 |
painter->drawPixmap(0, 0, pixmap1); |
| 533 |
painter->resetTransform(); |
| 534 |
|
| 535 |
trans.reset(); |
| 536 |
rot = 90 * (factor - 1); |
| 537 |
trans.translate(static_cast<qreal>(w * (1 - factor)), h / 2); |
| 538 |
trans.rotate(static_cast<qreal>(rot), Qt::YAxis); |
| 539 |
trans.translate(0, -h / 2); |
| 540 |
|
| 541 |
painter->setTransform(trans); |
| 542 |
painter->drawPixmap(0, 0, pixmap2); |
| 543 |
painter->resetTransform(); |
| 544 |
} |
| 545 |
|
| 546 |
void ImageAnimationPrivate::outsideToInside(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2) |
| 547 |
{ |
| 548 |
int w, h, x3, y3, dh, ddh; |
| 549 |
w = rect.width(); |
| 550 |
h = rect.height(); |
| 551 |
painter->drawPixmap(0, 0, pixmap1); |
| 552 |
dh = pixmap2.height() / 2; |
| 553 |
ddh = static_cast<int>(factor * dh); |
| 554 |
if (ddh < 1) { |
| 555 |
ddh = 1; |
| 556 |
} |
| 557 |
painter->drawPixmap(0, 0, pixmap2, 0, 0, pixmap2.width(), ddh); |
| 558 |
x3 = (w - pixmap2.width()) / 2; |
| 559 |
y3 = static_cast<int>(dh * (1.0f - factor) + h / 2); |
| 560 |
if (y3 != h / 2) |
| 561 |
y3 += 1; |
| 562 |
painter->drawPixmap(x3, y3, pixmap2, 0, pixmap2.height() - ddh, pixmap2.width(), ddh); |
| 563 |
} |
| 564 |
|
| 565 |
void ImageAnimationPrivate::moveLeftToRightEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2) |
| 566 |
{ |
| 567 |
int x, y, w; |
| 568 |
w = rect.width(); |
| 569 |
// h = rect.height(); |
| 570 |
x = static_cast<int>(0 + w * factor); |
| 571 |
y = 0; |
| 572 |
painter->drawPixmap(x, y, pixmap1); |
| 573 |
x = static_cast<int>(0 + w * (factor - 1)); |
| 574 |
y = 0; |
| 575 |
painter->drawPixmap(x, y, pixmap2); |
| 576 |
} |
| 577 |
|
| 578 |
//void ImageAnimationPrivate::moveRightToLeftEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2) |
| 579 |
//{ |
| 580 |
// Q_UNUSED(painter); |
| 581 |
// Q_UNUSED(rect); |
| 582 |
// Q_UNUSED(factor); |
| 583 |
// Q_UNUSED(pixmap1); |
| 584 |
// Q_UNUSED(pixmap2); |
| 585 |
//} |
| 586 |
|
| 587 |
//void ImageAnimationPrivate::moveBottomToUpEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2) |
| 588 |
//{ |
| 589 |
// Q_UNUSED(painter); |
| 590 |
// Q_UNUSED(rect); |
| 591 |
// Q_UNUSED(factor); |
| 592 |
// Q_UNUSED(pixmap1); |
| 593 |
// Q_UNUSED(pixmap2); |
| 594 |
//} |
| 595 |
|
| 596 |
//void ImageAnimationPrivate::moveUpToBottomEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2) |
| 597 |
//{ |
| 598 |
// Q_UNUSED(painter); |
| 599 |
// Q_UNUSED(rect); |
| 600 |
// Q_UNUSED(factor); |
| 601 |
// Q_UNUSED(pixmap1); |
| 602 |
// Q_UNUSED(pixmap2); |
| 603 |
//} |
| 604 |
|
| 605 |
//void ImageAnimationPrivate::moveBottomToLeftUpEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2) |
| 606 |
//{ |
| 607 |
// Q_UNUSED(painter); |
| 608 |
// Q_UNUSED(rect); |
| 609 |
// Q_UNUSED(factor); |
| 610 |
// Q_UNUSED(pixmap1); |
| 611 |
// Q_UNUSED(pixmap2); |
| 612 |
//} |
| 613 |
|
| 614 |
void ImageAnimationPrivate::setPathList(const QString &first, const QStringList &list) |
| 615 |
{ |
| 616 |
//尝试解决UT报告引用queue的空指针 |
| 617 |
if (queue == nullptr) { |
| 618 |
queue = new LoopQueue(this); |
| 619 |
queue->setBeginPathAndPath(first, list); |
| 620 |
} else { |
| 621 |
queue->setBeginPathAndPath(first, list); |
| 622 |
} |
| 623 |
// queue.reset(new LoopQueue(first, list)); |
| 624 |
setImage1(queue->last()); |
| 625 |
setImage2(queue->first()); |
| 626 |
} |
| 627 |
|
| 628 |
void ImageAnimationPrivate::startAnimation() |
| 629 |
{ |
| 630 |
qsrand(static_cast<uint>(QTime(0, 0, 0).secsTo(QTime::currentTime()))); |
| 631 |
if (m_isRandom) { |
| 632 |
m_animationType = static_cast<AnimationType>(qrand() % (3)); |
| 633 |
} else { |
| 634 |
int type = static_cast<int>(m_animationType); |
| 635 |
type++; |
| 636 |
if (type > 4) { |
| 637 |
type = 0; |
| 638 |
} |
| 639 |
m_animationType = static_cast<AnimationType>(type); |
| 640 |
} |
| 641 |
if (!m_continuousanimationTimer) { |
| 642 |
m_continuousanimationTimer = new QTimer(this); |
| 643 |
m_factor = 0.0f; |
| 644 |
m_funval = 0.0f; |
| 645 |
connect(m_continuousanimationTimer, &QTimer::timeout, this, &ImageAnimationPrivate::onContinuousAnimationTimer); |
| 646 |
} |
| 647 |
m_factor = 0.0f; |
| 648 |
m_funval = 0.0f; |
| 649 |
m_isAnimationIng = true; |
| 650 |
m_continuousanimationTimer->start(UPDATE_RATE); |
| 651 |
} |
| 652 |
|
| 653 |
void ImageAnimationPrivate::startSingleNextAnimation() |
| 654 |
{ |
| 655 |
if (m_isAnimationIng) { |
| 656 |
m_isAnimationIng = false; |
| 657 |
} else { |
| 658 |
setImage1(m_imageName2); |
| 659 |
setImage2(queue->jumpTonext()); |
| 660 |
startAnimation(); |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
void ImageAnimationPrivate::startSinglePreAnimation() |
| 665 |
{ |
| 666 |
if (m_isAnimationIng) { |
| 667 |
m_isAnimationIng = false; |
| 668 |
} else { |
| 669 |
setImage1(m_imageName2); |
| 670 |
setImage2(queue->jumpTopre()); |
| 671 |
startAnimation(); |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
void ImageAnimationPrivate::startStatic() |
| 676 |
{ |
| 677 |
if (!m_staticTimer) { |
| 678 |
m_staticTimer = new QTimer(this); |
| 679 |
m_staticTimer->setSingleShot(true); |
| 680 |
connect(m_staticTimer, &QTimer::timeout, this, &ImageAnimationPrivate::onStaticTimer); |
| 681 |
} |
| 682 |
m_isAnimationIng = false; |
| 683 |
m_staticTimer->start(2500); |
| 684 |
} |
| 685 |
|
| 686 |
void ImageAnimationPrivate::onContinuousAnimationTimer() |
| 687 |
{ |
| 688 |
Q_Q(ImageAnimation); |
| 689 |
m_funval += FACTOR_STEP; |
| 690 |
m_factor += GaussFunction(0.25, 0.5f, 5, m_funval); |
| 691 |
if (m_factor + 0.005f > 1) |
| 692 |
m_factor = 1.0f; |
| 693 |
if (m_funval > 1.0f) { |
| 694 |
m_isAnimationIng = false; |
| 695 |
if (m_PlayOrStatue == ImageAnimation::PlayStatue && m_SliderModel == ImageAnimation::AutoPlayModel) { |
| 696 |
m_continuousanimationTimer->stop(); |
| 697 |
m_funval = 0.0f; |
| 698 |
m_factor = 0.0f; |
| 699 |
startStatic(); |
| 700 |
} |
| 701 |
} else { |
| 702 |
m_continuousanimationTimer->start(UPDATE_RATE); |
| 703 |
q->update(); |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
void ImageAnimationPrivate::onStaticTimer() |
| 708 |
{ |
| 709 |
qDebug() << "ImageAnimationPrivate::onStaticTimer m_PlayOrStatue = " << ImageAnimation::PlayStatue; |
| 710 |
qDebug() << "ImageAnimationPrivate::onStaticTimer m_SliderModel = " << ImageAnimation::AutoPlayModel; |
| 711 |
|
| 712 |
if (m_PlayOrStatue == ImageAnimation::PlayStatue && m_SliderModel == ImageAnimation::AutoPlayModel) { |
| 713 |
qsrand(static_cast<uint>(QTime(0, 0, 0).secsTo(QTime::currentTime()))); |
| 714 |
m_animationType = static_cast<AnimationType>(qrand() % (3)); |
| 715 |
setImage1(m_imageName2); |
| 716 |
setImage2(queue->jumpTonext()); |
| 717 |
startAnimation(); |
| 718 |
} |
| 719 |
} |
| 720 |
|
| 721 |
void ImageAnimationPrivate::setImage1(const QString &imageName1_bar) |
| 722 |
{ |
| 723 |
m_imageName1 = imageName1_bar; |
| 724 |
QImage tImg; |
| 725 |
QString errMsg; |
| 726 |
UnionImage_NameSpace::loadStaticImageFromFile(imageName1_bar, tImg, errMsg); |
| 727 |
|
| 728 |
// 多屏显示问题,定位当前屏幕 |
| 729 |
// 修改:将QPixmap的变形操作放在QImage里完成 |
| 730 |
|
| 731 |
//屏幕ID |
| 732 |
int screenId = dApp->getDAppNew()->desktop()->screenNumber(q_ptr); |
| 733 |
|
| 734 |
//变形 |
| 735 |
if (tImg.width() > tImg.height()) { |
| 736 |
tImg = tImg.scaledToWidth(dApp->getDAppNew()->desktop()->screenGeometry(screenId).size().width()); |
| 737 |
if (tImg.height() > dApp->getDAppNew()->desktop()->screenGeometry(screenId).size().height()) { |
| 738 |
tImg = tImg.scaledToHeight(dApp->getDAppNew()->desktop()->screenGeometry(screenId).size().height()); |
| 739 |
} |
| 740 |
} else { |
| 741 |
tImg = tImg.scaledToHeight(dApp->getDAppNew()->desktop()->screenGeometry(screenId).size().height() + 8); |
| 742 |
if (tImg.width() > dApp->getDAppNew()->desktop()->screenGeometry(screenId).size().width()) { |
| 743 |
tImg = tImg.scaledToWidth(dApp->getDAppNew()->desktop()->screenGeometry(screenId).size().width()); |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
//绘制坐标 |
| 748 |
int beginX = 0; |
| 749 |
int beginY = 0; |
| 750 |
|
| 751 |
// 多屏显示去除x偏移 |
| 752 |
centrePoint = q_ptr->getCurScreenGeometry().center(); |
| 753 |
beginX = centrePoint.x() - tImg.width() / 2; |
| 754 |
beginX = beginX > 0 ? beginX : 0; |
| 755 |
beginY = centrePoint.y() - tImg.height() / 2; |
| 756 |
beginY = beginY > 0 ? beginY : 0; |
| 757 |
|
| 758 |
//绘制 |
| 759 |
m_pixmap1 = QPixmap(dApp->getDAppNew()->desktop()->screenGeometry(screenId).size()); |
| 760 |
QPainter pa1(&m_pixmap1); |
| 761 |
m_pixmap1.fill(QColor("#252525")); |
| 762 |
QPixmap p1 = QPixmap::fromImage(tImg); |
| 763 |
pa1.drawPixmap(beginX, beginY, p1); |
| 764 |
pa1.end(); |
| 765 |
|
| 766 |
} |
| 767 |
|
| 768 |
void ImageAnimationPrivate::setImage2(const QString &imageName2_bar) |
| 769 |
{ |
| 770 |
m_imageName2 = imageName2_bar; |
| 771 |
int beginX = 0, beginY = 0; |
| 772 |
QImage tImg; |
| 773 |
QString errMsg; |
| 774 |
UnionImage_NameSpace::loadStaticImageFromFile(imageName2_bar, tImg, errMsg); |
| 775 |
|
| 776 |
// 修改:将QPixmap的变形操作放在QImage里完成 |
| 777 |
// 双屏下或者多屏下 |
| 778 |
int screenId = dApp->getDAppNew()->desktop()->screenNumber(q_ptr); |
| 779 |
|
| 780 |
//变形 |
| 781 |
if (tImg.width() >= tImg.height()) { |
| 782 |
tImg = tImg.scaledToWidth(dApp->getDAppNew()->desktop()->screenGeometry(screenId).size().width()); |
| 783 |
if (tImg.height() > dApp->getDAppNew()->desktop()->screenGeometry(screenId).size().height()) { |
| 784 |
tImg = tImg.scaledToHeight(dApp->getDAppNew()->desktop()->screenGeometry(screenId).size().height()); |
| 785 |
} |
| 786 |
} else { |
| 787 |
tImg = tImg.scaledToHeight(dApp->getDAppNew()->desktop()->screenGeometry(screenId).size().height() + 8); |
| 788 |
if (tImg.width() > dApp->getDAppNew()->desktop()->screenGeometry(screenId).size().width()) { |
| 789 |
tImg = tImg.scaledToWidth(dApp->getDAppNew()->desktop()->screenGeometry(screenId).size().width()); |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
// 多屏显示下,要去除x偏移 |
| 794 |
centrePoint = q_ptr->getCurScreenGeometry().center(); |
| 795 |
beginX = centrePoint.x() - tImg.width() / 2; |
| 796 |
beginX = beginX > 0 ? beginX : 0; |
| 797 |
beginY = centrePoint.y() - tImg.height() / 2; |
| 798 |
beginY = beginY > 0 ? beginY : 0; |
| 799 |
|
| 800 |
//绘制 |
| 801 |
m_pixmap2 = QPixmap(dApp->getDAppNew()->desktop()->screenGeometry(screenId).size()); |
| 802 |
QPainter pa2(&m_pixmap2); |
| 803 |
m_pixmap2.fill(QColor("#252525")); |
| 804 |
QPixmap p2 = QPixmap::fromImage(tImg); |
| 805 |
pa2.drawPixmap(beginX, beginY, p2); |
| 806 |
pa2.end(); |
| 807 |
} |
| 808 |
|
| 809 |
/** |
| 810 |
**************************************************************************************************************** |
| 811 |
* ImageAnimation |
| 812 |
**************************************************************************************************************** |
| 813 |
*/ |
| 814 |
ImageAnimation::ImageAnimation(QWidget *parent) : |
| 815 |
QWidget(parent), current_target(EffectPlay), d_ptr(new ImageAnimationPrivate(this)) |
| 816 |
{ |
| 817 |
setAttribute(Qt::WA_TransparentForMouseEvents, true); |
| 818 |
setAttribute(Qt::WA_StyledBackground, true); |
| 819 |
QPalette pal(palette()); |
| 820 |
pal.setColor(QPalette::Background, Qt::black); |
| 821 |
setAutoFillBackground(true); |
| 822 |
setPalette(pal); |
| 823 |
} |
| 824 |
|
| 825 |
ImageAnimation::~ImageAnimation() |
| 826 |
{ |
| 827 |
Q_D(ImageAnimation); |
| 828 |
delete d; |
| 829 |
} |
| 830 |
|
| 831 |
void ImageAnimation::startSlideShow(const QString &beginPath, const QStringList &pathlist) |
| 832 |
{ |
| 833 |
Q_D(ImageAnimation); |
| 834 |
setPaintTarget(EffectPlay); |
| 835 |
d->setPathList(beginPath, pathlist); |
| 836 |
// d->startAnimation(); |
| 837 |
d->setPlayOrStatue(ImageAnimation::PlayStatue); |
| 838 |
d->setSlideModel(ImageAnimation::AutoPlayModel); |
| 839 |
d->startStatic(); |
| 840 |
} |
| 841 |
|
| 842 |
void ImageAnimation::endSlider() |
| 843 |
{ |
| 844 |
Q_D(ImageAnimation); |
| 845 |
d->endSlide(); |
| 846 |
} |
| 847 |
|
| 848 |
void ImageAnimation::playAndNext() |
| 849 |
{ |
| 850 |
Q_D(ImageAnimation); |
| 851 |
d->setPlayOrStatue(ImageAnimation::PlayStatue); |
| 852 |
d->setSlideModel(ImageAnimation::ManualPlayModel); |
| 853 |
setPaintTarget(EffectPlay); |
| 854 |
d->startSingleNextAnimation(); |
| 855 |
} |
| 856 |
|
| 857 |
void ImageAnimation::playAndPre() |
| 858 |
{ |
| 859 |
Q_D(ImageAnimation); |
| 860 |
d->setPlayOrStatue(ImageAnimation::PlayStatue); |
| 861 |
d->setSlideModel(ImageAnimation::ManualPlayModel); |
| 862 |
setPaintTarget(EffectPlay); |
| 863 |
d->startSinglePreAnimation(); |
| 864 |
} |
| 865 |
|
| 866 |
void ImageAnimation::pauseAndNext() |
| 867 |
{ |
| 868 |
Q_D(ImageAnimation); |
| 869 |
d->setPlayOrStatue(ImageAnimation::StopStatue); |
| 870 |
d->setSlideModel(ImageAnimation::ManualPlayModel); |
| 871 |
setPaintTarget(SkipToNext); |
| 872 |
d->startSingleNextAnimation(); |
| 873 |
update(); |
| 874 |
} |
| 875 |
|
| 876 |
void ImageAnimation::ifPauseAndContinue() |
| 877 |
{ |
| 878 |
Q_D(ImageAnimation); |
| 879 |
d->setPlayOrStatue(ImageAnimation::PlayStatue); |
| 880 |
d->setSlideModel(ImageAnimation::AutoPlayModel); |
| 881 |
setPaintTarget(EffectPlay); |
| 882 |
d->setImage1(d->m_imageName2); |
| 883 |
d->setImage2(d->queue->jumpTonext()); |
| 884 |
d->startAnimation(); |
| 885 |
} |
| 886 |
|
| 887 |
void ImageAnimation::setIsRandom(bool isRandom) |
| 888 |
{ |
| 889 |
Q_D(ImageAnimation); |
| 890 |
d->setIsRandom(isRandom); |
| 891 |
} |
| 892 |
|
| 893 |
const QRect ImageAnimation::getCurScreenGeometry() |
| 894 |
{ |
| 895 |
int screenId = dApp->getDAppNew()->desktop()->screenNumber(this); |
| 896 |
QRect tempRect = dApp->getDAppNew()->desktop()->screenGeometry(screenId); |
| 897 |
tempRect.setRect(0, 0, tempRect.width(), tempRect.height()); |
| 898 |
return tempRect; |
| 899 |
} |
| 900 |
|
| 901 |
//void ImageAnimation::pauseAndpre() |
| 902 |
//{ |
| 903 |
// setPaintTarget(TurnBackPre); |
| 904 |
// update(); |
| 905 |
//} |
| 906 |
|
| 907 |
void ImageAnimation::paintEvent(QPaintEvent *e) |
| 908 |
{ |
| 909 |
QWidget::paintEvent(e); |
| 910 |
Q_D(ImageAnimation); |
| 911 |
QPainter painter(this); |
| 912 |
painter.setRenderHint(QPainter::Antialiasing, true); |
| 913 |
QRect tempRect = getCurScreenGeometry(); |
| 914 |
switch (current_target) { |
| 915 |
case EffectPlay: { |
| 916 |
|
| 917 |
d->effectPainter(&painter, tempRect); |
| 918 |
break; |
| 919 |
} |
| 920 |
case SkipToNext: { |
| 921 |
d->forwardPainter(&painter, tempRect); |
| 922 |
break; |
| 923 |
} |
| 924 |
// case TurnBackPre: { |
| 925 |
// d->retreatPainter(&painter, tempRect); |
| 926 |
// break; |
| 927 |
// } |
| 928 |
case KeepStatic: { |
| 929 |
d->keepStaticPainter(&painter, tempRect); |
| 930 |
break; |
| 931 |
} |
| 932 |
} |
| 933 |
} |
| 934 |
|
| 935 |
void ImageAnimation::setPaintTarget(PaintTarget target) |
| 936 |
{ |
| 937 |
current_target = target; |
| 938 |
} |
| 939 |
|
| 940 |
#include "imageanimation.moc" |