1 /***************************************************************************
2                           constraintstudentsminhoursdailyform.cpp  -  description
3                              -------------------
4     begin                : July 19, 2007
5     copyright            : (C) 2007 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 "constraintstudentsminhoursdailyform.h"
23 #include "addconstraintstudentsminhoursdailyform.h"
24 #include "modifyconstraintstudentsminhoursdailyform.h"
25 
26 #include "helponstudentsminhoursdaily.h"
27 
28 #include <QListWidget>
29 #include <QScrollBar>
30 #include <QAbstractItemView>
31 
ConstraintStudentsMinHoursDailyForm(QWidget * parent)32 ConstraintStudentsMinHoursDailyForm::ConstraintStudentsMinHoursDailyForm(QWidget* parent): QDialog(parent)
33 {
34 	setupUi(this);
35 
36 	if(gt.rules.mode!=MORNINGS_AFTERNOONS)
37 		label->setText(tr("IMPORTANT: allow empty days for students is a nonstandard option. Only select it if your institution permits that"
38 		 " and if a timetable with empty days for students exists. Otherwise do not select it, for a good performance of generation."
39 		 " Press Help button for more information."));
40 	else
41 		label->setText(tr("If the user selects not allowed empty days, it is for real days (for the mornings-afternoons mode), also respecting minimum"
42 		 " hours daily for FET days (half days) with allow empty FET days"));
43 
44 	currentConstraintTextEdit->setReadOnly(true);
45 
46 	modifyConstraintPushButton->setDefault(true);
47 
48 	constraintsListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
49 
50 	connect(constraintsListWidget, SIGNAL(currentRowChanged(int)), this, SLOT(constraintChanged(int)));
51 	connect(addConstraintPushButton, SIGNAL(clicked()), this, SLOT(addConstraint()));
52 	connect(closePushButton, SIGNAL(clicked()), this, SLOT(close()));
53 	connect(removeConstraintPushButton, SIGNAL(clicked()), this, SLOT(removeConstraint()));
54 	connect(modifyConstraintPushButton, SIGNAL(clicked()), this, SLOT(modifyConstraint()));
55 	connect(constraintsListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(modifyConstraint()));
56 
57 	connect(helpPushButton, SIGNAL(clicked()), this, SLOT(help()));
58 
59 	centerWidgetOnScreen(this);
60 	restoreFETDialogGeometry(this);
61 
62 	this->filterChanged();
63 }
64 
~ConstraintStudentsMinHoursDailyForm()65 ConstraintStudentsMinHoursDailyForm::~ConstraintStudentsMinHoursDailyForm()
66 {
67 	saveFETDialogGeometry(this);
68 }
69 
filterOk(TimeConstraint * ctr)70 bool ConstraintStudentsMinHoursDailyForm::filterOk(TimeConstraint* ctr)
71 {
72 	if(ctr->type==CONSTRAINT_STUDENTS_MIN_HOURS_DAILY)
73 		return true;
74 	else
75 		return false;
76 }
77 
filterChanged()78 void ConstraintStudentsMinHoursDailyForm::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 ConstraintStudentsMinHoursDailyForm::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 ConstraintStudentsMinHoursDailyForm::addConstraint()
109 {
110 	AddConstraintStudentsMinHoursDailyForm form(this);
111 	setParentAndOtherThings(&form, this);
112 	form.exec();
113 
114 	filterChanged();
115 
116 	constraintsListWidget->setCurrentRow(constraintsListWidget->count()-1);
117 }
118 
modifyConstraint()119 void ConstraintStudentsMinHoursDailyForm::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 	ModifyConstraintStudentsMinHoursDailyForm form(this, (ConstraintStudentsMinHoursDaily*)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 ConstraintStudentsMinHoursDailyForm::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 ConstraintStudentsMinHoursDailyForm::help()
188 {
189 	HelpOnStudentsMinHoursDaily::help(this);
190 }
191