The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

118 lines
3.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #pragma once
  19. //==============================================================================
  20. class ItemPreviewComponent final : public Component
  21. {
  22. public:
  23. ItemPreviewComponent (const File& f) : file (f)
  24. {
  25. setOpaque (true);
  26. tryToLoadImage();
  27. }
  28. void paint (Graphics& g) override
  29. {
  30. g.fillAll (findColour (backgroundColourId));
  31. if (drawable != nullptr)
  32. {
  33. auto contentBounds = drawable->getDrawableBounds();
  34. if (auto* dc = dynamic_cast<DrawableComposite*> (drawable.get()))
  35. {
  36. auto r = dc->getContentArea();
  37. if (! r.isEmpty())
  38. contentBounds = r;
  39. }
  40. auto area = RectanglePlacement (RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize)
  41. .appliedTo (contentBounds, Rectangle<float> (4.0f, 22.0f, (float) getWidth() - 8.0f, (float) getHeight() - 26.0f));
  42. Path p;
  43. p.addRectangle (area);
  44. DropShadow (Colours::black.withAlpha (0.5f), 6, Point<int> (0, 1)).drawForPath (g, p);
  45. g.fillCheckerBoard (area, 24.0f, 24.0f, Colour (0xffffffff), Colour (0xffeeeeee));
  46. drawable->draw (g, 1.0f, RectanglePlacement (RectanglePlacement::stretchToFit)
  47. .getTransformToFit (contentBounds, area.toFloat()));
  48. }
  49. g.setFont (Font (14.0f, Font::bold));
  50. g.setColour (findColour (defaultTextColourId));
  51. g.drawMultiLineText (facts.joinIntoString ("\n"), 10, 15, getWidth() - 16);
  52. }
  53. private:
  54. StringArray facts;
  55. File file;
  56. std::unique_ptr<Drawable> drawable;
  57. void tryToLoadImage()
  58. {
  59. facts.clear();
  60. facts.add (file.getFullPathName());
  61. drawable.reset();
  62. if (auto input = std::unique_ptr<FileInputStream> (file.createInputStream()))
  63. {
  64. auto totalSize = input->getTotalLength();
  65. String formatName;
  66. if (auto* format = ImageFileFormat::findImageFormatForStream (*input))
  67. formatName = " " + format->getFormatName();
  68. input.reset();
  69. auto image = ImageCache::getFromFile (file);
  70. if (image.isValid())
  71. {
  72. auto* d = new DrawableImage();
  73. d->setImage (image);
  74. drawable.reset (d);
  75. facts.add (String (image.getWidth()) + " x " + String (image.getHeight()) + formatName);
  76. }
  77. if (totalSize > 0)
  78. facts.add (File::descriptionOfSizeInBytes (totalSize));
  79. }
  80. if (drawable == nullptr)
  81. if (auto svg = parseXML (file))
  82. drawable = Drawable::createFromSVG (*svg);
  83. facts.removeEmptyStrings (true);
  84. }
  85. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ItemPreviewComponent)
  86. };