mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-04-19 15:31:59 +02:00
allow deleting of temporary item in source tree view
This commit is contained in:
parent
35a7acd606
commit
ec2164b77e
@ -541,6 +541,18 @@ ViewManager::showHistory( int historyPosition )
|
||||
setPage( page, false );
|
||||
}
|
||||
|
||||
void
|
||||
ViewManager::removeFromHistory ( ViewPage* p )
|
||||
{
|
||||
if ( currentPage() == p )
|
||||
{
|
||||
historyBack();
|
||||
m_pageHistory.removeAll( p );
|
||||
} else
|
||||
if ( m_pageHistory.removeAll( p ) )
|
||||
setHistoryPosition( m_historyPosition - 1 );
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
ViewManager::setFilter( const QString& filter )
|
||||
|
@ -133,6 +133,7 @@ public slots:
|
||||
void historyBack();
|
||||
void historyForward();
|
||||
void showHistory( int historyPosition );
|
||||
void removeFromHistory( Tomahawk::ViewPage* p );
|
||||
|
||||
void setTreeMode();
|
||||
void setTableMode();
|
||||
|
@ -37,7 +37,6 @@ CollectionItem::CollectionItem( SourcesModel* mdl, SourceTreeItem* parent, cons
|
||||
, m_source( source )
|
||||
, m_playlists( 0 )
|
||||
, m_stations( 0 )
|
||||
, m_tempItem( 0 )
|
||||
, m_sourceInfoItem( 0 )
|
||||
, m_coolPlaylistsItem( 0 )
|
||||
, m_lovedTracksItem()
|
||||
@ -372,19 +371,27 @@ CollectionItem::tempPageActivated( Tomahawk::ViewPage* v )
|
||||
{
|
||||
QString name = v->title();
|
||||
m_curTempPage = v;
|
||||
if( !m_tempItem ) {
|
||||
if( m_tempItem.isNull() ) {
|
||||
emit beginRowsAdded( children().count(), children().count() );
|
||||
m_tempItem = new GenericPageItem( model(), this, name, QIcon( RESPATH "images/playlist-icon.png" ),
|
||||
boost::bind( &CollectionItem::tempItemClicked, this ),
|
||||
boost::bind( &CollectionItem::getTempPage, this )
|
||||
);
|
||||
m_tempItem = QWeakPointer< GenericPageItem >( new GenericPageItem( model(), this, name, QIcon( RESPATH "images/playlist-icon.png" ),
|
||||
boost::bind( &CollectionItem::tempItemClicked, this ),
|
||||
boost::bind( &CollectionItem::getTempPage, this ) ) );
|
||||
m_tempItem.data()->setDeleteFunc( boost::bind( &CollectionItem::deleteTempPage, this ) );
|
||||
m_tempItem.data()->setIsTemp( true );
|
||||
emit endRowsAdded();
|
||||
} else {
|
||||
m_tempItem->setText( name );
|
||||
m_tempItem.data()->setText( name );
|
||||
}
|
||||
|
||||
model()->linkSourceItemToPage( m_tempItem, v );
|
||||
emit selectRequest( m_tempItem );
|
||||
model()->linkSourceItemToPage( m_tempItem.data(), v );
|
||||
emit selectRequest( m_tempItem.data() );
|
||||
}
|
||||
|
||||
void
|
||||
CollectionItem::deleteTempPage()
|
||||
{
|
||||
model()->removeSourceItemLink( m_tempItem.data() );
|
||||
ViewManager::instance()->removeFromHistory( m_curTempPage );
|
||||
}
|
||||
|
||||
|
||||
|
@ -59,6 +59,7 @@ private slots:
|
||||
void tempPageActivated( Tomahawk::ViewPage* );
|
||||
Tomahawk::ViewPage* tempItemClicked();
|
||||
Tomahawk::ViewPage* getTempPage() const;
|
||||
void deleteTempPage();
|
||||
|
||||
Tomahawk::ViewPage* sourceInfoClicked();
|
||||
Tomahawk::ViewPage* getSourceInfoPage() const;
|
||||
@ -79,7 +80,7 @@ private:
|
||||
CategoryItem* m_playlists;
|
||||
CategoryItem* m_stations;
|
||||
|
||||
GenericPageItem* m_tempItem;
|
||||
QWeakPointer<GenericPageItem> m_tempItem;
|
||||
GenericPageItem* m_sourceInfoItem;
|
||||
GenericPageItem* m_coolPlaylistsItem;
|
||||
GenericPageItem* m_lovedTracksItem;
|
||||
|
@ -31,6 +31,7 @@ GenericPageItem::GenericPageItem( SourcesModel* model, SourceTreeItem* parent, c
|
||||
, m_icon( icon )
|
||||
, m_text( text )
|
||||
, m_sortValue( 0 )
|
||||
, m_temp( false )
|
||||
, m_show( show )
|
||||
, m_get( get )
|
||||
{
|
||||
@ -80,3 +81,24 @@ GenericPageItem::setText( const QString &text )
|
||||
m_text = text;
|
||||
emit updated();
|
||||
}
|
||||
|
||||
void
|
||||
GenericPageItem::deleteTempPage()
|
||||
{
|
||||
Q_ASSERT( m_temp );
|
||||
|
||||
if ( ViewManager::instance()->currentPage() == m_get() )
|
||||
{
|
||||
ViewManager::instance()->historyBack();
|
||||
}
|
||||
|
||||
m_delete();
|
||||
|
||||
int idx = parent()->children().indexOf( this );
|
||||
parent()->beginRowsRemoved( idx, idx );
|
||||
parent()->removeChild( this );
|
||||
parent()->endRowsRemoved();
|
||||
|
||||
deleteLater();
|
||||
}
|
||||
|
||||
|
@ -41,6 +41,12 @@ public:
|
||||
|
||||
void setText( const QString& text );
|
||||
void setSortValue( int value ) { m_sortValue = value; }
|
||||
|
||||
void setIsTemp( bool temp ) { m_temp = temp; }
|
||||
bool isTempItem() { return m_temp; }
|
||||
|
||||
void setDeleteFunc( boost::function<void()> deleter ) { m_delete = deleter; };
|
||||
void deleteTempPage();
|
||||
signals:
|
||||
void activated();
|
||||
|
||||
@ -48,8 +54,10 @@ private:
|
||||
QIcon m_icon;
|
||||
QString m_text;
|
||||
int m_sortValue;
|
||||
bool m_temp;
|
||||
boost::function< Tomahawk::ViewPage*() > m_show;
|
||||
boost::function< Tomahawk::ViewPage*() > m_get;
|
||||
boost::function< void() > m_delete;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -279,9 +279,11 @@ SourcesModel::viewPageActivated( Tomahawk::ViewPage* page )
|
||||
Q_ASSERT( m_sourceTreeLinks[ page ] );
|
||||
// qDebug() << "Got view page activated for item:" << m_sourceTreeLinks[ page ]->text();
|
||||
QModelIndex idx = indexFromItem( m_sourceTreeLinks[ page ] );
|
||||
Q_ASSERT( idx.isValid() );
|
||||
|
||||
emit selectRequest( idx );
|
||||
if ( !idx.isValid() )
|
||||
m_sourceTreeLinks.remove( page );
|
||||
else
|
||||
emit selectRequest( idx );
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -420,9 +422,30 @@ SourcesModel::linkSourceItemToPage( SourceTreeItem* item, ViewPage* p )
|
||||
if( m_viewPageDelayedCacheItem == p )
|
||||
emit selectRequest( indexFromItem( item ) );
|
||||
|
||||
if ( QObject* obj = dynamic_cast< QObject* >( p ) )
|
||||
{
|
||||
if( obj->metaObject()->indexOfSignal( "destroyed(QWidget*)" ) > -1 )
|
||||
connect( obj, SIGNAL( destroyed( QWidget* ) ), SLOT( onWidgetDestroyed( QWidget* ) ), Qt::UniqueConnection );
|
||||
}
|
||||
m_viewPageDelayedCacheItem = 0;
|
||||
}
|
||||
|
||||
void
|
||||
SourcesModel::onWidgetDestroyed( QWidget* w )
|
||||
{
|
||||
int ret = m_sourceTreeLinks.remove( dynamic_cast< Tomahawk::ViewPage* > ( w ) );
|
||||
qDebug() << "REMOVED STALE SOURCE PAGE?" << ret;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SourcesModel::removeSourceItemLink( SourceTreeItem* item )
|
||||
{
|
||||
QList< ViewPage* > pages = m_sourceTreeLinks.keys( item );
|
||||
foreach( ViewPage* p, pages )
|
||||
m_sourceTreeLinks.remove( p );
|
||||
}
|
||||
|
||||
|
||||
SourceTreeItem*
|
||||
SourcesModel::itemFromIndex( const QModelIndex& idx ) const
|
||||
|
@ -88,6 +88,7 @@ public:
|
||||
bool removeItem( const Tomahawk::source_ptr& source );
|
||||
|
||||
void linkSourceItemToPage( SourceTreeItem* item, Tomahawk::ViewPage* p );
|
||||
void removeSourceItemLink( SourceTreeItem* item );
|
||||
|
||||
QModelIndex indexFromItem( SourceTreeItem* item ) const;
|
||||
|
||||
@ -114,6 +115,7 @@ private slots:
|
||||
void onSourceAdded( const Tomahawk::source_ptr& source );
|
||||
void onSourceRemoved( const Tomahawk::source_ptr& source );
|
||||
|
||||
void onWidgetDestroyed( QWidget* w );
|
||||
private:
|
||||
SourceTreeItem* itemFromIndex( const QModelIndex& idx ) const;
|
||||
int rowForItem( SourceTreeItem* item ) const;
|
||||
|
@ -41,6 +41,7 @@
|
||||
#include "dropjob.h"
|
||||
|
||||
#include "utils/logger.h"
|
||||
#include "items/genericpageitems.h"
|
||||
|
||||
using namespace Tomahawk;
|
||||
|
||||
@ -64,9 +65,11 @@ protected:
|
||||
|
||||
editor->setGeometry( editor->geometry().adjusted( 2*TREEVIEW_INDENT_ADD, 0, 0, 0 ) );
|
||||
}
|
||||
virtual bool editorEvent( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index );
|
||||
|
||||
private:
|
||||
QAbstractItemView* m_parent;
|
||||
mutable int m_iconHeight;
|
||||
};
|
||||
|
||||
|
||||
@ -694,8 +697,26 @@ SourceDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, co
|
||||
else
|
||||
{
|
||||
QStyledItemDelegate::paint( painter, o, index );
|
||||
|
||||
if ( type == SourcesModel::GenericPage )
|
||||
{
|
||||
GenericPageItem* gpi = qobject_cast< GenericPageItem* >( item );
|
||||
Q_ASSERT( gpi );
|
||||
|
||||
// initStyleOption( &o3, index );
|
||||
if ( gpi->isTempItem() && o3.state & QStyle::State_MouseOver )
|
||||
{
|
||||
// draw close icon
|
||||
int padding = 3;
|
||||
m_iconHeight = ( o3.rect.height() - 2*padding );
|
||||
QPixmap p( RESPATH "images/list-remove.png" );
|
||||
p = p.scaledToHeight( m_iconHeight, Qt::SmoothTransformation );
|
||||
|
||||
QRect r ( o3.rect.right() - padding - m_iconHeight, padding + o3.rect.y(), m_iconHeight, m_iconHeight );
|
||||
painter->drawPixmap( r, p );
|
||||
}
|
||||
}
|
||||
/*QStyleOptionViewItemV4 opt = o;
|
||||
initStyleOption( &opt, index );
|
||||
|
||||
// shrink the indentations. count how indented this item is and remove it
|
||||
int indentMult = 0;
|
||||
@ -717,3 +738,34 @@ SourceDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, co
|
||||
painter->setFont( savedFont );
|
||||
#endif
|
||||
}
|
||||
|
||||
bool
|
||||
SourceDelegate::editorEvent ( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index )
|
||||
{
|
||||
|
||||
if ( event->type() == QEvent::MouseButtonRelease )
|
||||
{
|
||||
SourcesModel::RowType type = static_cast< SourcesModel::RowType >( index.data( SourcesModel::SourceTreeItemTypeRole ).toInt() );
|
||||
if ( type == SourcesModel::GenericPage )
|
||||
{
|
||||
GenericPageItem* gpi = qobject_cast< GenericPageItem* >( index.data( SourcesModel::SourceTreeItemRole ).value< SourceTreeItem* >() );
|
||||
Q_ASSERT( gpi );
|
||||
if ( gpi->isTempItem() )
|
||||
{
|
||||
QMouseEvent* ev = static_cast< QMouseEvent* >( event );
|
||||
|
||||
QStyleOptionViewItemV4 o = option;
|
||||
initStyleOption( &o, index );
|
||||
int padding = 3;
|
||||
QRect r ( o.rect.right() - padding - m_iconHeight, padding + o.rect.y(), m_iconHeight, m_iconHeight );
|
||||
|
||||
if ( r.contains( ev->pos() ) )
|
||||
{
|
||||
gpi->deleteTempPage();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return QStyledItemDelegate::editorEvent ( event, model, option, index );
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user