2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
23 ==============================================================================
29 ImagePreviewComponent::ImagePreviewComponent()
33 ImagePreviewComponent::~ImagePreviewComponent()
37 //==============================================================================
38 void ImagePreviewComponent::getThumbSize (int& w
, int& h
) const
40 auto availableW
= proportionOfWidth (0.97f
);
41 auto availableH
= getHeight() - 13 * 4;
43 auto scale
= jmin (1.0,
44 availableW
/ (double) w
,
45 availableH
/ (double) h
);
47 w
= roundToInt (scale
* w
);
48 h
= roundToInt (scale
* h
);
51 void ImagePreviewComponent::selectedFileChanged (const File
& file
)
53 if (fileToLoad
!= file
)
60 void ImagePreviewComponent::timerCallback()
64 currentThumbnail
= Image();
65 currentDetails
.clear();
68 FileInputStream
in (fileToLoad
);
70 if (in
.openedOk() && fileToLoad
.existsAsFile())
72 if (auto format
= ImageFileFormat::findImageFormatForStream (in
))
74 currentThumbnail
= format
->decodeImage (in
);
76 if (currentThumbnail
.isValid())
78 auto w
= currentThumbnail
.getWidth();
79 auto h
= currentThumbnail
.getHeight();
82 << fileToLoad
.getFileName() << "\n"
83 << format
->getFormatName() << "\n"
84 << w
<< " x " << h
<< " pixels\n"
85 << File::descriptionOfSizeInBytes (fileToLoad
.getSize());
89 currentThumbnail
= currentThumbnail
.rescaled (w
, h
);
95 void ImagePreviewComponent::paint (Graphics
& g
)
97 if (currentThumbnail
.isValid())
101 auto w
= currentThumbnail
.getWidth();
102 auto h
= currentThumbnail
.getHeight();
105 const int numLines
= 4;
106 auto totalH
= 13 * numLines
+ h
+ 4;
107 auto y
= (getHeight() - totalH
) / 2;
109 g
.drawImageWithin (currentThumbnail
,
110 (getWidth() - w
) / 2, y
, w
, h
,
111 RectanglePlacement::centred
| RectanglePlacement::onlyReduceInSize
,
114 g
.drawFittedText (currentDetails
,
115 0, y
+ h
+ 4, getWidth(), 100,
116 Justification::centredTop
, numLines
);
120 //==============================================================================
121 std::unique_ptr
<AccessibilityHandler
> ImagePreviewComponent::createAccessibilityHandler()
123 return std::make_unique
<AccessibilityHandler
> (*this, AccessibilityRole::image
);