BmpWidget.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "BmpWidget.h"
  2. #include "UserInterfaceUtility.h"
  3. #include <QPainter>
  4. #include <QFile>
  5. #include <QScopedPointer>
  6. #include <QEvent>
  7. class BmpWidgetPrivate
  8. {
  9. Q_DISABLE_COPY(BmpWidgetPrivate)
  10. public:
  11. explicit BmpWidgetPrivate(BmpWidget* parent);
  12. ~BmpWidgetPrivate();
  13. QScopedPointer<QPixmap> m_BackgroundPixmap;
  14. QScopedPointer<QImage> m_BackgroundImage;
  15. QString m_BackgroundPath;
  16. bool m_Reload;
  17. private:
  18. BmpWidget* m_Parent;
  19. };
  20. BmpWidget::BmpWidget(QWidget *parent)
  21. : QWidget(parent)
  22. , m_Private(new BmpWidgetPrivate(this))
  23. {
  24. }
  25. BmpWidget::~BmpWidget()
  26. {
  27. }
  28. void BmpWidget::setBackgroundBmpPath(const QString &path)
  29. {
  30. if (QFile::exists(path)) {
  31. if (!m_Private->m_BackgroundImage.isNull()) {
  32. m_Private->m_BackgroundImage.reset(NULL);
  33. }
  34. m_Private->m_BackgroundPixmap.reset(new QPixmap(path));
  35. m_Private->m_BackgroundPath = path;
  36. m_Private->m_Reload = false;
  37. if (isVisible()) {
  38. update();
  39. }
  40. }
  41. }
  42. void BmpWidget::setBackgroundBmpImage(QImage &image, const QString& path)
  43. {
  44. if ((!image.isNull())
  45. && (QFile::exists(path))) {
  46. if (!m_Private->m_BackgroundPixmap.isNull()) {
  47. m_Private->m_BackgroundPixmap.reset(NULL);
  48. }
  49. m_Private->m_BackgroundImage.reset(new QImage(image));
  50. m_Private->m_BackgroundPath = path;
  51. m_Private->m_Reload = false;
  52. if (isVisible()) {
  53. update();
  54. }
  55. }
  56. }
  57. void BmpWidget::styleChange()
  58. {
  59. m_Private->m_Reload = true;
  60. }
  61. void BmpWidget::paintEvent(QPaintEvent *event)
  62. {
  63. Q_UNUSED(event);
  64. QPainter painter(this);
  65. if (m_Private->m_Reload) {
  66. setBackgroundBmpPath(m_Private->m_BackgroundPath);
  67. }
  68. if (!m_Private->m_BackgroundPixmap.isNull()) {
  69. int x = (width() - m_Private->m_BackgroundPixmap->width()) >> 1;
  70. int y = (height() - m_Private->m_BackgroundPixmap->height()) >> 1;
  71. painter.drawPixmap(x, y, *m_Private->m_BackgroundPixmap);
  72. } else if (!m_Private->m_BackgroundImage.isNull()) {
  73. int x = (width() - m_Private->m_BackgroundImage->width()) >> 1;
  74. int y = (height() - m_Private->m_BackgroundImage->height()) >> 1;
  75. painter.drawImage(x, y, *m_Private->m_BackgroundImage);
  76. }
  77. }
  78. BmpWidgetPrivate::BmpWidgetPrivate(BmpWidget *parent)
  79. : m_Parent(parent)
  80. {
  81. m_BackgroundPath.clear();
  82. m_Reload = false;
  83. }
  84. BmpWidgetPrivate::~BmpWidgetPrivate()
  85. {
  86. }