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.

119 lines
3.8KB

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