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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// vim: set tabstop=4 shiftwidth=4 expandtab:
/*
Gwenview: an image viewer
Copyright 2009 Aurélien Gâteau <agateau@kde.org>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.

*/
// Self
#include "videoviewadapter.moc"

// Qt
#include <Phonon/AudioOutput>
#include <Phonon/MediaObject>
#include <Phonon/Path>
#include <Phonon/SeekSlider>
#include <Phonon/VideoWidget>
#include <Phonon/VolumeSlider>
#include <QGraphicsProxyWidget>
#include <QHBoxLayout>
#include <QMouseEvent>
#include <QToolButton>

// KDE
#include <kicon.h>
#include <kurl.h>

// Local
#include "document/documentfactory.h"
#include "hudwidget.h"
#include "widgetfloater.h"

namespace Gwenview
{

struct VideoViewAdapterPrivate {
    VideoViewAdapter* q;
    Phonon::MediaObject* mMediaObject;
    Phonon::VideoWidget* mVideoWidget;
    Phonon::AudioOutput* mAudioOutput;
    HudWidget* mHud;
    WidgetFloater* mFloater;
    QToolButton* mPlayPauseButton;

    Document::Ptr mDocument;

    void setupHud(QWidget* parent)
    {
        // Create hud content
        QWidget* widget = new QWidget;
        QHBoxLayout* layout = new QHBoxLayout(widget);

        mPlayPauseButton = new QToolButton;
        mPlayPauseButton->setAutoRaise(true);
        q->updatePlayPauseButton();
        QObject::connect(mPlayPauseButton, SIGNAL(clicked()),
                         q, SLOT(slotPlayPauseClicked()));
        QObject::connect(mMediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
                         q, SLOT(updatePlayPauseButton()));

        Phonon::SeekSlider* seekSlider = new Phonon::SeekSlider;
        seekSlider->setTracking(false);
        seekSlider->setIconVisible(false);
        seekSlider->setMediaObject(mMediaObject);

        Phonon::VolumeSlider* volumeSlider = new Phonon::VolumeSlider;
        volumeSlider->setAudioOutput(mAudioOutput);
        volumeSlider->setMinimumWidth(100);

        layout->addWidget(mPlayPauseButton);
        layout->addWidget(seekSlider, 5 /* stretch */);
        layout->addWidget(volumeSlider, 1 /* stretch */);
        widget->adjustSize();

        // Create hud
        mHud = new HudWidget;
        mHud->setAutoFillBackground(true);
        mHud->init(widget, HudWidget::OptionDoNotFollowChildSize | HudWidget::OptionOpaque);

        // Init floater
        mFloater = new WidgetFloater(parent);
        mFloater->setChildWidget(mHud);
        mFloater->setAlignment(Qt::AlignJustify | Qt::AlignBottom);

        mVideoWidget->installEventFilter(q);
    }

    bool isPlaying() const {
        switch (mMediaObject->state()) {
        case Phonon::StoppedState:
        case Phonon::PausedState:
            return false;
        default:
            return true;
        }
    }

    void updateHudVisibility(int yPos)
    {
        const int floaterY = mVideoWidget->height() - mFloater->verticalMargin() - mHud->sizeHint().height() * 3 / 2;
        if (mHud->isVisible() && yPos < floaterY) {
            mHud->hide();
        } else if (!mHud->isVisible() && yPos >= floaterY) {
            mHud->show();
        }
    }
};

VideoViewAdapter::VideoViewAdapter()
: d(new VideoViewAdapterPrivate)
{
    d->q = this;
    d->mMediaObject = new Phonon::MediaObject(this);
    connect(d->mMediaObject, SIGNAL(finished()), SIGNAL(videoFinished()));

    d->mVideoWidget = new Phonon::VideoWidget;
    d->mVideoWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    d->mVideoWidget->setAttribute(Qt::WA_Hover);

    Phonon::createPath(d->mMediaObject, d->mVideoWidget);

    d->mAudioOutput = new Phonon::AudioOutput(Phonon::VideoCategory, this);
    Phonon::createPath(d->mMediaObject, d->mAudioOutput);

    d->setupHud(d->mVideoWidget);

    QGraphicsProxyWidget* proxy = new QGraphicsProxyWidget;
    proxy->setWidget(d->mVideoWidget);
    setWidget(proxy);
}

VideoViewAdapter::~VideoViewAdapter()
{
    delete d;
}

void VideoViewAdapter::setDocument(Document::Ptr doc)
{
    d->mHud->show();
    d->mDocument = doc;
    d->mMediaObject->setCurrentSource(d->mDocument->url());
    d->mMediaObject->play();
}

Document::Ptr VideoViewAdapter::document() const
{
    return d->mDocument;
}

void VideoViewAdapter::slotPlayPauseClicked()
{
    if (d->isPlaying()) {
        d->mMediaObject->pause();
    } else {
        d->mMediaObject->play();
    }
}

bool VideoViewAdapter::eventFilter(QObject*, QEvent* event)
{
    if (event->type() == QEvent::MouseMove) {
        d->updateHudVisibility(static_cast<QMouseEvent*>(event)->y());
    }
    return false;
}

void VideoViewAdapter::updatePlayPauseButton()
{
    if (d->isPlaying()) {
        d->mPlayPauseButton->setIcon(KIcon("media-playback-pause"));
    } else {
        d->mPlayPauseButton->setIcon(KIcon("media-playback-start"));
    }
}

} // namespace