1 /***************************************************************************
2                           addconstraintteacherminhoursdailyform.cpp  -  description
3                              -------------------
4     begin                : Sept. 21, 2007
5     copyright            : (C) 2007 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 "addconstraintteacherminhoursdailyform.h"
23 #include "timeconstraint.h"
24 
AddConstraintTeacherMinHoursDailyForm(QWidget * parent)25 AddConstraintTeacherMinHoursDailyForm::AddConstraintTeacherMinHoursDailyForm(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 tmp1=teachersComboBox->minimumSizeHint();
38 	Q_UNUSED(tmp1);
39 
40 	updateMinHoursSpinBox();
41 
42 	teachersComboBox->clear();
43 	for(int i=0; i<gt.rules.teachersList.size(); i++){
44 		Teacher* tch=gt.rules.teachersList[i];
45 		teachersComboBox->addItem(tch->name);
46 	}
47 }
48 
~AddConstraintTeacherMinHoursDailyForm()49 AddConstraintTeacherMinHoursDailyForm::~AddConstraintTeacherMinHoursDailyForm()
50 {
51 	saveFETDialogGeometry(this);
52 }
53 
updateMinHoursSpinBox()54 void AddConstraintTeacherMinHoursDailyForm::updateMinHoursSpinBox(){
55 	minHoursSpinBox->setMinimum(2);
56 	minHoursSpinBox->setMaximum(gt.rules.nHoursPerDay);
57 	minHoursSpinBox->setValue(2);
58 }
59 
addCurrentConstraint()60 void AddConstraintTeacherMinHoursDailyForm::addCurrentConstraint()
61 {
62 	TimeConstraint *ctr=nullptr;
63 
64 	double weight;
65 	QString tmp=weightLineEdit->text();
66 	weight_sscanf(tmp, "%lf", &weight);
67 	if(weight<0.0 || weight>100.0){
68 		QMessageBox::warning(this, tr("FET information"),
69 			tr("Invalid weight (percentage)"));
70 		return;
71 	}
72 	if(weight!=100.0){
73 		QMessageBox::warning(this, tr("FET information"),
74 			tr("Invalid weight (percentage) - must be 100%"));
75 		return;
76 	}
77 
78 	QString teacher_name=teachersComboBox->currentText();
79 	int teacher_ID=gt.rules.searchTeacher(teacher_name);
80 	if(teacher_ID<0){
81 		QMessageBox::warning(this, tr("FET information"),
82 			tr("Invalid teacher"));
83 		return;
84 	}
85 
86 	if(!allowEmptyDaysCheckBox->isChecked()){
87 		if(gt.rules.mode!=MORNINGS_AFTERNOONS){
88 			QMessageBox::warning(this, tr("FET information"), tr("Allow empty days check box must be checked. If you need to not allow empty days for the teachers, "
89 				"please use the constraint teachers min days per week"));
90 			return;
91 		}
92 		else{
93 			QMessageBox::warning(this, tr("FET information"), tr("Allow empty days check box must be checked. If you need to not allow empty days for a teacher, "
94 				"please use the constraint teacher min days per week (but the min days per week constraint is for real days. You can also use the "
95 				"constraints teacher min mornings/afternoons per week.)"));
96 			return;
97 		}
98 	}
99 
100 	int min_hours=minHoursSpinBox->value();
101 
102 	ctr=new ConstraintTeacherMinHoursDaily(weight, min_hours, teacher_name, allowEmptyDaysCheckBox->isChecked());
103 
104 	bool tmp2=gt.rules.addTimeConstraint(ctr);
105 	if(tmp2)
106 		LongTextMessageBox::information(this, tr("FET information"),
107 			tr("Constraint added:")+"\n\n"+ctr->getDetailedDescription(gt.rules));
108 	else{
109 		QMessageBox::warning(this, tr("FET information"),
110 			tr("Constraint NOT added - please report error"));
111 		delete ctr;
112 	}
113 }
114 
on_allowEmptyDaysCheckBox_toggled()115 void AddConstraintTeacherMinHoursDailyForm::on_allowEmptyDaysCheckBox_toggled()
116 {
117 	bool k=allowEmptyDaysCheckBox->isChecked();
118 
119 	if(!k){
120 		allowEmptyDaysCheckBox->setChecked(true);
121 		if(gt.rules.mode!=MORNINGS_AFTERNOONS)
122 			QMessageBox::information(this, tr("FET information"), tr("This check box must remain checked. If you really need to not allow empty days for the teachers,"
123 				" please use constraint teachers min days per week"));
124 		else
125 			QMessageBox::information(this, tr("FET information"), tr("This check box must remain checked. If you really need to not allow empty days for this teacher,"
126 				" please use constraint teacher min days per week (but the min days per week constraint is for real days. You can also use the "
127 				"constraints teacher min mornings/afternoons per week.)"));
128 	}
129 }
130