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:
Luca Beltrame 2015-02-05 23:48:40 +01:00
parent f8b59e78b5
commit b88e8a6268
2 changed files with 27 additions and 40 deletions

View file

@ -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);
return variant;
}
if (role == Qt::DecorationRole) {
case Qt::DecorationRole:
{
const QPixmap pixmap = post->pixmap();
return pixmap;
}
if (role == Qt::ToolTipRole) {
case Qt::ToolTipRole:
return post->fileUrl().fileName();
}
if (role == DanbooruPostModel::PixmapRole) {
QPixmap pixmap = post->pixmap();
return pixmap;
}
if (role == FileUrlRole) {
case FileUrlRole:
return post->fileUrl();
}
if (role == ThumbUrlRole) {
case ThumbUrlRole:
return post->thumbnailUrl();
}
if (role == SizeRole) {
case SizeRole:
return post->size();
}
if (role == ResolutionRole) {
case ResolutionRole:
return QSize(post->width(), post->height());
}
if (role == RatingRole) {
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";

View file

@ -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,