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.

114 lines
3.1KB

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