1 /***************************************************************************
2                           modifyconstraintstudentssetactivitytagminhoursdailyform.cpp  -  description
3                              -------------------
4     begin                : 2019
5     copyright            : (C) 2019 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 "modifyconstraintstudentssetactivitytagminhoursdailyform.h"
21 #include "timeconstraint.h"
22 
ModifyConstraintStudentsSetActivityTagMinHoursDailyForm(QWidget * parent,ConstraintStudentsSetActivityTagMinHoursDaily * ctr)23 ModifyConstraintStudentsSetActivityTagMinHoursDailyForm::ModifyConstraintStudentsSetActivityTagMinHoursDailyForm(QWidget* parent, ConstraintStudentsSetActivityTagMinHoursDaily* 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 	QSize tmp4=activityTagsComboBox->minimumSizeHint();
38 	Q_UNUSED(tmp4);
39 
40 	this->_ctr=ctr;
41 
42 	weightLineEdit->setText(CustomFETString::number(ctr->weightPercentage));
43 
44 	updateStudentsComboBox(parent);
45 	updateActivityTagsComboBox();
46 
47 	minHoursSpinBox->setMinimum(1);
48 	minHoursSpinBox->setMaximum(gt.rules.nHoursPerDay);
49 	minHoursSpinBox->setValue(ctr->minHoursDaily);
50 
51 	allowEmptyDaysCheckBox->setChecked(ctr->allowEmptyDays);
52 }
53 
~ModifyConstraintStudentsSetActivityTagMinHoursDailyForm()54 ModifyConstraintStudentsSetActivityTagMinHoursDailyForm::~ModifyConstraintStudentsSetActivityTagMinHoursDailyForm()
55 {
56 	saveFETDialogGeometry(this);
57 }
58 
updateStudentsComboBox(QWidget * parent)59 void ModifyConstraintStudentsSetActivityTagMinHoursDailyForm::updateStudentsComboBox(QWidget* parent){
60 	int j=populateStudentsComboBox(studentsComboBox, this->_ctr->students);
61 	if(j<0)
62 		showWarningForInvisibleSubgroupConstraint(parent, this->_ctr->students);
63 	else
64 		assert(j>=0);
65 	studentsComboBox->setCurrentIndex(j);
66 }
67 
updateActivityTagsComboBox()68 void ModifyConstraintStudentsSetActivityTagMinHoursDailyForm::updateActivityTagsComboBox()
69 {
70 	activityTagsComboBox->clear();
71 	int j=-1;
72 	for(int i=0; i<gt.rules.activityTagsList.count(); i++){
73 		ActivityTag* at=gt.rules.activityTagsList.at(i);
74 		activityTagsComboBox->addItem(at->name);
75 		if(at->name==this->_ctr->activityTagName)
76 			j=i;
77 	}
78 	assert(j>=0);
79 	activityTagsComboBox->setCurrentIndex(j);
80 }
81 
ok()82 void ModifyConstraintStudentsSetActivityTagMinHoursDailyForm::ok()
83 {
84 	if(studentsComboBox->currentIndex()<0){
85 		showWarningCannotModifyConstraintInvisibleSubgroupConstraint(this, this->_ctr->students);
86 		return;
87 	}
88 
89 	double weight;
90 	QString tmp=weightLineEdit->text();
91 	weight_sscanf(tmp, "%lf", &weight);
92 	if(weight<100.0 || weight>100.0){
93 		QMessageBox::warning(this, tr("FET warning"),
94 			tr("Invalid weight (percentage). It has to be 100"));
95 		return;
96 	}
97 
98 	QString students_name=studentsComboBox->currentText();
99 	StudentsSet* s=gt.rules.searchStudentsSet(students_name);
100 	if(s==nullptr){
101 		QMessageBox::warning(this, tr("FET warning"),
102 			tr("Invalid students set"));
103 		return;
104 	}
105 
106 	QString activityTagName=activityTagsComboBox->currentText();
107 	int ati=gt.rules.searchActivityTag(activityTagName);
108 	if(ati<0){
109 		QMessageBox::warning(this, tr("FET warning"), tr("Invalid activity tag"));
110 		return;
111 	}
112 
113 	if(allowEmptyDaysCheckBox->isChecked() && minHoursSpinBox->value()==1){
114 		QMessageBox::warning(this, tr("FET warning"), tr("Allow empty days is selected and min hours daily is 1, so this would be a useless constraint."));
115 		return;
116 	}
117 
118 	this->_ctr->weightPercentage=weight;
119 	this->_ctr->students=students_name;
120 	this->_ctr->activityTagName=activityTagName;
121 	this->_ctr->minHoursDaily=minHoursSpinBox->value();
122 	this->_ctr->allowEmptyDays=allowEmptyDaysCheckBox->isChecked();
123 
124 	gt.rules.internalStructureComputed=false;
125 	setRulesModifiedAndOtherThings(&gt.rules);
126 
127 	this->close();
128 }
129 
cancel()130 void ModifyConstraintStudentsSetActivityTagMinHoursDailyForm::cancel()
131 {
132 	this->close();
133 }
134