1 /***************************************************************************
2                           addconstraintstudentsactivitytagminhoursdailyform.cpp  -  description
3                              -------------------
4     begin                : 2019
5     copyright            : (C) 2019 by Lalescu Liviu
6     email                : Please see https://lalescu.ro/liviu/ for details about contacting Liviu Lalescu (in particular, you can find here the e-mail address)
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software: you can redistribute it and/or modify  *
12  *   it under the terms of the GNU Affero General Public License as        *
13  *   published by the Free Software Foundation, either version 3 of the    *
14  *   License, or (at your option) any later version.                       *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #include <QMessageBox>
19 
20 #include "longtextmessagebox.h"
21 
22 #include "addconstraintstudentsactivitytagminhoursdailyform.h"
23 #include "timeconstraint.h"
24 
AddConstraintStudentsActivityTagMinHoursDailyForm(QWidget * parent)25 AddConstraintStudentsActivityTagMinHoursDailyForm::AddConstraintStudentsActivityTagMinHoursDailyForm(QWidget* parent): QDialog(parent)
26 {
27 	setupUi(this);
28 
29 	addConstraintPushButton->setDefault(true);
30 
31 	connect(addConstraintPushButton, SIGNAL(clicked()), this, SLOT(addCurrentConstraint()));
32 	connect(closePushButton, SIGNAL(clicked()), this, SLOT(close()));
33 
34 	centerWidgetOnScreen(this);
35 	restoreFETDialogGeometry(this);
36 
37 	QSize tmp4=activityTagsComboBox->minimumSizeHint();
38 	Q_UNUSED(tmp4);
39 
40 	minHoursSpinBox->setMinimum(1);
41 	minHoursSpinBox->setMaximum(gt.rules.nHoursPerDay);
42 	minHoursSpinBox->setValue(1);
43 
44 	allowEmptyDaysCheckBox->setChecked(false);
45 
46 	updateActivityTagsComboBox();
47 }
48 
~AddConstraintStudentsActivityTagMinHoursDailyForm()49 AddConstraintStudentsActivityTagMinHoursDailyForm::~AddConstraintStudentsActivityTagMinHoursDailyForm()
50 {
51 	saveFETDialogGeometry(this);
52 }
53 
updateActivityTagsComboBox()54 void AddConstraintStudentsActivityTagMinHoursDailyForm::updateActivityTagsComboBox()
55 {
56 	for(ActivityTag* at : qAsConst(gt.rules.activityTagsList))
57 		activityTagsComboBox->addItem(at->name);
58 }
59 
addCurrentConstraint()60 void AddConstraintStudentsActivityTagMinHoursDailyForm::addCurrentConstraint()
61 {
62 	TimeConstraint *ctr=nullptr;
63 
64 	double weight;
65 	QString tmp=weightLineEdit->text();
66 	weight_sscanf(tmp, "%lf", &weight);
67 	if(weight<100.0 || weight>100.0){
68 		QMessageBox::warning(this, tr("FET information"),
69 			tr("Invalid weight (percentage). It has to be 100"));
70 		return;
71 	}
72 
73 	int minHours=minHoursSpinBox->value();
74 
75 	QString activityTagName=activityTagsComboBox->currentText();
76 	int acttagindex=gt.rules.searchActivityTag(activityTagName);
77 	if(acttagindex<0){
78 		QMessageBox::warning(this, tr("FET warning"), tr("Invalid activity tag"));
79 		return;
80 	}
81 
82 	if(allowEmptyDaysCheckBox->isChecked() && minHoursSpinBox->value()==1){
83 		QMessageBox::warning(this, tr("FET warning"), tr("Allow empty days is selected and min hours daily is 1, so this would be a useless constraint."));
84 		return;
85 	}
86 
87 	ctr=new ConstraintStudentsActivityTagMinHoursDaily(weight, minHours, allowEmptyDaysCheckBox->isChecked(), activityTagName);
88 
89 	bool tmp2=gt.rules.addTimeConstraint(ctr);
90 	if(tmp2)
91 		LongTextMessageBox::information(this, tr("FET information"),
92 			tr("Constraint added:")+"\n\n"+ctr->getDetailedDescription(gt.rules));
93 	else{
94 		QMessageBox::warning(this, tr("FET information"),
95 			tr("Constraint NOT added - please report error"));
96 		delete ctr;
97 	}
98 }
99