File: bitmap_store.h

package info (click to toggle)
kicad 9.0.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 769,124 kB
  • sloc: cpp: 960,330; ansic: 121,001; xml: 66,428; python: 18,382; sh: 1,010; awk: 301; asm: 292; makefile: 227; javascript: 167; perl: 10
file content (120 lines) | stat: -rw-r--r-- 4,100 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef KICAD_BITMAP_STORE_H
#define KICAD_BITMAP_STORE_H

#include <memory>
#include <unordered_map>

#include <wx/bmpbndl.h>

#include <bitmaps/bitmap_info.h>
#include <kicommon.h>

class ASSET_ARCHIVE;
class wxImage;


namespace std
{
    template<> struct KICOMMON_API hash<std::pair<BITMAPS, int>>
    {
        size_t operator()( const std::pair<BITMAPS, int>& aPair ) const;
    };
}

/**
 * Helper to retrieve bitmaps while handling icon themes and scaling
 */
class KICOMMON_API BITMAP_STORE
{
public:
    BITMAP_STORE();

    ~BITMAP_STORE() = default;

    /**
     * Retrieves a bitmap from the given bitmap id
     * @param aBitmapId is from the BITMAPS enum in bitmaps_list.h
     * @param aHeight is the requested height in pixels of the source image, or -1 for any height
     */
    wxBitmap GetBitmap( BITMAPS aBitmapId, int aHeight = -1 );

    /**
     * Constructs and returns a bitmap bundle containing all available sizes of the given ID
     * @param aBitmapId is from the BITMAPS enum in bitmaps_list.h
     * @param aMinHeight is the minimum height of the bitmaps to include in the bundle.
     * This is important for uses of GetPreferredBitmap and more on wxBitmap bundles because
     * wx assumes the smallest bitmap is the "original" intended size. This is a problem where
     * some icons may be reused between controls at different intended sizes.
     */
    wxBitmapBundle GetBitmapBundle( BITMAPS aBitmapId, int aMinHeight = -1 );
     
    /**
     * Constructs and returns a bitmap bundle for the given icon ID, with the bitmaps
     * converted to disabled state according to the current UI theme.
     * @param aBitmapId is from the BITMAPS enum in bitmaps_list.h
     */
    wxBitmapBundle GetDisabledBitmapBundle( BITMAPS aBitmapId );

    /**
     * Retrieves a bitmap from the given bitmap id, scaled to a given factor.
     *
     * This factor is for legacy reasons divided by 4, so a scale factor of 4 will return the
     * original image.
     *
     * @todo this should be improved to take advantage of a number of different resolution PNGs
     * stored in the asset archive, so we take the closest PNG and scale it rather than always
     * starting with a low-resolution version.
     *
     * @param aBitmapId is from the BITMAPS enum in bitmaps_list.h
     * @param aScaleFactor is used to scale the bitmap uniformly
     * @param aHeight is the requested height in pixels of the source image to scale from
     */
    wxBitmap GetBitmapScaled( BITMAPS aBitmapId, int aScaleFactor, int aHeight = -1 );

    /**
     * Notifies the store that the icon theme has been changed by the user, so caches must be
     * invalidated.
     */
    void ThemeChanged();

    bool IsDarkTheme() const { return m_theme == wxT( "dark" ); }

private:

    wxImage getImage( BITMAPS aBitmapId, int aHeight = -1 );

    const wxString& bitmapName( BITMAPS aBitmapId, int aHeight = -1 );

    wxString computeBitmapName( BITMAPS aBitmapId, int aHeight = -1 );

    void buildBitmapInfoCache();

    std::unique_ptr<ASSET_ARCHIVE> m_archive;

    std::unordered_map<std::pair<BITMAPS, int>, wxString> m_bitmapNameCache;

    std::unordered_map<BITMAPS, std::vector<BITMAP_INFO>> m_bitmapInfoCache;

    wxString m_theme;
};

#endif // KICAD_BITMAP_STORE_H