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.

juce_ImagePreviewComponent.cpp 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. ImagePreviewComponent::ImagePreviewComponent()
  21. {
  22. }
  23. ImagePreviewComponent::~ImagePreviewComponent()
  24. {
  25. }
  26. //==============================================================================
  27. void ImagePreviewComponent::getThumbSize (int& w, int& h) const
  28. {
  29. auto availableW = proportionOfWidth (0.97f);
  30. auto availableH = getHeight() - 13 * 4;
  31. auto scale = jmin (1.0,
  32. availableW / (double) w,
  33. availableH / (double) h);
  34. w = roundToInt (scale * w);
  35. h = roundToInt (scale * h);
  36. }
  37. void ImagePreviewComponent::selectedFileChanged (const File& file)
  38. {
  39. if (fileToLoad != file)
  40. {
  41. fileToLoad = file;
  42. startTimer (100);
  43. }
  44. }
  45. void ImagePreviewComponent::timerCallback()
  46. {
  47. stopTimer();
  48. currentThumbnail = Image();
  49. currentDetails.clear();
  50. repaint();
  51. FileInputStream in (fileToLoad);
  52. if (in.openedOk() && fileToLoad.existsAsFile())
  53. {
  54. if (auto format = ImageFileFormat::findImageFormatForStream (in))
  55. {
  56. currentThumbnail = format->decodeImage (in);
  57. if (currentThumbnail.isValid())
  58. {
  59. auto w = currentThumbnail.getWidth();
  60. auto h = currentThumbnail.getHeight();
  61. currentDetails
  62. << fileToLoad.getFileName() << "\n"
  63. << format->getFormatName() << "\n"
  64. << w << " x " << h << " pixels\n"
  65. << File::descriptionOfSizeInBytes (fileToLoad.getSize());
  66. getThumbSize (w, h);
  67. currentThumbnail = currentThumbnail.rescaled (w, h);
  68. }
  69. }
  70. }
  71. }
  72. void ImagePreviewComponent::paint (Graphics& g)
  73. {
  74. if (currentThumbnail.isValid())
  75. {
  76. g.setFont (13.0f);
  77. auto w = currentThumbnail.getWidth();
  78. auto h = currentThumbnail.getHeight();
  79. getThumbSize (w, h);
  80. const int numLines = 4;
  81. auto totalH = 13 * numLines + h + 4;
  82. auto y = (getHeight() - totalH) / 2;
  83. g.drawImageWithin (currentThumbnail,
  84. (getWidth() - w) / 2, y, w, h,
  85. RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize,
  86. false);
  87. g.drawFittedText (currentDetails,
  88. 0, y + h + 4, getWidth(), 100,
  89. Justification::centredTop, numLines);
  90. }
  91. }
  92. //==============================================================================
  93. std::unique_ptr<AccessibilityHandler> ImagePreviewComponent::createAccessibilityHandler()
  94. {
  95. return std::make_unique<AccessibilityHandler> (*this, AccessibilityRole::image);
  96. }
  97. } // namespace juce