/* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. */ #include #include "moviemodel.h" MovieModel::MovieModel(QObject *parent) : QAbstractItemModel(parent) { mHeaders << tr("Title") << tr("Filename") << tr("MD5Sum") << tr("Size") << tr("Genre") << tr("Quality") << tr("Archived"); populate(); } MovieModel::~MovieModel(){ qDeleteAll(mItems); } QModelIndex MovieModel::index(int row, int column, const QModelIndex &parent) const{ if((parent != QModelIndex()) || (!hasIndex(row, column, parent)) || (row > mItems.size())){ return QModelIndex(); } MovieItem *internal = mItems.at(row); return createIndex(row, column, internal); } QVariant MovieModel::data(const QModelIndex &index, int role) const{ if(!index.isValid()){ return QVariant(); } if(role == Qt::DisplayRole){ MovieItem *item = static_cast(index.internalPointer()); Q_ASSERT(item != 0); switch (index.column()){ case MovieItem::Dvd: return QVariant(QString(tr("DVD %1")).arg(QString::number(item->dataAt(MovieItem::Dvd).toInt()))); } return item->dataAt(index.column()); } return QVariant(); } Qt::ItemFlags MovieModel::flags(const QModelIndex &index) const{ if(!index.isValid()){ return 0; } return Qt::ItemIsEnabled | Qt::ItemIsSelectable; } QVariant MovieModel::headerData(int section, Qt::Orientation o, int role) const{ if((o == Qt::Horizontal) && (role == Qt::DisplayRole)){ if(section < mHeaders.size()){ return mHeaders.at(section); } } return QVariant(); } bool MovieModel::insertRows(int row, int count, const QModelIndex &){ if(row > rowCount(QModelIndex())){ return false; } Q_ASSERT(count == 1); beginInsertRows(QModelIndex(), row, row); MovieItem *newItem = new MovieItem(-1, 0); mItems.insert(row, newItem); endInsertRows(); return true; } bool MovieModel::removeRows(int row, int count, const QModelIndex &){ if(row >= rowCount(QModelIndex())){ return false; } beginRemoveRows(QModelIndex(), row, row + count - 1); for(int i = row; i < (row + count); ++i){ MovieItem *item = mItems.at(i); delete item; mItems.removeAt(i); } endRemoveRows(); return true; } bool MovieModel::setDataAt(const QModelIndex &index, const QList &data, qint32 genre, const QList &actors, const QList &covers){ if(!index.isValid() || (data.size() > MovieItem::NumRows)){ return false; } MovieItem *item = static_cast(index.internalPointer()); Q_ASSERT(item != 0); if(item->id() != -1){ qWarning("ID not as expected: item->id() == %d", item->id()); return false; } return true; } void MovieModel::populate(){ QSqlQuery movieQuery("SELECT imovid FROM movies"); movieQuery.exec(); while(movieQuery.next()){ int id = movieQuery.value(0).toInt(); MovieItem *item = new MovieItem(id); mItems << item; } }