MediaListWidget.cpp 4.32 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*****************************************************************************
 * MediaListWidget.cpp: Multimedia list widget
 *****************************************************************************
 * Copyright (C) 2008-2009 the VLMC team
 *
 * Authors: Christophe Courtaut <christophe.courtaut@gmail.com>
 *
 * 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, Boston MA 02110-1301, USA.
 *****************************************************************************/
22

23
24
#include <QSvgRenderer>
#include <QPainter>
25
26
#include <QUrl>
#include <QtDebug>
27

28
29
#include "MediaListWidget.h"

30
31
32
MediaListWidget::MediaListWidget( QWidget* parent ) :
        QListWidget( parent ),
        m_lastClicked( NULL )
33
{
34
    m_svgRenderer = new QSvgRenderer( this );
35
36
    setIconSize( QSize( 64, 64 ) );
    setContentsMargins( 2, 2, 2, 2 );
37
    setAcceptDrops( true );
38
39
}

40
void    MediaListWidget::setType( Media::FileType fileType )
41
42
{
    this->m_Type = fileType;
43
44
    switch ( fileType )
    {
45
    case Media::Audio:
46
47
        m_svgRenderer->load( QString( ":/images/scalable/audio-scalable" ) );
        break;
48
    case Media::Image:
49
50
        m_svgRenderer->load( QString( ":/images/scalable/image-scalable" ) );
        break;
51
    case Media::Video:
52
53
54
        m_svgRenderer->load( QString( ":/images/scalable/video-scalable" ) );
        break;
    }
55
56
57
58
59
60
61
}

void    MediaListWidget::mousePressEvent( QMouseEvent* event )
{
    if ( event->button() == Qt::LeftButton)
        this->m_dragStartPos = event->pos();
    QListWidget::mousePressEvent( event );
62

63
    ListViewMediaItem* item = static_cast<ListViewMediaItem*>( currentItem() );
64
65
    if ( item == NULL )
        return ;
66
67
68
69
70
    if ( item->getMedia() != m_lastClicked )
    {
        m_lastClicked = item->getMedia();
        emit selectedMediaChanged( m_lastClicked );
    }
71
72
73
74
}

void    MediaListWidget::mouseMoveEvent( QMouseEvent* event )
{
75
    if ( currentItem() == NULL )
76
77
78
79
80
81
82
        return;
    if ( !( event->buttons() & Qt::LeftButton ) )
         return;
    if ( ( event->pos() - m_dragStartPos ).manhattanLength()
          < QApplication::startDragDistance() )
        return;

83
    ListViewMediaItem* item = static_cast<ListViewMediaItem*>( currentItem() );
84
    QMimeData* mimeData = new QMimeData;
85
    mimeData->setData( "vlmc/uuid", item->getMedia()->getUuid().toString().toAscii() );
86
87
    QDrag* drag = new QDrag( this );
    drag->setMimeData( mimeData );
88
    const QPixmap& dragPixmap = static_cast<ListViewMediaItem*>( currentItem() )->getMedia()->getSnapshot();
89
    drag->setPixmap( dragPixmap.scaled( 100, 100, Qt::KeepAspectRatio ) );
90
    drag->exec( Qt::CopyAction | Qt::MoveAction, Qt::CopyAction );
91
}
92

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
void MediaListWidget::paintEvent( QPaintEvent* event )
{
    QPainter painter( viewport() );

    painter.setOpacity( 0.2 );
    if ( m_svgRenderer->isValid() )
    {
        QRect destRect = viewport()->rect();
        QRect viewportRect = viewport()->rect();
        QSize svgRect = m_svgRenderer->defaultSize();
        qreal wRatio = (qreal)viewportRect.width() / (qreal)svgRect.width();
        qreal hRatio = (qreal)viewportRect.height() / (qreal)svgRect.height();
        if ( wRatio < hRatio )
        {
            destRect.setWidth( viewportRect.width() );
108
            destRect.setHeight( ( int ) ( svgRect.height() * wRatio ) );
109
110
111
        }
        else
        {
112
            destRect.setWidth( ( int ) ( svgRect.width() * hRatio ) );
113
114
115
116
117
118
119
120
121
122
            destRect.setHeight( viewportRect.height() );
        }

        int x = ( viewportRect.width() / 2 ) - ( destRect.width() / 2 );
        int y = ( viewportRect.height() / 2 ) - ( destRect.height() / 2 );
        destRect.setCoords( x, y, x + destRect.width(), y + destRect.height() );
        m_svgRenderer->render( &painter, destRect );
    }
    QListWidget::paintEvent( event );
}