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.

100 lines
3.0 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 <bitmaps/bitmap_info.h>
  24. class ASSET_ARCHIVE;
  25. class wxImage;
  26. namespace std
  27. {
  28. template<> struct hash<std::pair<BITMAPS, int>>
  29. {
  30. size_t operator()( const std::pair<BITMAPS, int>& aPair ) const;
  31. };
  32. }
  33. /**
  34. * Helper to retrieve bitmaps while handling icon themes and scaling
  35. */
  36. class BITMAP_STORE
  37. {
  38. public:
  39. BITMAP_STORE();
  40. ~BITMAP_STORE() = default;
  41. /**
  42. * Retrieves a bitmap from the given bitmap id
  43. * @param aBitmapId is from the BITMAPS enum in bitmaps_list.h
  44. * @param aHeight is the requested height in pixels of the source image, or -1 for any height
  45. */
  46. wxBitmap GetBitmap( BITMAPS aBitmapId, int aHeight = -1 );
  47. /**
  48. * Retrieves a bitmap from the given bitmap id, scaled to a given factor.
  49. *
  50. * This factor is for legacy reasons divided by 4, so a scale factor of 4 will return the
  51. * original image.
  52. *
  53. * @todo this should be improved to take advantage of a number of different resolution PNGs
  54. * stored in the asset archive, so we take the closest PNG and scale it rather than always
  55. * starting with a low-resolution version.
  56. *
  57. * @param aBitmapId is from the BITMAPS enum in bitmaps_list.h
  58. * @param aScaleFactor is used to scale the bitmap uniformly
  59. * @param aHeight is the requested height in pixels of the source image to scale from
  60. */
  61. wxBitmap GetBitmapScaled( BITMAPS aBitmapId, int aScaleFactor, int aHeight = -1 );
  62. /**
  63. * Notifies the store that the icon theme has been changed by the user, so caches must be
  64. * invalidated.
  65. */
  66. void ThemeChanged();
  67. bool IsDarkTheme() const { return m_theme == wxT( "dark" ); }
  68. private:
  69. wxImage getImage( BITMAPS aBitmapId, int aHeight = -1 );
  70. const wxString& bitmapName( BITMAPS aBitmapId, int aHeight = -1 );
  71. wxString computeBitmapName( BITMAPS aBitmapId, int aHeight = -1 );
  72. void buildBitmapInfoCache();
  73. std::unique_ptr<ASSET_ARCHIVE> m_archive;
  74. std::unordered_map<std::pair<BITMAPS, int>, wxString> m_bitmapNameCache;
  75. std::unordered_map<BITMAPS, std::vector<BITMAP_INFO>> m_bitmapInfoCache;
  76. wxString m_theme;
  77. };
  78. #endif // KICAD_BITMAP_STORE_H