BmpAnimationWidget.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "BmpAnimationWidget.h"
  2. #include "UserInterfaceUtility.h"
  3. #include <QPainter>
  4. #include <QFile>
  5. #include <QScopedPointer>
  6. #include <QEvent>
  7. class BmpAnimationWidgetPrivate
  8. {
  9. Q_DISABLE_COPY(BmpAnimationWidgetPrivate)
  10. public:
  11. explicit BmpAnimationWidgetPrivate(BmpAnimationWidget* parent);
  12. ~BmpAnimationWidgetPrivate();
  13. QImage m_BackGroundImage;
  14. QString m_BackgroundPath;
  15. bool m_Reload;
  16. bool m_bFull;
  17. bool m_bIgnore;
  18. private:
  19. BmpAnimationWidget* m_Parent;
  20. };
  21. BmpAnimationWidget::BmpAnimationWidget(QWidget *parent)
  22. : QWidget(parent)
  23. , m_Private(new BmpAnimationWidgetPrivate(this))
  24. {
  25. }
  26. BmpAnimationWidget::~BmpAnimationWidget()
  27. {
  28. }
  29. void BmpAnimationWidget::setBackgroundBmpPath(const QString &path)
  30. {
  31. QImageReader ir(path);
  32. m_Private->m_BackGroundImage = ir.read();
  33. if(!m_Private->m_BackGroundImage.isNull()){
  34. m_Private->m_BackGroundImage.scaled(width(),height(),Qt::KeepAspectRatioByExpanding);
  35. m_Private->m_BackgroundPath = path;
  36. m_Private->m_Reload = false;
  37. if (isVisible()) {
  38. update();
  39. }
  40. }
  41. }
  42. void BmpAnimationWidget::setFullMode()
  43. {
  44. m_Private->m_bFull = true;
  45. }
  46. void BmpAnimationWidget::setEventIgnore()
  47. {
  48. m_Private->m_bIgnore = true;
  49. }
  50. void BmpAnimationWidget::styleChange()
  51. {
  52. m_Private->m_Reload = true;
  53. }
  54. void BmpAnimationWidget::paintEvent(QPaintEvent *)
  55. {
  56. QPainter painter(this);
  57. painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
  58. if (m_Private->m_Reload) {
  59. setBackgroundBmpPath(m_Private->m_BackgroundPath);
  60. }
  61. if (!m_Private->m_BackGroundImage.isNull()) {
  62. int x = (width() - m_Private->m_BackGroundImage.width()) >> 1;
  63. int y = (height() - m_Private->m_BackGroundImage.height()) >> 1;
  64. QRect rect(0, 0, width(), height());
  65. if(m_Private->m_bFull) {
  66. painter.drawImage(rect, m_Private->m_BackGroundImage);
  67. } else {
  68. painter.drawImage(x, y, m_Private->m_BackGroundImage);
  69. }
  70. }
  71. }
  72. bool BmpAnimationWidget::event(QEvent *event)
  73. {
  74. switch (event->type()) {
  75. case QEvent::MouseButtonPress:
  76. case QEvent::MouseButtonRelease:
  77. case QEvent::MouseMove: {
  78. if(m_Private->m_bIgnore) {
  79. event->ignore();
  80. }
  81. break;
  82. }
  83. default: {
  84. break;
  85. }
  86. }
  87. return QWidget::event(event);
  88. }
  89. BmpAnimationWidgetPrivate::BmpAnimationWidgetPrivate(BmpAnimationWidget *parent) : m_Parent(parent)
  90. , m_bFull(false)
  91. , m_bIgnore(false)
  92. {
  93. m_BackgroundPath.clear();
  94. m_BackGroundImage.fill(0);
  95. m_Reload = false;
  96. }
  97. BmpAnimationWidgetPrivate::~BmpAnimationWidgetPrivate()
  98. {
  99. }