PageRenderTime 28ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/app/audioscrobbler/TagListWidget.cpp

https://github.com/dougma/lastfm-desktop
C++ | 197 lines | 135 code | 40 blank | 22 comment | 3 complexity | 231214222be2c600f5321fa311199080 MD5 | raw file
Possible License(s): GPL-3.0
  1. /*
  2. Copyright 2005-2009 Last.fm Ltd.
  3. - Primarily authored by Max Howell, Jono Cole and Doug Mansell
  4. This file is part of the Last.fm Desktop Application Suite.
  5. lastfm-desktop is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. lastfm-desktop is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with lastfm-desktop. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "TagListWidget.h"
  17. #include "PlayableMimeData.h"
  18. #include <lastfm/Tag>
  19. #include <lastfm/ws.h>
  20. #include <QDesktopServices>
  21. #include <QHeaderView>
  22. #include <QItemDelegate>
  23. #include <QMenu>
  24. #include <QUrl>
  25. TagListWidget::TagListWidget( QWidget* parent )
  26. : QTreeWidget( parent )
  27. , m_currentReply( 0 )
  28. {
  29. setColumnCount( 2 );
  30. setRootIsDecorated( false );
  31. setContextMenuPolicy( Qt::CustomContextMenu );
  32. setFrameStyle( NoFrame );
  33. setAlternatingRowColors( true );
  34. setDragEnabled( true );
  35. class TallerRowDelegate : public QItemDelegate
  36. {
  37. virtual QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
  38. {
  39. return QItemDelegate::sizeHint( option, index ) + QSize( 0, 4 );
  40. }
  41. };
  42. setItemDelegate( new TallerRowDelegate );
  43. QTreeWidget::hideColumn( 1 );
  44. QTreeWidget::header()->hide();
  45. m_menu = new QMenu( this );
  46. QActionGroup* group = new QActionGroup( this );
  47. QAction* a = m_menu->addAction( tr( "Sort by Popularity" ) );
  48. connect( a, SIGNAL(triggered()), SLOT(sortByPopularity()) );
  49. group->addAction( a );
  50. a->setCheckable( true );
  51. a->setChecked( true );
  52. a = m_menu->addAction( tr( "Sort Alphabetically" ) );
  53. connect( a, SIGNAL(triggered()), SLOT(sortAlphabetically()) );
  54. group->addAction( a );
  55. a->setCheckable( true );
  56. m_menu->addSeparator();
  57. a = m_menu->addAction( tr("Open Last.fm Page for this Tag") );
  58. connect( a, SIGNAL(triggered()), SLOT(openTagPageForCurrentItem()) );
  59. connect( this, SIGNAL(customContextMenuRequested( QPoint )), SLOT(showMenu( QPoint )) );
  60. connect( this, SIGNAL(doubleClicked( const QModelIndex& )), SLOT(onDoubleClicked ( const QModelIndex& )) );
  61. }
  62. QTreeWidgetItem*
  63. TagListWidget::createNewItem( QString tag )
  64. {
  65. tag = tag.toLower();
  66. QTreeWidgetItem* item = new QTreeWidgetItem( QStringList() << tag );
  67. QIcon icon;
  68. icon.addPixmap( QPixmap( ":/buckets/tag.png" ) );
  69. item->setIcon( 0, icon );
  70. addTopLevelItem( item );
  71. return item;
  72. }
  73. bool
  74. TagListWidget::add( QString tag )
  75. {
  76. //FIXME avoid duplicates
  77. createNewItem( tag );
  78. m_newTags += tag;
  79. return true;
  80. }
  81. void
  82. TagListWidget::showMenu( const QPoint& point )
  83. {
  84. m_menu->exec( mapToGlobal( point ) );
  85. }
  86. void
  87. TagListWidget::sortAlphabetically()
  88. {
  89. sortItems( 0, Qt::AscendingOrder );
  90. }
  91. void
  92. TagListWidget::sortByPopularity()
  93. {
  94. //I got here and wasn't sure if sortItems() should be used instead either --mxcl
  95. sortByColumn( 1, Qt::DescendingOrder );
  96. }
  97. void
  98. TagListWidget::openTagPageForCurrentItem()
  99. {
  100. QDesktopServices::openUrl( Tag( currentItem()->text( 0 ) ).www() );
  101. }
  102. void
  103. TagListWidget::setTagsRequest( QNetworkReply* r )
  104. {
  105. clear();
  106. delete m_currentReply;
  107. m_currentReply = r;
  108. connect( r, SIGNAL(finished()), SLOT(onTagsRequestFinished()) );
  109. }
  110. void
  111. TagListWidget::onTagsRequestFinished()
  112. {
  113. QNetworkReply* r = (QNetworkReply*)sender();
  114. QMap<int, QString> tags = Tag::list( r );
  115. QMapIterator<int, QString> i( tags );
  116. while (i.hasNext())
  117. {
  118. QTreeWidgetItem *entry = createNewItem( i.next().value() );
  119. // I couldn't make it sort properly otherwise, even the QVariant methods wouldn't work!
  120. entry->setText( 1, QString::number( 10 * 1000 + i.key() ) );
  121. }
  122. m_currentReply = 0;
  123. }
  124. QMimeData*
  125. TagListWidget::mimeData( const QList<QTreeWidgetItem *> items ) const
  126. {
  127. if( items.count() < 1 )
  128. return 0;
  129. Tag tag( items.first()->text( 0 ) );
  130. PlayableMimeData* pData = PlayableMimeData::createFromTag( tag );
  131. return pData;
  132. }
  133. #include <QPainter>
  134. TagIconView::TagIconView()
  135. {
  136. setAlternatingRowColors( false );
  137. disconnect( this, SIGNAL(customContextMenuRequested( QPoint )), 0, 0 );
  138. }
  139. void
  140. TagIconView::paintEvent( QPaintEvent* e )
  141. {
  142. TagListWidget::paintEvent( e );
  143. if (topLevelItemCount())
  144. return;
  145. QPainter p( viewport() );
  146. p.setPen( Qt::lightGray );
  147. #ifndef WIN32
  148. QFont f = p.font();
  149. f.setPointSize( 15 );
  150. p.setFont( f );
  151. #endif
  152. p.drawText( viewport()->rect(), Qt::AlignCenter, tr("Type a tag above,\nor choose from below") );
  153. }