QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskIcon.cpp
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#include "QskIcon.h"
7#include "QskGraphicProvider.h"
8
9#include <qvariant.h>
10#include <qhashfunctions.h>
11
12static void qskRegisterIcon()
13{
14 qRegisterMetaType< QskIcon >();
15
16#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
17 QMetaType::registerEqualsComparator< QskIcon >();
18#endif
19}
20
21Q_CONSTRUCTOR_FUNCTION( qskRegisterIcon )
22
23QskIcon::QskIcon()
24 : m_data( new Data() )
25{
26}
27
28QskIcon::QskIcon( const QUrl& source )
29 : m_source( source )
30 , m_data( new Data() )
31{
32}
33
34QskIcon::QskIcon( const QskGraphic& graphic )
35 : m_data( new Data() )
36{
37 if ( !graphic.isNull() )
38 m_data->graphic = new QskGraphic( graphic );
39}
40
41bool QskIcon::operator==( const QskIcon& other ) const noexcept
42{
43 if ( m_data == other.m_data )
44 return true; // copy
45
46 if ( !m_source.isEmpty() || !other.m_source.isEmpty() )
47 return m_source == other.m_source;
48
49 if ( m_data->graphic && other.m_data->graphic )
50 return m_data->graphic == other.m_data->graphic;
51
52 return false;
53}
54
55void QskIcon::setSource( const QUrl& source )
56{
57 if ( source != m_source )
58 {
59 m_source = source;
60
61#if 1
62 m_data.detach(); // needed ???
63#endif
64 m_data.reset();
65 }
66}
67
68void QskIcon::setGraphic( const QskGraphic& graphic )
69{
70 m_source.clear();
71 m_data.detach();
72
73 if ( m_data->graphic )
74 *m_data->graphic = graphic;
75 else
76 m_data->graphic = new QskGraphic( graphic );
77}
78
79QskGraphic QskIcon::graphic() const
80{
81 if ( m_data->graphic == nullptr && !m_source.isEmpty() )
82 {
83 /*
84 not detaching: lazy loading should affect all copies
85
86 note: even when loadGraphic returns a null graphic we
87 initialize m_iconData to avoid, that loading will
88 be repeated again and again.
89 */
90
91 m_data->graphic = new QskGraphic( Qsk::loadGraphic( m_source ) );
92 }
93
94 return m_data->graphic ? *m_data->graphic : QskGraphic();
95}
96
97QskHashValue QskIcon::hash( QskHashValue seed ) const
98{
99 if ( !m_source.isEmpty() )
100 return qHash( m_source, seed );
101
102 return maybeGraphic().hash( seed );
103}
104
105#ifndef QT_NO_DEBUG_STREAM
106
107#include <qdebug.h>
108
109QDebug operator<<( QDebug debug, const QskIcon& icon )
110{
111 QDebugStateSaver saver( debug );
112 debug.nospace();
113
114 debug << "Icon" << "( ";
115 if ( !icon.source().isEmpty() )
116 {
117 debug << icon.source();
118 }
119 else
120 {
121 const auto graphic = icon.maybeGraphic();
122 if ( !graphic.isNull() )
123 debug << "I:" << graphic.hash( 0 );
124 }
125
126 debug << " )";
127
128 return debug;
129}
130
131#endif
132
133#include "moc_QskIcon.cpp"
A paint device for scalable graphics.
Definition QskGraphic.h:28