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();
 | 
					        return QVariant();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (role == Qt::DisplayRole) {
 | 
					    switch(role) {
 | 
				
			||||||
 | 
					        case Qt::DisplayRole:
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            QVariant variant;
 | 
				
			||||||
 | 
					            variant.setValue(post);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        QVariant variant;
 | 
					            return variant;
 | 
				
			||||||
        variant.setValue(post);
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return variant;
 | 
					        case Qt::DecorationRole:
 | 
				
			||||||
    }
 | 
					        {
 | 
				
			||||||
 | 
					            const QPixmap pixmap = post->pixmap();
 | 
				
			||||||
    if (role == Qt::DecorationRole) {
 | 
					            return pixmap;
 | 
				
			||||||
        const QPixmap pixmap = post->pixmap();
 | 
					        }
 | 
				
			||||||
        return pixmap;
 | 
					        case Qt::ToolTipRole:
 | 
				
			||||||
    }
 | 
					            return post->fileUrl().fileName();
 | 
				
			||||||
 | 
					        case FileUrlRole:
 | 
				
			||||||
    if (role == Qt::ToolTipRole) {
 | 
					            return post->fileUrl();
 | 
				
			||||||
        return post->fileUrl().fileName();
 | 
					        case ThumbUrlRole:
 | 
				
			||||||
    }
 | 
					            return post->thumbnailUrl();
 | 
				
			||||||
 | 
					        case SizeRole:
 | 
				
			||||||
    if (role == DanbooruPostModel::PixmapRole) {
 | 
					            return post->size();
 | 
				
			||||||
        QPixmap pixmap = post->pixmap();
 | 
					        case ResolutionRole:
 | 
				
			||||||
        return pixmap;
 | 
					            return QSize(post->width(), post->height());
 | 
				
			||||||
    }
 | 
					        case RatingRole:
 | 
				
			||||||
 | 
					            return post->rating();
 | 
				
			||||||
    if (role == FileUrlRole) {
 | 
					        default:
 | 
				
			||||||
        return post->fileUrl();
 | 
					            return QVariant();
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    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();
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return QVariant();
 | 
					    return QVariant();
 | 
				
			||||||
| 
						 | 
					@ -143,7 +131,7 @@ void DanbooruPostModel::clear()
 | 
				
			||||||
QHash< int, QByteArray > DanbooruPostModel::roleNames() const
 | 
					QHash< int, QByteArray > DanbooruPostModel::roleNames() const
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    QHash<int, QByteArray> roles;
 | 
					    QHash<int, QByteArray> roles;
 | 
				
			||||||
    roles[PixmapRole] = "thumbPix";
 | 
					    roles[Qt::DecorationRole] = "thumbPix";
 | 
				
			||||||
    roles[FileUrlRole] = "fileUrl";
 | 
					    roles[FileUrlRole] = "fileUrl";
 | 
				
			||||||
    roles[ThumbUrlRole] = "thumbnailUrl";
 | 
					    roles[ThumbUrlRole] = "thumbnailUrl";
 | 
				
			||||||
    roles[RatingRole] = "rating";
 | 
					    roles[RatingRole] = "rating";
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -62,7 +62,6 @@ public:
 | 
				
			||||||
    QHash<int, QByteArray> roleNames() const;
 | 
					    QHash<int, QByteArray> roleNames() const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    enum PostRoles {
 | 
					    enum PostRoles {
 | 
				
			||||||
        PixmapRole = Qt::UserRole + 1000,
 | 
					 | 
				
			||||||
        FileUrlRole = Qt::UserRole + 1001,
 | 
					        FileUrlRole = Qt::UserRole + 1001,
 | 
				
			||||||
        ThumbUrlRole = Qt::UserRole + 1002,
 | 
					        ThumbUrlRole = Qt::UserRole + 1002,
 | 
				
			||||||
        SizeRole = Qt::UserRole + 1003,
 | 
					        SizeRole = Qt::UserRole + 1003,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue