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.

116 lines
3.6 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2021 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. */
  53. wxBitmapBundle GetBitmapBundle( BITMAPS aBitmapId );
  54. /**
  55. * Constructs and returns a bitmap bundle for the given icon ID, with the bitmaps
  56. * converted to disabled state according to the current UI theme.
  57. * @param aBitmapId is from the BITMAPS enum in bitmaps_list.h
  58. */
  59. wxBitmapBundle GetDisabledBitmapBundle( BITMAPS aBitmapId );
  60. /**
  61. * Retrieves a bitmap from the given bitmap id, scaled to a given factor.
  62. *
  63. * This factor is for legacy reasons divided by 4, so a scale factor of 4 will return the
  64. * original image.
  65. *
  66. * @todo this should be improved to take advantage of a number of different resolution PNGs
  67. * stored in the asset archive, so we take the closest PNG and scale it rather than always
  68. * starting with a low-resolution version.
  69. *
  70. * @param aBitmapId is from the BITMAPS enum in bitmaps_list.h
  71. * @param aScaleFactor is used to scale the bitmap uniformly
  72. * @param aHeight is the requested height in pixels of the source image to scale from
  73. */
  74. wxBitmap GetBitmapScaled( BITMAPS aBitmapId, int aScaleFactor, int aHeight = -1 );
  75. /**
  76. * Notifies the store that the icon theme has been changed by the user, so caches must be
  77. * invalidated.
  78. */
  79. void ThemeChanged();
  80. bool IsDarkTheme() const { return m_theme == wxT( "dark" ); }
  81. private:
  82. wxImage getImage( BITMAPS aBitmapId, int aHeight = -1 );
  83. const wxString& bitmapName( BITMAPS aBitmapId, int aHeight = -1 );
  84. wxString computeBitmapName( BITMAPS aBitmapId, int aHeight = -1 );
  85. void buildBitmapInfoCache();
  86. std::unique_ptr<ASSET_ARCHIVE> m_archive;
  87. std::unordered_map<std::pair<BITMAPS, int>, wxString> m_bitmapNameCache;
  88. std::unordered_map<BITMAPS, std::vector<BITMAP_INFO>> m_bitmapInfoCache;
  89. wxString m_theme;
  90. };
  91. #endif // KICAD_BITMAP_STORE_H