2018-03-29 07:58:25 +00:00
# include "gui/mainwindow.h"
2018-02-02 09:00:46 +00:00
# include "ui_mainwindow.h"
MainWindow : : MainWindow ( QWidget * parent ) :
QMainWindow ( parent ) ,
2018-02-12 13:24:11 +00:00
ui ( new Ui : : MainWindow ) {
2018-02-02 09:00:46 +00:00
ui - > setupUi ( this ) ;
2018-02-12 19:39:24 +00:00
2018-03-02 10:30:16 +00:00
ui - > ingredientsListView - > setModel ( & this - > ingredientModel ) ;
2018-03-11 07:57:57 +00:00
ui - > tagsListView - > setModel ( & this - > tagsListModel ) ;
2018-02-02 09:00:46 +00:00
}
2018-03-10 14:32:26 +00:00
MainWindow : : MainWindow ( RecipeDatabase * db , QWidget * parent ) : MainWindow ( parent ) {
this - > recipeDB = db ;
}
2018-02-12 13:24:11 +00:00
MainWindow : : ~ MainWindow ( ) {
2018-02-02 09:00:46 +00:00
delete ui ;
}
2018-02-12 13:24:11 +00:00
void MainWindow : : loadFromRecipe ( Recipe recipe ) {
2018-03-31 13:02:35 +00:00
if ( recipe . isEmpty ( ) ) {
setRecipeName ( " No recipes found. " ) ;
setAuthorName ( " Click 'New' to get started. " ) ;
2018-03-11 07:57:57 +00:00
} else {
2018-03-31 13:02:35 +00:00
setRecipeName ( recipe . getName ( ) ) ;
2018-04-01 07:20:36 +00:00
setAuthorName ( recipe . getAuthor ( ) ) ;
2018-03-31 13:02:35 +00:00
setInstruction ( recipe . getInstruction ( ) ) ;
setIngredients ( recipe . getIngredients ( ) ) ;
if ( recipe . getImage ( ) . isNull ( ) ) {
setImage ( QImage ( QString ( " :/images/images/no_image.png " ) ) ) ;
} else {
setImage ( recipe . getImage ( ) ) ;
}
setPrepTime ( recipe . getPrepTime ( ) ) ;
setCookTime ( recipe . getCookTime ( ) ) ;
setServings ( recipe . getServings ( ) ) ;
setTags ( recipe . getTags ( ) ) ;
this - > currentRecipe = recipe ;
2018-03-11 07:57:57 +00:00
}
2018-02-12 13:24:11 +00:00
}
void MainWindow : : setRecipeName ( string name ) {
ui - > recipeNameLabel - > setText ( QString : : fromStdString ( name ) ) ;
}
2018-02-12 14:15:04 +00:00
void MainWindow : : setInstruction ( Instruction instruction ) {
2018-02-12 19:39:24 +00:00
ui - > instructionsTextEdit - > setHtml ( QString : : fromStdString ( instruction . getHTML ( ) ) ) ;
2018-02-12 13:24:11 +00:00
}
2018-05-22 19:28:18 +00:00
void MainWindow : : setIngredients ( vector < Ingredient > ingredients ) {
2018-03-10 14:32:26 +00:00
this - > ingredientModel . setIngredients ( ingredients ) ;
}
void MainWindow : : setImage ( QImage img ) {
ui - > imageLabel - > setPixmap ( QPixmap : : fromImage ( img ) ) ;
}
2018-03-10 15:02:01 +00:00
void MainWindow : : setPrepTime ( QTime prepTime ) {
ui - > prepTimeLabel - > setText ( QString ( " Prep Time: " ) + prepTime . toString ( " hh:mm:ss " ) ) ;
}
void MainWindow : : setCookTime ( QTime cookTime ) {
ui - > cookTimeLabel - > setText ( QString ( " Cook Time: " ) + cookTime . toString ( " hh:mm:ss " ) ) ;
}
void MainWindow : : setServings ( float servings ) {
2018-03-29 14:09:58 +00:00
ui - > servingsLabel - > setText ( QString ( QString : : fromStdString ( StringUtils : : toString ( servings ) + " Serving " + ( ( servings ! = 1.0f ) ? " s " : " " ) ) ) ) ;
2018-03-11 07:57:57 +00:00
}
void MainWindow : : setTags ( vector < RecipeTag > tags ) {
this - > tagsListModel . setTags ( tags ) ;
2018-03-10 15:02:01 +00:00
}
2018-03-31 13:02:35 +00:00
void MainWindow : : setAuthorName ( string name ) {
2018-04-01 07:20:36 +00:00
if ( name . empty ( ) ) {
ui - > authorLabel - > setText ( " " ) ;
} else {
ui - > authorLabel - > setText ( QString : : fromStdString ( " By " + name ) ) ;
}
2018-03-31 13:02:35 +00:00
}
2018-03-10 14:32:26 +00:00
void MainWindow : : on_newButton_clicked ( ) {
NewRecipeDialog d ( this - > recipeDB , this ) ;
d . show ( ) ;
d . exec ( ) ;
if ( d . isAccepted ( ) ) {
Recipe r = d . getRecipe ( ) ;
if ( ! this - > recipeDB - > storeRecipe ( r ) ) {
2018-03-29 14:09:58 +00:00
QMessageBox : : critical ( this , QString ( " Unable to Save Recipe " ) , QString ( " The program was not able to successfully save the recipe. Make sure to give the recipe a name, instructions, and some ingredients! " ) ) ;
} else {
this - > loadFromRecipe ( r ) ;
2018-03-10 14:32:26 +00:00
}
}
2018-02-12 13:24:11 +00:00
}
2018-03-11 11:53:30 +00:00
void MainWindow : : on_openButton_clicked ( ) {
OpenRecipeDialog d ( this - > recipeDB , this ) ;
d . show ( ) ;
d . exec ( ) ;
2018-03-29 14:09:58 +00:00
if ( ! d . getSelectedRecipe ( ) . isEmpty ( ) ) {
this - > loadFromRecipe ( d . getSelectedRecipe ( ) ) ;
}
}
void MainWindow : : on_exitButton_clicked ( ) {
this - > close ( ) ;
2018-03-11 11:53:30 +00:00
}
2018-03-30 21:25:28 +00:00
void MainWindow : : on_editButton_clicked ( ) {
NewRecipeDialog d ( this - > recipeDB , this - > currentRecipe , this ) ;
2018-03-31 10:53:31 +00:00
string originalName = this - > currentRecipe . getName ( ) ;
2018-03-30 21:25:28 +00:00
d . show ( ) ;
d . exec ( ) ;
if ( d . isAccepted ( ) ) {
Recipe r = d . getRecipe ( ) ;
2018-03-31 10:53:31 +00:00
if ( ! this - > recipeDB - > updateRecipe ( r , originalName ) ) {
2018-03-30 21:25:28 +00:00
QMessageBox : : critical ( this , QString ( " Unable to Save Recipe " ) , QString ( " The program was not able to successfully save the recipe. Make sure to give the recipe a name, instructions, and some ingredients! " ) ) ;
} else {
this - > loadFromRecipe ( r ) ;
}
}
}