1 /***************************************************************************
2                           constrainttwosetsofactivitiesorderedform.cpp  -  description
3                              -------------------
4     begin                : 2021
5     copyright            : (C) 2021 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 "constrainttwosetsofactivitiesorderedform.h"
23 #include "addconstrainttwosetsofactivitiesorderedform.h"
24 #include "modifyconstrainttwosetsofactivitiesorderedform.h"
25 
26 #include <QListWidget>
27 #include <QScrollBar>
28 #include <QAbstractItemView>
29 
ConstraintTwoSetsOfActivitiesOrderedForm(QWidget * parent)30 ConstraintTwoSetsOfActivitiesOrderedForm::ConstraintTwoSetsOfActivitiesOrderedForm(QWidget* parent): QDialog(parent)
31 {
32 	setupUi(this);
33 
34 	currentConstraintTextEdit->setReadOnly(true);
35 
36 	modifyConstraintPushButton->setDefault(true);
37 
38 	constraintsListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
39 
40 	connect(addConstraintPushButton, SIGNAL(clicked()), this, SLOT(addConstraint()));
41 	connect(removeConstraintPushButton, SIGNAL(clicked()), this, SLOT(removeConstraint()));
42 	connect(closePushButton, SIGNAL(clicked()), this, SLOT(close()));
43 	connect(constraintsListWidget, SIGNAL(currentRowChanged(int)), this, SLOT(constraintChanged(int)));
44 	connect(modifyConstraintPushButton, SIGNAL(clicked()), this, SLOT(modifyConstraint()));
45 	connect(constraintsListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(modifyConstraint()));
46 
47 	centerWidgetOnScreen(this);
48 	restoreFETDialogGeometry(this);
49 
50 	QSize tmp1=teachersComboBox->minimumSizeHint();
51 	Q_UNUSED(tmp1);
52 	QSize tmp2=studentsComboBox->minimumSizeHint();
53 	Q_UNUSED(tmp2);
54 	QSize tmp3=subjectsComboBox->minimumSizeHint();
55 	Q_UNUSED(tmp3);
56 	QSize tmp4=activityTagsComboBox->minimumSizeHint();
57 	Q_UNUSED(tmp4);
58 
59 /////////////
60 	teachersComboBox->addItem("");
61 	for(int i=0; i<gt.rules.teachersList.size(); i++){
62 		Teacher* tch=gt.rules.teachersList[i];
63 		teachersComboBox->addItem(tch->name);
64 	}
65 	teachersComboBox->setCurrentIndex(0);
66 
67 	subjectsComboBox->addItem("");
68 	for(int i=0; i<gt.rules.subjectsList.size(); i++){
69 		Subject* sb=gt.rules.subjectsList[i];
70 		subjectsComboBox->addItem(sb->name);
71 	}
72 	subjectsComboBox->setCurrentIndex(0);
73 
74 	activityTagsComboBox->addItem("");
75 	for(int i=0; i<gt.rules.activityTagsList.size(); i++){
76 		ActivityTag* st=gt.rules.activityTagsList[i];
77 		activityTagsComboBox->addItem(st->name);
78 	}
79 	activityTagsComboBox->setCurrentIndex(0);
80 
81 	populateStudentsComboBox(studentsComboBox, QString(""), true);
82 	studentsComboBox->setCurrentIndex(0);
83 ///////////////
84 
85 	this->filterChanged();
86 
87 	connect(teachersComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(filterChanged()));
88 	connect(studentsComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(filterChanged()));
89 	connect(subjectsComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(filterChanged()));
90 	connect(activityTagsComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(filterChanged()));
91 }
92 
~ConstraintTwoSetsOfActivitiesOrderedForm()93 ConstraintTwoSetsOfActivitiesOrderedForm::~ConstraintTwoSetsOfActivitiesOrderedForm()
94 {
95 	saveFETDialogGeometry(this);
96 }
97 
filterOk(TimeConstraint * ctr)98 bool ConstraintTwoSetsOfActivitiesOrderedForm::filterOk(TimeConstraint* ctr)
99 {
100 	if(ctr->type!=CONSTRAINT_TWO_SETS_OF_ACTIVITIES_ORDERED)
101 		return false;
102 
103 	ConstraintTwoSetsOfActivitiesOrdered* c=(ConstraintTwoSetsOfActivitiesOrdered*) ctr;
104 
105 	QString tn=teachersComboBox->currentText();
106 	QString sbn=subjectsComboBox->currentText();
107 	QString atn=activityTagsComboBox->currentText();
108 	QString stn=studentsComboBox->currentText();
109 
110 	if(tn=="" && sbn=="" && atn=="" && stn=="")
111 		return true;
112 
113 	bool foundTeacher=false, foundStudents=false, foundSubject=false, foundActivityTag=false;
114 
115 	QList<int> tl=c->firstActivitiesIdsList+c->secondActivitiesIdsList;
116 
117 	for(int id : qAsConst(tl)){
118 		Activity* act=gt.rules.activitiesPointerHash.value(id, nullptr);
119 
120 		if(act!=nullptr){
121 			//teacher
122 			if(tn!=""){
123 				bool ok2=false;
124 				for(QStringList::const_iterator it=act->teachersNames.constBegin(); it!=act->teachersNames.constEnd(); it++)
125 					if(*it == tn){
126 						ok2=true;
127 						break;
128 					}
129 				if(ok2)
130 					foundTeacher=true;
131 			}
132 			else
133 				foundTeacher=true;
134 
135 			//subject
136 			if(sbn!="" && sbn!=act->subjectName)
137 				;
138 			else
139 				foundSubject=true;
140 
141 			//activity tag
142 			if(atn!="" && !act->activityTagsNames.contains(atn))
143 				;
144 			else
145 				foundActivityTag=true;
146 
147 			//students
148 			if(stn!=""){
149 				bool ok2=false;
150 				for(QStringList::const_iterator it=act->studentsNames.constBegin(); it!=act->studentsNames.constEnd(); it++)
151 					if(*it == stn){
152 						ok2=true;
153 						break;
154 				}
155 				if(ok2)
156 					foundStudents=true;
157 			}
158 			else
159 				foundStudents=true;
160 		}
161 	}
162 
163 	if(foundTeacher && foundStudents && foundSubject && foundActivityTag)
164 		return true;
165 	else
166 		return false;
167 }
168 
filterChanged()169 void ConstraintTwoSetsOfActivitiesOrderedForm::filterChanged()
170 {
171 	this->visibleConstraintsList.clear();
172 	constraintsListWidget->clear();
173 	for(int i=0; i<gt.rules.timeConstraintsList.size(); i++){
174 		TimeConstraint* ctr=gt.rules.timeConstraintsList[i];
175 		if(filterOk(ctr)){
176 			visibleConstraintsList.append(ctr);
177 			constraintsListWidget->addItem(ctr->getDescription(gt.rules));
178 		}
179 	}
180 
181 	if(constraintsListWidget->count()>0)
182 		constraintsListWidget->setCurrentRow(0);
183 	else
184 		constraintChanged(-1);
185 }
186 
constraintChanged(int index)187 void ConstraintTwoSetsOfActivitiesOrderedForm::constraintChanged(int index)
188 {
189 	if(index<0){
190 		currentConstraintTextEdit->setPlainText("");
191 		return;
192 	}
193 	QString s;
194 	assert(index<this->visibleConstraintsList.size());
195 	TimeConstraint* ctr=this->visibleConstraintsList.at(index);
196 	assert(ctr!=nullptr);
197 	s=ctr->getDetailedDescription(gt.rules);
198 	currentConstraintTextEdit->setPlainText(s);
199 }
200 
addConstraint()201 void ConstraintTwoSetsOfActivitiesOrderedForm::addConstraint()
202 {
203 	AddConstraintTwoSetsOfActivitiesOrderedForm form(this);
204 	setParentAndOtherThings(&form, this);
205 	form.exec();
206 
207 	filterChanged();
208 
209 	constraintsListWidget->setCurrentRow(constraintsListWidget->count()-1);
210 }
211 
modifyConstraint()212 void ConstraintTwoSetsOfActivitiesOrderedForm::modifyConstraint()
213 {
214 	int valv=constraintsListWidget->verticalScrollBar()->value();
215 	int valh=constraintsListWidget->horizontalScrollBar()->value();
216 
217 	int i=constraintsListWidget->currentRow();
218 	if(i<0){
219 		QMessageBox::information(this, tr("FET information"), tr("Invalid selected constraint"));
220 		return;
221 	}
222 	TimeConstraint* ctr=this->visibleConstraintsList.at(i);
223 
224 	ModifyConstraintTwoSetsOfActivitiesOrderedForm form(this, (ConstraintTwoSetsOfActivitiesOrdered*)ctr);
225 	setParentAndOtherThings(&form, this);
226 	form.exec();
227 
228 	filterChanged();
229 
230 	constraintsListWidget->verticalScrollBar()->setValue(valv);
231 	constraintsListWidget->horizontalScrollBar()->setValue(valh);
232 
233 	if(i>=constraintsListWidget->count())
234 		i=constraintsListWidget->count()-1;
235 
236 	if(i>=0)
237 		constraintsListWidget->setCurrentRow(i);
238 	else
239 		this->constraintChanged(-1);
240 }
241 
removeConstraint()242 void ConstraintTwoSetsOfActivitiesOrderedForm::removeConstraint()
243 {
244 	int i=constraintsListWidget->currentRow();
245 	if(i<0){
246 		QMessageBox::information(this, tr("FET information"), tr("Invalid selected constraint"));
247 		return;
248 	}
249 	TimeConstraint* ctr=this->visibleConstraintsList.at(i);
250 	QString s;
251 	s=tr("Remove constraint?");
252 	s+="\n\n";
253 	s+=ctr->getDetailedDescription(gt.rules);
254 
255 	QListWidgetItem* item;
256 
257 	switch( LongTextMessageBox::confirmation( this, tr("FET confirmation"),
258 		s, tr("Yes"), tr("No"), 0, 0, 1 ) ){
259 	case 0: // The user clicked the OK button or pressed Enter
260 		gt.rules.removeTimeConstraint(ctr);
261 
262 		visibleConstraintsList.removeAt(i);
263 		constraintsListWidget->setCurrentRow(-1);
264 		item=constraintsListWidget->takeItem(i);
265 		delete item;
266 
267 		break;
268 	case 1: // The user clicked the Cancel button or pressed Escape
269 		break;
270 	}
271 
272 	if(i>=constraintsListWidget->count())
273 		i=constraintsListWidget->count()-1;
274 	if(i>=0)
275 		constraintsListWidget->setCurrentRow(i);
276 	else
277 		this->constraintChanged(-1);
278 }
279