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.

132 lines
4.2KB

  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. #include "../Utility/jucer_ProjucerLookAndFeel.h"
  21. //==============================================================================
  22. /**
  23. */
  24. class ItemPreviewComponent : public Component
  25. {
  26. public:
  27. ItemPreviewComponent (const File& f) : file (f)
  28. {
  29. setOpaque (true);
  30. tryToLoadImage();
  31. }
  32. void paint (Graphics& g) override
  33. {
  34. g.fillAll (findColour (backgroundColourId));
  35. if (drawable != nullptr)
  36. {
  37. Rectangle<float> contentBounds (drawable->getDrawableBounds());
  38. if (DrawableComposite* dc = dynamic_cast<DrawableComposite*> (drawable.get()))
  39. {
  40. Rectangle<float> r (dc->getContentArea().resolve (nullptr));
  41. if (! r.isEmpty())
  42. contentBounds = r;
  43. }
  44. Rectangle<float> area = RectanglePlacement (RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize)
  45. .appliedTo (contentBounds, Rectangle<float> (4.0f, 22.0f, getWidth() - 8.0f, getHeight() - 26.0f));
  46. Path p;
  47. p.addRectangle (area);
  48. DropShadow (Colours::black.withAlpha (0.5f), 6, Point<int> (0, 1)).drawForPath (g, p);
  49. g.fillCheckerBoard (area.getSmallestIntegerContainer(), 24, 24,
  50. Colour (0xffffffff), Colour (0xffeeeeee));
  51. drawable->draw (g, 1.0f, RectanglePlacement (RectanglePlacement::stretchToFit)
  52. .getTransformToFit (contentBounds, area.toFloat()));
  53. }
  54. g.setFont (Font (14.0f, Font::bold));
  55. g.setColour (findColour (defaultTextColourId));
  56. g.drawMultiLineText (facts.joinIntoString ("\n"), 10, 15, getWidth() - 16);
  57. }
  58. private:
  59. StringArray facts;
  60. File file;
  61. ScopedPointer<Drawable> drawable;
  62. void tryToLoadImage()
  63. {
  64. facts.clear();
  65. facts.add (file.getFullPathName());
  66. drawable = nullptr;
  67. {
  68. ScopedPointer<InputStream> input (file.createInputStream());
  69. if (input != nullptr)
  70. {
  71. const int64 totalSize = input->getTotalLength();
  72. String formatName;
  73. if (ImageFileFormat* format = ImageFileFormat::findImageFormatForStream (*input))
  74. formatName = " " + format->getFormatName();
  75. input = nullptr;
  76. Image image (ImageCache::getFromFile (file));
  77. if (image.isValid())
  78. {
  79. DrawableImage* d = new DrawableImage();
  80. d->setImage (image);
  81. drawable = d;
  82. facts.add (String (image.getWidth()) + " x " + String (image.getHeight()) + formatName);
  83. }
  84. if (totalSize > 0)
  85. facts.add (File::descriptionOfSizeInBytes (totalSize));
  86. }
  87. }
  88. if (drawable == nullptr)
  89. {
  90. ScopedPointer<XmlElement> svg (XmlDocument::parse (file));
  91. if (svg != nullptr)
  92. drawable = Drawable::createFromSVG (*svg);
  93. }
  94. facts.removeEmptyStrings (true);
  95. }
  96. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ItemPreviewComponent)
  97. };