1 /***************************************************************************
2                           modifyconstraintteachersmingapsbetweenorderedpairofactivitytagsform.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 "modifyconstraintteachersmingapsbetweenorderedpairofactivitytagsform.h"
21 #include "timeconstraint.h"
22 
ModifyConstraintTeachersMinGapsBetweenOrderedPairOfActivityTagsForm(QWidget * parent,ConstraintTeachersMinGapsBetweenOrderedPairOfActivityTags * ctr)23 ModifyConstraintTeachersMinGapsBetweenOrderedPairOfActivityTagsForm::ModifyConstraintTeachersMinGapsBetweenOrderedPairOfActivityTagsForm
24  (QWidget* parent, ConstraintTeachersMinGapsBetweenOrderedPairOfActivityTags* ctr): QDialog(parent)
25 {
26 	setupUi(this);
27 
28 	okPushButton->setDefault(true);
29 
30 	connect(okPushButton, SIGNAL(clicked()), this, SLOT(ok()));
31 	connect(cancelPushButton, SIGNAL(clicked()), this, SLOT(cancel()));
32 
33 	connect(swapPushButton, SIGNAL(clicked()), this, SLOT(swap()));
34 
35 	centerWidgetOnScreen(this);
36 	restoreFETDialogGeometry(this);
37 
38 	QSize tmp4=firstActivityTagComboBox->minimumSizeHint();
39 	Q_UNUSED(tmp4);
40 	QSize tmp5=secondActivityTagComboBox->minimumSizeHint();
41 	Q_UNUSED(tmp5);
42 
43 	this->_ctr=ctr;
44 
45 	weightLineEdit->setText(CustomFETString::number(ctr->weightPercentage));
46 
47 	updateFirstActivityTagComboBox();
48 	updateSecondActivityTagComboBox();
49 
50 	minGapsSpinBox->setMinimum(1);
51 	minGapsSpinBox->setMaximum(gt.rules.nHoursPerDay);
52 	minGapsSpinBox->setValue(ctr->minGaps);
53 }
54 
~ModifyConstraintTeachersMinGapsBetweenOrderedPairOfActivityTagsForm()55 ModifyConstraintTeachersMinGapsBetweenOrderedPairOfActivityTagsForm::~ModifyConstraintTeachersMinGapsBetweenOrderedPairOfActivityTagsForm()
56 {
57 	saveFETDialogGeometry(this);
58 }
59 
updateFirstActivityTagComboBox()60 void ModifyConstraintTeachersMinGapsBetweenOrderedPairOfActivityTagsForm::updateFirstActivityTagComboBox()
61 {
62 	firstActivityTagComboBox->clear();
63 	int j=-1;
64 	for(int i=0; i<gt.rules.activityTagsList.count(); i++){
65 		ActivityTag* at=gt.rules.activityTagsList.at(i);
66 		firstActivityTagComboBox->addItem(at->name);
67 		if(at->name==this->_ctr->firstActivityTag)
68 			j=i;
69 	}
70 	assert(j>=0);
71 	firstActivityTagComboBox->setCurrentIndex(j);
72 }
73 
updateSecondActivityTagComboBox()74 void ModifyConstraintTeachersMinGapsBetweenOrderedPairOfActivityTagsForm::updateSecondActivityTagComboBox()
75 {
76 	secondActivityTagComboBox->clear();
77 	int j=-1;
78 	for(int i=0; i<gt.rules.activityTagsList.count(); i++){
79 		ActivityTag* at=gt.rules.activityTagsList.at(i);
80 		secondActivityTagComboBox->addItem(at->name);
81 		if(at->name==this->_ctr->secondActivityTag)
82 			j=i;
83 	}
84 	assert(j>=0);
85 	secondActivityTagComboBox->setCurrentIndex(j);
86 }
87 
ok()88 void ModifyConstraintTeachersMinGapsBetweenOrderedPairOfActivityTagsForm::ok()
89 {
90 	double weight;
91 	QString tmp=weightLineEdit->text();
92 	weight_sscanf(tmp, "%lf", &weight);
93 	if(weight<0.0 || weight>100.0){
94 		QMessageBox::warning(this, tr("FET warning"),
95 			tr("Invalid weight (percentage)"));
96 		return;
97 	}
98 	if(weight!=100.0){
99 		QMessageBox::warning(this, tr("FET information"),
100 			tr("Invalid weight (percentage) - must be 100%"));
101 		return;
102 	}
103 
104 	QString firstActivityTagName=firstActivityTagComboBox->currentText();
105 	int fati=gt.rules.searchActivityTag(firstActivityTagName);
106 	if(fati<0){
107 		QMessageBox::warning(this, tr("FET warning"), tr("Invalid activity tag"));
108 		return;
109 	}
110 
111 	QString secondActivityTagName=secondActivityTagComboBox->currentText();
112 	int sati=gt.rules.searchActivityTag(secondActivityTagName);
113 	if(sati<0){
114 		QMessageBox::warning(this, tr("FET warning"), tr("Invalid activity tag"));
115 		return;
116 	}
117 
118 	if(firstActivityTagName==secondActivityTagName){
119 		QMessageBox::warning(this, tr("FET warning"), tr("The two activity tags cannot be the same"));
120 		return;
121 	}
122 
123 	this->_ctr->weightPercentage=weight;
124 	this->_ctr->firstActivityTag=firstActivityTagName;
125 	this->_ctr->secondActivityTag=secondActivityTagName;
126 	this->_ctr->minGaps=minGapsSpinBox->value();
127 
128 	gt.rules.internalStructureComputed=false;
129 	setRulesModifiedAndOtherThings(&gt.rules);
130 
131 	this->close();
132 }
133 
cancel()134 void ModifyConstraintTeachersMinGapsBetweenOrderedPairOfActivityTagsForm::cancel()
135 {
136 	this->close();
137 }
138 
swap()139 void ModifyConstraintTeachersMinGapsBetweenOrderedPairOfActivityTagsForm::swap()
140 {
141 	int t1=firstActivityTagComboBox->currentIndex();
142 	int t2=secondActivityTagComboBox->currentIndex();
143 	firstActivityTagComboBox->setCurrentIndex(t2);
144 	secondActivityTagComboBox->setCurrentIndex(t1);
145 }
146