"Fossies" - the Fresh Open Source Software Archive

Member "fet-5.48.0/src/interface/addconstraintsubactivitiespreferredtimeslotsform.cpp" (21 Apr 2020, 10055 Bytes) of package /linux/privat/fet-5.48.0.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "addconstraintsubactivitiespreferredtimeslotsform.cpp" see the Fossies "Dox" file reference documentation.

    1 /***************************************************************************
    2                           addconstraintsubactivitiespreferredtimeslotsform.cpp  -  description
    3                              -------------------
    4     begin                : 2008
    5     copyright            : (C) 2008 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 <Qt>
   19 
   20 #include <QMessageBox>
   21 
   22 #include "tablewidgetupdatebug.h"
   23 
   24 #include "longtextmessagebox.h"
   25 
   26 #include "addconstraintsubactivitiespreferredtimeslotsform.h"
   27 #include "timeconstraint.h"
   28 
   29 #include <QHeaderView>
   30 #include <QTableWidget>
   31 #include <QTableWidgetItem>
   32 
   33 #include <QBrush>
   34 #include <QColor>
   35 
   36 #define YES     (QString(" "))
   37 #define NO      (QString("X"))
   38 
   39 AddConstraintSubactivitiesPreferredTimeSlotsForm::AddConstraintSubactivitiesPreferredTimeSlotsForm(QWidget* parent): QDialog(parent)
   40 {
   41     setupUi(this);
   42 
   43     durationCheckBox->setChecked(false);
   44     durationSpinBox->setEnabled(false);
   45     durationSpinBox->setMinimum(1);
   46     durationSpinBox->setMaximum(gt.rules.nHoursPerDay);
   47     durationSpinBox->setValue(1);
   48 
   49     addConstraintPushButton->setDefault(true);
   50 
   51     connect(preferredTimesTable, SIGNAL(itemClicked(QTableWidgetItem*)), this, SLOT(itemClicked(QTableWidgetItem*)));
   52     connect(addConstraintPushButton, SIGNAL(clicked()), this, SLOT(addConstraint()));
   53     connect(closePushButton, SIGNAL(clicked()), this, SLOT(close()));
   54     connect(setAllAllowedPushButton, SIGNAL(clicked()), this, SLOT(setAllSlotsAllowed()));
   55     connect(setAllNotAllowedPushButton, SIGNAL(clicked()), this, SLOT(setAllSlotsNotAllowed()));
   56 
   57     centerWidgetOnScreen(this);
   58     restoreFETDialogGeometry(this);
   59 
   60     QSize tmp1=teachersComboBox->minimumSizeHint();
   61     Q_UNUSED(tmp1);
   62     QSize tmp2=studentsComboBox->minimumSizeHint();
   63     Q_UNUSED(tmp2);
   64     QSize tmp3=subjectsComboBox->minimumSizeHint();
   65     Q_UNUSED(tmp3);
   66     QSize tmp4=activityTagsComboBox->minimumSizeHint();
   67     Q_UNUSED(tmp4);
   68 
   69     updateTeachersComboBox();
   70     updateStudentsComboBox();
   71     updateSubjectsComboBox();
   72     updateActivityTagsComboBox();
   73 
   74     preferredTimesTable->setRowCount(gt.rules.nHoursPerDay);
   75     preferredTimesTable->setColumnCount(gt.rules.nDaysPerWeek);
   76 
   77     for(int j=0; j<gt.rules.nDaysPerWeek; j++){
   78         QTableWidgetItem* item=new QTableWidgetItem(gt.rules.daysOfTheWeek[j]);
   79         preferredTimesTable->setHorizontalHeaderItem(j, item);
   80     }
   81     for(int i=0; i<gt.rules.nHoursPerDay; i++){
   82         QTableWidgetItem* item=new QTableWidgetItem(gt.rules.hoursOfTheDay[i]);
   83         preferredTimesTable->setVerticalHeaderItem(i, item);
   84     }
   85 
   86     for(int i=0; i<gt.rules.nHoursPerDay; i++)
   87         for(int j=0; j<gt.rules.nDaysPerWeek; j++){
   88             QTableWidgetItem* item=new QTableWidgetItem(YES);
   89             item->setTextAlignment(Qt::AlignCenter);
   90             item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
   91             colorItem(item);
   92             if(SHOW_TOOLTIPS_FOR_CONSTRAINTS_WITH_TABLES)
   93                 item->setToolTip(gt.rules.daysOfTheWeek[j]+QString("\n")+gt.rules.hoursOfTheDay[i]);
   94             preferredTimesTable->setItem(i, j, item);
   95         }
   96         
   97     preferredTimesTable->resizeRowsToContents();
   98 
   99     connect(preferredTimesTable->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(horizontalHeaderClicked(int)));
  100     connect(preferredTimesTable->verticalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(verticalHeaderClicked(int)));
  101 
  102     preferredTimesTable->setSelectionMode(QAbstractItemView::NoSelection);
  103             
  104     componentNumberSpinBox->setMinimum(1);
  105     componentNumberSpinBox->setMaximum(MAX_SPLIT_OF_AN_ACTIVITY);
  106     componentNumberSpinBox->setValue(1);
  107 
  108     tableWidgetUpdateBug(preferredTimesTable);
  109     
  110     setStretchAvailabilityTableNicely(preferredTimesTable);
  111 }
  112 
  113 AddConstraintSubactivitiesPreferredTimeSlotsForm::~AddConstraintSubactivitiesPreferredTimeSlotsForm()
  114 {
  115     saveFETDialogGeometry(this);
  116 }
  117 
  118 void AddConstraintSubactivitiesPreferredTimeSlotsForm::colorItem(QTableWidgetItem* item)
  119 {
  120     if(USE_GUI_COLORS){
  121 #if QT_VERSION >= QT_VERSION_CHECK(5,14,0)
  122         if(item->text()==YES)
  123             item->setBackground(QBrush(QColorConstants::DarkGreen));
  124         else
  125             item->setBackground(QBrush(QColorConstants::DarkRed));
  126         item->setForeground(QBrush(QColorConstants::LightGray));
  127 #else
  128         if(item->text()==YES)
  129             item->setBackground(QBrush(Qt::darkGreen));
  130         else
  131             item->setBackground(QBrush(Qt::darkRed));
  132         item->setForeground(QBrush(Qt::lightGray));
  133 #endif
  134     }
  135 }
  136 
  137 void AddConstraintSubactivitiesPreferredTimeSlotsForm::horizontalHeaderClicked(int col)
  138 {
  139     if(col>=0 && col<gt.rules.nDaysPerWeek){
  140         QString s=preferredTimesTable->item(0, col)->text();
  141         if(s==YES)
  142             s=NO;
  143         else{
  144             assert(s==NO);
  145             s=YES;
  146         }
  147 
  148         for(int row=0; row<gt.rules.nHoursPerDay; row++){
  149             preferredTimesTable->item(row, col)->setText(s);
  150             colorItem(preferredTimesTable->item(row,col));
  151         }
  152         tableWidgetUpdateBug(preferredTimesTable);
  153     }
  154 }
  155 
  156 void AddConstraintSubactivitiesPreferredTimeSlotsForm::verticalHeaderClicked(int row)
  157 {
  158     if(row>=0 && row<gt.rules.nHoursPerDay){
  159         QString s=preferredTimesTable->item(row, 0)->text();
  160         if(s==YES)
  161             s=NO;
  162         else{
  163             assert(s==NO);
  164             s=YES;
  165         }
  166     
  167         for(int col=0; col<gt.rules.nDaysPerWeek; col++){
  168             preferredTimesTable->item(row, col)->setText(s);
  169             colorItem(preferredTimesTable->item(row,col));
  170         }
  171         tableWidgetUpdateBug(preferredTimesTable);
  172     }
  173 }
  174 
  175 void AddConstraintSubactivitiesPreferredTimeSlotsForm::setAllSlotsAllowed()
  176 {
  177     for(int i=0; i<gt.rules.nHoursPerDay; i++)
  178         for(int j=0; j<gt.rules.nDaysPerWeek; j++){
  179             preferredTimesTable->item(i, j)->setText(YES);
  180             colorItem(preferredTimesTable->item(i,j));
  181         }
  182     tableWidgetUpdateBug(preferredTimesTable);
  183 }
  184 
  185 void AddConstraintSubactivitiesPreferredTimeSlotsForm::setAllSlotsNotAllowed()
  186 {
  187     for(int i=0; i<gt.rules.nHoursPerDay; i++)
  188         for(int j=0; j<gt.rules.nDaysPerWeek; j++){
  189             preferredTimesTable->item(i, j)->setText(NO);
  190             colorItem(preferredTimesTable->item(i,j));
  191         }
  192     tableWidgetUpdateBug(preferredTimesTable);
  193 }
  194 
  195 void AddConstraintSubactivitiesPreferredTimeSlotsForm::itemClicked(QTableWidgetItem* item)
  196 {
  197     QString s=item->text();
  198     if(s==YES)
  199         s=NO;
  200     else{
  201         assert(s==NO);
  202         s=YES;
  203     }
  204     item->setText(s);
  205     colorItem(item);
  206 
  207     tableWidgetUpdateBug(preferredTimesTable);
  208 }
  209 
  210 void AddConstraintSubactivitiesPreferredTimeSlotsForm::updateTeachersComboBox(){
  211     teachersComboBox->clear();
  212     teachersComboBox->addItem("");
  213     for(int i=0; i<gt.rules.teachersList.size(); i++){
  214         Teacher* t=gt.rules.teachersList[i];
  215         teachersComboBox->addItem(t->name);
  216     }
  217 }
  218 
  219 void AddConstraintSubactivitiesPreferredTimeSlotsForm::updateStudentsComboBox(){
  220     populateStudentsComboBox(studentsComboBox, QString(""), true);
  221 }
  222 
  223 void AddConstraintSubactivitiesPreferredTimeSlotsForm::updateSubjectsComboBox(){
  224     subjectsComboBox->clear();
  225     subjectsComboBox->addItem("");
  226     for(int i=0; i<gt.rules.subjectsList.size(); i++){
  227         Subject* s=gt.rules.subjectsList[i];
  228         subjectsComboBox->addItem(s->name);
  229     }
  230 }
  231 
  232 void AddConstraintSubactivitiesPreferredTimeSlotsForm::updateActivityTagsComboBox(){
  233     activityTagsComboBox->clear();
  234     activityTagsComboBox->addItem("");
  235     for(int i=0; i<gt.rules.activityTagsList.size(); i++){
  236         ActivityTag* s=gt.rules.activityTagsList[i];
  237         activityTagsComboBox->addItem(s->name);
  238     }
  239 }
  240 
  241 void AddConstraintSubactivitiesPreferredTimeSlotsForm::addConstraint()
  242 {
  243     TimeConstraint *ctr=NULL;
  244 
  245     int duration=-1;
  246     if(durationCheckBox->isChecked()){
  247         assert(durationSpinBox->isEnabled());
  248         duration=durationSpinBox->value();
  249     }
  250 
  251     double weight;
  252     QString tmp=weightLineEdit->text();
  253     weight_sscanf(tmp, "%lf", &weight);
  254     if(weight<0.0 || weight>100.0){
  255         QMessageBox::warning(this, tr("FET information"),
  256             tr("Invalid weight (percentage)"));
  257         return;
  258     }
  259 
  260     QString teacher=teachersComboBox->currentText();
  261     if(teacher!="")
  262         assert(gt.rules.searchTeacher(teacher)>=0);
  263 
  264     QString students=studentsComboBox->currentText();
  265     if(students!="")
  266         assert(gt.rules.searchStudentsSet(students)!=NULL);
  267 
  268     QString subject=subjectsComboBox->currentText();
  269     if(subject!="")
  270         assert(gt.rules.searchSubject(subject)>=0);
  271         
  272     QString activityTag=activityTagsComboBox->currentText();
  273     if(activityTag!="")
  274         assert(gt.rules.searchActivityTag(activityTag)>=0);
  275     
  276     QList<int> days_L;
  277     QList<int> hours_L;
  278     int n=0;
  279     for(int j=0; j<gt.rules.nDaysPerWeek; j++)
  280         for(int i=0; i<gt.rules.nHoursPerDay; i++)
  281             if(preferredTimesTable->item(i, j)->text()==YES){
  282                 days_L.append(j);
  283                 hours_L.append(i);
  284                 n++;
  285             }
  286 
  287     if(n<=0){
  288         int t=QMessageBox::question(this, tr("FET question"),
  289          tr("Warning: 0 slots selected. Are you sure?"),
  290          QMessageBox::Yes, QMessageBox::Cancel);
  291         
  292         if(t==QMessageBox::Cancel)
  293                 return;
  294     }
  295 
  296     ctr=new ConstraintSubactivitiesPreferredTimeSlots(weight, componentNumberSpinBox->value(), teacher, students, subject, activityTag, duration, n, days_L, hours_L);
  297 
  298     bool tmp2=gt.rules.addTimeConstraint(ctr);
  299     if(tmp2){
  300         QString s=tr("Constraint added:");
  301         s+="\n\n";
  302         s+=ctr->getDetailedDescription(gt.rules);
  303         LongTextMessageBox::information(this, tr("FET information"), s);
  304     }
  305     else{
  306         QMessageBox::warning(this, tr("FET information"),
  307             tr("Constraint NOT added - duplicate?", "'Duplicate' means that the constraint is identical to an existing constraint and cannot be added."));
  308         delete ctr;
  309     }
  310 }
  311 
  312 void AddConstraintSubactivitiesPreferredTimeSlotsForm::on_durationCheckBox_toggled()
  313 {
  314     durationSpinBox->setEnabled(durationCheckBox->isChecked());
  315 }
  316 
  317 #undef YES
  318 #undef NO