| 1 | /* |
| 2 | Copyright (C) 2007-2008 Tanguy Krotoff <tkrotoff@gmail.com> |
| 3 | Copyright (C) 2008 Lukas Durfina <lukas.durfina@gmail.com> |
| 4 | Copyright (C) 2009 Fathi Boudra <fabo@kde.org> |
| 5 | Copyright (C) 2009-2011 vlc-phonon AUTHORS <kde-multimedia@kde.org> |
| 6 | Copyright (C) 2011-2019 Harald Sitter <sitter@kde.org> |
| 7 | |
| 8 | This library is free software; you can redistribute it and/or |
| 9 | modify it under the terms of the GNU Lesser General Public |
| 10 | License as published by the Free Software Foundation; either |
| 11 | version 2.1 of the License, or (at your option) any later version. |
| 12 | |
| 13 | This library is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | Lesser General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU Lesser General Public |
| 19 | License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | |
| 22 | #ifndef PHONON_VLC_VIDEOWIDGET_H |
| 23 | #define PHONON_VLC_VIDEOWIDGET_H |
| 24 | |
| 25 | #include <QWidget> |
| 26 | |
| 27 | #include <phonon/videowidgetinterface.h> |
| 28 | |
| 29 | #ifdef Q_OS_MAC |
| 30 | #include "video/mac/vlcmacwidget.h" |
| 31 | typedef VlcMacWidget BaseWidget; |
| 32 | #else |
| 33 | typedef QWidget BaseWidget; |
| 34 | #endif |
| 35 | |
| 36 | #include "sinknode.h" |
| 37 | |
| 38 | namespace Phonon { |
| 39 | namespace VLC { |
| 40 | |
| 41 | class SurfacePainter; |
| 42 | |
| 43 | /** \brief Implements the Phonon VideoWidget MediaNode, responsible for displaying video |
| 44 | * |
| 45 | * Phonon video is displayed using this widget. It implements the VideoWidgetInterface. |
| 46 | * It is connected to a media object that provides the video source. Methods to control |
| 47 | * video settings such as brightness or contrast are provided. |
| 48 | */ |
| 49 | class VideoWidget : public BaseWidget, public SinkNode, public VideoWidgetInterface44 |
| 50 | { |
| 51 | Q_OBJECT |
| 52 | Q_INTERFACES(Phonon::VideoWidgetInterface44) |
| 53 | public: |
| 54 | /** |
| 55 | * Constructs a new VideoWidget with the given parent. The video settings members |
| 56 | * are set to their default values. |
| 57 | */ |
| 58 | explicit VideoWidget(QWidget *parent); |
| 59 | |
| 60 | /** |
| 61 | * Death to the VideoWidget! |
| 62 | */ |
| 63 | ~VideoWidget(); |
| 64 | |
| 65 | /** |
| 66 | * Connects the VideoWidget to a media object by setting the video widget |
| 67 | * window system identifier of the media object to that of the owned private |
| 68 | * video widget. It also connects the signal from the mediaObject regarding |
| 69 | * a resize of the video. |
| 70 | * |
| 71 | * If the mediaObject was connected to another VideoWidget, the connection is |
| 72 | * lost. |
| 73 | * |
| 74 | * \see MediaObject |
| 75 | * \param mediaObject What media object to connect to |
| 76 | * \reimp |
| 77 | */ |
| 78 | void handleConnectToMediaObject(MediaObject *mediaObject) override; |
| 79 | /** \reimp */ |
| 80 | void handleDisconnectFromMediaObject(MediaObject *mediaObject) override; |
| 81 | /** \reimp */ |
| 82 | void handleAddToMedia(Media *media) override; |
| 83 | |
| 84 | /** |
| 85 | * \return The aspect ratio previously set for the video widget |
| 86 | */ |
| 87 | Phonon::VideoWidget::AspectRatio aspectRatio() const override; |
| 88 | |
| 89 | /** |
| 90 | * Set the aspect ratio of the video. |
| 91 | * VLC accepted formats are x:y (4:3, 16:9, etc...) expressing the global image aspect. |
| 92 | */ |
| 93 | void setAspectRatio(Phonon::VideoWidget::AspectRatio aspect) override; |
| 94 | |
| 95 | /** |
| 96 | * \return The scale mode previously set for the video widget |
| 97 | */ |
| 98 | Phonon::VideoWidget::ScaleMode scaleMode() const override; |
| 99 | |
| 100 | /** |
| 101 | * Set how the video is scaled, keeping the aspect ratio into account when the video is resized. |
| 102 | * |
| 103 | * The ScaleMode enumeration describes how to treat aspect ratio during resizing of video. |
| 104 | * \li Phonon::VideoWidget::FitInView - the video will be fitted to fill the view keeping aspect ratio |
| 105 | * \li Phonon::VideoWidget::ScaleAndCrop - the video is scaled |
| 106 | */ |
| 107 | void setScaleMode(Phonon::VideoWidget::ScaleMode scale) override; |
| 108 | |
| 109 | /** |
| 110 | * \return The brightness previously set for the video widget |
| 111 | */ |
| 112 | qreal brightness() const override; |
| 113 | |
| 114 | /** |
| 115 | * Set the brightness of the video |
| 116 | */ |
| 117 | Q_INVOKABLE void setBrightness(qreal brightness) override; |
| 118 | |
| 119 | /** |
| 120 | * \return The contrast previously set for the video widget |
| 121 | */ |
| 122 | qreal contrast() const override; |
| 123 | |
| 124 | /** |
| 125 | * Set the contrast of the video |
| 126 | */ |
| 127 | Q_INVOKABLE void setContrast(qreal contrast) override; |
| 128 | |
| 129 | /** |
| 130 | * \return The hue previously set for the video widget |
| 131 | */ |
| 132 | qreal hue() const override; |
| 133 | |
| 134 | /** |
| 135 | * Set the hue of the video |
| 136 | */ |
| 137 | Q_INVOKABLE void setHue(qreal hue) override; |
| 138 | |
| 139 | /** |
| 140 | * \return The saturation previously set for the video widget |
| 141 | */ |
| 142 | qreal saturation() const override; |
| 143 | |
| 144 | /** |
| 145 | * Set the saturation of the video |
| 146 | */ |
| 147 | Q_INVOKABLE void setSaturation(qreal saturation) override; |
| 148 | |
| 149 | /** |
| 150 | * \return The owned widget that is used for the actual draw. |
| 151 | */ |
| 152 | QWidget *widget() override; |
| 153 | |
| 154 | /// \reimp |
| 155 | QSize sizeHint() const override; |
| 156 | |
| 157 | void setVisible(bool visible) override; |
| 158 | |
| 159 | private Q_SLOTS: |
| 160 | /// Updates the sizeHint to match the native size of the video. |
| 161 | /// \param hasVideo \c true when there is a video, \c false otherwise |
| 162 | void updateVideoSize(bool hasVideo); |
| 163 | |
| 164 | /** |
| 165 | * Sets all pending video adjusts (hue, brightness etc.) that the application |
| 166 | * wanted to set before the vidoe became available. |
| 167 | * |
| 168 | * \param videoAvailable whether or not video is available at the time of calling |
| 169 | */ |
| 170 | void processPendingAdjusts(bool videoAvailable); |
| 171 | |
| 172 | /** |
| 173 | * Clears all pending video adjusts (hue, brightness etc.). |
| 174 | */ |
| 175 | void clearPendingAdjusts(); |
| 176 | |
| 177 | protected: |
| 178 | /// \reimp |
| 179 | void paintEvent(QPaintEvent *event) override; |
| 180 | |
| 181 | private: |
| 182 | /** |
| 183 | * Sets whether filter adjust is active or not. |
| 184 | * |
| 185 | * \param adjust true if adjust is supposed to be activated, false if not |
| 186 | * |
| 187 | * \returns whether the adjust request was accepted, if not the callee should |
| 188 | * add the request to m_pendingAdjusts for later processing once a video |
| 189 | * became available. Adjusts get accepted always except when |
| 190 | * MediaObject::hasVideo() is false, so it is not related to the |
| 191 | * actual execution of the request. |
| 192 | */ |
| 193 | bool enableFilterAdjust(bool adjust = true); |
| 194 | |
| 195 | /** |
| 196 | * Converts a Phonon range to a VLC value range. |
| 197 | * |
| 198 | * A Phonon range is always a qreal between -1.0 and 1.0, a VLC range however |
| 199 | * can be any between 0 and 360. This function maps the Phonon value to an |
| 200 | * appropriate value within a specified target range. |
| 201 | * |
| 202 | * \param phononValue the incoming Phonon specific value, should be -1.0:1.0 |
| 203 | * should it however not be within that range will it be |
| 204 | * manually locked (i.e. exceeding values become either -1.0 or 1.0) |
| 205 | * \param upperBoundary the upper boundary for the target range. The lower |
| 206 | * boundary is currently always assumed to be 0 |
| 207 | * \param shift whether or not to shift the Phonon range to positive values |
| 208 | * before mapping to VLC values (useful when our 0 must be a VLC 0). |
| 209 | * Please note that if you do not shift the range will be reduced to |
| 210 | * 0:1, phononValue < 0 will be set to 0. |
| 211 | * |
| 212 | * \returns float usable to VLC |
| 213 | */ |
| 214 | static float phononRangeToVlcRange(qreal phononValue, float upperBoundary, |
| 215 | bool shift = true); |
| 216 | |
| 217 | /** |
| 218 | * \return The snapshot of the current video frame. |
| 219 | */ |
| 220 | QImage snapshot() const override; |
| 221 | |
| 222 | /** |
| 223 | * Enables the mighty surface painter (qpaints frames). |
| 224 | */ |
| 225 | void enableSurfacePainter(); |
| 226 | |
| 227 | /** |
| 228 | * Pending video adjusts the application tried to set before we actually |
| 229 | * had a video to set them on. |
| 230 | */ |
| 231 | QHash<QByteArray, qreal> m_pendingAdjusts; |
| 232 | |
| 233 | /** |
| 234 | * Original size of the video, needed for sizeHint(). |
| 235 | */ |
| 236 | QSize m_videoSize; |
| 237 | |
| 238 | Phonon::VideoWidget::AspectRatio m_aspectRatio; |
| 239 | Phonon::VideoWidget::ScaleMode m_scaleMode; |
| 240 | |
| 241 | bool m_filterAdjustActivated; |
| 242 | qreal m_brightness; |
| 243 | qreal m_contrast; |
| 244 | qreal m_hue; |
| 245 | qreal m_saturation; |
| 246 | |
| 247 | SurfacePainter *m_surfacePainter; |
| 248 | }; |
| 249 | |
| 250 | } // namespace VLC |
| 251 | } // namespace Phonon |
| 252 | |
| 253 | #endif // PHONON_VLC_VIDEOWIDGET_H |
| 254 | |