1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-24 01:39:42 +01:00

Initial work on weight for job view items, to keep predictable ordering (especially important for not having ACL checks jump around)

This commit is contained in:
Jeff Mitchell 2012-07-08 15:52:53 -04:00
parent ebb59b50c6
commit 3e1310eac4
9 changed files with 106 additions and 10 deletions

View File

@ -266,7 +266,7 @@ TomahawkWindow::setupSideBar()
m_sourcetree = new SourceTreeView( this );
JobStatusView* jobsView = new JobStatusView( m_sidebar );
m_jobsModel = new JobStatusModel( jobsView );
m_jobsModel = new JobStatusSortModel( jobsView );
jobsView->setModel( m_jobsModel );
m_queueView = new QueueView( m_sidebar );

View File

@ -39,7 +39,7 @@ namespace Tomahawk
}
}
class JobStatusModel;
class JobStatusSortModel;
class QSearchField;
class SourceTreeView;
class QAction;
@ -162,7 +162,7 @@ private:
QPushButton* m_queueButton;
QueueView* m_queueView;
AnimatedSplitter* m_sidebar;
JobStatusModel* m_jobsModel;
JobStatusSortModel* m_jobsModel;
QAction* m_backAction;
QAction* m_forwardAction;

View File

@ -64,6 +64,8 @@ public:
explicit ACLJobItem( ACLRegistry::User user, const QString &username );
virtual ~ACLJobItem();
virtual int weight() const { return 99; }
virtual QString rightColumnText() const { return QString(); }
virtual QString mainText() const { return QString(); }
virtual QPixmap icon() const { return QPixmap(); }

View File

@ -30,6 +30,7 @@ public:
void done();
virtual int weight() const { return 50; }
virtual QString rightColumnText() const { return QString(); }
virtual QString mainText() const;
virtual QPixmap icon() const;

View File

@ -44,6 +44,7 @@ public:
virtual ~JobStatusItem();
virtual QString type() const = 0;
virtual int weight() const { return 0; }
/// Please cache this.
virtual QPixmap icon() const = 0;

View File

@ -25,6 +25,70 @@
#include <QPixmap>
JobStatusSortModel::JobStatusSortModel( QObject* parent )
: QSortFilterProxyModel( parent )
, m_subModel( this )
{
setDynamicSortFilter( true );
connect( &m_subModel, SIGNAL( rowsInserted( QModelIndex, int, int ) ), this, SIGNAL( rowsInserted( QModelIndex, int, int ) ) );
connect( &m_subModel, SIGNAL( rowsRemoved( QModelIndex, int, int ) ), this, SIGNAL( rowsRemoved( QModelIndex, int, int ) ) );
connect( &m_subModel, SIGNAL( modelReset() ), this, SIGNAL( modelReset() ) );
connect( &m_subModel, SIGNAL( customDelegateJobInserted( int, JobStatusItem* ) ), this, SLOT( customDelegateJobInsertedSlot( int, JobStatusItem* ) ) );
connect( &m_subModel, SIGNAL( customDelegateJobRemoved( int ) ), this, SLOT( customDelegateJobRemovedSlot( int ) ) );
connect( &m_subModel, SIGNAL( refreshDelegates() ), this, SLOT( refreshDelegatesSlot() ) );
}
JobStatusSortModel::~JobStatusSortModel()
{
}
void
JobStatusSortModel::addJob( JobStatusItem* item )
{
m_subModel.addJob( item );
}
void
JobStatusSortModel::customDelegateJobInsertedSlot( int row, JobStatusItem* item )
{
emit customDelegateJobInserted( mapFromSource( m_subModel.index( row ) ).row(), item );
}
void
JobStatusSortModel::customDelegateJobRemovedSlot( int row )
{
emit customDelegateJobRemoved( mapFromSource( m_subModel.index( row ) ).row() );
}
void
JobStatusSortModel::refreshDelegatesSlot()
{
emit refreshDelegates();
}
bool
JobStatusSortModel::lessThan( const QModelIndex& left, const QModelIndex& right ) const
{
QVariant leftVar = left.data( JobStatusModel::JobDataRole );
JobStatusItem* leftItem = leftVar.value< JobStatusItem* >();
QVariant rightVar = right.data( JobStatusModel::JobDataRole );
JobStatusItem* rightItem = rightVar.value< JobStatusItem* >();
if ( !leftItem || !rightItem )
return false;
return leftItem->weight() < rightItem->weight();
}
JobStatusModel::JobStatusModel( QObject* parent )
: QAbstractListModel ( parent )
{

View File

@ -22,6 +22,7 @@
#include "DllMacro.h"
#include <QModelIndex>
#include <QSortFilterProxyModel>
#include <QQueue>
class QStyledItemDelegate;
@ -66,4 +67,30 @@ private:
QHash< QString, int > m_jobTypeCount;
};
class DLLEXPORT JobStatusSortModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
JobStatusSortModel( QObject* parent = 0 );
virtual ~JobStatusSortModel();
signals:
void customDelegateJobInserted( int row, JobStatusItem* item );
void customDelegateJobRemoved( int row );
void refreshDelegates();
public slots:
void addJob( JobStatusItem* item );
void customDelegateJobInsertedSlot( int row, JobStatusItem* item);
void customDelegateJobRemovedSlot( int row );
void refreshDelegatesSlot();
protected:
virtual bool lessThan( const QModelIndex & left, const QModelIndex & right ) const;
private:
JobStatusModel m_subModel;
};
#endif // JOBSTATUSMODEL_H

View File

@ -83,7 +83,7 @@ JobStatusView::JobStatusView( AnimatedSplitter* parent )
void
JobStatusView::setModel( JobStatusModel* m )
JobStatusView::setModel( JobStatusSortModel* m )
{
m_model = m;
m_view->setModel( m );
@ -114,7 +114,7 @@ JobStatusView::customDelegateJobInserted( int row, JobStatusItem* item )
tLog() << Q_FUNC_INFO << "delegate found";
connect( delegate, SIGNAL( update( const QModelIndex& ) ), m_view, SLOT( update( const QModelIndex & ) ) );
connect( delegate, SIGNAL( aclResult( ACLRegistry::ACL ) ), item, SLOT( aclResult( ACLRegistry::ACL ) ) );
delegate->emitSizeHintChanged( m_model->index( row ) );
delegate->emitSizeHintChanged( m_model->index( row, 0 ) );
}
else
tLog() << Q_FUNC_INFO << "delegate was not properly found!";
@ -127,6 +127,7 @@ void
JobStatusView::customDelegateJobRemoved( int row )
{
tLog() << Q_FUNC_INFO << "row is" << row;
checkCount();
}
@ -138,7 +139,7 @@ JobStatusView::refreshDelegates()
for ( int i = 0; i < count; i++ )
{
tLog() << Q_FUNC_INFO << "checking row" << i;
QModelIndex index = m_model->index( i );
QModelIndex index = m_model->index( i, 0 );
QVariant itemVar = index.data( JobStatusModel::JobDataRole );
if ( !itemVar.canConvert< JobStatusItem* >() || !itemVar.value< JobStatusItem* >() )
{

View File

@ -26,7 +26,7 @@
class QAbstractItemModel;
class QListView;
class JobStatusModel;
class JobStatusSortModel;
class JobStatusItem;
class StreamConnection;
class QStyledItemDelegate;
@ -47,9 +47,9 @@ public:
QSize sizeHint() const;
void setModel( JobStatusModel* model );
void setModel( JobStatusSortModel* model );
JobStatusModel* model() { return m_model; }
JobStatusSortModel* model() { return m_model; }
private slots:
void checkCount();
@ -59,7 +59,7 @@ private slots:
private:
QListView* m_view;
JobStatusModel* m_model;
JobStatusSortModel* m_model;
AnimatedSplitter* m_parent;
mutable int m_cachedHeight;