1 /***************************************************************************
2                           addconstraintstudentsafternoonintervalmaxdaysperweekform.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 "addconstraintstudentsafternoonintervalmaxdaysperweekform.h"
23 #include "timeconstraint.h"
24 
AddConstraintStudentsAfternoonIntervalMaxDaysPerWeekForm(QWidget * parent)25 AddConstraintStudentsAfternoonIntervalMaxDaysPerWeekForm::AddConstraintStudentsAfternoonIntervalMaxDaysPerWeekForm(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=startHourComboBox->minimumSizeHint();
38 	Q_UNUSED(tmp1);
39 	QSize tmp2=endHourComboBox->minimumSizeHint();
40 	Q_UNUSED(tmp2);
41 
42 	updateMaxDaysSpinBox();
43 	updateStartHoursComboBox();
44 	updateEndHoursComboBox();
45 }
46 
~AddConstraintStudentsAfternoonIntervalMaxDaysPerWeekForm()47 AddConstraintStudentsAfternoonIntervalMaxDaysPerWeekForm::~AddConstraintStudentsAfternoonIntervalMaxDaysPerWeekForm()
48 {
49 	saveFETDialogGeometry(this);
50 }
51 
updateMaxDaysSpinBox()52 void AddConstraintStudentsAfternoonIntervalMaxDaysPerWeekForm::updateMaxDaysSpinBox(){
53 	maxDaysSpinBox->setMinimum(0);
54 	maxDaysSpinBox->setMaximum(gt.rules.nDaysPerWeek/2);
55 	maxDaysSpinBox->setValue(gt.rules.nDaysPerWeek/2);
56 }
57 
updateStartHoursComboBox()58 void AddConstraintStudentsAfternoonIntervalMaxDaysPerWeekForm::updateStartHoursComboBox()
59 {
60 	startHourComboBox->clear();
61 	for(int i=0; i<gt.rules.nHoursPerDay; i++)
62 		startHourComboBox->addItem(gt.rules.hoursOfTheDay[i]);
63 	startHourComboBox->setCurrentIndex(gt.rules.nHoursPerDay-1);
64 }
65 
updateEndHoursComboBox()66 void AddConstraintStudentsAfternoonIntervalMaxDaysPerWeekForm::updateEndHoursComboBox()
67 {
68 	endHourComboBox->clear();
69 	for(int i=0; i<gt.rules.nHoursPerDay; i++)
70 		endHourComboBox->addItem(gt.rules.hoursOfTheDay[i]);
71 	endHourComboBox->addItem(tr("End of day"));
72 	endHourComboBox->setCurrentIndex(gt.rules.nHoursPerDay);
73 }
74 
addCurrentConstraint()75 void AddConstraintStudentsAfternoonIntervalMaxDaysPerWeekForm::addCurrentConstraint()
76 {
77 	TimeConstraint *ctr=nullptr;
78 
79 	double weight;
80 	QString tmp=weightLineEdit->text();
81 	weight_sscanf(tmp, "%lf", &weight);
82 	if(weight<0.0 || weight>100.0){
83 		QMessageBox::warning(this, tr("FET information"),
84 			tr("Invalid weight (percentage)"));
85 		return;
86 	}
87 	if(weight!=100.0){
88 		QMessageBox::warning(this, tr("FET information"),
89 			tr("Invalid weight (percentage) - it has to be 100%"));
90 		return;
91 	}
92 
93 	int max_days=maxDaysSpinBox->value();
94 
95 	int startHour=startHourComboBox->currentIndex();
96 	int endHour=endHourComboBox->currentIndex();
97 	if(startHour<0 || startHour>=gt.rules.nHoursPerDay){
98 		QMessageBox::warning(this, tr("FET information"),
99 			tr("Start hour invalid"));
100 		return;
101 	}
102 	if(endHour<0 || endHour>gt.rules.nHoursPerDay){
103 		QMessageBox::warning(this, tr("FET information"),
104 			tr("End hour invalid"));
105 		return;
106 	}
107 	if(endHour<=startHour){
108 		QMessageBox::warning(this, tr("FET information"),
109 			tr("Start hour cannot be greater or equal than end hour"));
110 		return;
111 	}
112 
113 	ctr=new ConstraintStudentsAfternoonIntervalMaxDaysPerWeek(weight, /*compulsory,*/ max_days, /*students_name,*/ startHour, endHour);
114 
115 	bool tmp2=gt.rules.addTimeConstraint(ctr);
116 	if(tmp2)
117 		LongTextMessageBox::information(this, tr("FET information"),
118 			tr("Constraint added:")+"\n\n"+ctr->getDetailedDescription(gt.rules));
119 	else{
120 		QMessageBox::warning(this, tr("FET information"),
121 			tr("Constraint NOT added - please report error"));
122 		delete ctr;
123 	}
124 }
125