QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskGraphicProviderMap.cpp
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#include "QskGraphicProviderMap.h"
7#include "QskGraphicProvider.h"
8
9#include <qhash.h>
10#include <qpointer.h>
11#include <qstring.h>
12
13static inline QString qskKey( const QString& providerId )
14{
15 return providerId.toLower();
16}
17
18class QskGraphicProviderMap::PrivateData
19{
20 public:
21 QHash< QString, QPointer< QskGraphicProvider > > hashTab;
22};
23
24QskGraphicProviderMap::QskGraphicProviderMap()
25 : m_data( new PrivateData() )
26{
27}
28
29QskGraphicProviderMap::~QskGraphicProviderMap()
30{
31 qDeleteAll( m_data->hashTab );
32}
33
34void QskGraphicProviderMap::clear()
35{
36 qDeleteAll( m_data->hashTab );
37 m_data->hashTab.clear();
38}
39
40void QskGraphicProviderMap::insert(
41 const QString& providerId, QskGraphicProvider* provider )
42{
43 if ( provider == nullptr )
44 {
45 remove( providerId );
46 return;
47 }
48
49 m_data->hashTab.insert( qskKey( providerId ),
50 QPointer< QskGraphicProvider >( provider ) );
51}
52
53void QskGraphicProviderMap::remove( const QString& providerId )
54{
55 delete take( providerId );
56}
57
58QskGraphicProvider* QskGraphicProviderMap::take( const QString& providerId )
59{
60 QskGraphicProvider* provider = nullptr;
61
62 const auto it = m_data->hashTab.constFind( qskKey( providerId ) );
63 if ( it != m_data->hashTab.constEnd() )
64 {
65 provider = it.value();
66 m_data->hashTab.erase( it );
67 }
68
69 return provider;
70}
71
72QskGraphicProvider* QskGraphicProviderMap::provider( const QString& providerId ) const
73{
74 const auto it = m_data->hashTab.constFind( qskKey( providerId ) );
75 if ( it == m_data->hashTab.constEnd() )
76 return nullptr;
77
78 if ( it.value().isNull() )
79 {
80 // someone outside has deleted the provider, we can remove the entry
81 m_data->hashTab.erase( it );
82 }
83
84 return it.value();
85}
86
87int QskGraphicProviderMap::size() const
88{
89 // might not be correct, when providers have been deleted
90 // from the outside. TODO ...
91
92 return m_data->hashTab.count();
93}