File indexing completed on 2024-05-05 04:49:27

0001 /****************************************************************************************
0002  * Copyright (c) 2003 Christian Muehlhaeuser <chris@chris.de>                           *
0003  * Copyright (c) 2008-2013 Mark Kretschmann <kretschmann@kde.org>                       *
0004  *                                                                                      *
0005  * This program is free software; you can redistribute it and/or modify it under        *
0006  * the terms of the GNU General Public License as published by the Free Software        *
0007  * Foundation; either version 2 of the License, or (at your option) any later           *
0008  * version.                                                                             *
0009  *                                                                                      *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0013  *                                                                                      *
0014  * You should have received a copy of the GNU General Public License along with         *
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0016  ****************************************************************************************/
0017 
0018 #ifndef AMAROK_OSD_H
0019 #define AMAROK_OSD_H
0020 
0021 #include "core/meta/forward_declarations.h"
0022 
0023 #include <QImage>
0024 #include <QPixmap>
0025 #include <QString>
0026 #include <QWidget> //baseclass
0027 
0028 #define OSD_WINDOW_OPACITY 0.74
0029 
0030 class QTimeLine;
0031 
0032 class OSDWidget : public QWidget
0033 {
0034     Q_OBJECT
0035 
0036     public:
0037         enum Alignment { Left, Middle, Center, Right };
0038 
0039         /** shadow size in every direction */
0040         static const int SHADOW_SIZE = 5;
0041 
0042         static const int FADING_DURATION = 400; //ms
0043          
0044     public Q_SLOTS:
0045         /** calls setText() then show(), after setting image if needed */
0046         void show( const QString &text, const QImage &newImage = QImage() );
0047 
0048         void ratingChanged( const short rating );
0049         void ratingChanged( const QString& path, int rating );
0050         void volumeChanged( int volume );
0051 
0052         /** reimplemented, show the OSD */
0053         virtual void show();
0054         /** reimplemented, hide the OSD */
0055         virtual void hide();
0056 
0057         void setVisible( bool visible ) override;
0058 
0059         /**
0060          * For the sake of simplicity, when these settings are
0061          * changed they do not take effect until the next time
0062          * the OSD is shown!
0063          *
0064          * To force an update call show();
0065          */
0066         void setDuration( int ms ) { m_duration = ms; }
0067         void setTextColor( const QColor &color );
0068 
0069         inline uint yOffset() const { return m_yOffset; }
0070         void setYOffset( int y ) { m_yOffset = y; }
0071 
0072         inline int alignment() const { return m_alignment; }
0073         void setAlignment( Alignment alignment ) { m_alignment = alignment; }
0074 
0075         inline int screen() const { return m_screen; }
0076         void setScreen( int screen );
0077 
0078         void setPaused( bool paused ) { m_paused = paused; }
0079         void setImage( const QImage &image ) { m_cover = image; }
0080         void setText( const QString &text ) { m_text = text; }
0081         void setRating( const short rating ) { m_rating = rating; }
0082         void setTranslucent( bool enabled ) { m_translucent = enabled; setWindowOpacity( maxOpacity() ); }
0083         void setFadeOpacity( qreal value );
0084         void setFontScale( int scale );
0085         void setHideWhenFullscreenWindowIsActive( bool hide );
0086 
0087     protected:
0088         explicit OSDWidget( QWidget *parent, const char *name = "osd" );
0089         ~OSDWidget() override;
0090 
0091         // work-around to get default point size on this platform, Qt API does not offer this directly
0092         inline qreal defaultPointSize() const { return QFont(font().family()).pointSizeF(); }
0093 
0094         inline qreal maxOpacity() const { return m_translucent ? OSD_WINDOW_OPACITY : 1.0; }
0095 
0096         /** determine new size and position */
0097         QRect determineMetrics( const int marginMetric );
0098 
0099         /**
0100          * @short Checks if the OSD is temporary disabled.
0101          * This is usually the case if the OSD should not be shown
0102          * if a fullscreen application is active (@see m_hideWhenFullscreenWindowIsActive)
0103          * (where the OSD could steal focus).
0104          */
0105         bool isTemporaryDisabled() const;
0106 
0107         /** resets the colours to defaults */
0108         void unsetColors();
0109 
0110         // Reimplemented from QWidget
0111         void paintEvent( QPaintEvent* ) override;
0112         void mousePressEvent( QMouseEvent* ) override;
0113         void changeEvent( QEvent* ) override;
0114 
0115         /** distance from screen edge */
0116         static const int MARGIN = 15;
0117 
0118     private:
0119         uint        m_margin;
0120         QSize       m_size;
0121         int         m_duration;
0122         QTimer     *m_timer;
0123         Alignment   m_alignment;
0124         int         m_screen;
0125         uint        m_yOffset;
0126         short       m_rating;
0127         int         m_volume;
0128         bool        m_showVolume;
0129         QString     m_text;
0130         QImage      m_cover;
0131         QPixmap     m_scaledCover;
0132         bool        m_paused;
0133         bool        m_hideWhenFullscreenWindowIsActive;
0134         QTimeLine*  m_fadeTimeLine;
0135         bool        m_translucent;
0136 };
0137 
0138 
0139 class OSDPreviewWidget : public OSDWidget
0140 {
0141     Q_OBJECT
0142 
0143 public:
0144     explicit OSDPreviewWidget( QWidget *parent );
0145 
0146 public Q_SLOTS:
0147     void setTextColor( const QColor &color ) { OSDWidget::setTextColor( color ); doUpdate(); }
0148     void setScreen( int screen ) { OSDWidget::setScreen( screen ); doUpdate(); }
0149     void setFontScale( int scale ) { OSDWidget::setFontScale( scale ); doUpdate(); }
0150     void setTranslucent( bool enabled ) { OSDWidget::setTranslucent( enabled ); doUpdate(); }
0151 
0152     void setUseCustomColors( const bool use, const QColor &fg );
0153 
0154     void hide() override { QWidget::hide(); }
0155 
0156 private:
0157     inline void doUpdate() { if( !isHidden() ) show(); }
0158 
0159 Q_SIGNALS:
0160     void positionChanged();
0161 
0162 protected:
0163     void mousePressEvent( QMouseEvent * ) override;
0164     void mouseReleaseEvent( QMouseEvent * ) override;
0165     void mouseMoveEvent( QMouseEvent * ) override;
0166 
0167 private:
0168     bool   m_dragging;
0169     QPoint m_dragYOffset;
0170 };
0171 
0172 
0173 
0174 namespace Amarok
0175 {
0176     class OSD : public OSDWidget
0177     {
0178         Q_OBJECT
0179 
0180     public:
0181         static OSD* instance();
0182         static void destroy();
0183 
0184         void applySettings();
0185         virtual void show( Meta::TrackPtr track );
0186 
0187         // Don't hide baseclass methods - prevent compiler warnings
0188         void show() override { OSDWidget::show(); }
0189 
0190     protected Q_SLOTS:
0191         void muteStateChanged( bool mute );
0192         void trackPlaying( const Meta::TrackPtr &track );
0193         void stopped();
0194         void paused();
0195         void metadataChanged();
0196 
0197     public Q_SLOTS:
0198         /**
0199          * When user pushs global shortcut or uses script to toggle
0200          * even if it is disabled()
0201          */
0202         void forceToggleOSD();
0203 
0204     private:
0205         OSD();
0206         ~OSD() override;
0207 
0208         static OSD* s_instance;
0209 
0210         Meta::TrackPtr m_currentTrack;
0211     };
0212 }
0213 
0214 #endif /*AMAROK_OSD_H*/