1 /***************************************************************************
2 addconstraintstudentssethomeroomsform.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
21
22 #include "longtextmessagebox.h"
23
24 #include "addconstraintstudentssethomeroomsform.h"
25
26 #include <QListWidget>
27 #include <QAbstractItemView>
28
AddConstraintStudentsSetHomeRoomsForm(QWidget * parent)29 AddConstraintStudentsSetHomeRoomsForm::AddConstraintStudentsSetHomeRoomsForm(QWidget* parent): QDialog(parent)
30 {
31 setupUi(this);
32
33 addConstraintPushButton->setDefault(true);
34
35 roomsListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
36 selectedRoomsListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
37
38 connect(closePushButton, SIGNAL(clicked()), this, SLOT(close()));
39 connect(addConstraintPushButton, SIGNAL(clicked()), this, SLOT(addConstraint()));
40 connect(roomsListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(addRoom()));
41 connect(selectedRoomsListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(removeRoom()));
42 connect(clearPushButton, SIGNAL(clicked()), this, SLOT(clear()));
43
44 centerWidgetOnScreen(this);
45 restoreFETDialogGeometry(this);
46
47 QSize tmp2=studentsComboBox->minimumSizeHint();
48 Q_UNUSED(tmp2);
49
50 updateRoomsListWidget();
51
52 populateStudentsComboBox(studentsComboBox);
53 }
54
~AddConstraintStudentsSetHomeRoomsForm()55 AddConstraintStudentsSetHomeRoomsForm::~AddConstraintStudentsSetHomeRoomsForm()
56 {
57 saveFETDialogGeometry(this);
58 }
59
updateRoomsListWidget()60 void AddConstraintStudentsSetHomeRoomsForm::updateRoomsListWidget()
61 {
62 roomsListWidget->clear();
63 selectedRoomsListWidget->clear();
64
65 for(int i=0; i<gt.rules.roomsList.size(); i++){
66 Room* rm=gt.rules.roomsList[i];
67 roomsListWidget->addItem(rm->name);
68 }
69 }
70
addConstraint()71 void AddConstraintStudentsSetHomeRoomsForm::addConstraint()
72 {
73 SpaceConstraint *ctr=nullptr;
74
75 double weight;
76 QString tmp=weightLineEdit->text();
77 weight_sscanf(tmp, "%lf", &weight);
78 if(weight<0.0 || weight>100){
79 QMessageBox::warning(this, tr("FET information"),
80 tr("Invalid weight"));
81 return;
82 }
83
84 if(selectedRoomsListWidget->count()==0){
85 QMessageBox::warning(this, tr("FET information"),
86 tr("Empty list of selected rooms"));
87 return;
88 }
89 /*if(selectedRoomsListWidget->count()==1){
90 QMessageBox::warning(this, tr("FET information"),
91 tr("Only one selected room - please use constraint students set home room if you want a single room"));
92 return;
93 }*/
94
95 QString students=studentsComboBox->currentText();
96 assert(gt.rules.searchStudentsSet(students)!=nullptr);
97
98 QStringList roomsList;
99 for(int i=0; i<selectedRoomsListWidget->count(); i++)
100 roomsList.append(selectedRoomsListWidget->item(i)->text());
101
102 ctr=new ConstraintStudentsSetHomeRooms(weight, students, roomsList);
103 bool tmp2=gt.rules.addSpaceConstraint(ctr);
104
105 if(tmp2){
106 QString s=tr("Constraint added:");
107 s+="\n\n";
108 s+=ctr->getDetailedDescription(gt.rules);
109 LongTextMessageBox::information(this, tr("FET information"), s);
110 }
111 else{
112 QMessageBox::warning(this, tr("FET information"),
113 tr("Constraint NOT added - please report error"));
114 delete ctr;
115 }
116 }
117
addRoom()118 void AddConstraintStudentsSetHomeRoomsForm::addRoom()
119 {
120 if(roomsListWidget->currentRow()<0)
121 return;
122 QString rmName=roomsListWidget->currentItem()->text();
123 assert(rmName!="");
124 int i;
125 //duplicate?
126 for(i=0; i<selectedRoomsListWidget->count(); i++)
127 if(rmName==selectedRoomsListWidget->item(i)->text())
128 break;
129 if(i<selectedRoomsListWidget->count())
130 return;
131 selectedRoomsListWidget->addItem(rmName);
132 selectedRoomsListWidget->setCurrentRow(selectedRoomsListWidget->count()-1);
133 }
134
removeRoom()135 void AddConstraintStudentsSetHomeRoomsForm::removeRoom()
136 {
137 if(selectedRoomsListWidget->currentRow()<0 || selectedRoomsListWidget->count()<=0)
138 return;
139 int tmp=selectedRoomsListWidget->currentRow();
140
141 selectedRoomsListWidget->setCurrentRow(-1);
142 QListWidgetItem* item=selectedRoomsListWidget->takeItem(tmp);
143 delete item;
144 if(tmp<selectedRoomsListWidget->count())
145 selectedRoomsListWidget->setCurrentRow(tmp);
146 else
147 selectedRoomsListWidget->setCurrentRow(selectedRoomsListWidget->count()-1);
148 }
149
clear()150 void AddConstraintStudentsSetHomeRoomsForm::clear()
151 {
152 selectedRoomsListWidget->clear();
153 }
154