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.

116 lines
3.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. ImagePreviewComponent::ImagePreviewComponent()
  19. {
  20. }
  21. ImagePreviewComponent::~ImagePreviewComponent()
  22. {
  23. }
  24. //==============================================================================
  25. void ImagePreviewComponent::getThumbSize (int& w, int& h) const
  26. {
  27. const int availableW = proportionOfWidth (0.97f);
  28. const int availableH = getHeight() - 13 * 4;
  29. const double scale = jmin (1.0,
  30. availableW / (double) w,
  31. availableH / (double) h);
  32. w = roundToInt (scale * w);
  33. h = roundToInt (scale * h);
  34. }
  35. void ImagePreviewComponent::selectedFileChanged (const File& file)
  36. {
  37. if (fileToLoad != file)
  38. {
  39. fileToLoad = file;
  40. startTimer (100);
  41. }
  42. }
  43. void ImagePreviewComponent::timerCallback()
  44. {
  45. stopTimer();
  46. currentThumbnail = Image::null;
  47. currentDetails = String::empty;
  48. repaint();
  49. ScopedPointer<FileInputStream> in (fileToLoad.createInputStream());
  50. if (in != nullptr)
  51. {
  52. if (ImageFileFormat* const format = ImageFileFormat::findImageFormatForStream (*in))
  53. {
  54. currentThumbnail = format->decodeImage (*in);
  55. if (currentThumbnail.isValid())
  56. {
  57. int w = currentThumbnail.getWidth();
  58. int h = currentThumbnail.getHeight();
  59. currentDetails
  60. << fileToLoad.getFileName() << "\n"
  61. << format->getFormatName() << "\n"
  62. << w << " x " << h << " pixels\n"
  63. << File::descriptionOfSizeInBytes (fileToLoad.getSize());
  64. getThumbSize (w, h);
  65. currentThumbnail = currentThumbnail.rescaled (w, h);
  66. }
  67. }
  68. }
  69. }
  70. void ImagePreviewComponent::paint (Graphics& g)
  71. {
  72. if (currentThumbnail.isValid())
  73. {
  74. g.setFont (13.0f);
  75. int w = currentThumbnail.getWidth();
  76. int h = currentThumbnail.getHeight();
  77. getThumbSize (w, h);
  78. const int numLines = 4;
  79. const int totalH = 13 * numLines + h + 4;
  80. const int y = (getHeight() - totalH) / 2;
  81. g.drawImageWithin (currentThumbnail,
  82. (getWidth() - w) / 2, y, w, h,
  83. RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize,
  84. false);
  85. g.drawFittedText (currentDetails,
  86. 0, y + h + 4, getWidth(), 100,
  87. Justification::centredTop, numLines);
  88. }
  89. }