defuze.me  Client
eventmodel.cpp
00001 #include "eventmodel.hpp"
00002 
00003 using namespace Scheduler;
00004 
00005 QMap<int, QString>  EventModel::colors;
00006 
00007 EventModel::EventModel(SchedulerPlugin *scheduler, QString title, QString description, short day, int start, int duration, int id) :
00008     scheduler(scheduler), id(id), title(title), description(description), day(day), start(start), duration(duration), color(-1)
00009 {
00010     this->setText(title);
00011     this->setToolTip(this->description);
00012 
00013     // Various style properties
00014     this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
00015     this->setAlignment(Qt::AlignCenter);
00016     this->setWordWrap(true);
00017     this->setStyle();
00018 
00019     this->connect(this, SIGNAL(clicked()), this, SLOT(eventClicked()));
00020 
00021     return;
00022 }
00023 
00024 int EventModel::save()
00025 {
00026     QSqlQuery query;
00027 
00028     if ( ! id)
00029     {
00030         query.prepare("INSERT INTO events (title, description, day, start, duration) "
00031                       "VALUES (:title, :description, :day, :start, :duration)");
00032     }
00033     else
00034     {
00035         query.prepare("UPDATE events "
00036                       "SET title = :title, description = :description, day = :day, start = :start, duration = :duration "
00037                       "WHERE id = :id");
00038     }
00039 
00040     query.bindValue(":id", id);
00041     query.bindValue(":title", title);
00042     query.bindValue(":description", description);
00043     query.bindValue(":day", day);
00044     query.bindValue(":start", start);
00045     query.bindValue(":duration", duration);
00046 
00047     if (!query.exec())
00048     {
00049         qDebug() << QString("Can't save event: %1").arg(query.lastError().text());
00050         return 0;
00051     }
00052 
00053     if ( ! id)
00054         return this->id = query.lastInsertId().toInt();
00055     else
00056         return id;
00057 }
00058 
00059 void    EventModel::saveColor(int color)
00060 {
00061     QSqlQuery query("INSERT OR REPLACE INTO colors_events"
00062                     "(color_id, event_id) VALUES (:color, :event)");
00063 
00064     query.bindValue(":color", color);
00065     query.bindValue(":event", id);
00066 
00067     if (query.exec())
00068         this->color = color;
00069     else
00070         qDebug() << QString("Can't save color : %1").arg(query.lastError().text());
00071 }
00072 
00073 void    EventModel::savePlaylists(QList<int> playlists)
00074 {
00075     QSqlQuery query("DELETE FROM events_playlists "
00076                     "WHERE event_id = :id");
00077 
00078     query.bindValue(":id", this->id);
00079 
00080     if ( ! query.exec())
00081         qDebug() << QString("Can't delete playlists associations : %1").arg(query.lastError().text());
00082 
00083     query.clear();
00084 
00085     for (int index = 0; index < playlists.count(); index += 1)
00086     {
00087         query.prepare("INSERT INTO events_playlists (event_id, playlist_id) "
00088                       "VALUES (:event, :playlist)");
00089 
00090         query.bindValue(":event", this->id);
00091         query.bindValue(":playlist", playlists.at(index));
00092 
00093         if (!query.exec())
00094             qDebug() << QString("Can't save playlists associations : %1").arg(query.lastError().text());
00095 
00096         query.clear();
00097     }
00098 
00099     return;
00100 }
00101 
00102 int EventModel::getId() const
00103 {
00104     return (this->id) ? this->id : 0 ;
00105 }
00106 
00107 QString EventModel::getTitle() const
00108 {
00109     return this->title;
00110 }
00111 
00112 QString EventModel::getDescription() const
00113 {
00114     return this->description;
00115 }
00116 
00117 short   EventModel::getDay() const
00118 {
00119     return this->day;
00120 }
00121 
00122 int EventModel::getStartTime() const
00123 {
00124     return this->start;
00125 }
00126 
00127 int EventModel::getDuration() const
00128 {
00129     return this->duration;
00130 }
00131 
00132 QDateTime   EventModel::nextInstance() const
00133 {
00134     QDate   date = QDate::currentDate();
00135     QTime   curTime = QTime::currentTime();
00136     QTime   time(start / 60, start % 60);
00137 
00138     // Search for the good day
00139     if (date.dayOfWeek() == day + 1 && time <= curTime)
00140         date = date.addDays(7);
00141     while (date.dayOfWeek() != day + 1)
00142         date = date.addDays(1);
00143 
00144     return QDateTime(date, time);
00145 }
00146 
00147 const QString&  EventModel::getColor()
00148 {
00149     return  getColorByID(getColorID());
00150 }
00151 
00152 int EventModel::getColorID()
00153 {
00154     if (color >= 0)
00155         return color;
00156     QSqlQuery query("SELECT color_id FROM colors_events WHERE event_id = :id");
00157 
00158     query.bindValue(":id", id);
00159 
00160     if (!query.exec())
00161         qDebug() << QString("Can't select : %1").arg(query.lastError().text());
00162     else if (query.next())
00163         return query.value(0).toInt();
00164 
00165     return 0;
00166 }
00167 
00168 const QString& EventModel::getColorByID(int colorID)
00169 {
00170     if (!colors.contains(colorID))
00171     {
00172         QSqlQuery query("SELECT color FROM colors WHERE id = :id");
00173 
00174         query.bindValue(":id", colorID);
00175 
00176         colors[colorID] = "000000";
00177         if (!query.exec())
00178             qDebug() << QString("Can't select color : %1").arg(query.lastError().text());
00179         else if (query.next())
00180             colors[colorID] = query.value(0).toString();
00181     }
00182     return colors[colorID];
00183 }
00184 
00185 QList<int> EventModel::getPlaylists()
00186 {
00187     QList<int>  items;
00188 
00189     QSqlQuery   query("SELECT playlist_id FROM events_playlists WHERE event_id = :id");
00190 
00191     query.bindValue(":id", this->id);
00192 
00193     if (!query.exec())
00194         qDebug() << QString("Can't select : %1").arg(query.lastError().text());
00195     else
00196     {
00197         while (query.next())
00198             items.append(query.value(0).toInt());
00199     }
00200 
00201     return items;
00202 }
00203 
00204 void    EventModel::remove()
00205 {
00206     QSqlQuery query("DELETE FROM events_playlists WHERE event_id = :p_id;");
00207 
00208     query.bindValue(":event", this->id);
00209 
00210     if (!query.exec())
00211         qDebug() << QString("Can't delete event or dependencies : %1").arg(query.lastError().text());
00212 
00213     query.prepare("DELETE FROM colors_events WHERE event_id = :c_id;");
00214 
00215     query.bindValue(":event", this->id);
00216 
00217     if (!query.exec())
00218         qDebug() << QString("Can't delete event or dependencies : %1").arg(query.lastError().text());
00219 
00220     query.prepare("DELETE FROM events WHERE id = :event;");
00221 
00222     query.bindValue(":event", this->id);
00223 
00224     if (!query.exec())
00225         qDebug() << QString("Can't delete event or dependencies : %1").arg(query.lastError().text());
00226 
00227     return;
00228 }
00229 
00230 void    EventModel::eventClicked()
00231 {
00232     scheduler->getEventWidget()->setUpdate(this);
00233 
00234     this->focusIn();
00235 }
00236 
00237 void    EventModel::focusIn()
00238 {
00239     QString stylesheet  = this->styleSheet();
00240 
00241     this->setStyleSheet(stylesheet + "background-image: url(:/scheduler/bg-selected);");
00242 }
00243 
00244 void    EventModel::focusOut()
00245 {
00246     this->setStyle();
00247 }
00248 
00249 void    EventModel::setStyle()
00250 {
00251     QString color((this->getColor() != NULL) ? this->getColor() : "FFAD46");
00252     QColor  newColor            = QColor("#" + color);
00253     QString newBackgroundColor  = newColor.lighter(70).name();
00254 
00255     this->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #" + color + ", stop: 1 " + newBackgroundColor + "); color: #333; border-radius: 3px; margin: 1px; text-align: center;");
00256 }
00257 
00258 void    EventModel::mousePressEvent(QMouseEvent *)
00259 {
00260     this->focusIn();
00261     emit clicked();
00262 }