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