File indexing completed on 2024-04-28 04:50:50

0001 /*
0002  * mediawidget.h
0003  *
0004  * Copyright (C) 2007-2011 Christoph Pfister <christophpfister@gmail.com>
0005  *
0006  * This program is free software; you can redistribute it and/or modify
0007  * it under the terms of the GNU General Public License as published by
0008  * the Free Software Foundation; either version 2 of the License, or
0009  * (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License along
0017  * with this program; if not, write to the Free Software Foundation, Inc.,
0018  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0019  */
0020 
0021 #ifndef MEDIAWIDGET_H
0022 #define MEDIAWIDGET_H
0023 
0024 #include <QWidget>
0025 #include <QIcon>
0026 #include <QWeakPointer>
0027 #include <QUrl>
0028 #include <QToolBar>
0029 #include <QMenu>
0030 
0031 class KActionCollection;
0032 class KToolBar;
0033 class QActionGroup;
0034 class QComboBox;
0035 class QMenu;
0036 class QPushButton;
0037 class QSlider;
0038 class QStringListModel;
0039 class QWidgetAction;
0040 
0041 class AbstractMediaWidget;
0042 class MediaSource;
0043 class OsdWidget;
0044 class SeekSlider;
0045 
0046 class MediaWidget : public QWidget
0047 {
0048     Q_OBJECT
0049 public:
0050     MediaWidget(QMenu *menu_, QToolBar *toolBar, KActionCollection *collection,
0051         QWidget *parent);
0052     ~MediaWidget();
0053 
0054     static QString extensionFilter(); // usable for KFileDialog::setFilter()
0055 
0056     enum AspectRatio
0057     {
0058         AspectRatioAuto,
0059         AspectRatio1_1,
0060         AspectRatio4_3,
0061         AspectRatio5_4,
0062         AspectRatio16_9,
0063         AspectRatio16_10,
0064         AspectRatio221_100,
0065         AspectRatio235_100,
0066         AspectRatio239_100,
0067     };
0068 
0069     enum DeinterlaceMode
0070     {
0071         DeinterlaceDisabled,
0072         DeinterlaceDiscard,
0073         DeinterlaceBob,
0074         DeinterlaceLinear,
0075         DeinterlaceYadif,
0076         DeinterlaceYadif2x,
0077         DeinterlacePhosphor,
0078         DeinterlaceX,
0079         DeinterlaceMean,
0080         DeinterlaceBlend,
0081         DeinterlaceIvtc,
0082     };
0083 
0084     enum DisplayMode
0085     {
0086         NormalMode,
0087         FullScreenMode,
0088         FullScreenReturnToMinimalMode,
0089         MinimalMode
0090     };
0091 
0092     enum MetadataType
0093     {
0094         Title,
0095         Artist,
0096         Album,
0097         TrackNumber
0098     };
0099 
0100     enum PlaybackStatus
0101     {
0102         Idle,
0103         Playing,
0104         Paused
0105     };
0106 
0107     DisplayMode getDisplayMode() const;
0108     void setDisplayMode(DisplayMode displayMode_);
0109 
0110     /*
0111      * loads the media and starts playback
0112      */
0113 
0114     void play(MediaSource *source_);
0115     void play(const QUrl &url, const QUrl &subtitleUrl = QUrl());
0116     void playAudioCd(const QString &device);
0117     void playVideoCd(const QString &device);
0118     void playDvd(const QString &device);
0119 
0120     OsdWidget *getOsdWidget();
0121 
0122     PlaybackStatus getPlaybackStatus() const;
0123     int getPosition() const; // milliseconds
0124     int getVolume() const; // 0 - 100
0125 
0126     void play(); // (re-)starts the current media
0127     void togglePause();
0128     void setPosition(int position); // milliseconds
0129     void setVolume(int volume); // 0 - 100
0130     void toggleMuted();
0131     void mediaSourceDestroyed(MediaSource *mediaSource);
0132     void setAspectRatio(MediaWidget::AspectRatio aspectRatio);
0133 
0134 public slots:
0135     void previous();
0136     void next();
0137     void stop();
0138     void increaseVolume();
0139     void decreaseVolume();
0140     void toggleFullScreen();
0141     void toggleMinimalMode();
0142     void shortSkipBackward();
0143     void shortSkipForward();
0144     void longSkipBackward();
0145     void longSkipForward();
0146     void getAudioDevices();
0147     void openSubtitle();
0148     void setSubtitle(QUrl text);
0149 
0150 public:
0151     void playbackFinished();
0152     void playbackStatusChanged();
0153     void currentTotalTimeChanged();
0154     void metadataChanged();
0155     void seekableChanged();
0156     void audioStreamsChanged();
0157     void subtitlesChanged();
0158     void titlesChanged();
0159     void chaptersChanged();
0160     void anglesChanged();
0161     void dvdMenuChanged();
0162     void videoSizeChanged();
0163 
0164 signals:
0165     void displayModeChanged();
0166     void changeCaption(const QString &caption);
0167     void resizeToVideo(float resizeFactor);
0168 
0169     void playlistPrevious();
0170     void playlistNext();
0171     void playlistUrlsDropped(const QList<QUrl> &urls);
0172     void playlistTrackLengthChanged(int length);
0173     void playlistTrackMetadataChanged(const QMap<MediaWidget::MetadataType, QString> &metadata);
0174     void osdKeyPressed(int key);
0175 
0176 private slots:
0177     void checkScreenSaver(bool noDisable = false);
0178 
0179     void setAudioCard();
0180     void mutedChanged();
0181     void volumeChanged(int volume);
0182     void seek(int position);
0183     void deinterlacingChanged(QAction *action);
0184     void aspectRatioChanged(QAction *action);
0185     void setVideoSize();
0186     void autoResizeTriggered(QAction *action);
0187     void pausedChanged(bool paused);
0188     void timeButtonClicked();
0189     void jumpToPosition();
0190     void currentAudioStreamChanged(int currentAudioStream);
0191     void currentSubtitleChanged(int currentSubtitle);
0192     void toggleMenu();
0193     void currentTitleChanged(QAction *action);
0194     void currentChapterChanged(QAction *action);
0195     void currentAngleChanged(QAction *action);
0196     void shortSkipDurationChanged(int shortSkipDuration);
0197     void longSkipDurationChanged(int longSkipDuration);
0198 
0199 private:
0200     void contextMenuEvent(QContextMenuEvent *event) override;
0201     void mouseDoubleClickEvent(QMouseEvent *event) override;
0202     void dragEnterEvent(QDragEnterEvent *event) override;
0203     void dropEvent(QDropEvent *event) override;
0204     void keyPressEvent(QKeyEvent *event) override;
0205     void resizeEvent(QResizeEvent *event) override;
0206     void wheelEvent(QWheelEvent *event) override;
0207     void setVolumeUnderMouse(int volume);
0208 
0209     bool event(QEvent* event) override;
0210 
0211     QMenu *menu;
0212     AbstractMediaWidget *backend;
0213     OsdWidget *osdWidget;
0214 
0215     QWidgetAction *actionPrevious;
0216     QWidgetAction *actionPlayPause;
0217     QString textPlay;
0218     QString textPause;
0219     QIcon iconPlay;
0220     QIcon iconPause;
0221     QWidgetAction *actionStop;
0222     QWidgetAction *actionNext;
0223     QWidgetAction *fullScreenAction;
0224     QWidgetAction *minimalModeAction;
0225     QComboBox *audioStreamBox;
0226     QComboBox *subtitleBox;
0227     QStringListModel *audioStreamModel;
0228     QStringListModel *subtitleModel;
0229     QString textSubtitlesOff;
0230     QWidgetAction *muteAction;
0231     QIcon mutedIcon;
0232     QIcon unmutedIcon;
0233     QSlider *volumeSlider;
0234     SeekSlider *seekSlider;
0235     QWidgetAction *longSkipBackwardAction;
0236     QWidgetAction *shortSkipBackwardAction;
0237     QWidgetAction *shortSkipForwardAction;
0238     QWidgetAction *longSkipForwardAction;
0239     QWidgetAction *menuAction;
0240     QMenu *audioDevMenu;
0241     QMenu *titleMenu;
0242     QMenu *chapterMenu;
0243     QMenu *angleMenu;
0244     QActionGroup *titleGroup;
0245     QActionGroup *chapterGroup;
0246     QActionGroup *angleGroup;
0247     QMenu *navigationMenu;
0248     QWidgetAction *jumpToPositionAction;
0249     QPushButton *timeButton;
0250 
0251     DisplayMode displayMode;
0252     int autoResizeFactor;
0253     int deinterlaceMode;
0254     QScopedPointer<MediaSource> dummySource;
0255     MediaSource *source;
0256     bool blockBackendUpdates;
0257     bool muted;
0258     bool screenSaverSuspended;
0259     bool showElapsedTime;
0260 };
0261 
0262 class MediaSource
0263 {
0264 public:
0265     MediaSource() { }
0266 
0267     virtual ~MediaSource()
0268     {
0269         setMediaWidget(NULL);
0270     }
0271 
0272     enum Type
0273     {
0274         Url,
0275         AudioCd,
0276         VideoCd,
0277         Dvd,
0278         Dvb
0279     };
0280 
0281     virtual Type getType() const { return Url; }
0282     virtual QUrl getUrl() const { return QUrl(); }
0283     virtual void validateCurrentTotalTime(int &, int &) const { }
0284     virtual bool hideCurrentTotalTime() const { return false; }
0285     virtual bool overrideAudioStreams() const { return false; }
0286     virtual bool overrideSubtitles() const { return false; }
0287     virtual QStringList getAudioStreams() const { return QStringList(); }
0288     virtual QStringList getSubtitles() const { return QStringList(); }
0289     virtual int getCurrentAudioStream() const { return -1; }
0290     virtual int getCurrentSubtitle() const { return -1; }
0291     virtual bool overrideCaption() const { return false; }
0292     virtual QString getDefaultCaption() const { return QString(); }
0293     virtual void setCurrentAudioStream(int ) { }
0294     virtual void setCurrentSubtitle(int ) { }
0295     virtual void trackLengthChanged(int ) { }
0296     virtual void metadataChanged(const QMap<MediaWidget::MetadataType, QString> &) { }
0297     virtual void playbackFinished() { }
0298     virtual void playbackStatusChanged(MediaWidget::PlaybackStatus ) { }
0299     virtual void replay() { }
0300     virtual void previous() { }
0301     virtual void next() { }
0302 
0303     void setMediaWidget(const MediaWidget *mediaWidget)
0304     {
0305         MediaWidget *oldMediaWidget = weakMediaWidget.data();
0306 
0307         if (mediaWidget != oldMediaWidget) {
0308             if (oldMediaWidget != NULL) {
0309                 oldMediaWidget->mediaSourceDestroyed(this);
0310             }
0311 // FIXME:
0312 //          weakMediaWidget = mediaWidget;
0313         }
0314     }
0315 private:
0316     QWeakPointer<MediaWidget> weakMediaWidget;
0317 };
0318 
0319 #endif /* MEDIAWIDGET_H */