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