swift
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 
16 namespace 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) { return *this; }
25 
26  QReadLocker readLock(&other.m_lock);
27  auto tuple = std::make_tuple(other.m_pixmap, other.m_hasCachedPixmap, other.m_array);
28  readLock.unlock(); // avoid deadlock
29 
30  QWriteLocker writeLock(&this->m_lock);
31  std::tie(m_pixmap, m_hasCachedPixmap, m_array) = tuple;
32  return *this;
33  }
34 
35  const QPixmap &CPixmap::pixmap() const
36  {
37  QWriteLocker lock(&this->m_lock);
38  if (this->m_hasCachedPixmap) { return this->m_pixmap; }
39 
40  // this part here becomes relevant when marshalling via DBus is used
41  // in this case only the array is transferred
42  this->m_hasCachedPixmap = true;
43  if (this->m_array.isEmpty()) { return this->m_pixmap; }
44  bool s = this->m_pixmap.loadFromData(this->m_array, "PNG");
45  Q_ASSERT(s);
46  Q_UNUSED(s);
47  return this->m_pixmap;
48  }
49 
50  bool CPixmap::isNull() const
51  {
52  QReadLocker lock(&this->m_lock);
53  if (this->m_hasCachedPixmap) { return false; }
54  return (this->m_array.isEmpty() || this->m_array.isNull());
55  }
56 
57  CPixmap::operator QPixmap() const { return pixmap(); }
58 
59  QString CPixmap::convertToQString(bool i18n) const
60  {
61  Q_UNUSED(i18n);
62  return "Pixmap";
63  }
64 
65  QPixmap CPixmap::toPixmap() const { return this->pixmap(); }
66 
67  void CPixmap::fillByteArray()
68  {
69  // no lock needed because this is a private method only called from a constructor
70  QBuffer buffer(&this->m_array);
71  buffer.open(QIODevice::WriteOnly);
72  this->m_pixmap.save(&buffer, "PNG");
73  buffer.close();
74  }
75 
76 } // namespace swift::misc
Pixmap which can be transferred via DBus.
Definition: pixmap.h:28
bool isNull() const
With Pixmap?
Definition: pixmap.cpp:50
const QPixmap & pixmap() const
Corresponding pixmap.
Definition: pixmap.cpp:35
CPixmap()=default
Default constructor.
QString convertToQString(bool i18n=false) const
Cast as QString.
Definition: pixmap.cpp:59
CPixmap & operator=(const CPixmap &other)
Copy assignment (because of mutex)
Definition: pixmap.cpp:22
QPixmap toPixmap() const
Corresponding pixmap.
Definition: pixmap.cpp:65
Mix of the most commonly used mixin classes.
Definition: valueobject.h:114
Free functions in swift::misc.
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition: valueobject.h:67