RecipeDB/model/recipe/ingredients/ingredientlistmodel.cpp

56 lines
1.6 KiB
C++
Raw Normal View History

#include "model/recipe/ingredients/ingredientlistmodel.h"
2018-02-12 13:24:11 +00:00
IngredientListModel::IngredientListModel(){
this->ingredients = vector<Ingredient>();
2018-02-12 13:24:11 +00:00
}
int IngredientListModel::rowCount(const QModelIndex &parent) const{
2018-03-31 20:15:01 +00:00
Q_UNUSED(parent);
return this->ingredients.size();
2018-02-12 13:24:11 +00:00
}
QVariant IngredientListModel::data(const QModelIndex &index, int role) const{
int row = index.row();
Ingredient i = this->ingredients[row];
2018-05-22 21:21:15 +00:00
string displayStr = i.getContent();
switch(role){
case Qt::DisplayRole:
return QString::fromStdString(displayStr);
}
2018-02-12 13:24:11 +00:00
return QVariant();
2018-02-12 13:24:11 +00:00
}
void IngredientListModel::setIngredients(vector<Ingredient> ingredients){
2018-02-12 13:24:11 +00:00
this->ingredients = ingredients;
QModelIndex index = createIndex(0, 0);
QModelIndex bottomIndex = createIndex(ingredients.size()-1, 0);
emit dataChanged(index, bottomIndex);
}
bool IngredientListModel::addIngredient(Ingredient i){
//Add only if it doesn't exist already.
for (Ingredient ing : this->ingredients){
if (!ing.getContent().compare(i.getContent())){
return false;
}
}
//The ingredient doesn't exist already, so we'll add it.
2018-05-22 21:21:15 +00:00
this->ingredients.push_back(i);
QModelIndex index = createIndex(this->ingredients.size()-1, 0);
QModelIndex bottomIndex = createIndex(this->ingredients.size()-1, 0);
emit dataChanged(index, bottomIndex);
return true;
}
void IngredientListModel::deleteIngredient(int index){
this->ingredients.erase(this->ingredients.begin() + index);
emit dataChanged(createIndex(0, 0), createIndex(this->ingredients.size()-1, 0));
}
vector<Ingredient> IngredientListModel::getIngredients(){
return this->ingredients;
}