2018-03-04 08:05:20 +00:00
# include "newrecipedialog.h"
# include "ui_newrecipedialog.h"
NewRecipeDialog : : NewRecipeDialog ( QWidget * parent ) :
QDialog ( parent ) ,
2018-03-10 07:51:17 +00:00
ui ( new Ui : : NewRecipeDialog ) {
2018-03-04 08:05:20 +00:00
ui - > setupUi ( this ) ;
2018-03-10 09:42:22 +00:00
2018-03-10 11:36:14 +00:00
setModal ( true ) ;
2018-03-10 09:42:22 +00:00
ui - > ingredientsListView - > setModel ( & this - > ingredientListModel ) ;
2018-03-10 11:36:14 +00:00
ui - > tagsListView - > setModel ( & this - > tagsListModel ) ;
2018-03-04 08:05:20 +00:00
}
2018-03-10 07:51:17 +00:00
NewRecipeDialog : : NewRecipeDialog ( RecipeDatabase * db , QWidget * parent ) : NewRecipeDialog ( parent ) {
this - > recipeDB = db ;
this - > populateIngredientsBox ( ) ;
this - > populateUnitsBox ( ) ;
2018-03-10 11:36:14 +00:00
this - > populateTagsBox ( ) ;
2018-03-10 07:51:17 +00:00
}
NewRecipeDialog : : ~ NewRecipeDialog ( ) {
2018-03-04 08:05:20 +00:00
delete ui ;
}
2018-03-10 07:51:17 +00:00
2018-03-10 11:36:14 +00:00
Recipe NewRecipeDialog : : getRecipe ( ) {
Recipe r ( ui - > recipeNameEdit - > text ( ) . toStdString ( ) ,
this - > ingredientListModel . getIngredients ( ) ,
ui - > instructionsTextEdit - > toHtml ( ) . toStdString ( ) ,
2018-03-10 12:22:04 +00:00
this - > img , //Image
2018-03-10 11:36:14 +00:00
this - > tagsListModel . getTags ( ) , //Tags
QDate : : currentDate ( ) ,
ui - > prepTimeEdit - > time ( ) ,
ui - > cookTimeEdit - > time ( ) ,
( float ) ui - > servingsSpinBox - > value ( ) ) ;
2018-03-10 12:22:04 +00:00
return r ;
2018-03-10 11:36:14 +00:00
}
bool NewRecipeDialog : : isAccepted ( ) const {
return this - > accepted ;
}
2018-03-10 07:51:17 +00:00
void NewRecipeDialog : : populateIngredientsBox ( ) {
this - > ingredients = this - > recipeDB - > retrieveAllIngredients ( ) ;
ui - > ingredientNameBox - > clear ( ) ;
for ( unsigned int i = 0 ; i < this - > ingredients . size ( ) ; i + + ) {
QString s = QString : : fromStdString ( this - > ingredients [ i ] . getName ( ) ) ;
ui - > ingredientNameBox - > insertItem ( i , s ) ;
}
}
void NewRecipeDialog : : populateUnitsBox ( ) {
this - > units = this - > recipeDB - > retrieveAllUnitsOfMeasure ( ) ;
ui - > unitComboBox - > clear ( ) ;
for ( unsigned int i = 0 ; i < this - > units . size ( ) ; i + + ) {
QString s = QString : : fromStdString ( this - > units [ i ] . getName ( ) ) ;
ui - > unitComboBox - > insertItem ( i , s ) ;
}
}
2018-03-10 09:42:22 +00:00
2018-03-10 11:36:14 +00:00
void NewRecipeDialog : : populateTagsBox ( ) {
this - > tags = this - > recipeDB - > retrieveAllTags ( ) ;
ui - > tagsComboBox - > clear ( ) ;
for ( unsigned int i = 0 ; i < this - > tags . size ( ) ; i + + ) {
QString s = QString : : fromStdString ( this - > tags [ i ] . getValue ( ) ) ;
ui - > tagsComboBox - > insertItem ( i , s ) ;
}
}
2018-03-10 09:42:22 +00:00
void NewRecipeDialog : : on_addIngredientButton_clicked ( ) {
//Construct a recipe ingredient from the supplied data.
Ingredient i = this - > ingredients [ ui - > ingredientNameBox - > currentIndex ( ) ] ;
UnitOfMeasure u = this - > units [ ui - > unitComboBox - > currentIndex ( ) ] ;
RecipeIngredient ri ( i , ui - > quantitySpinBox - > value ( ) , u , ui - > commentsLineEdit - > text ( ) . toStdString ( ) ) ;
this - > ingredientListModel . addIngredient ( ri ) ;
2018-03-30 09:33:08 +00:00
ui - > commentsLineEdit - > clear ( ) ;
2018-03-10 09:42:22 +00:00
}
void NewRecipeDialog : : on_italicsButton_clicked ( ) {
ui - > instructionsTextEdit - > setFontItalic ( ui - > italicsButton - > isChecked ( ) ) ;
}
void NewRecipeDialog : : on_boldButton_clicked ( ) {
if ( ui - > boldButton - > isChecked ( ) ) {
ui - > instructionsTextEdit - > setFontWeight ( QFont : : Bold ) ;
} else {
ui - > instructionsTextEdit - > setFontWeight ( QFont : : Normal ) ;
}
}
2018-03-10 11:36:14 +00:00
void NewRecipeDialog : : on_buttonBox_accepted ( ) {
this - > accepted = true ;
this - > close ( ) ;
}
void NewRecipeDialog : : on_buttonBox_rejected ( ) {
this - > close ( ) ;
}
void NewRecipeDialog : : on_addTagButton_clicked ( ) {
//Add a tag to the list of those prepared to be added.
this - > tagsListModel . addTag ( this - > tags [ ui - > tagsComboBox - > currentIndex ( ) ] ) ;
}
2018-03-10 11:58:31 +00:00
void NewRecipeDialog : : on_deleteTagButton_clicked ( ) {
2018-03-10 12:22:04 +00:00
QModelIndexList indexList = ui - > tagsListView - > selectionModel ( ) - > selectedIndexes ( ) ;
2018-03-10 11:58:31 +00:00
for ( QModelIndexList : : iterator it = indexList . begin ( ) ; it ! = indexList . end ( ) ; + + it ) {
QModelIndex i = * it ;
this - > tagsListModel . deleteTag ( i . row ( ) ) ;
}
}
2018-03-10 12:22:04 +00:00
void NewRecipeDialog : : on_selectImageButton_clicked ( ) {
QString filename = QFileDialog : : getOpenFileName ( this , " Open Image " , QString ( ) , " Image Files (*.png *.jpg *.bmp) " ) ;
if ( ! filename . isEmpty ( ) ) {
this - > img = QImage ( filename ) ;
ui - > imageDisplayLabel - > setPixmap ( QPixmap ( filename ) ) ;
}
}
2018-03-10 13:06:24 +00:00
2018-03-29 14:09:58 +00:00
void NewRecipeDialog : : on_removeIngredientButton_clicked ( ) {
2018-03-10 13:06:24 +00:00
QModelIndexList indexList = ui - > ingredientsListView - > selectionModel ( ) - > selectedIndexes ( ) ;
for ( QModelIndexList : : iterator it = indexList . begin ( ) ; it ! = indexList . end ( ) ; + + it ) {
QModelIndex i = * it ;
this - > ingredientListModel . deleteIngredient ( i . row ( ) ) ;
}
}
2018-03-29 14:09:58 +00:00
void NewRecipeDialog : : on_deleteIngredientButton_clicked ( ) {
int index = ui - > ingredientNameBox - > currentIndex ( ) ;
Ingredient ing = this - > ingredients . at ( index ) ;
QMessageBox : : StandardButton reply = QMessageBox : : question ( this ,
QString : : fromStdString ( " Delete Ingredient " ) ,
QString : : fromStdString ( " Are you sure you want to delete the ingredient " + ing . getName ( ) + " ? " ) ) ;
if ( reply = = QMessageBox : : Yes ) {
bool success = this - > recipeDB - > deleteIngredient ( ing . getName ( ) ) ;
if ( ! success ) {
QMessageBox : : critical ( this , QString : : fromStdString ( " Error " ) , QString : : fromStdString ( " Unable to delete ingredient: " + ing . getName ( ) + " , some recipes use it! " ) ) ;
} else {
this - > populateIngredientsBox ( ) ;
}
}
}
2018-03-10 13:06:24 +00:00
void NewRecipeDialog : : on_newIngredientButton_clicked ( ) {
NewIngredientDialog d ( this ) ;
d . show ( ) ;
if ( d . exec ( ) = = QDialog : : Accepted ) {
Ingredient i = d . getIngredient ( ) ;
this - > recipeDB - > storeIngredient ( i ) ;
this - > populateIngredientsBox ( ) ;
2018-03-30 09:33:08 +00:00
ui - > ingredientNameBox - > setCurrentText ( QString : : fromStdString ( i . getName ( ) ) ) ;
2018-03-10 13:06:24 +00:00
}
}
void NewRecipeDialog : : on_newTagButton_clicked ( ) {
2018-03-10 13:56:43 +00:00
NewTagDialog d ( this ) ;
2018-03-10 13:06:24 +00:00
d . show ( ) ;
2018-03-10 13:56:43 +00:00
if ( d . exec ( ) = = QDialog : : Accepted ) {
RecipeTag tag = d . getTag ( ) ;
//Temporarily add this to the tags list, and it will be saved if the recipe is saved.
this - > tags . push_back ( tag ) ;
2018-03-11 07:57:57 +00:00
this - > tagsListModel . addTag ( tag ) ;
2018-03-10 13:56:43 +00:00
ui - > tagsComboBox - > clear ( ) ;
for ( unsigned int i = 0 ; i < this - > tags . size ( ) ; i + + ) {
QString s = QString : : fromStdString ( this - > tags [ i ] . getValue ( ) ) ;
ui - > tagsComboBox - > insertItem ( i , s ) ;
}
}
}
2018-03-10 13:06:24 +00:00
2018-03-10 13:56:43 +00:00
void NewRecipeDialog : : on_removeTagButton_clicked ( ) {
2018-03-29 14:09:58 +00:00
unsigned int index = ui - > tagsComboBox - > currentIndex ( ) ;
if ( index > = this - > tags . size ( ) ) {
2018-03-10 13:56:43 +00:00
return ;
}
RecipeTag tag = this - > tags [ ui - > tagsComboBox - > currentIndex ( ) ] ;
2018-03-29 09:21:00 +00:00
string content = " Are you sure you wish to delete the following tag: \n " + tag . getValue ( ) + " \n This will delete the tag for all recipes that use it. " ;
2018-03-10 13:56:43 +00:00
QMessageBox : : StandardButton reply = QMessageBox : : question ( this , QString ( " Delete Tag " ) , QString ( content . c_str ( ) ) ) ;
if ( reply = = QMessageBox : : Yes ) {
this - > recipeDB - > deleteTag ( tag ) ;
this - > populateTagsBox ( ) ;
}
2018-03-10 13:06:24 +00:00
}
2018-03-11 07:57:57 +00:00
void NewRecipeDialog : : on_newUnitButton_clicked ( ) {
NewUnitDialog d ( this ) ;
d . show ( ) ;
if ( d . exec ( ) = = QDialog : : Accepted ) {
UnitOfMeasure u = d . getUnit ( ) ;
2018-03-11 08:33:48 +00:00
if ( ! this - > recipeDB - > storeUnitOfMeasure ( u ) | | u . getName ( ) . empty ( ) | | u . getNamePlural ( ) . empty ( ) | | u . getAbbreviation ( ) . empty ( ) ) {
2018-03-11 07:57:57 +00:00
QMessageBox : : critical ( this , " Error " , " Unable to store new unit. " ) ;
} else {
this - > populateUnitsBox ( ) ;
}
}
}
2018-03-29 14:09:58 +00:00
void NewRecipeDialog : : on_deleteUnitButton_clicked ( ) {
int index = ui - > unitComboBox - > currentIndex ( ) ;
UnitOfMeasure unit = this - > units [ index ] ;
QMessageBox : : StandardButton reply = QMessageBox : : question ( this ,
QString : : fromStdString ( " Delete Unit Of Measure " ) ,
QString : : fromStdString ( " Are you sure you want to delete the unit " + unit . getName ( ) + " ? " ) ) ;
if ( reply = = QMessageBox : : Yes ) {
if ( ! this - > recipeDB - > deleteUnitOfMeasure ( unit . getName ( ) ) ) {
QMessageBox : : critical ( this , " Error " , " Unable to delete unit. There may be recipes which still use it! " ) ;
} else {
this - > populateUnitsBox ( ) ;
}
}
}