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.

124 lines
3.8KB

  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. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. ImagePreviewComponent::ImagePreviewComponent()
  21. {
  22. }
  23. ImagePreviewComponent::~ImagePreviewComponent()
  24. {
  25. }
  26. //==============================================================================
  27. void ImagePreviewComponent::getThumbSize (int& w, int& h) const
  28. {
  29. const int availableW = proportionOfWidth (0.97f);
  30. const int availableH = getHeight() - 13 * 4;
  31. const double 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::null;
  49. currentDetails = String::empty;
  50. repaint();
  51. ScopedPointer <FileInputStream> in (fileToLoad.createInputStream());
  52. if (in != nullptr)
  53. {
  54. ImageFileFormat* const format = ImageFileFormat::findImageFormatForStream (*in);
  55. if (format != nullptr)
  56. {
  57. currentThumbnail = format->decodeImage (*in);
  58. if (currentThumbnail.isValid())
  59. {
  60. int w = currentThumbnail.getWidth();
  61. int h = currentThumbnail.getHeight();
  62. currentDetails
  63. << fileToLoad.getFileName() << "\n"
  64. << format->getFormatName() << "\n"
  65. << w << " x " << h << " pixels\n"
  66. << File::descriptionOfSizeInBytes (fileToLoad.getSize());
  67. getThumbSize (w, h);
  68. currentThumbnail = currentThumbnail.rescaled (w, h);
  69. }
  70. }
  71. }
  72. }
  73. void ImagePreviewComponent::paint (Graphics& g)
  74. {
  75. if (currentThumbnail.isValid())
  76. {
  77. g.setFont (13.0f);
  78. int w = currentThumbnail.getWidth();
  79. int h = currentThumbnail.getHeight();
  80. getThumbSize (w, h);
  81. const int numLines = 4;
  82. const int totalH = 13 * numLines + h + 4;
  83. const int y = (getHeight() - totalH) / 2;
  84. g.drawImageWithin (currentThumbnail,
  85. (getWidth() - w) / 2, y, w, h,
  86. RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize,
  87. false);
  88. g.drawFittedText (currentDetails,
  89. 0, y + h + 4, getWidth(), 100,
  90. Justification::centredTop, numLines);
  91. }
  92. }
  93. END_JUCE_NAMESPACE