C++ (Qt)//model.hclass DownloadQueueModel : public QAbstractItemModel{ Q_OBJECT enum ColumnRoles { NameRole = 0, SizeRole, DownloadedRole, TransferredRole, SourcesRole, SpeedRole, EstimatedTimeRole, MaxRole = EstimatedTimeRole }; public: DownloadQueueModel(QObject *parent = 0); int columnCount( const QModelIndex & parent = QModelIndex() ) const; int rowCount( const QModelIndex & parent = QModelIndex() ) const; QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const; QModelIndex index( int row, int column, const QModelIndex & parent = QModelIndex() ) const; QVariant headerData(int section, Qt::Orientation orientation, int role) const; QModelIndex parent( const QModelIndex &child ) const; bool hasChildren( const QModelIndex & parent ) const; bool insertRows( int row, int count, const QModelIndex & parent = QModelIndex() ); void addFiles(const QString& linkList); private: QFileIconProvider *m_iconProvider; QFileIconProvider m_defaultIconProvider; QList<PartFile*> m_fileListToAdd;}; //model.cpp#define EXTRACT_PARTFILE(x) static_cast<PartFile*>((x).internalPointer()) DownloadQueueModel::DownloadQueueModel( QObject *parent ) : QAbstractItemModel(parent){ m_iconProvider = &m_defaultIconProvider;} int DownloadQueueModel::columnCount( const QModelIndex & parent ) const{ Q_UNUSED(parent); return MaxRole;} int DownloadQueueModel::rowCount( const QModelIndex &parent ) const{ Q_UNUSED(parent); return theApp->downloadQueue->fileCount();} QVariant DownloadQueueModel::data( const QModelIndex &index, int role ) const{ if(!index.isValid()) return QVariant(); PartFile *partFile = NULL; switch(role) { case Qt::DecorationRole: if(index.column() == NameRole) { partFile = EXTRACT_PARTFILE(index); return m_iconProvider->icon(QFileIconProvider::File); } case Qt::DisplayRole: partFile = EXTRACT_PARTFILE(index); switch(index.column()) { case NameRole: return partFile->fileName(); break; case SizeRole: return partFile->size(); break; case DownloadedRole: return partFile->downloaded(); break; case TransferredRole: return partFile->transferred(); break; case SourcesRole: return partFile->sourceCount(); break; case SpeedRole: return 0; break; case EstimatedTimeRole: return 0; break; } } return QVariant();} QModelIndex DownloadQueueModel::index( int row, int column, const QModelIndex &parent ) const{ if(hasIndex(row, column, parent)) return createIndex(row, column, theApp->downloadQueue->m_partFiles.at(row)); return QModelIndex();} QVariant DownloadQueueModel::headerData(int section, Qt::Orientation orientation, int role) const{ if (orientation != Qt::Horizontal || role != Qt::DisplayRole) return QAbstractItemModel::headerData(section, orientation, role); switch(section) { case NameRole: return tr("Name"); break; case SizeRole: return tr("Size"); case DownloadedRole: return tr("Downloaded"); break; case TransferredRole: return tr("Transferred"); break; case SourcesRole: return tr("Sources"); break; case SpeedRole: return tr("Speed"); break; case EstimatedTimeRole: return tr("ETA"); break; } return QVariant();} QModelIndex DownloadQueueModel::parent( const QModelIndex& child ) const{ Q_UNUSED(child); return QModelIndex();} bool DownloadQueueModel::hasChildren( const QModelIndex& parent ) const{ // root if(EXTRACT_PARTFILE(parent) == NULL) return true; return false;} void DownloadQueueModel::addFiles(const QString& linkList){ m_fileListToAdd = theApp->downloadQueue->prepareToAdd(linkList); if(m_fileListToAdd.count() == 0) return; int row = rowCount(); insertRows(row, m_fileListToAdd.count()); emit dataChanged(index(row,0), index(rowCount()-1, columnCount()-1));} bool DownloadQueueModel::insertRows( int row, int count, const QModelIndex & parent ){ Q_UNUSED(parent); beginInsertRows(QModelIndex(), row, row + count - 1); theApp->downloadQueue->addFiles(m_fileListToAdd); m_fileListToAdd.clear(); endInsertRows(); return true;}
C++ (Qt)//model.hclass DownloadQueueModel : public QAbstractItemModel{ Q_OBJECT Q_DISABLE_COPY(DownloadQueueModel) enum ColumnRoles { NameRole = 0, SizeRole, DownloadedRole, TransferredRole, SourcesRole, SpeedRole, EstimatedTimeRole, MaxRole = EstimatedTimeRole }; public: explicit DownloadQueueModel(QObject *parent = 0); int columnCount( const QModelIndex &parent = QModelIndex() ) const; int rowCount( const QModelIndex &parent = QModelIndex() ) const; QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const; QModelIndex parent( const QModelIndex &child ) const; QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const; bool insertRows( int row, int count, const QModelIndex &parent = QModelIndex() ); QVariant headerData(int section, Qt::Orientation orientation, int role) const; bool hasChildren( const QModelIndex &parent = QModelIndex() ) const; void addFiles(const QString &linkList); private: QFileIconProvider *m_iconProvider; QFileIconProvider m_defaultIconProvider; QList<PartFile*> m_fileListToAdd;};//model.cpp#define EXTRACT_PARTFILE(x) static_cast<PartFile*>((x).internalPointer()) DownloadQueueModel::DownloadQueueModel( QObject *parent ) : QAbstractItemModel(parent){ m_iconProvider = &m_defaultIconProvider;} int DownloadQueueModel::columnCount( const QModelIndex & parent ) const{ Q_UNUSED(parent); return MaxRole;} int DownloadQueueModel::rowCount( const QModelIndex &parent ) const{ if (parent.isValid()) return 0; return theApp->downloadQueue->fileCount();} QVariant DownloadQueueModel::data( const QModelIndex &index, int role ) const{ if(!index.isValid()) return QVariant(); PartFile *partFile = NULL; switch(role) { case Qt::DecorationRole: if(index.column() == NameRole) { partFile = EXTRACT_PARTFILE(index); return m_iconProvider->icon(QFileIconProvider::File); } case Qt::DisplayRole: partFile = EXTRACT_PARTFILE(index); switch(index.column()) { case NameRole: return partFile->fileName(); break; case SizeRole: return partFile->size(); break; case DownloadedRole: return partFile->downloaded(); break; case TransferredRole: return partFile->transferred(); break; case SourcesRole: return partFile->sourceCount(); break; case SpeedRole: return 0; break; case EstimatedTimeRole: return 0; break; } } return QVariant();} QModelIndex DownloadQueueModel::index( int row, int column, const QModelIndex &parent ) const{ if(hasIndex(row, column, parent)) return createIndex(row, column, theApp->downloadQueue->m_partFiles.at(row)); return QModelIndex();} QVariant DownloadQueueModel::headerData(int section, Qt::Orientation orientation, int role) const{ if (orientation != Qt::Horizontal || role != Qt::DisplayRole) return QAbstractItemModel::headerData(section, orientation, role); switch(section) { case NameRole: return tr("Name"); break; case SizeRole: return tr("Size"); case DownloadedRole: return tr("Downloaded"); break; case TransferredRole: return tr("Transferred"); break; case SourcesRole: return tr("Sources"); break; case SpeedRole: return tr("Speed"); break; case EstimatedTimeRole: return tr("ETA"); break; } return QVariant();} QModelIndex DownloadQueueModel::parent( const QModelIndex& child ) const{ Q_UNUSED(child); return QModelIndex();} bool DownloadQueueModel::hasChildren( const QModelIndex& parent ) const{ // root if(EXTRACT_PARTFILE(parent) == NULL) return true; return false;} bool DownloadQueueModel::insertRows( int row, int count, const QModelIndex & parent ){ if (count < 1 || row < 0 || row > rowCount(parent)) return false; beginInsertRows(QModelIndex(), row, row + count - 1); theApp->downloadQueue->addFiles(m_fileListToAdd); m_fileListToAdd.clear(); endInsertRows(); return true;} void DownloadQueueModel::addFiles(const QString& linkList){ m_fileListToAdd = theApp->downloadQueue->prepareToAdd(linkList); if(m_fileListToAdd.count() == 0) return; int row = rowCount(); insertRows(row, m_fileListToAdd.count());}
Diff int DownloadQueueModel::rowCount( const QModelIndex &parent ) const {- Q_UNUSED(parent);+ if (parent.isValid())+ return 0;+ return theApp->downloadQueue->fileCount(); }