Add custom roles to the post model and export them to QML
This commit is contained in:
		
					parent
					
						
							
								4fbfdf753e
							
						
					
				
			
			
				commit
				
					
						b0185c136b
					
				
			
		
					 2 changed files with 50 additions and 0 deletions
				
			
		| 
						 | 
					@ -98,6 +98,31 @@ QVariant DanbooruPostModel::data(const QModelIndex &index, int role) const
 | 
				
			||||||
        return post->fileUrl().fileName();
 | 
					        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();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return QVariant();
 | 
					    return QVariant();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -115,4 +140,18 @@ void DanbooruPostModel::clear()
 | 
				
			||||||
    reset();
 | 
					    reset();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					QHash< int, QByteArray > DanbooruPostModel::roleNames() const
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    QHash<int, QByteArray> roles;
 | 
				
			||||||
 | 
					    roles[PixmapRole] = "thumbPix";
 | 
				
			||||||
 | 
					    roles[FileUrlRole] = "fileUrl";
 | 
				
			||||||
 | 
					    roles[ThumbUrlRole] = "thumbnailUrl";
 | 
				
			||||||
 | 
					    roles[RatingRole] = "rating";
 | 
				
			||||||
 | 
					    roles[TagRole] = "tags";
 | 
				
			||||||
 | 
					    roles[SizeRole] = "fileSize";
 | 
				
			||||||
 | 
					    roles[ResolutionRole] = "resolution";
 | 
				
			||||||
 | 
					    return roles;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -59,6 +59,17 @@ public:
 | 
				
			||||||
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
 | 
					    int rowCount(const QModelIndex &parent = QModelIndex()) const;
 | 
				
			||||||
    QVariant data(const QModelIndex &index, int role) const;
 | 
					    QVariant data(const QModelIndex &index, int role) const;
 | 
				
			||||||
    void clear();
 | 
					    void clear();
 | 
				
			||||||
 | 
					    QHash<int, QByteArray> roleNames() const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    enum PostRoles {
 | 
				
			||||||
 | 
					        PixmapRole = Qt::UserRole + 1000,
 | 
				
			||||||
 | 
					        FileUrlRole = Qt::UserRole + 1001,
 | 
				
			||||||
 | 
					        ThumbUrlRole = Qt::UserRole + 1002,
 | 
				
			||||||
 | 
					        SizeRole = Qt::UserRole + 1003,
 | 
				
			||||||
 | 
					        RatingRole = Qt::UserRole + 1004,
 | 
				
			||||||
 | 
					        TagRole = Qt::UserRole + 1005,
 | 
				
			||||||
 | 
					        ResolutionRole = Qt::UserRole + 1006
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
    QVector<DanbooruPost *> m_items;
 | 
					    QVector<DanbooruPost *> m_items;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue