File: booklistmodel.cpp

package info (click to toggle)
kirigami-addons 1.11.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,324 kB
  • sloc: ansic: 31,525; cpp: 3,607; xml: 82; java: 76; makefile: 4; sh: 1
file content (48 lines) | stat: -rw-r--r-- 1,111 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
 * Copyright 2024 Evgeny Chesnokov <echesnokov@astralinux.ru>
 * SPDX-License-Identifier: LGPL-2.0-or-later
 */

#include "booklistmodel.h"

#include "book.h"

BookListModel::BookListModel(const QList<Book *> &books, QObject *parent)
    : QAbstractListModel(parent)
    , m_books(books)
{
}

int BookListModel::rowCount(const QModelIndex & /* parent */) const
{
    return m_books.count();
}

QVariant BookListModel::data(const QModelIndex &index, int role) const
{
    if (index.row() < 0 || index.row() >= m_books.count())
        return QVariant();

    Book *book = m_books[index.row()];

    if (role == TitleRole)
        return book->title();
    else if (role == AuthorRole)
        return book->author();
    else if (role == YearRole)
        return book->year();
    else if (role == RatingRole)
        return book->rating();

    return QVariant();
}

QHash<int, QByteArray> BookListModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[TitleRole] = "title";
    roles[AuthorRole] = "author";
    roles[YearRole] = "year";
    roles[RatingRole] = "rating";
    return roles;
}