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.

118 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. ImageFileFormat* const format = ImageFileFormat::findImageFormatForStream (*in);
  53. if (format != nullptr)
  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. }