From ac87e7ef602371280eaf3d58b6c5ec71f9e94aa9 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sun, 18 Jan 2015 21:47:05 +0100 Subject: [PATCH] refs #368, pixmap class which is DBus compliant --- src/blackmisc/pixmap.cpp | 62 ++++++++++++++++++++++++++++++++++++++ src/blackmisc/pixmap.h | 64 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 src/blackmisc/pixmap.cpp create mode 100644 src/blackmisc/pixmap.h diff --git a/src/blackmisc/pixmap.cpp b/src/blackmisc/pixmap.cpp new file mode 100644 index 000000000..10d1b212f --- /dev/null +++ b/src/blackmisc/pixmap.cpp @@ -0,0 +1,62 @@ +/* Copyright (C) 2013 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "pixmap.h" +#include + +namespace BlackMisc +{ + + CPixmap::CPixmap(const QPixmap &pixmap) : m_pixmap(pixmap), m_hasCachedPixmap(true) + { + this->fillByteArray(); + } + + const QPixmap &CPixmap::pixmap() const + { + if (this->m_hasCachedPixmap) { return this->m_pixmap; } + this->m_hasCachedPixmap = true; + if (this->m_array.isEmpty()) { return this->m_pixmap; } + bool s = this->m_pixmap.loadFromData(this->m_array, "PNG"); + Q_ASSERT(s); + Q_UNUSED(s); + return this->m_pixmap; + } + + bool CPixmap::isNull() const + { + if (this->m_hasCachedPixmap) { return false; } + return (this->m_array.isEmpty() || this->m_array.isNull()); + } + + CPixmap::operator QPixmap() const + { + return QPixmap(pixmap()); + } + + QString CPixmap::convertToQString(bool i18n) const + { + Q_UNUSED(i18n); + return "Pixmap"; + } + + QPixmap CPixmap::toPixmap() const + { + return this->pixmap(); + } + + void CPixmap::fillByteArray() + { + QBuffer buffer(&this->m_array); + buffer.open(QIODevice::WriteOnly); + this->m_pixmap.save(&buffer, "PNG"); + buffer.close(); + } + +} // namespace diff --git a/src/blackmisc/pixmap.h b/src/blackmisc/pixmap.h new file mode 100644 index 000000000..240df1a56 --- /dev/null +++ b/src/blackmisc/pixmap.h @@ -0,0 +1,64 @@ +/* Copyright (C) 2013 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKMISC_PIXMAP_H +#define BLACKMISC_PIXMAP_H + +#include "valueobject.h" +#include + +namespace BlackMisc +{ + //! Value object for icons. An icon is stored in the global icon repository and + //! identified by its index. It contains no(!) pyhsical data for the icon itself. + class CPixmap : public CValueObjectStdTuple + { + public: + //! Default constructor. + CPixmap() = default; + + //! Constructor. + CPixmap(const QPixmap &pixmap); + + //! Corresponding pixmap + const QPixmap &pixmap() const; + + //! Implicit conversion + operator QPixmap() const; + + //! With Pixmap? + bool isNull() const; + + protected: + //! \copydoc CValueObject::convertToQString + virtual QString convertToQString(bool i18n = false) const override; + + //! \copydoc CValueObject::toPixmap + virtual QPixmap toPixmap() const override; + + private: + BLACK_ENABLE_TUPLE_CONVERSION(BlackMisc::CPixmap) + + //! + void fillByteArray(); + + mutable QPixmap m_pixmap; //!< cached pixmap, mutable because of lazy initialization + mutable bool m_hasCachedPixmap = false; //!< pixmap? Mutable because of lazy initialization + QByteArray m_array; //!< data of pixmap + }; +} // namespace + +BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::CPixmap, ( + attr(o.m_array, flags()) + )) +Q_DECLARE_METATYPE(BlackMisc::CPixmap) + +#endif // guard