covermanager.cpp
来自「Amarok是一款在LINUX或其他类UNIX操作系统中运行的音频播放器软件。 」· C++ 代码 · 共 1,068 行 · 第 1/3 页
CPP
1,068 行
// (c) Pierpaolo Di Panfilo 2004// (c) 2005 Isaiah Damron <xepo@trifault.net>// See COPYING file for licensing information#include "amarok.h"#include "amarokconfig.h"#include "browserToolBar.h"#include "clicklineedit.h"#include "debug.h"#include "collectionbrowser.h" //manipulateThe()#include "collectiondb.h"#include "config.h"#include "coverfetcher.h"#include "covermanager.h"#include "pixmapviewer.h"#include "playlist.h"#include <qdesktopwidget.h> //ctor: desktop size#include <qfile.h>#include <qfontmetrics.h> //paintItem()#include <qimage.h>#include <qlabel.h>#include <qlayout.h>#include <qobjectlist.h> //used to delete all cover fetchers#include <qpainter.h> //paintItem()#include <qpalette.h> //paintItem()#include <qpixmap.h>#include <qpoint.h>#include <qprogressdialog.h>#include <qrect.h>#include <qstringlist.h>#include <qtooltip.h>#include <qtimer.h> //search filter timer#include <qtooltip.h>#include <qvbox.h>#include <kapplication.h>#include <kconfig.h>#include <kcursor.h>#include <kfiledialog.h>#include <kiconloader.h>#include <klistview.h>#include <klocale.h>#include <kmessagebox.h> //showCoverMenu()#include <kmultipledrag.h>#include <kio/netaccess.h>#include <kpopupmenu.h> //showCoverMenu()#include <kprogress.h>#include <kpushbutton.h>#include <ksqueezedtextlabel.h> //status label#include <kstatusbar.h>#include <kstringhandler.h> //paintItem#include <ktoolbar.h>#include <ktoolbarbutton.h> //clear filter button#include <kurl.h>#include <kurldrag.h>#include <kwin.h>static QString artistToSelectInInitFunction;CoverManager *CoverManager::s_instance = 0;class ArtistItem : public KListViewItem{ public: ArtistItem(QListView *view, QListViewItem *item, const QString &text) : KListViewItem(view, item, text) {} protected: int compare( QListViewItem* i, int col, bool ascending ) const { Q_UNUSED(col); Q_UNUSED(ascending); QString a = text(0); QString b = i->text(0); if ( a.startsWith( "the ", false ) ) CollectionView::manipulateThe( a, true ); if ( b.startsWith( "the ", false ) ) CollectionView::manipulateThe( b, true ); return QString::localeAwareCompare( a.lower(), b.lower() ); }};CoverManager::CoverManager() : QSplitter( 0, "TheCoverManager" ) , m_timer( new QTimer( this ) ) //search filter timer , m_fetchCounter( 0 ) , m_fetchingCovers( 0 ) , m_coversFetched( 0 ) , m_coverErrors( 0 ){ DEBUG_BLOCK s_instance = this; // Sets caption and icon correctly (needed e.g. for GNOME) kapp->setTopWidget( this ); setCaption( kapp->makeStdCaption( i18n("Cover Manager") ) ); setWFlags( WDestructiveClose ); setMargin( 4 ); //artist listview m_artistView = new KListView( this ); m_artistView->addColumn(i18n( "Albums By" )); m_artistView->setFullWidth( true ); m_artistView->setSorting( 0 ); m_artistView->setMinimumWidth( 180 ); ArtistItem *item = 0; //load artists from the collection db const QStringList artists = CollectionDB::instance()->artistList( false, false ); foreach( artists ) { QString artist = *it; item = new ArtistItem( m_artistView, item, artist ); item->setPixmap( 0, SmallIcon( Amarok::icon( "artist" ) ) ); } m_artistView->sort(); m_artistView->setSorting( -1 ); ArtistItem *last = static_cast<ArtistItem *>(m_artistView->lastItem()); item = new ArtistItem( m_artistView, 0, i18n( "All Albums" ) ); item->setPixmap( 0, SmallIcon( Amarok::icon( "album" ) ) ); QueryBuilder qb; qb.addReturnValue( QueryBuilder::tabAlbum, QueryBuilder::valName ); qb.groupBy( QueryBuilder::tabAlbum, QueryBuilder::valName ); qb.setOptions( QueryBuilder::optOnlyCompilations ); qb.setLimit( 0, 1 ); if ( qb.run().count() ) { item = new ArtistItem( m_artistView, last, i18n( "Various Artists" ) ); item->setPixmap( 0, SmallIcon("personal") ); } QVBox *vbox = new QVBox( this ); QHBox *hbox = new QHBox( vbox ); vbox->setSpacing( 4 ); hbox->setSpacing( 4 ); { //<Search LineEdit> QHBox *searchBox = new QHBox( hbox ); KToolBar* searchToolBar = new Browser::ToolBar( searchBox ); KToolBarButton *button = new KToolBarButton( "locationbar_erase", 0, searchToolBar ); m_searchEdit = new ClickLineEdit( i18n( "Enter search terms here" ), searchToolBar ); m_searchEdit->setFrame( QFrame::Sunken ); searchToolBar->setStretchableWidget( m_searchEdit ); connect( button, SIGNAL(clicked()), m_searchEdit, SLOT(clear()) ); QToolTip::add( button, i18n( "Clear search field" ) ); QToolTip::add( m_searchEdit, i18n( "Enter space-separated terms to search in the albums" ) ); hbox->setStretchFactor( searchBox, 1 ); } //</Search LineEdit> // view menu m_viewMenu = new KPopupMenu( this ); m_viewMenu->insertItem( i18n("All Albums"), AllAlbums ); m_viewMenu->insertItem( i18n("Albums With Cover"), AlbumsWithCover ); m_viewMenu->insertItem( i18n("Albums Without Cover"), AlbumsWithoutCover ); m_viewMenu->setItemChecked( AllAlbums, true ); connect( m_viewMenu, SIGNAL( activated(int) ), SLOT( changeView(int) ) ); #ifdef AMAZON_SUPPORT // amazon locale menu m_amazonLocaleMenu = new KPopupMenu( this ); m_amazonLocaleMenu->insertItem( i18n("International"), CoverFetcher::International ); m_amazonLocaleMenu->insertItem( i18n("Canada"), CoverFetcher::Canada ); m_amazonLocaleMenu->insertItem( i18n("France"), CoverFetcher::France ); m_amazonLocaleMenu->insertItem( i18n("Germany"), CoverFetcher::Germany ); m_amazonLocaleMenu->insertItem( i18n("Japan"), CoverFetcher::Japan); m_amazonLocaleMenu->insertItem( i18n("United Kingdom"), CoverFetcher::UK ); connect( m_amazonLocaleMenu, SIGNAL( activated(int) ), SLOT( changeLocale(int) ) ); #endif KToolBar* toolBar = new KToolBar( hbox ); toolBar->setIconText( KToolBar::IconTextRight ); toolBar->setFrameShape( QFrame::NoFrame ); toolBar->insertButton( "view_choose", 1, m_viewMenu, true, i18n( "View" ) ); #ifdef AMAZON_SUPPORT toolBar->insertButton( "babelfish", 2, m_amazonLocaleMenu, true, i18n( "Amazon Locale" ) ); QString locale = AmarokConfig::amazonLocale(); m_currentLocale = CoverFetcher::localeStringToID( locale ); m_amazonLocaleMenu->setItemChecked( m_currentLocale, true ); //fetch missing covers button m_fetchButton = new KPushButton( KGuiItem( i18n("Fetch Missing Covers"), Amarok::icon( "download" ) ), hbox ); connect( m_fetchButton, SIGNAL(clicked()), SLOT(fetchMissingCovers()) ); #endif //cover view m_coverView = new CoverView( vbox ); //status bar KStatusBar *m_statusBar = new KStatusBar( vbox ); m_statusBar->addWidget( m_statusLabel = new KSqueezedTextLabel( m_statusBar ), 4 ); m_statusLabel->setIndent( 3 ); m_statusBar->addWidget( m_progressBox = new QHBox( m_statusBar ), 1, true ); KPushButton *stopButton = new KPushButton( KGuiItem(i18n("Abort"), "stop"), m_progressBox ); connect( stopButton, SIGNAL(clicked()), SLOT(stopFetching()) ); m_progress = new KProgress( m_progressBox ); m_progress->setCenterIndicator( true ); const int h = m_statusLabel->height() + 3; m_statusLabel->setFixedHeight( h ); m_progressBox->setFixedHeight( h ); m_progressBox->hide(); // signals and slots connections connect( m_artistView, SIGNAL(selectionChanged( QListViewItem* ) ), SLOT(slotArtistSelected( QListViewItem* )) ); connect( m_coverView, SIGNAL(contextMenuRequested( QIconViewItem*, const QPoint& )), SLOT(showCoverMenu( QIconViewItem*, const QPoint& )) ); connect( m_coverView, SIGNAL(executed( QIconViewItem* )), SLOT(coverItemExecuted( QIconViewItem* )) ); connect( m_timer, SIGNAL(timeout()), SLOT(slotSetFilter()) ); connect( m_searchEdit, SIGNAL(textChanged( const QString& )), SLOT(slotSetFilterTimeout()) ); #ifdef AMAZON_SUPPORT connect( CollectionDB::instance(), SIGNAL(coverFetched( const QString&, const QString& )), SLOT(coverFetched( const QString&, const QString& )) ); connect( CollectionDB::instance(), SIGNAL(coverRemoved( const QString&, const QString& )), SLOT(coverRemoved( const QString&, const QString& )) ); connect( CollectionDB::instance(), SIGNAL(coverFetcherError( const QString& )), SLOT(coverFetcherError()) ); #endif m_currentView = AllAlbums; QSize size = QApplication::desktop()->screenGeometry( this ).size() / 1.5; resize( Amarok::config( "Cover Manager" )->readSizeEntry( "Window Size", &size ) ); show(); QTimer::singleShot( 0, this, SLOT(init()) );}CoverManager::~CoverManager(){ DEBUG_BLOCK Amarok::config( "Cover Manager" )->writeEntry( "Window Size", size() ); s_instance = 0;}void CoverManager::init(){ DEBUG_BLOCK QListViewItem *item = 0; if ( !artistToSelectInInitFunction.isEmpty() ) for ( item = m_artistView->firstChild(); item; item = item->nextSibling() ) if ( item->text( 0 ) == artistToSelectInInitFunction ) break; if ( item == 0 ) item = m_artistView->firstChild(); m_artistView->setSelected( item, true );}CoverViewDialog::CoverViewDialog( const QString& artist, const QString& album, QWidget *parent ) : QDialog( parent, 0, false, WDestructiveClose | WType_TopLevel | WNoAutoErase ) , m_pixmap( CollectionDB::instance()->albumImage( artist, album, false, 0 ) ){ KWin::setType( winId(), NET::Utility ); kapp->setTopWidget( this ); setCaption( kapp->makeStdCaption( i18n("%1 - %2").arg( artist, album ) ) ); m_layout = new QHBoxLayout( this ); m_layout->setAutoAdd( true ); m_pixmapViewer = new PixmapViewer( this, m_pixmap ); setFixedSize( m_pixmapViewer->maximalSize() );}void CoverManager::viewCover( const QString& artist, const QString& album, QWidget *parent ) //static{ //QDialog means "escape" works as expected QDialog *dialog = new CoverViewDialog( artist, album, parent ); dialog->show();}QString CoverManager::amazonTld() //static{ if (AmarokConfig::amazonLocale() == "us") return "com"; else if (AmarokConfig::amazonLocale()== "jp") return "co.jp"; else if (AmarokConfig::amazonLocale() == "uk") return "co.uk"; else if (AmarokConfig::amazonLocale() == "ca") return "ca"; else return AmarokConfig::amazonLocale();}void CoverManager::fetchMissingCovers() //SLOT{ #ifdef AMAZON_SUPPORT DEBUG_BLOCK for ( QIconViewItem *item = m_coverView->firstItem(); item; item = item->nextItem() ) { CoverViewItem *coverItem = static_cast<CoverViewItem*>( item ); if( !coverItem->hasCover() ) { m_fetchCovers += coverItem->artist() + " @@@ " + coverItem->album(); m_fetchingCovers++; } } if( !m_fetchCounter ) //loop isn't started yet fetchCoversLoop(); updateStatusBar(); m_fetchButton->setEnabled( false ); #endif}void CoverManager::fetchCoversLoop() //SLOT{ #ifdef AMAZON_SUPPORT if( m_fetchCounter < m_fetchCovers.count() ) { //get artist and album from keyword const QStringList values = QStringList::split( " @@@ ", m_fetchCovers[m_fetchCounter], true ); if( values.count() > 1 ) CollectionDB::instance()->fetchCover( this, values[0], values[1], m_fetchCovers.count() != 1); //edit mode when fetching 1 cover m_fetchCounter++; // Wait 1 second, since amazon caps the number of accesses per client QTimer::singleShot( 1000, this, SLOT( fetchCoversLoop() ) ); } else { m_fetchCovers.clear(); m_fetchCounter = 0; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?