queuemanager.cpp

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

CPP
537
字号
        QueueManager::instance()->addItems( after );    }}///////////////////////////////////////////////////////////////////////////////////////////// CLASS QueueManager//////////////////////////////////////////////////////////////////////////////////////////QueueManager *QueueManager::s_instance = 0;QueueManager::QueueManager( QWidget *parent, const char *name )    : KDialogBase( KDialogBase::Swallow, 0, parent, name, false, 0, Ok|Apply|Cancel ){    s_instance = this;    // Gives the window a small title bar, and skips a taskbar entry    KWin::setType( winId(), NET::Utility );    KWin::setState( winId(), NET::SkipTaskbar );    kapp->setTopWidget( this );    setCaption( kapp->makeStdCaption( i18n("Queue Manager") ) );    setInitialSize( QSize( 400, 260 ) );    QVBox *mainBox = new QVBox( this );    setMainWidget( mainBox );    QHBox *box = new QHBox( mainWidget() );    box->setSpacing( 5 );    m_listview = new QueueList( box );    QVBox *buttonBox = new QVBox( box );    m_up     = new KPushButton( KGuiItem( QString::null, "up" ), buttonBox );    m_down   = new KPushButton( KGuiItem( QString::null, "down" ), buttonBox );    m_remove = new KPushButton( KGuiItem( QString::null, Amarok::icon( "dequeue_track" ) ), buttonBox );    m_add    = new KPushButton( KGuiItem( QString::null, Amarok::icon( "queue_track" ) ), buttonBox );    m_clear  = new KPushButton( KGuiItem( QString::null, Amarok::icon( "playlist_clear" ) ), buttonBox );    QToolTip::add( m_up,     i18n( "Move up" ) );    QToolTip::add( m_down,   i18n( "Move down" ) );    QToolTip::add( m_remove, i18n( "Remove" ) );    QToolTip::add( m_add,    i18n( "Enqueue track" ) );    QToolTip::add( m_clear,  i18n( "Clear queue" ) );    m_up->setEnabled( false );    m_down->setEnabled( false );    m_remove->setEnabled( false );    m_add->setEnabled( false );    m_clear->setEnabled( false );    connect( m_up,     SIGNAL( clicked() ), m_listview, SLOT( moveSelectedUp() ) );    connect( m_down,   SIGNAL( clicked() ), m_listview, SLOT( moveSelectedDown() ) );    connect( m_remove, SIGNAL( clicked() ), this,       SLOT( removeSelected() ) );    connect( m_add,    SIGNAL( clicked() ), this,       SLOT( addItems() ) );    connect( m_clear,  SIGNAL( clicked() ), m_listview, SLOT( clear() ) );    Playlist *pl = Playlist::instance();    connect( pl,         SIGNAL( selectionChanged() ),    SLOT( updateButtons() ) );    connect( m_listview, SIGNAL( selectionChanged() ),    SLOT( updateButtons() ) );    connect( pl,         SIGNAL( queueChanged(const PLItemList &, const PLItemList &) ),                         SLOT( changeQueuedItems(const PLItemList &, const PLItemList &) ) );    connect( this,       SIGNAL( applyClicked()), SLOT( applyNow() ) );    connect( m_listview, SIGNAL( changed() ), this, SLOT ( changed() ) );    s_instance->enableButtonApply(false);    insertItems();}QueueManager::~QueueManager(){    s_instance = 0;}voidQueueManager::applyNow(){    Playlist *pl = Playlist::instance();    pl->changeFromQueueManager( newQueue() );    s_instance->enableButtonApply(false);}voidQueueManager::addItems( QListViewItem *after ){    /*        HACK!!!!! We can know which items where dragged since they should still be selected        I do this, because:        - Dragging items from the playlist provides urls        - Providing urls, requires iterating through the entire list in order to find which          item was selected.  Possibly a very expensive task - worst case: O(n)        - After a drag, those items are still selected in the playlist, so we can find out          which PlaylistItems were dragged by selectedItems();    */    if( !after )        after = m_listview->lastChild();    QPtrList<QListViewItem> list = Playlist::instance()->selectedItems();    bool item_added = false;    for( QListViewItem *item = list.first(); item; item = list.next() )    {        #define item static_cast<PlaylistItem*>(item)        QValueList<PlaylistItem*> current = m_map.values();        if( current.find( item ) == current.end() ) //avoid duplication        {            QString title = i18n("%1 - %2").arg( item->artist(), item->title() );            after = new QueueItem( m_listview, after, title );            m_map[ after ] = item;            item_added = true;        }        #undef item    }    if( item_added )        emit m_listview->changed();}voidQueueManager::changeQueuedItems( const PLItemList &in, const PLItemList &out ) //SLOT{    QPtrListIterator<PlaylistItem> it(in);    for( it.toFirst(); it; ++it ) addQueuedItem( *it );    it = QPtrListIterator<PlaylistItem>(out);    for( it.toFirst(); it; ++it ) removeQueuedItem( *it );}voidQueueManager::addQueuedItem( PlaylistItem *item ){    Playlist *pl = Playlist::instance();    if( !pl ) return; //should never happen    const int index = pl->m_nextTracks.findRef( item );    QListViewItem *after;    if( !index ) after = 0;    else    {        int find = m_listview->childCount();        if( index - 1 <= find )            find = index - 1;        after = m_listview->itemAtIndex( find );    }    QValueList<PlaylistItem*>         current = m_map.values();    QValueListIterator<PlaylistItem*> newItem = current.find( item );    QString title = i18n("%1 - %2").arg( item->artist(), item->title() );    if( newItem == current.end() ) //avoid duplication    {        after = new QueueItem( m_listview, after, title );        m_map[ after ] = item;    }}voidQueueManager::removeQueuedItem( PlaylistItem *item ){    Playlist *pl = Playlist::instance();    if( !pl ) return; //should never happen    QValueList<PlaylistItem*>         current = m_map.values();    QValueListIterator<PlaylistItem*> newItem = current.find( item );    QString title = i18n("%1 - %2").arg( item->artist(), item->title() );    QListViewItem *removableItem = m_listview->findItem( title, 0 );    if( removableItem )    {        //Remove the key from the map, so we can re-queue the item        QMapIterator<QListViewItem*, PlaylistItem*> end(  m_map.end() );        for( QMapIterator<QListViewItem*, PlaylistItem*> it = m_map.begin(); it != end; ++it )        {            if( it.data() == item )            {                m_map.remove( it );                //Remove the item from the queuelist                m_listview->takeItem( removableItem );                delete removableItem;                return;            }        }    }}/// Playlist uses this to determine the altered queue and reflect the changes.QPtrList<PlaylistItem>QueueManager::newQueue(){    QPtrList<PlaylistItem> queue;    for( QListViewItem *key = m_listview->firstChild(); key; key = key->nextSibling() )    {        queue.append( m_map[ key ] );    }    return queue;}voidQueueManager::insertItems(){    QPtrList<PlaylistItem> list = Playlist::instance()->m_nextTracks;    QListViewItem *last = 0;    for( PlaylistItem *item = list.first(); item; item = list.next() )    {        QString title = i18n("%1 - %2").arg( item->artist(), item->title() );        last = new QueueItem( m_listview, last, title );        m_map[ last ] = item;    }    updateButtons();}voidQueueManager::changed() // SLOT{  s_instance->enableButtonApply(true);}voidQueueManager::removeSelected() //SLOT{    QPtrList<QListViewItem>  selected = m_listview->selectedItems();    bool item_removed = false;    for( QListViewItem *item = selected.first(); item; item = selected.next() )    {        //Remove the key from the map, so we can re-queue the item        QMapIterator<QListViewItem*, PlaylistItem*> it = m_map.find( item );        m_map.remove( it );        //Remove the item from the queuelist        m_listview->takeItem( item );        delete item;        item_removed = true;    }    if( item_removed )        emit m_listview->changed();}voidQueueManager::updateButtons() //SLOT{    const bool enablePL = !Playlist::instance()->selectedItems().isEmpty();    const bool emptyLV  = m_listview->isEmpty();    const bool enableQL = m_listview->hasSelection() && !emptyLV;    m_up->setEnabled( enableQL );    m_down->setEnabled( enableQL );    m_remove->setEnabled( enableQL );    m_add->setEnabled( enablePL );    m_clear->setEnabled( !emptyLV );}#include "queuemanager.moc"

⌨️ 快捷键说明

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