#include "BmpWidget.h" #include "UserInterfaceUtility.h" #include #include #include #include class BmpWidgetPrivate { Q_DISABLE_COPY(BmpWidgetPrivate) public: explicit BmpWidgetPrivate(BmpWidget* parent); ~BmpWidgetPrivate(); QScopedPointer m_BackgroundPixmap; QScopedPointer m_BackgroundImage; QString m_BackgroundPath; bool m_Reload; private: BmpWidget* m_Parent; }; BmpWidget::BmpWidget(QWidget *parent) : QWidget(parent) , m_Private(new BmpWidgetPrivate(this)) { } BmpWidget::~BmpWidget() { } void BmpWidget::setBackgroundBmpPath(const QString &path) { if (QFile::exists(path)) { if (!m_Private->m_BackgroundImage.isNull()) { m_Private->m_BackgroundImage.reset(NULL); } m_Private->m_BackgroundPixmap.reset(new QPixmap(path)); m_Private->m_BackgroundPath = path; m_Private->m_Reload = false; if (isVisible()) { update(); } } } void BmpWidget::setBackgroundBmpImage(QImage &image, const QString& path) { if ((!image.isNull()) && (QFile::exists(path))) { if (!m_Private->m_BackgroundPixmap.isNull()) { m_Private->m_BackgroundPixmap.reset(NULL); } m_Private->m_BackgroundImage.reset(new QImage(image)); m_Private->m_BackgroundPath = path; m_Private->m_Reload = false; if (isVisible()) { update(); } } } void BmpWidget::styleChange() { m_Private->m_Reload = true; } void BmpWidget::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); if (m_Private->m_Reload) { setBackgroundBmpPath(m_Private->m_BackgroundPath); } if (!m_Private->m_BackgroundPixmap.isNull()) { int x = (width() - m_Private->m_BackgroundPixmap->width()) >> 1; int y = (height() - m_Private->m_BackgroundPixmap->height()) >> 1; painter.drawPixmap(x, y, *m_Private->m_BackgroundPixmap); } else if (!m_Private->m_BackgroundImage.isNull()) { int x = (width() - m_Private->m_BackgroundImage->width()) >> 1; int y = (height() - m_Private->m_BackgroundImage->height()) >> 1; painter.drawImage(x, y, *m_Private->m_BackgroundImage); } } BmpWidgetPrivate::BmpWidgetPrivate(BmpWidget *parent) : m_Parent(parent) { m_BackgroundPath.clear(); m_Reload = false; } BmpWidgetPrivate::~BmpWidgetPrivate() { }