playlistbrowseritem.cpp

来自「Amarok是一款在LINUX或其他类UNIX操作系统中运行的音频播放器软件。 」· C++ 代码 · 共 2,077 行 · 第 1/5 页

CPP
2,077
字号
        menu.insertItem( SmallIconSet(Amarok::icon( "add_playlist" )), i18n("Create Playlist..."), PLAYLIST );        menu.insertItem( SmallIconSet(Amarok::icon( "add_playlist" )), i18n("Import Playlist..."), PLAYLIST_IMPORT );    }    else if( parentCat == static_cast<QListViewItem*>(PlaylistBrowser::instance()->m_smartCategory) )        menu.insertItem( SmallIconSet(Amarok::icon( "add_playlist" )), i18n("New Smart Playlist..."), SMART );    else if( parentCat == static_cast<QListViewItem*>(PlaylistBrowser::instance()->m_dynamicCategory) )        menu.insertItem( SmallIconSet(Amarok::icon( "add_playlist" )), i18n("New Dynamic Playlist..."), DYNAMIC );    else if( parentCat == static_cast<QListViewItem*>(PlaylistBrowser::instance()->m_streamsCategory) )        menu.insertItem( SmallIconSet(Amarok::icon( "add_playlist" )), i18n("Add Radio Stream..."), STREAM );    else if( parentCat == static_cast<QListViewItem*>(PlaylistBrowser::instance()->m_lastfmCategory) )    {        menu.insertItem( SmallIconSet(Amarok::icon( "add_playlist" )), i18n("Add Last.fm Radio..."), LASTFM );        menu.insertItem( SmallIconSet(Amarok::icon( "add_playlist" )), i18n("Add Custom Last.fm Radio..."), LASTFMCUSTOM );    }    else if( parentCat == static_cast<QListViewItem*>(PlaylistBrowser::instance()->m_podcastCategory) )    {        isPodcastFolder = true;        menu.insertItem( SmallIconSet( Amarok::icon( "add_playlist" ) ), i18n("Add Podcast..."), PODCAST );        menu.insertItem( SmallIconSet( Amarok::icon( "refresh" ) ), i18n("Refresh All Podcasts"), REFRESH );        menu.insertSeparator();        menu.insertItem( SmallIconSet( Amarok::icon( "configure" ) ), i18n( "&Configure Podcasts..." ), CONFIG );        if( parentCat->childCount() == 0 )            menu.setItemEnabled( CONFIG, false );        if( parentCat == this )            menu.insertItem( SmallIconSet( Amarok::icon( "configure" ) ), i18n("Scan Interval..."), INTERVAL );    }    menu.insertSeparator();    menu.insertItem( SmallIconSet( Amarok::icon( "files" ) ), i18n("Create Sub-Folder"), CREATE );    QListViewItem *tracker = 0;    PlaylistCategory *newFolder = 0;    int c;    QString name;    switch( menu.exec( position ) ) {        case RENAME:            PlaylistBrowser::instance()->renameSelectedItem();            break;        case REMOVE:            PlaylistBrowser::instance()->removeSelectedItems();            break;        case PLAYLIST:            PlaylistBrowser::instance()->createPlaylist( this, false );            break;        case PLAYLIST_IMPORT:            PlaylistBrowser::instance()->openPlaylist( this );            break;        case SMART:            PlaylistBrowser::instance()->addSmartPlaylist( this );            break;        case STREAM:            PlaylistBrowser::instance()->addStream( this );            break;        case DYNAMIC:            ConfigDynamic::dynamicDialog( PlaylistBrowser::instance() );            break;        case LASTFM:            PlaylistBrowser::instance()->addLastFmRadio( this );            break;        case LASTFMCUSTOM:            PlaylistBrowser::instance()->addLastFmCustomRadio( this );            break;        case PODCAST:            PlaylistBrowser::instance()->addPodcast( this );            break;        case REFRESH:            PlaylistBrowser::instance()->refreshPodcasts( this );            break;        case CONFIG:            PlaylistBrowser::instance()->configurePodcasts( this );            break;        case CREATE:            tracker = firstChild();            for( c = 0 ; isCategory( tracker ); tracker = tracker->nextSibling() )            {                if( tracker->text(0).startsWith( i18n("Folder") ) )                    c++;                if( !isCategory( tracker->nextSibling() ) )                    break;            }            name = i18n("Folder");            if( c ) name = i18n("Folder %1").arg(c);            if( tracker == firstChild() && !isCategory( tracker ) ) tracker = 0;            newFolder = new PlaylistCategory( this, tracker, name, true );            newFolder->startRename( 0 );            if( isPodcastFolder )            {                c = CollectionDB::instance()->addPodcastFolder( newFolder->text(0), id(), false );                newFolder->setId( c );            }            break;        case INTERVAL:            PlaylistBrowser::instance()->changePodcastInterval();            break;    }}voidPlaylistCategory::paintCell( QPainter *p, const QColorGroup &cg, int column, int width, int align ){    QFont font( p->font() );    if( !m_folder ) {        font.setBold( true );    }    p->setFont( font );    KListViewItem::paintCell( p, cg, column, width, align );}////////////////////////////////////////////////////////////////////////////////    CLASS PlaylistEntry////////////////////////////////////////////////////////////////////////////PlaylistEntry::PlaylistEntry( QListViewItem *parent, QListViewItem *after, const KURL &url, int tracks, int length )    : PlaylistBrowserEntry( parent, after )    , m_url( url )    , m_length( length )    , m_trackCount( tracks )    , m_loading( false )    , m_loaded( false )    , m_dynamic( false )    , m_loading1( new QPixmap( locate("data", "amarok/images/loading1.png" ) ) )    , m_loading2( new QPixmap( locate("data", "amarok/images/loading2.png" ) ) )    , m_lastTrack( 0 ){    m_trackList.setAutoDelete( true );    tmp_droppedTracks.setAutoDelete( false );    setDragEnabled( true );    setRenameEnabled( 0, false );    setExpandable( true );    setPixmap( 0, SmallIcon( Amarok::icon( "playlist" ) ) );    if( !m_trackCount )    {        setText(0, i18n("Loading Playlist") );        load();   //load the playlist file    }    // set text is called from within customEvent()}PlaylistEntry::PlaylistEntry( QListViewItem *parent, QListViewItem *after, const QDomElement &xmlDefinition )    : PlaylistBrowserEntry( parent, after )    , m_loading( false )    , m_loaded( false )    , m_dynamic( false )    , m_loading1( new QPixmap( locate("data", "amarok/images/loading1.png" ) ) )    , m_loading2( new QPixmap( locate("data", "amarok/images/loading2.png" ) ) )    , m_lastTrack( 0 ){    m_url.setPath( xmlDefinition.attribute( "file" ) );    m_trackCount = xmlDefinition.namedItem( "tracks" ).toElement().text().toInt();    m_length = xmlDefinition.namedItem( "length" ).toElement().text().toInt();    QString title = xmlDefinition.attribute( "title" );    if( title.isEmpty() )    {        title = fileBaseName( m_url.path() );        title.replace( '_', ' ' );    }    setText( 0, title );    m_trackList.setAutoDelete( true );    tmp_droppedTracks.setAutoDelete( false );    setDragEnabled( true );    setRenameEnabled( 0, false );    setExpandable( true );    setPixmap( 0, SmallIcon( Amarok::icon( "playlist" ) ) );    if( !m_trackCount )    {        setText(0, i18n("Loading Playlist") );        load();   //load the playlist file    }    // set text is called from within customEvent()}PlaylistEntry::~PlaylistEntry(){    m_trackList.clear();    tmp_droppedTracks.setAutoDelete( true );    tmp_droppedTracks.clear();}void PlaylistEntry::load(){    if( m_loading )  return;    m_trackList.clear();    m_length = 0;    m_loaded = false;    m_loading = true;    //starts loading animation    m_iconCounter = 1;    startAnimation();    connect( &m_animationTimer, SIGNAL(timeout()), this, SLOT(slotAnimation()) );    //delete all children, so that we don't duplicate things    while( firstChild() )        delete firstChild();     //read the playlist file in a thread    ThreadManager::instance()->queueJob( new PlaylistReader( this, m_url.path() ) );}void PlaylistEntry::startAnimation(){    if( !m_animationTimer.isActive() )        m_animationTimer.start( ANIMATION_INTERVAL );}void PlaylistEntry::stopAnimation(){    m_animationTimer.stop();    m_dynamic ?        setPixmap( 0, SmallIcon( Amarok::icon( "favorites" ) ) ):        setPixmap( 0, SmallIcon( Amarok::icon( "playlist" ) ) );}void PlaylistEntry::slotAnimation(){    m_iconCounter % 2 ?        setPixmap( 0, *m_loading1 ):        setPixmap( 0, *m_loading2 );    m_iconCounter++;}void PlaylistEntry::insertTracks( QListViewItem *after, KURL::List list ){    QValueList<MetaBundle> bundles;    foreachType( KURL::List, list )        bundles += MetaBundle( *it );    insertTracks( after, bundles );}void PlaylistEntry::insertTracks( QListViewItem *after, QValueList<MetaBundle> bundles ){    int pos = 0;    if( after ) {        pos = m_trackList.find( static_cast<PlaylistTrackItem*>(after)->trackInfo() ) + 1;        if( pos == -1 )            return;    }    uint k = 0;    foreachType( QValueList<MetaBundle>, bundles )    {        TrackItemInfo *newInfo = new TrackItemInfo( *it );        m_length += newInfo->length();        m_trackCount++;        if( after ) {            m_trackList.insert( pos+k, newInfo );            if( isOpen() )                after = new PlaylistTrackItem( this, after, newInfo );        }        else {            if( m_loaded && !m_loading ) {                m_trackList.append( newInfo );                if( isOpen() )  //append the track item to the playlist                    m_lastTrack = new PlaylistTrackItem( this, m_lastTrack, newInfo );            }            else                tmp_droppedTracks.append( newInfo );        }        ++k;    }    if ( !m_loading ) {        PlaylistBrowser::instance()->savePlaylist( this );        if ( !m_loaded )            tmp_droppedTracks.clear(); // after saving, dropped tracks are on the file    }}void PlaylistEntry::removeTrack( QListViewItem *item, bool isLast ){    #define item static_cast<PlaylistTrackItem*>(item)    //remove a track and update playlist stats    TrackItemInfo *info = item->trackInfo();    m_length -= info->length();    m_trackCount--;    m_trackList.remove( info );    if( item == m_lastTrack ) {        QListViewItem *above = item->itemAbove();        m_lastTrack = above ? static_cast<PlaylistTrackItem *>( above ) : 0;    }    delete item;    #undef item    if( isLast )        PlaylistBrowser::instance()->savePlaylist( this );}void PlaylistEntry::customEvent( QCustomEvent *e ){    if( e->type() != (int)PlaylistReader::JobFinishedEvent )        return;#define playlist static_cast<PlaylistReader*>(e)    QString str = playlist->title;    if ( str.isEmpty() )        str = fileBaseName( m_url.path() );    str.replace( '_', ' ' );    setText( 0, str );    foreachType( BundleList, playlist->bundles )    {        const MetaBundle &b = *it;        TrackItemInfo *info = new TrackItemInfo( b );        m_trackList.append( info );        m_length += info->length();        if( isOpen() )            m_lastTrack = new PlaylistTrackItem( this, m_lastTrack, info );    }#undef playlist    //the tracks dropped on the playlist while it wasn't loaded are added to the track list    if( tmp_droppedTracks.count() ) {        for ( TrackItemInfo *info = tmp_droppedTracks.first(); info; info = tmp_droppedTracks.next() ) {            m_trackList.append( info );        }        tmp_droppedTracks.clear();    }    m_loading = false;    m_loaded = true;    stopAnimation();  //stops the loading animation    if( m_trackCount && !m_dynamic && !isDynamic() ) setOpen( true );    else listView()->repaintItem( this );    m_trackCount = m_trackList.count();}/** *  We destroy the tracks on collapsing the entry.  However, if we are using dynamic mode, then we leave them *  because adding from a custom list is problematic if the entry has no children.  Using load() is not effective *  since this is a threaded operation and would require pulling apart the entire class to make it work. */void PlaylistEntry::setOpen( bool open ){    if( open == isOpen())        return;    if( open ) {    //expand        if( m_loaded ) {            //create track items            for ( TrackItemInfo *info = m_trackList.first(); info; info = m_trackList.next() )                m_lastTrack = new PlaylistTrackItem( this, m_lastTrack, info );        }        else if( !isDynamic() || !m_dynamic ) {            load();            return;        }    }    else if( !isDynamic() || !m_dynamic ) {    //collapse        //delete all children        while( firstChild() )            delete firstChild();        m_lastTrack = 0;    }    QListViewItem::setOpen( open );    PlaylistBrowser::instance()->savePlaylists();}int PlaylistEntry::compare( QListViewItem* i, int /*col*/ ) const{    PlaylistEntry* item = static_cast<PlaylistEntry*>(i);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?