1 /***************************************************************************
2                           constraintstudentssetmingapsbetweenroomchangesform.cpp  -  description
3                              -------------------
4     begin                : 2019
5     copyright            : (C) 2019 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 "constraintstudentssetmingapsbetweenroomchangesform.h"
23 #include "addconstraintstudentssetmingapsbetweenroomchangesform.h"
24 #include "modifyconstraintstudentssetmingapsbetweenroomchangesform.h"
25 
26 #include <QListWidget>
27 #include <QScrollBar>
28 #include <QAbstractItemView>
29 
ConstraintStudentsSetMinGapsBetweenRoomChangesForm(QWidget * parent)30 ConstraintStudentsSetMinGapsBetweenRoomChangesForm::ConstraintStudentsSetMinGapsBetweenRoomChangesForm(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 
46 	connect(constraintsListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(modifyConstraint()));
47 
48 	centerWidgetOnScreen(this);
49 	restoreFETDialogGeometry(this);
50 
51 	QSize tmp2=studentsComboBox->minimumSizeHint();
52 	Q_UNUSED(tmp2);
53 
54 	populateStudentsComboBox(studentsComboBox, QString(""), true);
55 
56 	this->filterChanged();
57 
58 	connect(studentsComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(filterChanged()));
59 }
60 
~ConstraintStudentsSetMinGapsBetweenRoomChangesForm()61 ConstraintStudentsSetMinGapsBetweenRoomChangesForm::~ConstraintStudentsSetMinGapsBetweenRoomChangesForm()
62 {
63 	saveFETDialogGeometry(this);
64 }
65 
filterOk(SpaceConstraint * ctr)66 bool ConstraintStudentsSetMinGapsBetweenRoomChangesForm::filterOk(SpaceConstraint* ctr)
67 {
68 	if(ctr->type==CONSTRAINT_STUDENTS_SET_MIN_GAPS_BETWEEN_ROOM_CHANGES){
69 		ConstraintStudentsSetMinGapsBetweenRoomChanges* c=(ConstraintStudentsSetMinGapsBetweenRoomChanges*) ctr;
70 		return c->studentsName==studentsComboBox->currentText() || studentsComboBox->currentText()=="";
71 	}
72 	else
73 		return false;
74 }
75 
filterChanged()76 void ConstraintStudentsSetMinGapsBetweenRoomChangesForm::filterChanged()
77 {
78 	this->visibleConstraintsList.clear();
79 	constraintsListWidget->clear();
80 	for(int i=0; i<gt.rules.spaceConstraintsList.size(); i++){
81 		SpaceConstraint* ctr=gt.rules.spaceConstraintsList[i];
82 		if(filterOk(ctr)){
83 			visibleConstraintsList.append(ctr);
84 			constraintsListWidget->addItem(ctr->getDescription(gt.rules));
85 		}
86 	}
87 
88 	if(constraintsListWidget->count()>0)
89 		constraintsListWidget->setCurrentRow(0);
90 	else
91 		this->constraintChanged(-1);
92 }
93 
constraintChanged(int index)94 void ConstraintStudentsSetMinGapsBetweenRoomChangesForm::constraintChanged(int index)
95 {
96 	if(index<0){
97 		currentConstraintTextEdit->setPlainText("");
98 		return;
99 	}
100 	assert(index<this->visibleConstraintsList.size());
101 	SpaceConstraint* ctr=this->visibleConstraintsList.at(index);
102 	assert(ctr!=nullptr);
103 	currentConstraintTextEdit->setPlainText(ctr->getDetailedDescription(gt.rules));
104 }
105 
addConstraint()106 void ConstraintStudentsSetMinGapsBetweenRoomChangesForm::addConstraint()
107 {
108 	AddConstraintStudentsSetMinGapsBetweenRoomChangesForm form(this);
109 	setParentAndOtherThings(&form, this);
110 	form.exec();
111 
112 	filterChanged();
113 
114 	constraintsListWidget->setCurrentRow(constraintsListWidget->count()-1);
115 }
116 
modifyConstraint()117 void ConstraintStudentsSetMinGapsBetweenRoomChangesForm::modifyConstraint()
118 {
119 	int valv=constraintsListWidget->verticalScrollBar()->value();
120 	int valh=constraintsListWidget->horizontalScrollBar()->value();
121 
122 	int i=constraintsListWidget->currentRow();
123 	if(i<0){
124 		QMessageBox::information(this, tr("FET information"), tr("Invalid selected constraint"));
125 		return;
126 	}
127 	SpaceConstraint* ctr=this->visibleConstraintsList.at(i);
128 
129 	ModifyConstraintStudentsSetMinGapsBetweenRoomChangesForm form(this, (ConstraintStudentsSetMinGapsBetweenRoomChanges*)ctr);
130 	setParentAndOtherThings(&form, this);
131 	form.exec();
132 
133 	filterChanged();
134 
135 	constraintsListWidget->verticalScrollBar()->setValue(valv);
136 	constraintsListWidget->horizontalScrollBar()->setValue(valh);
137 
138 	if(i>=constraintsListWidget->count())
139 		i=constraintsListWidget->count()-1;
140 
141 	if(i>=0)
142 		constraintsListWidget->setCurrentRow(i);
143 	else
144 		this->constraintChanged(-1);
145 }
146 
removeConstraint()147 void ConstraintStudentsSetMinGapsBetweenRoomChangesForm::removeConstraint()
148 {
149 	int i=constraintsListWidget->currentRow();
150 	if(i<0){
151 		QMessageBox::information(this, tr("FET information"), tr("Invalid selected constraint"));
152 		return;
153 	}
154 	SpaceConstraint* ctr=this->visibleConstraintsList.at(i);
155 	QString s;
156 	s=tr("Remove constraint?");
157 	s+="\n\n";
158 	s+=ctr->getDetailedDescription(gt.rules);
159 
160 	QListWidgetItem* item;
161 
162 	switch( LongTextMessageBox::confirmation( this, tr("FET confirmation"),
163 		s, tr("Yes"), tr("No"), 0, 0, 1 ) ){
164 	case 0: // The user clicked the OK button or pressed Enter
165 		gt.rules.removeSpaceConstraint(ctr);
166 
167 		visibleConstraintsList.removeAt(i);
168 		constraintsListWidget->setCurrentRow(-1);
169 		item=constraintsListWidget->takeItem(i);
170 		delete item;
171 
172 		break;
173 	case 1: // The user clicked the Cancel button or pressed Escape
174 		break;
175 	}
176 
177 	if(i>=constraintsListWidget->count())
178 		i=constraintsListWidget->count()-1;
179 	if(i>=0)
180 		constraintsListWidget->setCurrentRow(i);
181 	else
182 		this->constraintChanged(-1);
183 }
184