You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
4.0 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef KICAD_BITMAP_STORE_H
  20. #define KICAD_BITMAP_STORE_H
  21. #include <memory>
  22. #include <unordered_map>
  23. #include <wx/bmpbndl.h>
  24. #include <bitmaps/bitmap_info.h>
  25. #include <kicommon.h>
  26. class ASSET_ARCHIVE;
  27. class wxImage;
  28. namespace std
  29. {
  30. template<> struct KICOMMON_API hash<std::pair<BITMAPS, int>>
  31. {
  32. size_t operator()( const std::pair<BITMAPS, int>& aPair ) const;
  33. };
  34. }
  35. /**
  36. * Helper to retrieve bitmaps while handling icon themes and scaling
  37. */
  38. class KICOMMON_API BITMAP_STORE
  39. {
  40. public:
  41. BITMAP_STORE();
  42. ~BITMAP_STORE() = default;
  43. /**
  44. * Retrieves a bitmap from the given bitmap id
  45. * @param aBitmapId is from the BITMAPS enum in bitmaps_list.h
  46. * @param aHeight is the requested height in pixels of the source image, or -1 for any height
  47. */
  48. wxBitmap GetBitmap( BITMAPS aBitmapId, int aHeight = -1 );
  49. /**
  50. * Constructs and returns a bitmap bundle containing all available sizes of the given ID
  51. * @param aBitmapId is from the BITMAPS enum in bitmaps_list.h
  52. * @param aMinHeight is the minimum height of the bitmaps to include in the bundle.
  53. * This is important for uses of GetPreferredBitmap and more on wxBitmap bundles because
  54. * wx assumes the smallest bitmap is the "original" intended size. This is a problem where
  55. * some icons may be reused between controls at different intended sizes.
  56. */
  57. wxBitmapBundle GetBitmapBundle( BITMAPS aBitmapId, int aMinHeight = -1 );
  58. /**
  59. * Constructs and returns a bitmap bundle for the given icon ID, with the bitmaps
  60. * converted to disabled state according to the current UI theme.
  61. * @param aBitmapId is from the BITMAPS enum in bitmaps_list.h
  62. */
  63. wxBitmapBundle GetDisabledBitmapBundle( BITMAPS aBitmapId );
  64. /**
  65. * Retrieves a bitmap from the given bitmap id, scaled to a given factor.
  66. *
  67. * This factor is for legacy reasons divided by 4, so a scale factor of 4 will return the
  68. * original image.
  69. *
  70. * @todo this should be improved to take advantage of a number of different resolution PNGs
  71. * stored in the asset archive, so we take the closest PNG and scale it rather than always
  72. * starting with a low-resolution version.
  73. *
  74. * @param aBitmapId is from the BITMAPS enum in bitmaps_list.h
  75. * @param aScaleFactor is used to scale the bitmap uniformly
  76. * @param aHeight is the requested height in pixels of the source image to scale from
  77. */
  78. wxBitmap GetBitmapScaled( BITMAPS aBitmapId, int aScaleFactor, int aHeight = -1 );
  79. /**
  80. * Notifies the store that the icon theme has been changed by the user, so caches must be
  81. * invalidated.
  82. */
  83. void ThemeChanged();
  84. bool IsDarkTheme() const { return m_theme == wxT( "dark" ); }
  85. private:
  86. wxImage getImage( BITMAPS aBitmapId, int aHeight = -1 );
  87. const wxString& bitmapName( BITMAPS aBitmapId, int aHeight = -1 );
  88. wxString computeBitmapName( BITMAPS aBitmapId, int aHeight = -1 );
  89. void buildBitmapInfoCache();
  90. std::unique_ptr<ASSET_ARCHIVE> m_archive;
  91. std::unordered_map<std::pair<BITMAPS, int>, wxString> m_bitmapNameCache;
  92. std::unordered_map<BITMAPS, std::vector<BITMAP_INFO>> m_bitmapInfoCache;
  93. wxString m_theme;
  94. };
  95. #endif // KICAD_BITMAP_STORE_H