1 /***************************************************************************
2                           constraintactivitypreferredroomsform.cpp  -  description
3                              -------------------
4     begin                : 28 March 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 "constraintactivitypreferredroomsform.h"
23 #include "addconstraintactivitypreferredroomsform.h"
24 #include "modifyconstraintactivitypreferredroomsform.h"
25 
26 #include <QListWidget>
27 #include <QScrollBar>
28 #include <QAbstractItemView>
29 
ConstraintActivityPreferredRoomsForm(QWidget * parent)30 ConstraintActivityPreferredRoomsForm::ConstraintActivityPreferredRoomsForm(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 	QSize tmp5=roomsComboBox->minimumSizeHint();
60 	Q_UNUSED(tmp5);
61 
62 	roomsComboBox->addItem("");
63 	for(int i=0; i<gt.rules.roomsList.size(); i++){
64 		Room* rm=gt.rules.roomsList[i];
65 		roomsComboBox->addItem(rm->name);
66 	}
67 
68 	/////////////
69 	teachersComboBox->addItem("");
70 	for(int i=0; i<gt.rules.teachersList.size(); i++){
71 		Teacher* tch=gt.rules.teachersList[i];
72 		teachersComboBox->addItem(tch->name);
73 	}
74 	teachersComboBox->setCurrentIndex(0);
75 
76 	subjectsComboBox->addItem("");
77 	for(int i=0; i<gt.rules.subjectsList.size(); i++){
78 		Subject* sb=gt.rules.subjectsList[i];
79 		subjectsComboBox->addItem(sb->name);
80 	}
81 	subjectsComboBox->setCurrentIndex(0);
82 
83 	activityTagsComboBox->addItem("");
84 	for(int i=0; i<gt.rules.activityTagsList.size(); i++){
85 		ActivityTag* st=gt.rules.activityTagsList[i];
86 		activityTagsComboBox->addItem(st->name);
87 	}
88 	activityTagsComboBox->setCurrentIndex(0);
89 
90 	populateStudentsComboBox(studentsComboBox, QString(""), true);
91 	studentsComboBox->setCurrentIndex(0);
92 ///////////////
93 
94 	this->filterChanged();
95 
96 	connect(roomsComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(filterChanged()));
97 	connect(teachersComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(filterChanged()));
98 	connect(studentsComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(filterChanged()));
99 	connect(subjectsComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(filterChanged()));
100 	connect(activityTagsComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(filterChanged()));
101 }
102 
~ConstraintActivityPreferredRoomsForm()103 ConstraintActivityPreferredRoomsForm::~ConstraintActivityPreferredRoomsForm()
104 {
105 	saveFETDialogGeometry(this);
106 }
107 
filterChanged()108 void ConstraintActivityPreferredRoomsForm::filterChanged()
109 {
110 	this->visibleConstraintsList.clear();
111 	constraintsListWidget->clear();
112 	for(int i=0; i<gt.rules.spaceConstraintsList.size(); i++){
113 		SpaceConstraint* ctr=gt.rules.spaceConstraintsList[i];
114 		if(filterOk(ctr)){
115 			visibleConstraintsList.append(ctr);
116 			constraintsListWidget->addItem(ctr->getDescription(gt.rules));
117 		}
118 	}
119 
120 	if(constraintsListWidget->count()>0)
121 		constraintsListWidget->setCurrentRow(0);
122 	else
123 		this->constraintChanged(-1);
124 }
125 
filterOk(SpaceConstraint * ctr)126 bool ConstraintActivityPreferredRoomsForm::filterOk(SpaceConstraint* ctr)
127 {
128 	if(ctr->type!=CONSTRAINT_ACTIVITY_PREFERRED_ROOMS)
129 		return false;
130 
131 	ConstraintActivityPreferredRooms* c=(ConstraintActivityPreferredRooms*) ctr;
132 
133 	QString tn=teachersComboBox->currentText();
134 	QString sbn=subjectsComboBox->currentText();
135 	QString atn=activityTagsComboBox->currentText();
136 	QString stn=studentsComboBox->currentText();
137 
138 	bool found=true;
139 
140 	int id=c->activityId;
141 	/*Activity* act=nullptr;
142 	for(Activity* a : qAsConst(gt.rules.activitiesList))
143 		if(a->id==id)
144 			act=a;*/
145 	Activity* act=gt.rules.activitiesPointerHash.value(id, nullptr);
146 
147 	found=true;
148 
149 	if(act!=nullptr){
150 		//teacher
151 		if(tn!=""){
152 			bool ok2=false;
153 			for(QStringList::const_iterator it=act->teachersNames.constBegin(); it!=act->teachersNames.constEnd(); it++)
154 				if(*it == tn){
155 					ok2=true;
156 					break;
157 				}
158 			if(!ok2)
159 				found=false;
160 		}
161 
162 		//subject
163 		if(sbn!="" && sbn!=act->subjectName)
164 			found=false;
165 
166 		//activity tag
167 		if(atn!="" && !act->activityTagsNames.contains(atn))
168 			found=false;
169 
170 		//students
171 		if(stn!=""){
172 			bool ok2=false;
173 			for(QStringList::const_iterator it=act->studentsNames.constBegin(); it!=act->studentsNames.constEnd(); it++)
174 				if(*it == stn){
175 					ok2=true;
176 					break;
177 			}
178 			if(!ok2)
179 				found=false;
180 		}
181 	}
182 
183 	if(!found)
184 		return false;
185 
186 	return roomsComboBox->currentText()==""
187 	 || c->roomsNames.contains(roomsComboBox->currentText());
188 }
189 
constraintChanged(int index)190 void ConstraintActivityPreferredRoomsForm::constraintChanged(int index)
191 {
192 	if(index<0){
193 		currentConstraintTextEdit->setPlainText("");
194 		return;
195 	}
196 	QString s;
197 	assert(index<this->visibleConstraintsList.size());
198 	SpaceConstraint* ctr=this->visibleConstraintsList.at(index);
199 	assert(ctr!=nullptr);
200 	s=ctr->getDetailedDescription(gt.rules);
201 	currentConstraintTextEdit->setPlainText(s);
202 }
203 
addConstraint()204 void ConstraintActivityPreferredRoomsForm::addConstraint()
205 {
206 	AddConstraintActivityPreferredRoomsForm form(this);
207 	setParentAndOtherThings(&form, this);
208 	form.exec();
209 
210 	this->filterChanged();
211 
212 	constraintsListWidget->setCurrentRow(constraintsListWidget->count()-1);
213 }
214 
modifyConstraint()215 void ConstraintActivityPreferredRoomsForm::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 	SpaceConstraint* ctr=this->visibleConstraintsList.at(i);
226 
227 	ModifyConstraintActivityPreferredRoomsForm form(this, (ConstraintActivityPreferredRooms*)ctr);
228 	setParentAndOtherThings(&form, this);
229 	form.exec();
230 
231 	this->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 ConstraintActivityPreferredRoomsForm::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 	SpaceConstraint* 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.removeSpaceConstraint(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