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