Clean up the model
Add custom roles for QML Set roleNames() Use switch() instead of if() Remove PixmapRole, DecorationRole is enough
This commit is contained in:
		
					parent
					
						
							
								f8b59e78b5
							
						
					
				
			
			
				commit
				
					
						b88e8a6268
					
				
			
		
					 2 changed files with 27 additions and 40 deletions
				
			
		| 
						 | 
				
			
			@ -81,46 +81,34 @@ QVariant DanbooruPostModel::data(const QModelIndex &index, int role) const
 | 
			
		|||
        return QVariant();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (role == Qt::DisplayRole) {
 | 
			
		||||
    switch(role) {
 | 
			
		||||
        case Qt::DisplayRole:
 | 
			
		||||
        {
 | 
			
		||||
            QVariant variant;
 | 
			
		||||
            variant.setValue(post);
 | 
			
		||||
 | 
			
		||||
        QVariant variant;
 | 
			
		||||
        variant.setValue(post);
 | 
			
		||||
            return variant;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return variant;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (role == Qt::DecorationRole) {
 | 
			
		||||
        const QPixmap pixmap = post->pixmap();
 | 
			
		||||
        return pixmap;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (role == Qt::ToolTipRole) {
 | 
			
		||||
        return post->fileUrl().fileName();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (role == DanbooruPostModel::PixmapRole) {
 | 
			
		||||
        QPixmap pixmap = post->pixmap();
 | 
			
		||||
        return pixmap;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (role == FileUrlRole) {
 | 
			
		||||
        return post->fileUrl();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (role == ThumbUrlRole) {
 | 
			
		||||
        return post->thumbnailUrl();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (role == SizeRole) {
 | 
			
		||||
        return post->size();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (role == ResolutionRole) {
 | 
			
		||||
        return QSize(post->width(), post->height());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (role == RatingRole) {
 | 
			
		||||
        return post->rating();
 | 
			
		||||
        case Qt::DecorationRole:
 | 
			
		||||
        {
 | 
			
		||||
            const QPixmap pixmap = post->pixmap();
 | 
			
		||||
            return pixmap;
 | 
			
		||||
        }
 | 
			
		||||
        case Qt::ToolTipRole:
 | 
			
		||||
            return post->fileUrl().fileName();
 | 
			
		||||
        case FileUrlRole:
 | 
			
		||||
            return post->fileUrl();
 | 
			
		||||
        case ThumbUrlRole:
 | 
			
		||||
            return post->thumbnailUrl();
 | 
			
		||||
        case SizeRole:
 | 
			
		||||
            return post->size();
 | 
			
		||||
        case ResolutionRole:
 | 
			
		||||
            return QSize(post->width(), post->height());
 | 
			
		||||
        case RatingRole:
 | 
			
		||||
            return post->rating();
 | 
			
		||||
        default:
 | 
			
		||||
            return QVariant();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return QVariant();
 | 
			
		||||
| 
						 | 
				
			
			@ -143,7 +131,7 @@ void DanbooruPostModel::clear()
 | 
			
		|||
QHash< int, QByteArray > DanbooruPostModel::roleNames() const
 | 
			
		||||
{
 | 
			
		||||
    QHash<int, QByteArray> roles;
 | 
			
		||||
    roles[PixmapRole] = "thumbPix";
 | 
			
		||||
    roles[Qt::DecorationRole] = "thumbPix";
 | 
			
		||||
    roles[FileUrlRole] = "fileUrl";
 | 
			
		||||
    roles[ThumbUrlRole] = "thumbnailUrl";
 | 
			
		||||
    roles[RatingRole] = "rating";
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -62,7 +62,6 @@ public:
 | 
			
		|||
    QHash<int, QByteArray> roleNames() const;
 | 
			
		||||
 | 
			
		||||
    enum PostRoles {
 | 
			
		||||
        PixmapRole = Qt::UserRole + 1000,
 | 
			
		||||
        FileUrlRole = Qt::UserRole + 1001,
 | 
			
		||||
        ThumbUrlRole = Qt::UserRole + 1002,
 | 
			
		||||
        SizeRole = Qt::UserRole + 1003,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue