Audio plugin host https://kx.studio/carla
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.5KB

  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. namespace juce
  20. {
  21. ImagePreviewComponent::ImagePreviewComponent()
  22. {
  23. }
  24. ImagePreviewComponent::~ImagePreviewComponent()
  25. {
  26. }
  27. //==============================================================================
  28. void ImagePreviewComponent::getThumbSize (int& w, int& h) const
  29. {
  30. const int availableW = proportionOfWidth (0.97f);
  31. const int availableH = getHeight() - 13 * 4;
  32. const double scale = jmin (1.0,
  33. availableW / (double) w,
  34. availableH / (double) h);
  35. w = roundToInt (scale * w);
  36. h = roundToInt (scale * h);
  37. }
  38. void ImagePreviewComponent::selectedFileChanged (const File& file)
  39. {
  40. if (fileToLoad != file)
  41. {
  42. fileToLoad = file;
  43. startTimer (100);
  44. }
  45. }
  46. void ImagePreviewComponent::timerCallback()
  47. {
  48. stopTimer();
  49. currentThumbnail = Image();
  50. currentDetails.clear();
  51. repaint();
  52. ScopedPointer<FileInputStream> in (fileToLoad.createInputStream());
  53. if (in != nullptr && in->getFile().existsAsFile())
  54. {
  55. if (ImageFileFormat* const format = ImageFileFormat::findImageFormatForStream (*in))
  56. {
  57. currentThumbnail = format->decodeImage (*in);
  58. if (currentThumbnail.isValid())
  59. {
  60. int w = currentThumbnail.getWidth();
  61. int h = currentThumbnail.getHeight();
  62. currentDetails
  63. << fileToLoad.getFileName() << "\n"
  64. << format->getFormatName() << "\n"
  65. << w << " x " << h << " pixels\n"
  66. << File::descriptionOfSizeInBytes (fileToLoad.getSize());
  67. getThumbSize (w, h);
  68. currentThumbnail = currentThumbnail.rescaled (w, h);
  69. }
  70. }
  71. }
  72. }
  73. void ImagePreviewComponent::paint (Graphics& g)
  74. {
  75. if (currentThumbnail.isValid())
  76. {
  77. g.setFont (13.0f);
  78. int w = currentThumbnail.getWidth();
  79. int h = currentThumbnail.getHeight();
  80. getThumbSize (w, h);
  81. const int numLines = 4;
  82. const int totalH = 13 * numLines + h + 4;
  83. const int y = (getHeight() - totalH) / 2;
  84. g.drawImageWithin (currentThumbnail,
  85. (getWidth() - w) / 2, y, w, h,
  86. RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize,
  87. false);
  88. g.drawFittedText (currentDetails,
  89. 0, y + h + 4, getWidth(), 100,
  90. Justification::centredTop, numLines);
  91. }
  92. }
  93. } // namespace juce