1 /***************************************************************************
2 modifyconstraintstudentsethomeroomform.cpp - description
3 -------------------
4 begin : April 8, 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 "modifyconstraintstudentssethomeroomsform.h"
21
22 #include <QListWidget>
23 #include <QAbstractItemView>
24
ModifyConstraintStudentsSetHomeRoomsForm(QWidget * parent,ConstraintStudentsSetHomeRooms * ctr)25 ModifyConstraintStudentsSetHomeRoomsForm::ModifyConstraintStudentsSetHomeRoomsForm(QWidget* parent, ConstraintStudentsSetHomeRooms* ctr): QDialog(parent)
26 {
27 setupUi(this);
28
29 okPushButton->setDefault(true);
30
31 roomsListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
32 selectedRoomsListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
33
34 connect(cancelPushButton, SIGNAL(clicked()), this, SLOT(cancel()));
35 connect(okPushButton, SIGNAL(clicked()), this, SLOT(ok()));
36 connect(roomsListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(addRoom()));
37 connect(selectedRoomsListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(removeRoom()));
38 connect(clearPushButton, SIGNAL(clicked()), this, SLOT(clear()));
39
40 centerWidgetOnScreen(this);
41 restoreFETDialogGeometry(this);
42
43 QSize tmp2=studentsComboBox->minimumSizeHint();
44 Q_UNUSED(tmp2);
45
46 updateRoomsListWidget();
47
48 this->_ctr=ctr;
49
50 /////////
51 //students
52 int j=populateStudentsComboBox(studentsComboBox, this->_ctr->studentsName);
53 if(j<0)
54 showWarningForInvisibleSubgroupConstraint(parent, this->_ctr->studentsName);
55 else
56 assert(j>=0);
57 studentsComboBox->setCurrentIndex(j);
58 /////////
59
60 weightLineEdit->setText(CustomFETString::number(ctr->weightPercentage));
61
62 for(QStringList::const_iterator it=ctr->roomsNames.constBegin(); it!=ctr->roomsNames.constEnd(); it++)
63 selectedRoomsListWidget->addItem(*it);
64 }
65
~ModifyConstraintStudentsSetHomeRoomsForm()66 ModifyConstraintStudentsSetHomeRoomsForm::~ModifyConstraintStudentsSetHomeRoomsForm()
67 {
68 saveFETDialogGeometry(this);
69 }
70
updateRoomsListWidget()71 void ModifyConstraintStudentsSetHomeRoomsForm::updateRoomsListWidget()
72 {
73 roomsListWidget->clear();
74 selectedRoomsListWidget->clear();
75
76 for(int i=0; i<gt.rules.roomsList.size(); i++){
77 Room* rm=gt.rules.roomsList[i];
78 roomsListWidget->addItem(rm->name);
79 }
80 }
81
ok()82 void ModifyConstraintStudentsSetHomeRoomsForm::ok()
83 {
84 if(studentsComboBox->currentIndex()<0){
85 showWarningCannotModifyConstraintInvisibleSubgroupConstraint(this, this->_ctr->studentsName);
86 return;
87 }
88
89 double weight;
90 QString tmp=weightLineEdit->text();
91 weight_sscanf(tmp, "%lf", &weight);
92 if(weight<0.0 || weight>100){
93 QMessageBox::warning(this, tr("FET information"),
94 tr("Invalid weight"));
95 return;
96 }
97
98 if(selectedRoomsListWidget->count()==0){
99 QMessageBox::warning(this, tr("FET information"),
100 tr("Empty list of selected rooms"));
101 return;
102 }
103 /*if(selectedRoomsListWidget->count()==1){
104 QMessageBox::warning(this, tr("FET information"),
105 tr("Only one selected room - please use constraint students set home room if you want a single room"));
106 return;
107 }*/
108
109 QString students=studentsComboBox->currentText();
110 assert(gt.rules.searchStudentsSet(students)!=nullptr);
111
112 QStringList roomsList;
113 for(int i=0; i<selectedRoomsListWidget->count(); i++)
114 roomsList.append(selectedRoomsListWidget->item(i)->text());
115
116 this->_ctr->weightPercentage=weight;
117
118 this->_ctr->studentsName=students;
119
120 this->_ctr->roomsNames=roomsList;
121
122 gt.rules.internalStructureComputed=false;
123 setRulesModifiedAndOtherThings(>.rules);
124
125 this->close();
126 }
127
cancel()128 void ModifyConstraintStudentsSetHomeRoomsForm::cancel()
129 {
130 this->close();
131 }
132
addRoom()133 void ModifyConstraintStudentsSetHomeRoomsForm::addRoom()
134 {
135 if(roomsListWidget->currentRow()<0)
136 return;
137 QString rmName=roomsListWidget->currentItem()->text();
138 assert(rmName!="");
139 int i;
140 //duplicate?
141 for(i=0; i<selectedRoomsListWidget->count(); i++)
142 if(rmName==selectedRoomsListWidget->item(i)->text())
143 break;
144 if(i<selectedRoomsListWidget->count())
145 return;
146 selectedRoomsListWidget->addItem(rmName);
147 selectedRoomsListWidget->setCurrentRow(selectedRoomsListWidget->count()-1);
148 }
149
removeRoom()150 void ModifyConstraintStudentsSetHomeRoomsForm::removeRoom()
151 {
152 if(selectedRoomsListWidget->currentRow()<0 || selectedRoomsListWidget->count()<=0)
153 return;
154 int tmp=selectedRoomsListWidget->currentRow();
155
156 selectedRoomsListWidget->setCurrentRow(-1);
157 QListWidgetItem* item=selectedRoomsListWidget->takeItem(tmp);
158 delete item;
159 if(tmp<selectedRoomsListWidget->count())
160 selectedRoomsListWidget->setCurrentRow(tmp);
161 else
162 selectedRoomsListWidget->setCurrentRow(selectedRoomsListWidget->count()-1);
163 }
164
clear()165 void ModifyConstraintStudentsSetHomeRoomsForm::clear()
166 {
167 selectedRoomsListWidget->clear();
168 }
169