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.

128 lines
4.1KB

  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. Rectangle<float> contentBounds (drawable->getDrawableBounds());
  35. if (DrawableComposite* dc = dynamic_cast<DrawableComposite*> (drawable.get()))
  36. {
  37. Rectangle<float> r (dc->getContentArea().resolve (nullptr));
  38. if (! r.isEmpty())
  39. contentBounds = r;
  40. }
  41. Rectangle<float> 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.getSmallestIntegerContainer(), 24, 24,
  47. Colour (0xffffffff), Colour (0xffeeeeee));
  48. drawable->draw (g, 1.0f, RectanglePlacement (RectanglePlacement::stretchToFit)
  49. .getTransformToFit (contentBounds, area.toFloat()));
  50. }
  51. g.setFont (Font (14.0f, Font::bold));
  52. g.setColour (findColour (defaultTextColourId));
  53. g.drawMultiLineText (facts.joinIntoString ("\n"), 10, 15, getWidth() - 16);
  54. }
  55. private:
  56. StringArray facts;
  57. File file;
  58. ScopedPointer<Drawable> drawable;
  59. void tryToLoadImage()
  60. {
  61. facts.clear();
  62. facts.add (file.getFullPathName());
  63. drawable = nullptr;
  64. {
  65. ScopedPointer<InputStream> input (file.createInputStream());
  66. if (input != nullptr)
  67. {
  68. const int64 totalSize = input->getTotalLength();
  69. String formatName;
  70. if (ImageFileFormat* format = ImageFileFormat::findImageFormatForStream (*input))
  71. formatName = " " + format->getFormatName();
  72. input = nullptr;
  73. Image image (ImageCache::getFromFile (file));
  74. if (image.isValid())
  75. {
  76. DrawableImage* d = new DrawableImage();
  77. d->setImage (image);
  78. drawable = d;
  79. facts.add (String (image.getWidth()) + " x " + String (image.getHeight()) + formatName);
  80. }
  81. if (totalSize > 0)
  82. facts.add (File::descriptionOfSizeInBytes (totalSize));
  83. }
  84. }
  85. if (drawable == nullptr)
  86. {
  87. ScopedPointer<XmlElement> svg (XmlDocument::parse (file));
  88. if (svg != nullptr)
  89. drawable = Drawable::createFromSVG (*svg);
  90. }
  91. facts.removeEmptyStrings (true);
  92. }
  93. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ItemPreviewComponent)
  94. };