| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- #include "BmpWidget.h"
- #include "UserInterfaceUtility.h"
- #include <QPainter>
- #include <QFile>
- #include <QScopedPointer>
- #include <QEvent>
- class BmpWidgetPrivate
- {
- Q_DISABLE_COPY(BmpWidgetPrivate)
- public:
- explicit BmpWidgetPrivate(BmpWidget* parent);
- ~BmpWidgetPrivate();
- QScopedPointer<QPixmap> m_BackgroundPixmap;
- QScopedPointer<QImage> 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()
- {
- }
|