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