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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
/*
* Copyright 2024 Evgeny Chesnokov <echesnokov@astralinux.ru>
* SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "booktablemodel.h"
#include "book.h"
BookTableModel::BookTableModel(const QList<Book *> &books, QObject *parent)
: QAbstractTableModel(parent)
, m_books(books)
{
}
int BookTableModel::rowCount(const QModelIndex & /* parent */) const
{
return m_books.count();
}
int BookTableModel::columnCount(const QModelIndex & /* parent */) const
{
return 4; // for title, author, year, rating
}
QVariant BookTableModel::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 == Qt::DisplayRole) {
switch (index.column()) {
case TitleRole:
return book->title();
case AuthorRole:
return book->author();
case YearRole:
return book->year();
case RatingRole:
return book->rating();
}
}
return QVariant();
}
QVariant BookTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
if (section == TitleRole) {
return QStringLiteral("Book");
}
if (section == AuthorRole) {
return QStringLiteral("Author");
}
if (section == YearRole) {
return QStringLiteral("Year");
}
if (section == RatingRole) {
return QStringLiteral("Rating");
}
}
return QVariant();
}
|