1 /***************************************************************************
2                           addconstraintactivitypreferredroomsform.cpp  -  description
3                              -------------------
4     begin                : March 28, 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 "addconstraintactivitypreferredroomsform.h"
23 #include "spaceconstraint.h"
24 
25 #include <QListWidget>
26 #include <QAbstractItemView>
27 
AddConstraintActivityPreferredRoomsForm(QWidget * parent)28 AddConstraintActivityPreferredRoomsForm::AddConstraintActivityPreferredRoomsForm(QWidget* parent): QDialog(parent)
29 {
30 	setupUi(this);
31 
32 	addConstraintPushButton->setDefault(true);
33 
34 	roomsListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
35 	selectedRoomsListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
36 
37 	connect(closePushButton, SIGNAL(clicked()), this, SLOT(close()));
38 	connect(addConstraintPushButton, SIGNAL(clicked()), this, SLOT(addConstraint()));
39 	connect(roomsListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(addRoom()));
40 	connect(selectedRoomsListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(removeRoom()));
41 	connect(clearPushButton, SIGNAL(clicked()), this, SLOT(clear()));
42 
43 	centerWidgetOnScreen(this);
44 	restoreFETDialogGeometry(this);
45 
46 	QSize tmp1=teachersComboBox->minimumSizeHint();
47 	Q_UNUSED(tmp1);
48 	QSize tmp2=studentsComboBox->minimumSizeHint();
49 	Q_UNUSED(tmp2);
50 	QSize tmp3=subjectsComboBox->minimumSizeHint();
51 	Q_UNUSED(tmp3);
52 	QSize tmp4=activityTagsComboBox->minimumSizeHint();
53 	Q_UNUSED(tmp4);
54 
55 	QSize tmp5=activitiesComboBox->minimumSizeHint();
56 	Q_UNUSED(tmp5);
57 
58 	activitiesComboBox->setMaximumWidth(maxRecommendedWidth(this));
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 	filterChanged();
85 
86 	updateRoomsListWidget();
87 
88 	connect(teachersComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(filterChanged()));
89 	connect(studentsComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(filterChanged()));
90 	connect(subjectsComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(filterChanged()));
91 	connect(activityTagsComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(filterChanged()));
92 }
93 
~AddConstraintActivityPreferredRoomsForm()94 AddConstraintActivityPreferredRoomsForm::~AddConstraintActivityPreferredRoomsForm()
95 {
96 	saveFETDialogGeometry(this);
97 }
98 
filterOk(Activity * act)99 bool AddConstraintActivityPreferredRoomsForm::filterOk(Activity* act)
100 {
101 	QString tn=teachersComboBox->currentText();
102 	QString stn=studentsComboBox->currentText();
103 	QString sbn=subjectsComboBox->currentText();
104 	QString atn=activityTagsComboBox->currentText();
105 	int ok=true;
106 
107 	//teacher
108 	if(tn!=""){
109 		bool ok2=false;
110 		for(QStringList::const_iterator it=act->teachersNames.constBegin(); it!=act->teachersNames.constEnd(); it++)
111 			if(*it == tn){
112 				ok2=true;
113 				break;
114 			}
115 		if(!ok2)
116 			ok=false;
117 	}
118 
119 	//subject
120 	if(sbn!="" && sbn!=act->subjectName)
121 		ok=false;
122 
123 	//activity tag
124 	if(atn!="" && !act->activityTagsNames.contains(atn))
125 		ok=false;
126 
127 	//students
128 	if(stn!=""){
129 		bool ok2=false;
130 		for(QStringList::const_iterator it=act->studentsNames.constBegin(); it!=act->studentsNames.constEnd(); it++)
131 			if(*it == stn){
132 				ok2=true;
133 				break;
134 			}
135 		if(!ok2)
136 			ok=false;
137 	}
138 
139 	return ok;
140 }
141 
filterChanged()142 void AddConstraintActivityPreferredRoomsForm::filterChanged(){
143 	activitiesComboBox->clear();
144 	activitiesList.clear();
145 
146 	for(int i=0; i<gt.rules.activitiesList.size(); i++){
147 		Activity* act=gt.rules.activitiesList[i];
148 
149 		if(filterOk(act)){
150 			activitiesComboBox->addItem(act->getDescription(gt.rules));
151 			this->activitiesList.append(act->id);
152 		}
153 	}
154 }
155 
updateRoomsListWidget()156 void AddConstraintActivityPreferredRoomsForm::updateRoomsListWidget()
157 {
158 	roomsListWidget->clear();
159 	selectedRoomsListWidget->clear();
160 
161 	for(int i=0; i<gt.rules.roomsList.size(); i++){
162 		Room* rm= gt.rules.roomsList[i];
163 		roomsListWidget->addItem(rm->name);
164 	}
165 }
166 
addConstraint()167 void AddConstraintActivityPreferredRoomsForm::addConstraint()
168 {
169 	SpaceConstraint *ctr=nullptr;
170 
171 	double weight;
172 	QString tmp=weightLineEdit->text();
173 	weight_sscanf(tmp, "%lf", &weight);
174 	if(weight<0.0 || weight>100){
175 		QMessageBox::warning(this, tr("FET information"),
176 			tr("Invalid weight (percentage)"));
177 		return;
178 	}
179 
180 	if(selectedRoomsListWidget->count()==0){
181 		QMessageBox::warning(this, tr("FET information"),
182 			tr("Empty list of selected rooms"));
183 		return;
184 	}
185 	/*if(selectedRoomsListWidget->count()==1){
186 		QMessageBox::warning(this, tr("FET information"),
187 			tr("Only one selected room - please use constraint activity preferred room if you want a single room"));
188 		return;
189 	}*/
190 
191 	int id;
192 	int tmp2=activitiesComboBox->currentIndex();
193 	if(tmp2<0 || tmp2>=gt.rules.activitiesList.size() || tmp2>=activitiesList.size()){
194 		QMessageBox::warning(this, tr("FET information"),
195 			tr("Invalid activity"));
196 		return;
197 	}
198 	else
199 		id=activitiesList.at(tmp2);
200 
201 	QStringList roomsList;
202 	for(int i=0; i<selectedRoomsListWidget->count(); i++)
203 		roomsList.append(selectedRoomsListWidget->item(i)->text());
204 
205 	ctr=new ConstraintActivityPreferredRooms(weight, id, roomsList);
206 	bool tmp3=gt.rules.addSpaceConstraint(ctr);
207 
208 	if(tmp3){
209 		QString s=tr("Constraint added:");
210 		s+="\n\n";
211 		s+=ctr->getDetailedDescription(gt.rules);
212 		LongTextMessageBox::information(this, tr("FET information"), s);
213 	}
214 	else{
215 		QMessageBox::warning(this, tr("FET information"),
216 			tr("Constraint NOT added - please report error"));
217 		delete ctr;
218 	}
219 }
220 
addRoom()221 void AddConstraintActivityPreferredRoomsForm::addRoom()
222 {
223 	if(roomsListWidget->currentRow()<0)
224 		return;
225 	QString rmName=roomsListWidget->currentItem()->text();
226 	assert(rmName!="");
227 	int i;
228 	//duplicate?
229 	for(i=0; i<selectedRoomsListWidget->count(); i++)
230 		if(rmName==selectedRoomsListWidget->item(i)->text())
231 			break;
232 	if(i<selectedRoomsListWidget->count())
233 		return;
234 	selectedRoomsListWidget->addItem(rmName);
235 	selectedRoomsListWidget->setCurrentRow(selectedRoomsListWidget->count()-1);
236 }
237 
removeRoom()238 void AddConstraintActivityPreferredRoomsForm::removeRoom()
239 {
240 	if(selectedRoomsListWidget->currentRow()<0 || selectedRoomsListWidget->count()<=0)
241 		return;
242 	int tmp=selectedRoomsListWidget->currentRow();
243 
244 	selectedRoomsListWidget->setCurrentRow(-1);
245 	QListWidgetItem* item=selectedRoomsListWidget->takeItem(tmp);
246 	delete item;
247 	if(tmp<selectedRoomsListWidget->count())
248 		selectedRoomsListWidget->setCurrentRow(tmp);
249 	else
250 		selectedRoomsListWidget->setCurrentRow(selectedRoomsListWidget->count()-1);
251 }
252 
clear()253 void AddConstraintActivityPreferredRoomsForm::clear()
254 {
255 	selectedRoomsListWidget->clear();
256 }
257