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.

122 lines
3.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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_JUCEHEADER__
  18. #define __JUCER_FILEPREVIEWCOMPONENT_JUCEHEADER__
  19. //==============================================================================
  20. /**
  21. */
  22. class ItemPreviewComponent : public Component
  23. {
  24. public:
  25. ItemPreviewComponent (const File& f)
  26. : file (f)
  27. {
  28. setOpaque (true);
  29. tryToLoadImage();
  30. }
  31. void paint (Graphics& g)
  32. {
  33. IntrojucerLookAndFeel::fillWithBackgroundTexture (*this, g);
  34. if (drawable != nullptr)
  35. {
  36. Rectangle<int> area = RectanglePlacement (RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize)
  37. .appliedTo (drawable->getBounds(), Rectangle<int> (4, 22, getWidth() - 8, getHeight() - 26));
  38. Path p;
  39. p.addRectangle (area);
  40. DropShadow (Colours::black.withAlpha (0.5f), 6, Point<int> (0, 1)).drawForPath (g, p);
  41. g.fillCheckerBoard (area, 24, 24, Colour (0xffffffff), Colour (0xffeeeeee));
  42. g.setOpacity (1.0f);
  43. drawable->drawWithin (g, area.toFloat(), RectanglePlacement::stretchToFit, 1.0f);
  44. }
  45. g.setFont (Font (14.0f, Font::bold));
  46. g.setColour (findColour (mainBackgroundColourId).contrasting());
  47. g.drawMultiLineText (facts.joinIntoString ("\n"), 10, 15, getWidth() - 16);
  48. }
  49. private:
  50. StringArray facts;
  51. File file;
  52. ScopedPointer<Drawable> drawable;
  53. void tryToLoadImage()
  54. {
  55. facts.clear();
  56. facts.add (file.getFullPathName());
  57. drawable = nullptr;
  58. {
  59. ScopedPointer <InputStream> input (file.createInputStream());
  60. if (input != nullptr)
  61. {
  62. const int64 totalSize = input->getTotalLength();
  63. String formatName;
  64. if (ImageFileFormat* format = ImageFileFormat::findImageFormatForStream (*input))
  65. formatName = " " + format->getFormatName();
  66. input = nullptr;
  67. Image image (ImageCache::getFromFile (file));
  68. if (image.isValid())
  69. {
  70. DrawableImage* d = new DrawableImage();
  71. d->setImage (image);
  72. drawable = d;
  73. facts.add (String (image.getWidth()) + " x " + String (image.getHeight()) + formatName);
  74. }
  75. if (totalSize > 0)
  76. facts.add (File::descriptionOfSizeInBytes (totalSize));
  77. }
  78. }
  79. if (drawable == nullptr)
  80. {
  81. ScopedPointer<XmlElement> svg (XmlDocument::parse (file));
  82. if (svg != nullptr)
  83. drawable = Drawable::createFromSVG (*svg);
  84. }
  85. facts.removeEmptyStrings (true);
  86. }
  87. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ItemPreviewComponent)
  88. };
  89. #endif // __JUCER_FILEPREVIEWCOMPONENT_JUCEHEADER__