Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

TimeSlotPage_wdgt.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2006 by Graeme Foster                                   *
00003  *   email    foster.graeme@gmail.com                                      *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include "TimeSlotPage_wdgt.h"
00021 #include "TimeSlotItem.h"
00022 
00023 TimeSlotPageWidget::TimeSlotPageWidget(ControlPanel * window
00024                                            ,QWidget * widgetParent
00025                                       ,QBoxLayout * layoutParent
00026                                       ,dbListManager * dataLists)
00027 : RootPageWidget(window, widgetParent, layoutParent, dataLists)
00028 {
00029         wdgt.setupUi(this);
00030         wdgt.cbxInst->init(dataLists);
00031         createActions();
00032         selectedTimeSlot = 0;
00033         KeyModifier = Qt::NoModifier;
00034 }
00035 
00036 void TimeSlotPageWidget::createActions()
00037 {
00038         wdgt.grpWeekDay->installEventFilter(this);
00039         wdgt.grpWeekend->installEventFilter(this);
00040         
00041         connect (wdgt.cbxInst, SIGNAL(currentIndexChanged(QString)), this, SLOT(cbxInstSelected(QString)));
00042         connect (wdgt.btnAddSlots, SIGNAL(clicked()), this, SLOT(addSlotsClicked()));
00043         connect (wdgt.tblTimeSlots, SIGNAL(clicked(QModelIndex)), this, SLOT(TimeSlotClicked(QModelIndex)));
00044 }
00045 
00046 void TimeSlotPageWidget::toggleWeekDay(bool)
00047 {
00048         toggleCheckBox(wdgt.chkMonday);
00049         toggleCheckBox(wdgt.chkTuesday);
00050         toggleCheckBox(wdgt.chkWednesday);
00051         toggleCheckBox(wdgt.chkThursday);
00052         toggleCheckBox(wdgt.chkFriday);
00053 }
00054 
00055 void TimeSlotPageWidget::toggleWeekend(bool)
00056 {
00057         toggleCheckBox(wdgt.chkSaturday);
00058         toggleCheckBox(wdgt.chkSunday);
00059 }
00060 
00061 void TimeSlotPageWidget::toggleCheckBox(QCheckBox * chk)
00062 {
00063         if (chk->checkState() == Qt::Unchecked)
00064                 chk->setCheckState(Qt::Checked);
00065         else
00066                 chk->setCheckState(Qt::Unchecked);
00067 }
00068 
00069 void TimeSlotPageWidget::cbxInstSelected(QString name)
00070 {
00071         instObj = dataLists->getInstFromName(name);
00072         if (instObj == 0)
00073                 wdgt.tblTimeSlots->setModel(0);
00074         else
00075         {
00076                 TimeSlotModel * model = new TimeSlotModel(dataLists, instObj);
00077                 dataLists->registerModel(model
00078                                         ,dbListManager::regTimeSlot
00079                                         );
00080                 wdgt.tblTimeSlots->setModel(model);
00081         }
00082 }
00083 
00084 void TimeSlotPageWidget::addSlotsClicked()
00085 {
00086         QString msg;
00087         mainWindow->statusBar()->showMessage("Adding slots...");
00088         if (validateData(&msg))
00089                 addSlots();
00090         else
00091                 mainWindow->statusBar()->showMessage(msg);
00092 }
00093 
00094 void TimeSlotPageWidget::TimeSlotClicked(const QModelIndex & index)
00095 {
00096         QString msg;
00097         selectedTimeSlot = static_cast<DailyTimeSlotItem*>(index.internalPointer());
00098         TimeSlot * tsObj = selectedTimeSlot->getItemData(index.column());
00099         TimeSlotModel * tsModel = static_cast<TimeSlotModel*>(wdgt.tblTimeSlots->model());
00100         Qt::KeyboardModifiers mod = QApplication::keyboardModifiers ();
00101         if (mod == Qt::ControlModifier)
00102         {
00103                 if (tsObj == 0)
00104                 {
00105                         msg = tsModel->cellHeading(index.row(), index.column());
00106                         QString day = tsModel->DayHeading(index.row());
00107                         int duration = 0;
00108                         QTime startTime = tsModel->StartTimeHeading(index.column());
00109                         QTime finishTime = tsModel->FinishTimeHeading(index.column());
00110                         duration = startTime.secsTo(finishTime) / 60;
00111                         int result = addTimeSlot(instObj, TimeSlot::getDays(day), duration, startTime);
00112                         if (result == -30)
00113                                 msg = tr("The slot overlaps with another and cannot be added");
00114                 }
00115                 else
00116                         selectedTimeSlot->deleteItem(index.column());
00117         }
00118         else
00119         {
00120                 if (tsObj == 0)
00121                         msg = "Ctrl + Click to add this timeslot";
00122                 else
00123                         msg = "Ctrl + Click to remove the timeslot " + tsObj->getDescription();
00124         }
00125         mainWindow->statusBar()->showMessage(msg);
00126 }
00127 
00128 bool TimeSlotPageWidget::validateData(QString *msg)
00129 {
00130         bool valid = true;
00131         valid = validateDay(msg);
00132         
00133         QString name = wdgt.cbxInst->currentText();
00134         instObj = dataLists->getInstFromName(name);
00135         if (instObj == 0)
00136         {
00137                 if (!msg->isNull())
00138                         msg->append("; ");
00139                 msg->append("You must select an institute");
00140                 valid = false;
00141         }
00142         
00143         duration = wdgt.edtDuration->text().toInt();
00144         if (duration == 0)
00145         {
00146                 if (!msg->isNull())
00147                         msg->append("; ");
00148                 msg->append("The duration is missing");
00149                 valid = false;
00150         }
00151         int startTimeHour = wdgt.edtStartTimeHour->text().toInt();
00152         int startTimeMin  = wdgt.edtStartTimeMin->text().toInt();
00153         startTime = QTime(startTimeHour, startTimeMin);
00154         if (startTimeHour == 0 && startTimeMin == 0)
00155         {
00156                 if (!msg->isNull())
00157                         msg->append("; ");
00158                 msg->append("The start time is missing");
00159                 valid = false;
00160         }
00161         if (!startTime.isValid())
00162         {
00163                 if (!msg->isNull())
00164                         msg->append("; ");
00165                 msg->append("The start time is not valid");
00166                 valid = false;
00167         }
00168                 
00169         repeat = wdgt.edtRepeat->text().toInt();
00170         if (repeat == 0)
00171         {
00172                 if (!msg->isNull())
00173                         msg->append("; ");
00174                 msg->append("The times to repeat is missing");
00175                 valid = false;
00176         }
00177         return valid;
00178 }
00179 
00180 bool TimeSlotPageWidget::validateDay(QString *msg)
00181 {
00182         selectedDays.clear();
00183         if (wdgt.chkMonday->checkState()==Qt::Checked)
00184                 selectedDays << TimeSlot::Monday;
00185         if (wdgt.chkTuesday->checkState()==Qt::Checked)
00186                 selectedDays << TimeSlot::Tuesday;
00187         if (wdgt.chkWednesday->checkState()==Qt::Checked)
00188                 selectedDays << TimeSlot::Wednesday;
00189         if (wdgt.chkThursday->checkState()==Qt::Checked)
00190                 selectedDays << TimeSlot::Thursday;
00191         if (wdgt.chkFriday->checkState()==Qt::Checked)
00192                 selectedDays << TimeSlot::Friday;
00193         if (wdgt.chkSaturday->checkState()==Qt::Checked)
00194                 selectedDays << TimeSlot::Saturday;
00195         if (wdgt.chkSunday->checkState()==Qt::Checked)
00196                 selectedDays << TimeSlot::Sunday;
00197         if (selectedDays.empty())
00198         {
00199                 if (!msg->isNull())
00200                         msg->append("; ");
00201                 msg->append("Please select at least one day");
00202         }
00203         return !selectedDays.isEmpty();
00204 }
00205 
00206 void TimeSlotPageWidget::addSlots()
00207 {
00208         int slotCount = 0;
00209         int result;
00210         int overlap = 0;
00211         QString msg;
00212         QSetIterator<TimeSlot::days> itr(selectedDays);
00213         while (itr.hasNext())
00214         {
00215                 TimeSlot::days thisDay = itr.next();
00216                 for (int i=0; i<repeat; i++)
00217                 {
00218                         addTimeSlot(instObj, thisDay, duration, startTime,i);
00219                         if (result == -30)
00220                                 overlap++;
00221                         if (result >= 0)
00222                                 slotCount++;
00223                 }
00224         }
00225         msg = tr("Slots added ") + QString::number(slotCount);
00226         if (overlap== 1)
00227                 msg += tr(". An overlapping slot was ignored.");
00228         if (overlap > 1)
00229                 msg += tr(". ") + QString::number(overlap) + tr(" overlapping slots were ignored.");
00230         mainWindow->statusBar()->showMessage(msg);
00231 }
00232 
00233 int TimeSlotPageWidget::addTimeSlot(Institute * inst, TimeSlot::days theDay, int duration, QTime startTime, int offset)
00234 {
00235         TimeSlot * timeslot;
00236         timeslot = new TimeSlot(DataClass::NOT_ON_DB
00237                                ,inst
00238                                ,theDay
00239                                ,duration
00240                                ,startTime.addSecs(offset*duration*60));
00241         int result = timeslot->save(TimeSlot::insertSQL, TimeSlot::updateSQL, TimeSlot::errorSQL);
00242         if (result >= 0)
00243         {
00244                 TimeSlotItem * tsiObj = new TimeSlotItem(timeslot);
00245                 QModelIndex index = QModelIndex();
00246                 dataLists->insert(tsiObj, dataLists->TimeSlotList(),index, tsiObj->getItemData()->dcCompareCI);
00247         }
00248         update(); // get the widget to repaint itself.
00249         return result;
00250 }
00251 
00252 bool TimeSlotPageWidget::eventFilter(QObject * target, QEvent * event)
00253 {
00254         if (event->type() == QEvent::MouseButtonPress)
00255         {
00256                 if (target == wdgt.grpWeekend)
00257                         toggleWeekend(true);
00258                 if (target == wdgt.grpWeekDay)
00259                         toggleWeekDay(true);
00260         }
00261         return  QWidget::eventFilter(target, event);
00262 }

Generated on Thu Apr 6 16:27:17 2006 for time-table by  doxygen 1.4.4