1 /***************************************************************************
2                           addconstraintstudentssetminmorningsperweekform.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 "longtextmessagebox.h"
21 
22 #include "addconstraintstudentssetminmorningsperweekform.h"
23 #include "timeconstraint.h"
24 
AddConstraintStudentsSetMinMorningsPerWeekForm(QWidget * parent)25 AddConstraintStudentsSetMinMorningsPerWeekForm::AddConstraintStudentsSetMinMorningsPerWeekForm(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 tmp2=studentsComboBox->minimumSizeHint();
38 	Q_UNUSED(tmp2);
39 
40 	updateMinMorningsSpinBox();
41 	updateStudentsComboBox();
42 }
43 
~AddConstraintStudentsSetMinMorningsPerWeekForm()44 AddConstraintStudentsSetMinMorningsPerWeekForm::~AddConstraintStudentsSetMinMorningsPerWeekForm()
45 {
46 	saveFETDialogGeometry(this);
47 }
48 
updateStudentsComboBox()49 void AddConstraintStudentsSetMinMorningsPerWeekForm::updateStudentsComboBox()
50 {
51 	populateStudentsComboBox(studentsComboBox);
52 }
53 
updateMinMorningsSpinBox()54 void AddConstraintStudentsSetMinMorningsPerWeekForm::updateMinMorningsSpinBox(){
55 	minMorningsSpinBox->setMinimum(1);
56 	minMorningsSpinBox->setMaximum(gt.rules.nDaysPerWeek/2);
57 	minMorningsSpinBox->setValue(1);
58 }
59 
addCurrentConstraint()60 void AddConstraintStudentsSetMinMorningsPerWeekForm::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) - it has to be 100%"));
75 		return;
76 	}
77 
78 	int min_mornings=minMorningsSpinBox->value();
79 
80 	QString students_name=studentsComboBox->currentText();
81 	StudentsSet* s=gt.rules.searchStudentsSet(students_name);
82 	if(s==nullptr){
83 		QMessageBox::warning(this, tr("FET information"),
84 			tr("Invalid students set"));
85 		return;
86 	}
87 
88 	ctr=new ConstraintStudentsSetMinMorningsPerWeek(weight, min_mornings, students_name);
89 
90 	bool tmp2=gt.rules.addTimeConstraint(ctr);
91 	if(tmp2)
92 		LongTextMessageBox::information(this, tr("FET information"),
93 			tr("Constraint added:")+"\n\n"+ctr->getDetailedDescription(gt.rules));
94 	else{
95 		QMessageBox::warning(this, tr("FET information"),
96 			tr("Constraint NOT added - please report error"));
97 		delete ctr;
98 	}
99 }
100