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