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