~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to lib/documentview/videoviewadapter.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
ImportĀ upstreamĀ versionĀ 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
 
1
// vim: set tabstop=4 shiftwidth=4 expandtab:
2
2
/*
3
3
Gwenview: an image viewer
4
4
Copyright 2009 AurĆ©lien GĆ¢teau <agateau@kde.org>
28
28
#include <Phonon/SeekSlider>
29
29
#include <Phonon/VideoWidget>
30
30
#include <Phonon/VolumeSlider>
 
31
#include <QGraphicsProxyWidget>
31
32
#include <QHBoxLayout>
32
33
#include <QMouseEvent>
33
34
#include <QToolButton>
41
42
#include "hudwidget.h"
42
43
#include "widgetfloater.h"
43
44
 
44
 
namespace Gwenview {
45
 
 
 
45
namespace Gwenview
 
46
{
46
47
 
47
48
struct VideoViewAdapterPrivate {
48
 
        VideoViewAdapter* q;
49
 
        Phonon::MediaObject* mMediaObject;
50
 
        Phonon::VideoWidget* mVideoWidget;
51
 
        Phonon::AudioOutput* mAudioOutput;
52
 
        HudWidget* mHud;
53
 
        WidgetFloater* mFloater;
54
 
        QToolButton* mPlayPauseButton;
55
 
 
56
 
        Document::Ptr mDocument;
57
 
 
58
 
        void setupHud(QWidget* parent) {
59
 
                // Create hud content
60
 
                QWidget* widget = new QWidget;
61
 
                QHBoxLayout* layout = new QHBoxLayout(widget);
62
 
 
63
 
                mPlayPauseButton = new QToolButton;
64
 
                mPlayPauseButton->setAutoRaise(true);
65
 
                q->updatePlayPauseButton();
66
 
                QObject::connect(mPlayPauseButton, SIGNAL(clicked()),
67
 
                        q, SLOT(slotPlayPauseClicked()));
68
 
                QObject::connect(mMediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
69
 
                        q, SLOT(updatePlayPauseButton()));
70
 
 
71
 
                Phonon::SeekSlider* seekSlider = new Phonon::SeekSlider;
72
 
                seekSlider->setTracking(false);
73
 
                seekSlider->setIconVisible(false);
74
 
                seekSlider->setMediaObject(mMediaObject);
75
 
 
76
 
                Phonon::VolumeSlider* volumeSlider = new Phonon::VolumeSlider;
77
 
                volumeSlider->setAudioOutput(mAudioOutput);
78
 
                volumeSlider->setMinimumWidth(100);
79
 
 
80
 
                layout->addWidget(mPlayPauseButton);
81
 
                layout->addWidget(seekSlider, 5 /* stretch */);
82
 
                layout->addWidget(volumeSlider, 1 /* stretch */);
83
 
                widget->adjustSize();
84
 
 
85
 
                // Create hud
86
 
                mHud = new HudWidget;
87
 
                mHud->setAutoFillBackground(true);
88
 
                mHud->init(widget, HudWidget::OptionDoNotFollowChildSize | HudWidget::OptionOpaque);
89
 
 
90
 
                // Init floater
91
 
                mFloater = new WidgetFloater(parent);
92
 
                mFloater->setChildWidget(mHud);
93
 
                mFloater->setAlignment(Qt::AlignJustify | Qt::AlignBottom);
94
 
 
95
 
                mVideoWidget->installEventFilter(q);
96
 
        }
97
 
 
98
 
 
99
 
        bool isPlaying() const {
100
 
                switch (mMediaObject->state()) {
101
 
                case Phonon::StoppedState:
102
 
                case Phonon::PausedState:
103
 
                        return false;
104
 
                default:
105
 
                        return true;
106
 
                }
107
 
        }
108
 
 
109
 
 
110
 
        void updateHudVisibility(int yPos) {
111
 
                const int floaterY = mVideoWidget->height() - mFloater->verticalMargin() - mHud->sizeHint().height() * 3 / 2;
112
 
                if (mHud->isVisible() && yPos < floaterY) {
113
 
                        mHud->hide();
114
 
                } else if (!mHud->isVisible() && yPos >= floaterY) {
115
 
                        mHud->show();
116
 
                }
117
 
        }
 
49
    VideoViewAdapter* q;
 
50
    Phonon::MediaObject* mMediaObject;
 
51
    Phonon::VideoWidget* mVideoWidget;
 
52
    Phonon::AudioOutput* mAudioOutput;
 
53
    HudWidget* mHud;
 
54
    WidgetFloater* mFloater;
 
55
    QToolButton* mPlayPauseButton;
 
56
 
 
57
    Document::Ptr mDocument;
 
58
 
 
59
    void setupHud(QWidget* parent)
 
60
    {
 
61
        // Create hud content
 
62
        QWidget* widget = new QWidget;
 
63
        QHBoxLayout* layout = new QHBoxLayout(widget);
 
64
 
 
65
        mPlayPauseButton = new QToolButton;
 
66
        mPlayPauseButton->setAutoRaise(true);
 
67
        q->updatePlayPauseButton();
 
68
        QObject::connect(mPlayPauseButton, SIGNAL(clicked()),
 
69
                         q, SLOT(slotPlayPauseClicked()));
 
70
        QObject::connect(mMediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
 
71
                         q, SLOT(updatePlayPauseButton()));
 
72
 
 
73
        Phonon::SeekSlider* seekSlider = new Phonon::SeekSlider;
 
74
        seekSlider->setTracking(false);
 
75
        seekSlider->setIconVisible(false);
 
76
        seekSlider->setMediaObject(mMediaObject);
 
77
 
 
78
        Phonon::VolumeSlider* volumeSlider = new Phonon::VolumeSlider;
 
79
        volumeSlider->setAudioOutput(mAudioOutput);
 
80
        volumeSlider->setMinimumWidth(100);
 
81
 
 
82
        layout->addWidget(mPlayPauseButton);
 
83
        layout->addWidget(seekSlider, 5 /* stretch */);
 
84
        layout->addWidget(volumeSlider, 1 /* stretch */);
 
85
        widget->adjustSize();
 
86
 
 
87
        // Create hud
 
88
        mHud = new HudWidget;
 
89
        mHud->setAutoFillBackground(true);
 
90
        mHud->init(widget, HudWidget::OptionDoNotFollowChildSize | HudWidget::OptionOpaque);
 
91
 
 
92
        // Init floater
 
93
        mFloater = new WidgetFloater(parent);
 
94
        mFloater->setChildWidget(mHud);
 
95
        mFloater->setAlignment(Qt::AlignJustify | Qt::AlignBottom);
 
96
 
 
97
        mVideoWidget->installEventFilter(q);
 
98
    }
 
99
 
 
100
    bool isPlaying() const {
 
101
        switch (mMediaObject->state()) {
 
102
        case Phonon::StoppedState:
 
103
        case Phonon::PausedState:
 
104
            return false;
 
105
        default:
 
106
            return true;
 
107
        }
 
108
    }
 
109
 
 
110
    void updateHudVisibility(int yPos)
 
111
    {
 
112
        const int floaterY = mVideoWidget->height() - mFloater->verticalMargin() - mHud->sizeHint().height() * 3 / 2;
 
113
        if (mHud->isVisible() && yPos < floaterY) {
 
114
            mHud->hide();
 
115
        } else if (!mHud->isVisible() && yPos >= floaterY) {
 
116
            mHud->show();
 
117
        }
 
118
    }
118
119
};
119
120
 
120
 
 
121
 
VideoViewAdapter::VideoViewAdapter(QWidget* parent)
122
 
: AbstractDocumentViewAdapter(parent)
123
 
, d(new VideoViewAdapterPrivate) {
124
 
        d->q = this;
125
 
        d->mMediaObject = new Phonon::MediaObject(this);
126
 
        connect(d->mMediaObject, SIGNAL(finished()), SIGNAL(videoFinished()));
127
 
 
128
 
        d->mVideoWidget = new Phonon::VideoWidget(parent);
129
 
        d->mVideoWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
130
 
        d->mVideoWidget->setAttribute(Qt::WA_Hover);
131
 
 
132
 
        Phonon::createPath(d->mMediaObject, d->mVideoWidget);
133
 
 
134
 
        d->mAudioOutput = new Phonon::AudioOutput(Phonon::VideoCategory, this);
135
 
        Phonon::createPath(d->mMediaObject, d->mAudioOutput);
136
 
 
137
 
        d->setupHud(d->mVideoWidget);
138
 
 
139
 
        setWidget(d->mVideoWidget);
140
 
}
141
 
 
142
 
 
143
 
void VideoViewAdapter::installEventFilterOnViewWidgets(QObject* object) {
144
 
        d->mVideoWidget->installEventFilter(object);
145
 
}
146
 
 
147
 
 
148
 
VideoViewAdapter::~VideoViewAdapter() {
149
 
        delete d;
150
 
}
151
 
 
152
 
 
153
 
void VideoViewAdapter::setDocument(Document::Ptr doc) {
154
 
        d->mHud->show();
155
 
        d->mDocument = doc;
156
 
        d->mMediaObject->setCurrentSource(d->mDocument->url());
157
 
        d->mMediaObject->play();
158
 
}
159
 
 
160
 
 
161
 
Document::Ptr VideoViewAdapter::document() const {
162
 
        return d->mDocument;
163
 
}
164
 
 
165
 
 
166
 
void VideoViewAdapter::slotPlayPauseClicked() {
167
 
        if (d->isPlaying()) {
168
 
                d->mMediaObject->pause();
169
 
        } else {
170
 
                d->mMediaObject->play();
171
 
        }
172
 
}
173
 
 
174
 
 
175
 
bool VideoViewAdapter::eventFilter(QObject*, QEvent* event) {
176
 
        if (event->type() == QEvent::MouseMove) {
177
 
                d->updateHudVisibility(static_cast<QMouseEvent*>(event)->y());
178
 
        }
179
 
        return false;
180
 
}
181
 
 
182
 
 
183
 
void VideoViewAdapter::updatePlayPauseButton() {
184
 
        if (d->isPlaying()) {
185
 
                d->mPlayPauseButton->setIcon(KIcon("media-playback-pause"));
186
 
        } else {
187
 
                d->mPlayPauseButton->setIcon(KIcon("media-playback-start"));
188
 
        }
189
 
}
190
 
 
 
121
VideoViewAdapter::VideoViewAdapter()
 
122
: d(new VideoViewAdapterPrivate)
 
123
{
 
124
    d->q = this;
 
125
    d->mMediaObject = new Phonon::MediaObject(this);
 
126
    connect(d->mMediaObject, SIGNAL(finished()), SIGNAL(videoFinished()));
 
127
 
 
128
    d->mVideoWidget = new Phonon::VideoWidget;
 
129
    d->mVideoWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
 
130
    d->mVideoWidget->setAttribute(Qt::WA_Hover);
 
131
 
 
132
    Phonon::createPath(d->mMediaObject, d->mVideoWidget);
 
133
 
 
134
    d->mAudioOutput = new Phonon::AudioOutput(Phonon::VideoCategory, this);
 
135
    Phonon::createPath(d->mMediaObject, d->mAudioOutput);
 
136
 
 
137
    d->setupHud(d->mVideoWidget);
 
138
 
 
139
    QGraphicsProxyWidget* proxy = new QGraphicsProxyWidget;
 
140
    proxy->setWidget(d->mVideoWidget);
 
141
    setWidget(proxy);
 
142
}
 
143
 
 
144
VideoViewAdapter::~VideoViewAdapter()
 
145
{
 
146
    delete d;
 
147
}
 
148
 
 
149
void VideoViewAdapter::setDocument(Document::Ptr doc)
 
150
{
 
151
    d->mHud->show();
 
152
    d->mDocument = doc;
 
153
    d->mMediaObject->setCurrentSource(d->mDocument->url());
 
154
    d->mMediaObject->play();
 
155
}
 
156
 
 
157
Document::Ptr VideoViewAdapter::document() const
 
158
{
 
159
    return d->mDocument;
 
160
}
 
161
 
 
162
void VideoViewAdapter::slotPlayPauseClicked()
 
163
{
 
164
    if (d->isPlaying()) {
 
165
        d->mMediaObject->pause();
 
166
    } else {
 
167
        d->mMediaObject->play();
 
168
    }
 
169
}
 
170
 
 
171
bool VideoViewAdapter::eventFilter(QObject*, QEvent* event)
 
172
{
 
173
    if (event->type() == QEvent::MouseMove) {
 
174
        d->updateHudVisibility(static_cast<QMouseEvent*>(event)->y());
 
175
    }
 
176
    return false;
 
177
}
 
178
 
 
179
void VideoViewAdapter::updatePlayPauseButton()
 
180
{
 
181
    if (d->isPlaying()) {
 
182
        d->mPlayPauseButton->setIcon(KIcon("media-playback-pause"));
 
183
    } else {
 
184
        d->mPlayPauseButton->setIcon(KIcon("media-playback-start"));
 
185
    }
 
186
}
191
187
 
192
188
} // namespace