/src/sourcetree/items/temporarypageitem.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 200 lines · 141 code · 42 blank · 17 comment · 10 complexity · 573311ab85abac95712ee5138079cd49 MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
  4. *
  5. * Tomahawk 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. *
  10. * Tomahawk is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "TemporaryPageItem.h"
  19. #include "GlobalActionManager.h"
  20. #include "ViewManager.h"
  21. #include "widgets/infowidgets/AlbumInfoWidget.h"
  22. #include "widgets/infowidgets/ArtistInfoWidget.h"
  23. #include "widgets/infowidgets/TrackInfoWidget.h"
  24. #include "widgets/SearchWidget.h"
  25. #include "utils/Closure.h"
  26. #include <QAction>
  27. namespace {
  28. enum LinkType {
  29. ArtistLink,
  30. AlbumLink,
  31. TrackLink
  32. };
  33. }
  34. using namespace Tomahawk;
  35. TemporaryPageItem::TemporaryPageItem ( SourcesModel* mdl, SourceTreeItem* parent, ViewPage* page, int sortValue )
  36. : SourceTreeItem( mdl, parent, SourcesModel::TemporaryPage )
  37. , m_page( page )
  38. , m_icon( QIcon( RESPATH "images/playlist-icon.png" ) )
  39. , m_sortValue( sortValue )
  40. {
  41. QAction* action = 0;
  42. if ( dynamic_cast< ArtistInfoWidget* >( page ) )
  43. {
  44. action = new QAction( tr( "Copy Artist Link" ), this );
  45. action->setProperty( "linkType", (int)ArtistLink );
  46. m_icon = QIcon( RESPATH "images/artist-icon.png" );
  47. }
  48. else if ( dynamic_cast< AlbumInfoWidget* >( page ) )
  49. {
  50. action = new QAction( tr( "Copy Album Link" ), this );
  51. action->setProperty( "linkType", (int)AlbumLink );
  52. m_icon = QIcon( RESPATH "images/album-icon.png" );
  53. }
  54. else if ( dynamic_cast< TrackInfoWidget* >( page ) )
  55. {
  56. action = new QAction( tr( "Copy Track Link" ), this );
  57. action->setProperty( "linkType", (int)TrackLink );
  58. m_icon = QIcon( RESPATH "images/track-icon-sidebar.png" );
  59. }
  60. else if ( dynamic_cast< SearchWidget* >( page ) )
  61. {
  62. m_icon = QIcon( RESPATH "images/search-icon.png" );
  63. }
  64. if ( action )
  65. {
  66. m_customActions << action;
  67. NewClosure( action, SIGNAL( triggered() ), this, SLOT( linkActionTriggered( QAction* ) ), action );
  68. }
  69. model()->linkSourceItemToPage( this, page );
  70. }
  71. TemporaryPageItem::~TemporaryPageItem()
  72. {
  73. delete m_page;
  74. }
  75. QString
  76. TemporaryPageItem::text() const
  77. {
  78. return m_page->title();
  79. }
  80. void
  81. TemporaryPageItem::activate()
  82. {
  83. ViewManager::instance()->show( m_page );
  84. }
  85. QIcon
  86. TemporaryPageItem::icon() const
  87. {
  88. return m_icon;
  89. }
  90. int
  91. TemporaryPageItem::peerSortValue() const
  92. {
  93. return m_sortValue;
  94. }
  95. int
  96. TemporaryPageItem::IDValue() const
  97. {
  98. return m_sortValue;
  99. }
  100. void
  101. TemporaryPageItem::removeFromList()
  102. {
  103. ViewManager::instance()->destroyPage( m_page );
  104. model()->removeSourceItemLink( this );
  105. int idx = parent()->children().indexOf( this );
  106. parent()->beginRowsRemoved( idx, idx );
  107. parent()->removeChild( this );
  108. parent()->endRowsRemoved();
  109. emit removed();
  110. deleteLater();
  111. }
  112. void
  113. TemporaryPageItem::linkActionTriggered( QAction* action )
  114. {
  115. Q_ASSERT( action );
  116. if ( !action )
  117. return;
  118. const LinkType type = (LinkType)action->property( "linkType" ).toInt();
  119. switch( type )
  120. {
  121. case ArtistLink:
  122. {
  123. ArtistInfoWidget* aPage = dynamic_cast< ArtistInfoWidget* >( m_page );
  124. Q_ASSERT( aPage );
  125. GlobalActionManager::instance()->copyOpenLink( aPage->artist() );
  126. break;
  127. }
  128. case AlbumLink:
  129. {
  130. AlbumInfoWidget* aPage = dynamic_cast< AlbumInfoWidget* >( m_page );
  131. Q_ASSERT( aPage );
  132. GlobalActionManager::instance()->copyOpenLink( aPage->album() );
  133. break;
  134. }
  135. case TrackLink:
  136. {
  137. TrackInfoWidget* tPage = dynamic_cast< TrackInfoWidget* >( m_page );
  138. Q_ASSERT( tPage );
  139. GlobalActionManager::instance()->copyToClipboard( tPage->query() );
  140. break;
  141. }
  142. }
  143. }
  144. QList< QAction* >
  145. TemporaryPageItem::customActions() const
  146. {
  147. return m_customActions;
  148. }
  149. ViewPage*
  150. TemporaryPageItem::page() const
  151. {
  152. return m_page;
  153. }
  154. bool
  155. TemporaryPageItem::isBeingPlayed() const
  156. {
  157. return m_page->isBeingPlayed();
  158. }