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 71 72 73 74
| #include "ApplicationModel.h"
ApplicationModel::ApplicationModel(QObject* parent) : QAbstractListModel(parent)
{}
ApplicationModel::ApplicationModel(const QList<Application>& applicationList, QObject* parent) :
QAbstractListModel(parent),
applist_(applicationList)
{}
QVariant ApplicationModel::data(const QModelIndex& index, int role) const
{
int row = index.row();
if ((index.column() == 0) && (row >= 0) && (row < rowCount()))
{
return QVariant(QVariant::UserType, &applist_[row]);
}
return (QVariant::Invalid);
}
Qt::ItemFlags ApplicationModel::flags(const QModelIndex& index) const
{
int row = index.row();
if ((index.column() == 0) && (row >= 0) && (row < rowCount()))
{
return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
}
return 0;
}
bool ApplicationModel::insertRows(int row, int count, const QModelIndex& parent)
{
if (parent != QModelIndex()) return false;
beginInsertRows(parent, row, row + count - 1);
for (int i = row; i < row + count; ++i)
{
applist_.insert(i, Application());
}
endInsertRows();
return true;
}
bool ApplicationModel::removeRows(int row, int count, const QModelIndex& parent)
{
if (parent != QModelIndex()) return false;
if (!(row < rowCount() && row + count <= rowCount())) return false;
beginRemoveRows(parent, row, row + count - 1);
for (int i = 0; i < count; ++i)
{
applist_.removeAt(row);
}
endRemoveRows();
return true;
}
int ApplicationModel::rowCount(const QModelIndex& parent) const
{
if (parent != QModelIndex()) return 0;
return applist_.size();
}
bool ApplicationModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
if (!value.canConvert<Application>()) return false;
int row = index.row();
if (!((index.column() == 0) && (row >= 0) && (row < rowCount()))) return false;
applist_[row] = value.value<Application>();
return true;
}
QList<Application> ApplicationModel::applicationList() const
{
return applist_;
} |
Partager