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-02-13 08:48:57 +00:00
|
|
|
this->ingredients = vector<RecipeIngredient>();
|
2018-02-12 13:24:11 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 08:48:57 +00:00
|
|
|
int IngredientListModel::rowCount(const QModelIndex &parent) const{
|
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-04 08:05:20 +00:00
|
|
|
RecipeIngredient i = this->ingredients[row];
|
|
|
|
|
|
|
|
string displayStr;
|
|
|
|
|
|
|
|
if (std::ceil(i.getQuantity()) == i.getQuantity()){
|
|
|
|
//The quantity is an integer and should be casted.
|
|
|
|
displayStr += std::to_string((int)i.getQuantity());
|
|
|
|
} else {
|
2018-03-10 09:42:22 +00:00
|
|
|
float q = i.getQuantity();
|
|
|
|
displayStr += toString(q);
|
2018-03-04 08:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
displayStr += " " + i.getUnit().getAbbreviation() + " " + i.getName();
|
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-02-13 08:48:57 +00:00
|
|
|
void IngredientListModel::setIngredients(vector<RecipeIngredient> 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IngredientListModel::addIngredient(RecipeIngredient ri){
|
|
|
|
//Add only if it doesn't exist already.
|
|
|
|
for (unsigned int i = 0; i < this->ingredients.size(); i++){
|
|
|
|
if (!this->ingredients[i].getName().compare(ri.getName())){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this->ingredients.push_back(ri);
|
|
|
|
QModelIndex index = createIndex(this->ingredients.size()-1, 0);
|
|
|
|
QModelIndex bottomIndex = createIndex(this->ingredients.size()-1, 0);
|
|
|
|
emit dataChanged(index, bottomIndex);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<RecipeIngredient> IngredientListModel::getIngredients(){
|
|
|
|
return this->ingredients;
|
|
|
|
}
|
|
|
|
|
|
|
|
string toString(float val){
|
|
|
|
float decimal = std::fmod(val, 1.0f);
|
|
|
|
int places = 1;
|
|
|
|
while (std::fmod(decimal * 10, 1.0f) > 0){
|
|
|
|
decimal *= 10;
|
|
|
|
places++;
|
|
|
|
}
|
|
|
|
char buffer[50];
|
|
|
|
string arg = "%."+std::to_string(places)+"f";
|
|
|
|
sprintf(buffer, arg.c_str(), val);
|
|
|
|
string s = buffer;
|
|
|
|
return s;
|
2018-02-12 13:24:11 +00:00
|
|
|
}
|