JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_ImagePreviewComponent.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
32
36
37//==============================================================================
38void ImagePreviewComponent::getThumbSize (int& w, int& h) const
39{
40 auto availableW = proportionOfWidth (0.97f);
41 auto availableH = getHeight() - 13 * 4;
42
43 auto scale = jmin (1.0,
44 availableW / (double) w,
45 availableH / (double) h);
46
47 w = roundToInt (scale * w);
48 h = roundToInt (scale * h);
49}
50
52{
53 if (fileToLoad != file)
54 {
55 fileToLoad = file;
56 startTimer (100);
57 }
58}
59
61{
62 stopTimer();
63
64 currentThumbnail = Image();
65 currentDetails.clear();
66 repaint();
67
68 FileInputStream in (fileToLoad);
69
70 if (in.openedOk() && fileToLoad.existsAsFile())
71 {
72 if (auto format = ImageFileFormat::findImageFormatForStream (in))
73 {
74 currentThumbnail = format->decodeImage (in);
75
76 if (currentThumbnail.isValid())
77 {
78 auto w = currentThumbnail.getWidth();
79 auto h = currentThumbnail.getHeight();
80
81 currentDetails
82 << fileToLoad.getFileName() << "\n"
83 << format->getFormatName() << "\n"
84 << w << " x " << h << " pixels\n"
86
87 getThumbSize (w, h);
88
89 currentThumbnail = currentThumbnail.rescaled (w, h);
90 }
91 }
92 }
93}
94
96{
97 if (currentThumbnail.isValid())
98 {
99 g.setFont (13.0f);
100
101 auto w = currentThumbnail.getWidth();
102 auto h = currentThumbnail.getHeight();
103 getThumbSize (w, h);
104
105 const int numLines = 4;
106 auto totalH = 13 * numLines + h + 4;
107 auto y = (getHeight() - totalH) / 2;
108
109 g.drawImageWithin (currentThumbnail,
110 (getWidth() - w) / 2, y, w, h,
112 false);
113
114 g.drawFittedText (currentDetails,
115 0, y + h + 4, getWidth(), 100,
117 }
118}
119
120//==============================================================================
122{
123 return std::make_unique<AccessibilityHandler> (*this, AccessibilityRole::image);
124}
125
126} // namespace juce
int proportionOfWidth(float proportion) const noexcept
Returns a proportion of the component's width.
int getHeight() const noexcept
Returns the component's height in pixels.
void repaint()
Marks the whole component as needing to be redrawn.
int getWidth() const noexcept
Returns the component's width in pixels.
An input stream that reads from a local file.
bool openedOk() const noexcept
Returns true if the stream opened without problems.
Represents a local file or directory.
Definition juce_File.h:45
bool existsAsFile() const
Checks whether the file exists and is a file rather than a directory.
int64 getSize() const
Returns the size of the file in bytes.
String getFileName() const
Returns the last section of the pathname.
static String descriptionOfSizeInBytes(int64 bytes)
Utility function to convert a file size in bytes to a neat string description.
A graphics context, used for drawing a component or image.
void drawFittedText(const String &text, int x, int y, int width, int height, Justification justificationFlags, int maximumNumberOfLines, float minimumHorizontalScale=0.0f) const
Tries to draw a text string inside a given space.
void setFont(const Font &newFont)
Changes the font to use for subsequent text-drawing functions.
void drawImageWithin(const Image &imageToDraw, int destX, int destY, int destWidth, int destHeight, RectanglePlacement placementWithinTarget, bool fillAlphaChannelWithCurrentBrush=false) const
Draws an image to fit within a designated rectangle.
static ImageFileFormat * findImageFormatForStream(InputStream &input)
Tries the built-in formats to see if it can find one to read this stream.
ImagePreviewComponent()
Creates an ImagePreviewComponent.
void selectedFileChanged(const File &newSelectedFile) override
Called to indicate that the user's currently selected file has changed.
void paint(Graphics &) override
Components can override this method to draw their content.
std::unique_ptr< AccessibilityHandler > createAccessibilityHandler() override
Override this method to return a custom AccessibilityHandler for this component.
void timerCallback() override
The user-defined callback routine that actually gets called periodically.
Holds a fixed-size bitmap.
Definition juce_Image.h:58
int getWidth() const noexcept
Returns the image's width (in pixels).
int getHeight() const noexcept
Returns the image's height (in pixels).
Image rescaled(int newWidth, int newHeight, Graphics::ResamplingQuality quality=Graphics::mediumResamplingQuality) const
Returns a rescaled version of this image.
bool isValid() const noexcept
Returns true if this image isn't null.
Definition juce_Image.h:147
@ centredTop
Indicates that the item should be centred horizontally and placed at the top.
@ onlyReduceInSize
Indicates that the source rectangle can be reduced in size if required, but should never be made larg...
@ centred
A shorthand value that is equivalent to (xMid | yMid).
void clear() noexcept
Resets this string to be empty.
void stopTimer() noexcept
Stops the timer.
void startTimer(int intervalInMilliseconds) noexcept
Starts the timer and sets the length of interval required.
JUCE Namespace.
constexpr Type jmin(Type a, Type b)
Returns the smaller of two values.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
int roundToInt(const FloatType value) noexcept
Fast floating-point-to-integer conversion.