1 /***************************************************************************
2                           addconstraintteachermaxrealdaysperweekform.cpp  -  description
3                              -------------------
4     begin                : 2021
5     copyright            : (C) 2021 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 "addconstraintteachermaxrealdaysperweekform.h"
23 #include "timeconstraint.h"
24 
AddConstraintTeacherMaxRealDaysPerWeekForm(QWidget * parent)25 AddConstraintTeacherMaxRealDaysPerWeekForm::AddConstraintTeacherMaxRealDaysPerWeekForm(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 	updateMaxDaysSpinBox();
41 	updateTeachersComboBox();
42 }
43 
~AddConstraintTeacherMaxRealDaysPerWeekForm()44 AddConstraintTeacherMaxRealDaysPerWeekForm::~AddConstraintTeacherMaxRealDaysPerWeekForm()
45 {
46 	saveFETDialogGeometry(this);
47 }
48 
updateTeachersComboBox()49 void AddConstraintTeacherMaxRealDaysPerWeekForm::updateTeachersComboBox(){
50 	teachersComboBox->clear();
51 	for(int i=0; i<gt.rules.teachersList.size(); i++){
52 		Teacher* tch=gt.rules.teachersList[i];
53 		teachersComboBox->addItem(tch->name);
54 	}
55 }
56 
updateMaxDaysSpinBox()57 void AddConstraintTeacherMaxRealDaysPerWeekForm::updateMaxDaysSpinBox(){
58 	maxDaysSpinBox->setMinimum(1);
59 	maxDaysSpinBox->setMaximum(gt.rules.nDaysPerWeek/2);
60 	maxDaysSpinBox->setValue(gt.rules.nDaysPerWeek/2);
61 }
62 
addCurrentConstraint()63 void AddConstraintTeacherMaxRealDaysPerWeekForm::addCurrentConstraint()
64 {
65 	TimeConstraint *ctr=nullptr;
66 
67 	double weight;
68 	QString tmp=weightLineEdit->text();
69 	weight_sscanf(tmp, "%lf", &weight);
70 	if(weight<0.0 || weight>100.0){
71 		QMessageBox::warning(this, tr("FET information"),
72 			tr("Invalid weight (percentage)"));
73 		return;
74 	}
75 	if(weight!=100.0){
76 		QMessageBox::warning(this, tr("FET information"),
77 			tr("Invalid weight (percentage) - it has to be 100%"));
78 		return;
79 	}
80 
81 	int max_days=maxDaysSpinBox->value();
82 
83 	QString teacher_name=teachersComboBox->currentText();
84 	int teacher_ID=gt.rules.searchTeacher(teacher_name);
85 	if(teacher_ID<0){
86 		QMessageBox::warning(this, tr("FET information"),
87 			tr("Invalid teacher"));
88 		return;
89 	}
90 
91 	ctr=new ConstraintTeacherMaxRealDaysPerWeek(weight, max_days, teacher_name);
92 
93 	bool tmp2=gt.rules.addTimeConstraint(ctr);
94 	if(tmp2)
95 		LongTextMessageBox::information(this, tr("FET information"),
96 			tr("Constraint added:")+"\n\n"+ctr->getDetailedDescription(gt.rules));
97 	else{
98 		QMessageBox::warning(this, tr("FET information"),
99 			tr("Constraint NOT added - please report error"));
100 		delete ctr;
101 	}
102 }
103