PageRenderTime 174ms CodeModel.GetById 91ms app.highlight 14ms RepoModel.GetById 66ms app.codeStats 0ms

/src/sourcetree/items/temporarypageitem.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 200 lines | 141 code | 42 blank | 17 comment | 9 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
 19#include "TemporaryPageItem.h"
 20
 21#include "GlobalActionManager.h"
 22#include "ViewManager.h"
 23#include "widgets/infowidgets/AlbumInfoWidget.h"
 24#include "widgets/infowidgets/ArtistInfoWidget.h"
 25#include "widgets/infowidgets/TrackInfoWidget.h"
 26#include "widgets/SearchWidget.h"
 27#include "utils/Closure.h"
 28
 29#include <QAction>
 30
 31namespace {
 32    enum LinkType {
 33        ArtistLink,
 34        AlbumLink,
 35        TrackLink
 36    };
 37}
 38
 39using namespace Tomahawk;
 40
 41TemporaryPageItem::TemporaryPageItem ( SourcesModel* mdl, SourceTreeItem* parent, ViewPage* page, int sortValue )
 42    : SourceTreeItem( mdl, parent, SourcesModel::TemporaryPage )
 43    , m_page( page )
 44    , m_icon( QIcon( RESPATH "images/playlist-icon.png" ) )
 45    , m_sortValue( sortValue )
 46{
 47    QAction* action = 0;
 48
 49    if ( dynamic_cast< ArtistInfoWidget* >( page ) )
 50    {
 51        action = new QAction( tr( "Copy Artist Link" ), this );
 52        action->setProperty( "linkType", (int)ArtistLink );
 53
 54        m_icon = QIcon( RESPATH "images/artist-icon.png" );
 55    }
 56    else if ( dynamic_cast< AlbumInfoWidget* >( page ) )
 57    {
 58        action = new QAction( tr( "Copy Album Link" ), this );
 59        action->setProperty( "linkType", (int)AlbumLink );
 60
 61        m_icon = QIcon( RESPATH "images/album-icon.png" );
 62    }
 63    else if ( dynamic_cast< TrackInfoWidget* >( page ) )
 64    {
 65        action = new QAction( tr( "Copy Track Link" ), this );
 66        action->setProperty( "linkType", (int)TrackLink );
 67
 68        m_icon = QIcon( RESPATH "images/track-icon-sidebar.png" );
 69    }
 70    else if ( dynamic_cast< SearchWidget* >( page ) )
 71    {
 72        m_icon = QIcon( RESPATH "images/search-icon.png" );
 73    }
 74
 75    if ( action )
 76    {
 77        m_customActions << action;
 78        NewClosure( action, SIGNAL( triggered() ), this, SLOT( linkActionTriggered( QAction* ) ), action );
 79    }
 80
 81    model()->linkSourceItemToPage( this, page );
 82}
 83
 84
 85TemporaryPageItem::~TemporaryPageItem()
 86{
 87    delete m_page;
 88}
 89
 90
 91QString
 92TemporaryPageItem::text() const
 93{
 94    return m_page->title();
 95}
 96
 97
 98void
 99TemporaryPageItem::activate()
100{
101    ViewManager::instance()->show( m_page );
102}
103
104
105QIcon
106TemporaryPageItem::icon() const
107{
108    return m_icon;
109}
110
111
112int
113TemporaryPageItem::peerSortValue() const
114{
115    return m_sortValue;
116}
117
118
119int
120TemporaryPageItem::IDValue() const
121{
122    return m_sortValue;
123}
124
125
126void
127TemporaryPageItem::removeFromList()
128{
129    ViewManager::instance()->destroyPage( m_page );
130
131    model()->removeSourceItemLink( this );
132
133    int idx = parent()->children().indexOf( this );
134    parent()->beginRowsRemoved( idx, idx );
135    parent()->removeChild( this );
136    parent()->endRowsRemoved();
137
138    emit removed();
139
140    deleteLater();
141}
142
143
144void
145TemporaryPageItem::linkActionTriggered( QAction* action )
146{
147    Q_ASSERT( action );
148    if ( !action )
149        return;
150
151    const LinkType type = (LinkType)action->property( "linkType" ).toInt();
152    switch( type )
153    {
154    case ArtistLink:
155    {
156        ArtistInfoWidget* aPage = dynamic_cast< ArtistInfoWidget* >( m_page );
157        Q_ASSERT( aPage );
158        GlobalActionManager::instance()->copyOpenLink( aPage->artist() );
159
160        break;
161    }
162    case AlbumLink:
163    {
164        AlbumInfoWidget* aPage = dynamic_cast< AlbumInfoWidget* >( m_page );
165        Q_ASSERT( aPage );
166        GlobalActionManager::instance()->copyOpenLink( aPage->album() );
167
168        break;
169    }
170    case TrackLink:
171    {
172        TrackInfoWidget* tPage = dynamic_cast< TrackInfoWidget* >( m_page );
173        Q_ASSERT( tPage );
174        GlobalActionManager::instance()->copyToClipboard( tPage->query() );
175
176        break;
177    }
178    }
179}
180
181
182QList< QAction* >
183TemporaryPageItem::customActions() const
184{
185    return m_customActions;
186}
187
188
189ViewPage*
190TemporaryPageItem::page() const
191{
192    return m_page;
193}
194
195
196bool
197TemporaryPageItem::isBeingPlayed() const
198{
199    return m_page->isBeingPlayed();
200}