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