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.

130 lines
4.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. #include "../Utility/jucer_ProjucerLookAndFeel.h"
  19. //==============================================================================
  20. /**
  21. */
  22. class ItemPreviewComponent : public Component
  23. {
  24. public:
  25. ItemPreviewComponent (const File& f) : file (f)
  26. {
  27. setOpaque (true);
  28. tryToLoadImage();
  29. }
  30. void paint (Graphics& g) override
  31. {
  32. g.fillAll (findColour (backgroundColourId));
  33. if (drawable != nullptr)
  34. {
  35. Rectangle<float> contentBounds (drawable->getDrawableBounds());
  36. if (DrawableComposite* dc = dynamic_cast<DrawableComposite*> (drawable.get()))
  37. {
  38. Rectangle<float> r (dc->getContentArea().resolve (nullptr));
  39. if (! r.isEmpty())
  40. contentBounds = r;
  41. }
  42. Rectangle<float> area = RectanglePlacement (RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize)
  43. .appliedTo (contentBounds, Rectangle<float> (4.0f, 22.0f, getWidth() - 8.0f, getHeight() - 26.0f));
  44. Path p;
  45. p.addRectangle (area);
  46. DropShadow (Colours::black.withAlpha (0.5f), 6, Point<int> (0, 1)).drawForPath (g, p);
  47. g.fillCheckerBoard (area.getSmallestIntegerContainer(), 24, 24,
  48. Colour (0xffffffff), Colour (0xffeeeeee));
  49. drawable->draw (g, 1.0f, RectanglePlacement (RectanglePlacement::stretchToFit)
  50. .getTransformToFit (contentBounds, area.toFloat()));
  51. }
  52. g.setFont (Font (14.0f, Font::bold));
  53. g.setColour (findColour (defaultTextColourId));
  54. g.drawMultiLineText (facts.joinIntoString ("\n"), 10, 15, getWidth() - 16);
  55. }
  56. private:
  57. StringArray facts;
  58. File file;
  59. ScopedPointer<Drawable> drawable;
  60. void tryToLoadImage()
  61. {
  62. facts.clear();
  63. facts.add (file.getFullPathName());
  64. drawable = nullptr;
  65. {
  66. ScopedPointer<InputStream> input (file.createInputStream());
  67. if (input != nullptr)
  68. {
  69. const int64 totalSize = input->getTotalLength();
  70. String formatName;
  71. if (ImageFileFormat* format = ImageFileFormat::findImageFormatForStream (*input))
  72. formatName = " " + format->getFormatName();
  73. input = nullptr;
  74. Image image (ImageCache::getFromFile (file));
  75. if (image.isValid())
  76. {
  77. DrawableImage* d = new DrawableImage();
  78. d->setImage (image);
  79. drawable = d;
  80. facts.add (String (image.getWidth()) + " x " + String (image.getHeight()) + formatName);
  81. }
  82. if (totalSize > 0)
  83. facts.add (File::descriptionOfSizeInBytes (totalSize));
  84. }
  85. }
  86. if (drawable == nullptr)
  87. {
  88. ScopedPointer<XmlElement> svg (XmlDocument::parse (file));
  89. if (svg != nullptr)
  90. drawable = Drawable::createFromSVG (*svg);
  91. }
  92. facts.removeEmptyStrings (true);
  93. }
  94. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ItemPreviewComponent)
  95. };