1 /***************************************************************************
2                           modifyconstraintstudentssetminhourspermorningform.cpp  -  description
3                              -------------------
4     begin                : 2020
5     copyright            : (C) 2020 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 "modifyconstraintstudentssetminhourspermorningform.h"
21 #include "timeconstraint.h"
22 
ModifyConstraintStudentsSetMinHoursPerMorningForm(QWidget * parent,ConstraintStudentsSetMinHoursPerMorning * ctr)23 ModifyConstraintStudentsSetMinHoursPerMorningForm::ModifyConstraintStudentsSetMinHoursPerMorningForm(QWidget* parent, ConstraintStudentsSetMinHoursPerMorning* ctr): QDialog(parent)
24 {
25 	setupUi(this);
26 
27 	okPushButton->setDefault(true);
28 
29 	connect(okPushButton, SIGNAL(clicked()), this, SLOT(ok()));
30 	connect(cancelPushButton, SIGNAL(clicked()), this, SLOT(cancel()));
31 
32 	centerWidgetOnScreen(this);
33 	restoreFETDialogGeometry(this);
34 
35 	QSize tmp2=studentsComboBox->minimumSizeHint();
36 	Q_UNUSED(tmp2);
37 
38 	this->_ctr=ctr;
39 
40 	weightLineEdit->setText(CustomFETString::number(ctr->weightPercentage));
41 
42 	//assert(ctr->allowEmptyDays==true);
43 	allowEmptyMorningsCheckBox->setChecked(ctr->allowEmptyMornings);
44 
45 	connect(allowEmptyMorningsCheckBox, SIGNAL(toggled(bool)), this, SLOT(allowEmptyMorningsCheckBoxToggled())); //after set checked!
46 
47 	if(gt.rules.mode==MORNINGS_AFTERNOONS || ENABLE_STUDENTS_MIN_HOURS_PER_MORNING_WITH_ALLOW_EMPTY_MORNINGS)
48 		allowLabel->setText(tr("Advanced usage: enabled"));
49 	else
50 		allowLabel->setText(tr("Advanced usage: not enabled"));
51 
52 	updateStudentsComboBox(parent);
53 
54 	minHoursSpinBox->setMinimum(1);
55 	minHoursSpinBox->setMaximum(gt.rules.nHoursPerDay);
56 	minHoursSpinBox->setValue(ctr->minHoursPerMorning);
57 }
58 
~ModifyConstraintStudentsSetMinHoursPerMorningForm()59 ModifyConstraintStudentsSetMinHoursPerMorningForm::~ModifyConstraintStudentsSetMinHoursPerMorningForm()
60 {
61 	saveFETDialogGeometry(this);
62 }
63 
updateStudentsComboBox(QWidget * parent)64 void ModifyConstraintStudentsSetMinHoursPerMorningForm::updateStudentsComboBox(QWidget* parent){
65 	int j=populateStudentsComboBox(studentsComboBox, this->_ctr->students);
66 	if(j<0)
67 		showWarningForInvisibleSubgroupConstraint(parent, this->_ctr->students);
68 	else
69 		assert(j>=0);
70 	studentsComboBox->setCurrentIndex(j);
71 }
72 
ok()73 void ModifyConstraintStudentsSetMinHoursPerMorningForm::ok()
74 {
75 	if(studentsComboBox->currentIndex()<0){
76 		showWarningCannotModifyConstraintInvisibleSubgroupConstraint(this, this->_ctr->students);
77 		return;
78 	}
79 
80 	double weight;
81 	QString tmp=weightLineEdit->text();
82 	weight_sscanf(tmp, "%lf", &weight);
83 	if(weight<0.0 || weight>100.0){
84 		QMessageBox::warning(this, tr("FET information"),
85 			tr("Invalid weight (percentage)"));
86 		return;
87 	}
88 	if(weight!=100.0){
89 		QMessageBox::warning(this, tr("FET information"),
90 			tr("Invalid weight (percentage) - it has to be 100%"));
91 		return;
92 	}
93 
94 	if(gt.rules.mode!=MORNINGS_AFTERNOONS){
95 		if(!ENABLE_STUDENTS_MIN_HOURS_PER_MORNING_WITH_ALLOW_EMPTY_MORNINGS && allowEmptyMorningsCheckBox->isChecked()){
96 			QMessageBox::warning(this, tr("FET warning"), tr("Empty mornings for students min hours per morning constraints are not enabled. You must enable them from the Settings->Advanced menu."));
97 			return;
98 		}
99 
100 		//2021-03-26 - I think I commented out this check because the user might combine this constraint with a min hours daily constraint.
101 		/*if(allowEmptyMorningsCheckBox->isChecked() && minHoursSpinBox->value()<2){
102 			QMessageBox::warning(this, tr("FET warning"), tr("If you allow empty mornings, the min hours must be at least 2 (to make it a non-trivial constraint)"));
103 			return;
104 		}*/
105 	}
106 
107 	QString students_name=studentsComboBox->currentText();
108 	StudentsSet* s=gt.rules.searchStudentsSet(students_name);
109 	if(s==nullptr){
110 		QMessageBox::warning(this, tr("FET information"),
111 			tr("Invalid students set"));
112 		return;
113 	}
114 
115 	this->_ctr->weightPercentage=weight;
116 	this->_ctr->students=students_name;
117 	this->_ctr->minHoursPerMorning=minHoursSpinBox->value();
118 
119 	this->_ctr->allowEmptyMornings=allowEmptyMorningsCheckBox->isChecked();
120 
121 	gt.rules.internalStructureComputed=false;
122 	setRulesModifiedAndOtherThings(&gt.rules);
123 
124 	this->close();
125 }
126 
cancel()127 void ModifyConstraintStudentsSetMinHoursPerMorningForm::cancel()
128 {
129 	this->close();
130 }
131 
allowEmptyMorningsCheckBoxToggled()132 void ModifyConstraintStudentsSetMinHoursPerMorningForm::allowEmptyMorningsCheckBoxToggled()
133 {
134 	if(gt.rules.mode!=MORNINGS_AFTERNOONS){
135 		bool k=allowEmptyMorningsCheckBox->isChecked();
136 
137 		if(k && !ENABLE_STUDENTS_MIN_HOURS_PER_MORNING_WITH_ALLOW_EMPTY_MORNINGS){
138 			allowEmptyMorningsCheckBox->setChecked(false);
139 			QString s=tr("Advanced usage is not enabled. To be able to select 'Allow empty mornings' for the constraints of type min hours per morning for students, you must enable the option from the Settings->Advanced menu.",
140 				"'Allow empty mornings' is an option which the user can enable and then he can select it.");
141 			s+="\n\n";
142 			s+=tr("Explanation: only select this option if your institution allows empty mornings for students and a timetable is possible with empty mornings for students."
143 				" Otherwise, it is IMPERATIVE (for performance reasons) to not select this option (or FET may not be able to find a timetable).");
144 			s+="\n\n";
145 			s+=tr("Use with caution.");
146 			QMessageBox::information(this, tr("FET information"), s);
147 		}
148 	}
149 }
150