swift
Loading...
Searching...
No Matches
pixmap.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
4#include "misc/pixmap.h"
5
6#include <tuple>
7
8#include <QBuffer>
9#include <QIODevice>
10#include <QReadLocker>
11#include <QWriteLocker>
12#include <QtGlobal>
13
15
16namespace swift::misc
17{
18 CPixmap::CPixmap(const QPixmap &pixmap) : m_pixmap(pixmap), m_hasCachedPixmap(true) { this->fillByteArray(); }
19
20 CPixmap::CPixmap(const CPixmap &other) : CValueObject(other) { *this = other; }
21
23 {
24 if (this == &other)
25 {
26 return *this;
27 }
28
29 QReadLocker readLock(&other.m_lock);
30 auto tuple = std::make_tuple(other.m_pixmap, other.m_hasCachedPixmap, other.m_array);
31 readLock.unlock(); // avoid deadlock
32
33 QWriteLocker writeLock(&this->m_lock);
34 std::tie(m_pixmap, m_hasCachedPixmap, m_array) = tuple;
35 return *this;
36 }
37
38 const QPixmap &CPixmap::pixmap() const
39 {
40 QWriteLocker lock(&this->m_lock);
41 if (this->m_hasCachedPixmap)
42 {
43 return this->m_pixmap;
44 }
45
46 // this part here becomes relevant when marshalling via DBus is used
47 // in this case only the array is transferred
48 this->m_hasCachedPixmap = true;
49 if (this->m_array.isEmpty())
50 {
51 return this->m_pixmap;
52 }
53 bool s = this->m_pixmap.loadFromData(this->m_array, "PNG");
54 Q_ASSERT(s);
55 Q_UNUSED(s);
56 return this->m_pixmap;
57 }
58
59 bool CPixmap::isNull() const
60 {
61 QReadLocker lock(&this->m_lock);
62 if (this->m_hasCachedPixmap)
63 {
64 return false;
65 }
66 return (this->m_array.isEmpty() || this->m_array.isNull());
67 }
68
69 CPixmap::operator QPixmap() const { return pixmap(); }
70
72 {
73 Q_UNUSED(i18n);
74 return "Pixmap";
75 }
76
77 QPixmap CPixmap::toPixmap() const { return this->pixmap(); }
78
79 void CPixmap::fillByteArray()
80 {
81 // no lock needed because this is a private method only called from a constructor
82 QBuffer buffer(&this->m_array);
83 buffer.open(QIODevice::WriteOnly);
84 this->m_pixmap.save(&buffer, "PNG");
85 buffer.close();
86 }
87
88} // namespace swift::misc
bool isNull() const
With Pixmap?
Definition pixmap.cpp:59
const QPixmap & pixmap() const
Corresponding pixmap.
Definition pixmap.cpp:38
CPixmap()=default
Default constructor.
QString convertToQString(bool i18n=false) const
Cast as QString.
Definition pixmap.cpp:71
CPixmap & operator=(const CPixmap &other)
Copy assignment (because of mutex).
Definition pixmap.cpp:22
QPixmap toPixmap() const
Corresponding pixmap.
Definition pixmap.cpp:77
Free functions in swift::misc.
bool save(QIODevice *device, const char *format, int quality) const const
void unlock()
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition valueobject.h:67