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