1 /***************************************************************************
2                           constraintactivitiesnotoverlappingform.cpp  -  description
3                              -------------------
4     begin                : Feb 11, 2005
5     copyright            : (C) 2005 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 "constraintactivitiesnotoverlappingform.h"
23 #include "addconstraintactivitiesnotoverlappingform.h"
24 #include "modifyconstraintactivitiesnotoverlappingform.h"
25 
26 #include <QListWidget>
27 #include <QScrollBar>
28 #include <QAbstractItemView>
29 
ConstraintActivitiesNotOverlappingForm(QWidget * parent)30 ConstraintActivitiesNotOverlappingForm::ConstraintActivitiesNotOverlappingForm(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(constraintsListWidget, SIGNAL(currentRowChanged(int)), this, SLOT(constraintChanged(int)));
41 	connect(addConstraintPushButton, SIGNAL(clicked()), this, SLOT(addConstraint()));
42 	connect(closePushButton, SIGNAL(clicked()), this, SLOT(close()));
43 	connect(removeConstraintPushButton, SIGNAL(clicked()), this, SLOT(removeConstraint()));
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 
~ConstraintActivitiesNotOverlappingForm()93 ConstraintActivitiesNotOverlappingForm::~ConstraintActivitiesNotOverlappingForm()
94 {
95 	saveFETDialogGeometry(this);
96 }
97 
filterOk(TimeConstraint * ctr)98 bool ConstraintActivitiesNotOverlappingForm::filterOk(TimeConstraint* ctr)
99 {
100 	if(ctr->type!=CONSTRAINT_ACTIVITIES_NOT_OVERLAPPING)
101 		return false;
102 
103 	ConstraintActivitiesNotOverlapping* c=(ConstraintActivitiesNotOverlapping*) 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 	for(int i=0; i<c->n_activities; i++){
116 		//bool found=true;
117 
118 		int id=c->activitiesId[i];
119 		/*Activity* act=nullptr;
120 		for(Activity* a : qAsConst(gt.rules.activitiesList))
121 			if(a->id==id)
122 				act=a;*/
123 		Activity* act=gt.rules.activitiesPointerHash.value(id, nullptr);
124 
125 		if(act!=nullptr){
126 			//teacher
127 			if(tn!=""){
128 				bool ok2=false;
129 				for(QStringList::const_iterator it=act->teachersNames.constBegin(); it!=act->teachersNames.constEnd(); it++)
130 					if(*it == tn){
131 						ok2=true;
132 						break;
133 					}
134 				if(ok2)
135 					foundTeacher=true;
136 			}
137 			else
138 				foundTeacher=true;
139 
140 			//subject
141 			if(sbn!="" && sbn!=act->subjectName)
142 				;
143 			else
144 				foundSubject=true;
145 
146 			//activity tag
147 			if(atn!="" && !act->activityTagsNames.contains(atn))
148 				;
149 			else
150 				foundActivityTag=true;
151 
152 			//students
153 			if(stn!=""){
154 				bool ok2=false;
155 				for(QStringList::const_iterator it=act->studentsNames.constBegin(); it!=act->studentsNames.constEnd(); it++)
156 					if(*it == stn){
157 						ok2=true;
158 						break;
159 				}
160 				if(ok2)
161 					foundStudents=true;
162 			}
163 			else
164 				foundStudents=true;
165 		}
166 	}
167 
168 	if(foundTeacher && foundStudents && foundSubject && foundActivityTag)
169 		return true;
170 	else
171 		return false;
172 }
173 
filterChanged()174 void ConstraintActivitiesNotOverlappingForm::filterChanged()
175 {
176 	this->visibleConstraintsList.clear();
177 	constraintsListWidget->clear();
178 	for(int i=0; i<gt.rules.timeConstraintsList.size(); i++){
179 		TimeConstraint* ctr=gt.rules.timeConstraintsList[i];
180 		if(filterOk(ctr)){
181 			visibleConstraintsList.append(ctr);
182 			constraintsListWidget->addItem(ctr->getDescription(gt.rules));
183 		}
184 	}
185 
186 	if(constraintsListWidget->count()>0)
187 		constraintsListWidget->setCurrentRow(0);
188 	else
189 		constraintChanged(-1);
190 }
191 
constraintChanged(int index)192 void ConstraintActivitiesNotOverlappingForm::constraintChanged(int index)
193 {
194 	if(index<0){
195 		currentConstraintTextEdit->setPlainText("");
196 		return;
197 	}
198 	assert(index<this->visibleConstraintsList.size());
199 	TimeConstraint* ctr=this->visibleConstraintsList.at(index);
200 	assert(ctr!=nullptr);
201 	currentConstraintTextEdit->setPlainText(ctr->getDetailedDescription(gt.rules));
202 }
203 
addConstraint()204 void ConstraintActivitiesNotOverlappingForm::addConstraint()
205 {
206 	AddConstraintActivitiesNotOverlappingForm form(this);
207 	setParentAndOtherThings(&form, this);
208 	form.exec();
209 
210 	filterChanged();
211 
212 	constraintsListWidget->setCurrentRow(constraintsListWidget->count()-1);
213 }
214 
modifyConstraint()215 void ConstraintActivitiesNotOverlappingForm::modifyConstraint()
216 {
217 	int valv=constraintsListWidget->verticalScrollBar()->value();
218 	int valh=constraintsListWidget->horizontalScrollBar()->value();
219 
220 	int i=constraintsListWidget->currentRow();
221 	if(i<0){
222 		QMessageBox::information(this, tr("FET information"), tr("Invalid selected constraint"));
223 		return;
224 	}
225 	TimeConstraint* ctr=this->visibleConstraintsList.at(i);
226 
227 	ModifyConstraintActivitiesNotOverlappingForm form(this, (ConstraintActivitiesNotOverlapping*)ctr);
228 	setParentAndOtherThings(&form, this);
229 	form.exec();
230 
231 	filterChanged();
232 
233 	constraintsListWidget->verticalScrollBar()->setValue(valv);
234 	constraintsListWidget->horizontalScrollBar()->setValue(valh);
235 
236 	if(i>=constraintsListWidget->count())
237 		i=constraintsListWidget->count()-1;
238 
239 	if(i>=0)
240 		constraintsListWidget->setCurrentRow(i);
241 	else
242 		this->constraintChanged(-1);
243 }
244 
removeConstraint()245 void ConstraintActivitiesNotOverlappingForm::removeConstraint()
246 {
247 	int i=constraintsListWidget->currentRow();
248 	if(i<0){
249 		QMessageBox::information(this, tr("FET information"), tr("Invalid selected constraint"));
250 		return;
251 	}
252 	TimeConstraint* ctr=this->visibleConstraintsList.at(i);
253 	QString s;
254 	s=tr("Remove constraint?");
255 	s+="\n\n";
256 	s+=ctr->getDetailedDescription(gt.rules);
257 
258 	QListWidgetItem* item;
259 
260 	switch( LongTextMessageBox::confirmation( this, tr("FET confirmation"),
261 		s, tr("Yes"), tr("No"), 0, 0, 1 ) ){
262 	case 0: // The user clicked the OK button or pressed Enter
263 		gt.rules.removeTimeConstraint(ctr);
264 
265 		visibleConstraintsList.removeAt(i);
266 		constraintsListWidget->setCurrentRow(-1);
267 		item=constraintsListWidget->takeItem(i);
268 		delete item;
269 
270 		break;
271 	case 1: // The user clicked the Cancel button or pressed Escape
272 		break;
273 	}
274 
275 	if(i>=constraintsListWidget->count())
276 		i=constraintsListWidget->count()-1;
277 	if(i>=0)
278 		constraintsListWidget->setCurrentRow(i);
279 	else
280 		this->constraintChanged(-1);
281 }
282