defuze.me  Client
eventwidget.cpp
00001 #include "eventwidget.hpp"
00002 
00003 using namespace Scheduler;
00004 
00005 EventWidget::EventWidget(SchedulerPlugin *scheduler, QWidget *parent) :
00006     ModuleWidget(parent), scheduler(scheduler)
00007 {
00008     // @TODO Need to be fixed, need to restart application when new playlist(s) are added
00009     this->playlistsPlugin   = scheduler->getPlugins()->cast<Playlists::PlaylistsPlugin>("playlists");
00010     this->playlists         = this->playlistsPlugin->getPlaylistList();
00011 
00012     setupUi(this);
00013 
00014     this->event = false;
00015 
00016     this->initialize();
00017 }
00018 
00019 void    EventWidget::setUpdate(EventModel *updateEvent)
00020 {
00021     this->clear();
00022 
00023     this->event = updateEvent;
00024 
00025     this->title->setText(updateEvent->getTitle());
00026     this->description->setText(updateEvent->getDescription());
00027     this->day->setCurrentIndex(updateEvent->getDay());
00028     this->start->setTime(QTime().addSecs(updateEvent->getStartTime() * 60));
00029     this->duration->setTime(QTime().addSecs(updateEvent->getDuration() * 60));
00030 
00031     int colorID = (updateEvent->getColorID() != 0) ? updateEvent->getColorID() : 1 ;
00032     this->colors->at(colorID - 1)->setChecked(true);
00033 
00034     QList<int> items = updateEvent->getPlaylists();
00035 
00036     for (int index = 0; index < this->playlistsWidget->count(); index += 1)
00037     {
00038         int widgetItemID    = ((EventPlaylistItem*)this->playlistsWidget->item(index))->id;
00039 
00040         if (items.contains(widgetItemID))
00041             ((EventPlaylistItem*)this->playlistsWidget->item(index))->setSelected(true);
00042     }
00043 
00044     this->submitEventButton->setText(tr("Update"));
00045     this->deleteEventButton->setEnabled(true);
00046 }
00047 
00048 void    EventWidget::initialize()
00049 {
00050     this->colors    = new QList<EventColor*>();
00051 
00052     QSqlQuery   query("SELECT id, color FROM colors");
00053 
00054     if (! query.exec())
00055         qDebug() << QString("Can't get colors : %1").arg(query.lastError().text());
00056     else
00057     {
00058         while (query.next())
00059         {
00060             EventColor *color = new EventColor(this, query.value(0).toInt(), query.value(1).toString());
00061             connect(color, SIGNAL(clicked()), color, SLOT(on_colorPicker_clicked()));
00062             this->colorLayout->addWidget(color, 0, this->colorLayout->columnCount(), 1, 1);
00063             this->colors->append(color);
00064         }
00065     }
00066 
00067     QStringList days;
00068     days << tr("Monday") << tr("Tuesday") << tr("Wednesday") << tr("Thursday") << tr("Friday") << tr("Saturday") << tr("Sunday");
00069 
00070     this->day->addItems(days);
00071 
00072     this->playlistsWidget   = new EventPlaylist(this);
00073     this->playlistsLayout->addWidget(this->playlistsWidget, 0, 0);
00074     this->playlistsWidget->loadPlaylists();
00075 }
00076 
00077 void    EventWidget::clear()
00078 {
00079     this->title->clear();
00080     this->description->clear();
00081     this->day->setCurrentIndex(0);
00082     this->start->setTime(QTime(0, 0, 0, 0));
00083     this->duration->setTime(QTime(0, 0, 0, 0));
00084 
00085     for (int index = 0; index < this->colors->count(); index += 1)
00086         this->colors->at(index)->setChecked(false);
00087 
00088     for (int index = 0; index < this->playlistsWidget->count(); index += 1)
00089         ((EventPlaylistItem*)this->playlistsWidget->item(index))->setSelected(false);
00090 
00091     this->submitEventButton->setText(tr("Add event"));
00092     this->deleteEventButton->setEnabled(false);
00093 
00094     if (this->event)
00095     {
00096         this->event->focusOut();
00097         this->event = false;
00098     }
00099 }
00100 
00101 bool    EventWidget::check()
00102 {
00103     if (this->title->text().length() <= 0)
00104         return false;
00105     for (int index = 0; index < this->colors->count(); index += 1)
00106     {
00107         if (this->colors->at(index)->isChecked())
00108             return true;
00109     }
00110     return false;
00111 }
00112 
00113 void    EventWidget::on_submitEventButton_clicked()
00114 {
00115     if ( ! this->check())
00116         return;
00117 
00118     bool update         = false;
00119     int startTime       = this->start->time().hour() * 60 + this->start->time().minute();
00120     int durationTime    = this->duration->time().hour() * 60 + this->duration->time().minute();
00121 
00122     if (this->event)
00123     {
00124         event->title = title->text();
00125         event->description = description->text();
00126         event->day = day->currentIndex();
00127         event->start = startTime;
00128         event->duration = durationTime;
00129         update = true;
00130     }
00131     else
00132     {
00133         this->event = new EventModel(scheduler, title->text(), description->text(),
00134                                      day->currentIndex(), startTime, durationTime);
00135         this->scheduler->getModel()->getEvents()->append(event);
00136     }
00137 
00138     event->save();
00139     int colorId = this->colors->at(0)->getId();
00140 
00141     for (int index = 0; index < this->colors->count(); index += 1)
00142     {
00143         if (this->colors->at(index)->isChecked())
00144         {
00145             colorId     = this->colors->at(index)->getId();
00146             continue;
00147         }
00148     }
00149 
00150     event->saveColor(colorId);
00151     event->savePlaylists(this->playlistsWidget->getSelectedPlaylists());
00152     this->scheduler->getWidget()->displayEvent(event);
00153 
00154     if (update)
00155         emit updateEvent(event);
00156     else
00157         emit newEvent(event);
00158 
00159 
00160     this->clear();
00161 }
00162 
00163 void    EventWidget::on_deleteEventButton_clicked()
00164 {
00165     emit removeEvent(this->event);
00166 
00167     this->event->remove();
00168     delete this->event;
00169     this->event = false;
00170     this->clear();
00171 }