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.

134 lines
4.4KB

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