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.

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