/* * Copyright 2024 Evgeny Chesnokov * SPDX-License-Identifier: LGPL-2.0-or-later */ #include "booklistmodel.h" #include "book.h" BookListModel::BookListModel(const QList &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 BookListModel::roleNames() const { QHash roles; roles[TitleRole] = "title"; roles[AuthorRole] = "author"; roles[YearRole] = "year"; roles[RatingRole] = "rating"; return roles; }