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.

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