work on favourite
authorpavelpa <pavelpa@localhost>
Sat, 16 Jan 2010 15:31:15 +0000 (15:31 +0000)
committerpavelpa <pavelpa@localhost>
Sat, 16 Jan 2010 15:31:15 +0000 (15:31 +0000)
 - created 'favourite' column in EVENT table
 - modified 'ormrecord' for setting record's elements
 - favourities view not implemented

src/fosdem.pro
src/mvc/delegate.cpp
src/mvc/delegate.h
src/mvc/event.cpp
src/mvc/event.h
src/mvc/treeview.cpp
src/orm/ormrecord.h
src/sql/sqlengine.cpp

index 69179f8..5e72c0e 100644 (file)
@@ -1,4 +1,5 @@
 TEMPLATE = subdirs
-SUBDIRS  = orm mvc sql gui app test
+SUBDIRS  = orm mvc sql gui app
+#SUBDIRS += test
 CONFIG += ordered
 
index c6174bf..71e967a 100644 (file)
@@ -83,8 +83,11 @@ void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, cons
         }
 
         // draw Controls
-        painter->drawImage(mControls[FavouriteControl]->drawPoint(option.rect),*mControls[FavouriteControl]->image());
-        painter->drawImage(mControls[AlarmControl]->drawPoint(option.rect),*mControls[AlarmControl]->image());
+        if(static_cast<Event*>(index.internalPointer())->isFavourite())
+            painter->drawImage(mControls[FavouriteControlOn]->drawPoint(option.rect),*mControls[FavouriteControlOn]->image());
+        else
+            painter->drawImage(mControls[FavouriteControlOff]->drawPoint(option.rect),*mControls[FavouriteControlOff]->image());
+        painter->drawImage(mControls[AlarmControlOn]->drawPoint(option.rect),*mControls[AlarmControlOn]->image());
         painter->drawImage(mControls[MapControl]->drawPoint(option.rect),*mControls[MapControl]->image());
     }
     else // doesn't have parent - time-groups' elements (top items)
@@ -200,20 +203,30 @@ Delegate::ControlId Delegate::whichControlClicked(const QModelIndex &aIndex, con
 void Delegate::defineControls()
 {
     Control *control;
-    // FAVOURITE ICON
-    control = new Control(FavouriteControl,QString(":icons/favourite-on.png"));
+    // FAVOURITE ICONs
+    // on
+    control = new Control(FavouriteControlOn,QString(":icons/favourite-on.png"));
     control->setDrawPoint(QPoint(-control->image()->width()-SPACER,SPACER));
-    mControls.insert(FavouriteControl,control);
-
-    // ALARM ICON
-    control = new Control(AlarmControl,QString(":icons/alarm-on.png"));
-    control->setDrawPoint(QPoint(-mControls[FavouriteControl]->image()->width()-control->image()->width()-2*SPACER,SPACER));
-    mControls.insert(AlarmControl,control);
+    mControls.insert(FavouriteControlOn,control);
+    // off
+    control = new Control(FavouriteControlOff,QString(":icons/favourite-off.png"));
+    control->setDrawPoint(QPoint(-control->image()->width()-SPACER,SPACER));
+    mControls.insert(FavouriteControlOff,control);
+
+    // ALARM ICONs
+    // on
+    control = new Control(AlarmControlOn,QString(":icons/alarm-on.png"));
+    control->setDrawPoint(QPoint(-mControls[FavouriteControlOn]->image()->width()-control->image()->width()-2*SPACER,SPACER));
+    mControls.insert(AlarmControlOn,control);
+    // off
+    control = new Control(AlarmControlOff,QString(":icons/alarm-off.png"));
+    control->setDrawPoint(QPoint(-mControls[FavouriteControlOff]->image()->width()-control->image()->width()-2*SPACER,SPACER));
+    mControls.insert(AlarmControlOff,control);
 
     // MAP ICON
     control = new Control(MapControl,QString(":icons/compass.png"));
-    control->setDrawPoint(QPoint(-mControls[AlarmControl]->image()->width()-control->image()->width()
-                                 -mControls[FavouriteControl]->image()->width()-3*SPACER,SPACER));
+    control->setDrawPoint(QPoint(-mControls[AlarmControlOn]->image()->width()-control->image()->width()
+                                 -mControls[FavouriteControlOn]->image()->width()-3*SPACER,SPACER));
     mControls.insert(MapControl,control);
 }
 
index a11cfab..63c74dc 100644 (file)
@@ -14,8 +14,10 @@ class Delegate : public QItemDelegate
         enum ControlId
         {
             ControlNone = 0,
-            FavouriteControl,
-            AlarmControl,
+            FavouriteControlOn,
+            FavouriteControlOff,
+            AlarmControlOn,
+            AlarmControlOff,
             MapControl
         };
 
index ffc805a..9caf2ab 100644 (file)
@@ -7,7 +7,8 @@ QSqlRecord const Event::sColumns = Event::toRecord(QList<QSqlField>()
     << QSqlField("duration", QVariant::Int)
     << QSqlField("xid_activity", QVariant::Int)
     << QSqlField("type", QVariant::String)
-    << QSqlField("language", QVariant::String));
+    << QSqlField("language", QVariant::String)
+    << QSqlField("favourite", QVariant::Bool));
 
 QString const Event::sTableName = QString("event");
 
@@ -30,3 +31,4 @@ QList<Event> Event::getByDate(const QDate& date, int conferenceId)
 
     return load(query);
 }
+
index 05c74a8..76216e7 100644 (file)
@@ -7,7 +7,6 @@
 
 #include <ormrecord.h>
 
-
 /**
   NoSuchEventException is thrown when required event does not exist.
 */
@@ -33,6 +32,7 @@ public:
     int activityId() const { return value("xid_activity").toInt(); }
     QString type() const { return value("type").toString(); }
     QString language() const { return value("language").toString(); }
+    bool isFavourite() const { return value("favourite").toBool(); }
 
     void setId(int id) { setValue("id", id); }
     void setConferenceId(int conferenceId) { setValue("xid_conference", conferenceId); }
@@ -41,10 +41,10 @@ public:
     void setActivityId(int activityId) { setValue("xid_activity", activityId); }
     void setType(const QString& type) { setValue("type", type); }
     void setLanguage(const QString& language) { setValue("language", language); }
+    void setFavourite(bool favourite) { setValue("favourite", favourite); }
 
 friend class EventTest;
 };
 
-
-
 #endif // EVENT_H
+
index c8127d0..e872d14 100644 (file)
@@ -2,6 +2,7 @@
 
 #include "treeview.h"
 #include "delegate.h"
+#include "event.h"
 
 #include <QDebug>
 
@@ -30,13 +31,32 @@ void TreeView::testForControlClicked(const QModelIndex &aIndex, const QPoint &aP
     Delegate *delegate = static_cast<Delegate*>(itemDelegate(aIndex));
     switch(delegate->whichControlClicked(aIndex,aPoint))
     {
-        case Delegate::FavouriteControl:
+        case Delegate::FavouriteControlOn:
+        case Delegate::FavouriteControlOff:
             {
                 // handle Favourite Control clicked
-                qDebug() << "FAVOURITE CLICKED: " << qVariantValue<QString>(aIndex.data());
+                Event event = Event::getById(aIndex.data().toInt(),1);
+                if(event.isFavourite())
+                {
+                    static_cast<Event*>(aIndex.internalPointer())->setFavourite(false); // list of events
+                    event.setFavourite(false); // update DB
+                }
+                else
+                {
+                    static_cast<Event*>(aIndex.internalPointer())->setFavourite(true); // list of events
+                    event.setFavourite(true);
+                }
+                qDebug() << " FAVOURITE [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.isFavourite();
+                event.update("favourite");
+                // TODO: since the Favourite icon has changed, update TreeView accordingly
+                // not really working solution is the following
+                // maybe the call to MainWindow->update() fix the problem ???
+                QTreeView::update();
+                update();
             }
             break;
-        case Delegate::AlarmControl:
+        case Delegate::AlarmControlOn:
+        case Delegate::AlarmControlOff:
             {
                 // handle Alarm Control clicked
                 qDebug() << "ALARM CLICKED: " << qVariantValue<QString>(aIndex.data());
index 0017921..8d4212a 100644 (file)
@@ -33,6 +33,7 @@ class OrmRecord : protected QSqlRecord
 public:
     OrmRecord();
     static T hydrate(const QSqlRecord& record);
+    void update(QString col, QVariant value = QVariant()); // updates specified column 'col'
 
 protected:
     QVariant value(QString col) const;
@@ -44,6 +45,7 @@ protected:
     // auxiliary methods
     static QString columnsForSelect(const QString& prefix = QString());
     static QString selectQuery();
+    static QString updateQuery();
     static QSqlRecord toRecord(const QList<QSqlField> & columnList);
 
     static QVariant convertToC(QVariant value, QVariant::Type colType);
@@ -64,6 +66,24 @@ T OrmRecord<T>::hydrate(const QSqlRecord& record)
     return object;
 }
 
+// updates specified column 'col'
+// if the value is not specified  as an argument,
+// it's taken from the reford itself
+// see also: setValue() method for more details
+template <typename T>
+void OrmRecord<T>::update(QString col, QVariant value)
+{
+    QSqlQuery query;
+    query.prepare(QString(updateQuery() + "SET %1 = :col WHERE id = :id").arg(col));
+    if(value.isValid()) // take 'col' value from the method's arguments
+        query.bindValue(":col", value);
+    else // take 'col' value from the record; see setValue()
+        query.bindValue(":col", convertToDb(this->value(col), this->value(col).type()));
+    query.bindValue(":id", this->value("id"));
+    //query.bindValue(":id", convertToDb(value("id"), QVariant::Int));
+    query.exec();
+}
+
 template <typename T>
 QVariant OrmRecord<T>::value(QString col) const
 {
@@ -102,12 +122,6 @@ QList<T> OrmRecord<T>::load(QSqlQuery query)
     {
         if (!query.exec())
         {
-            //TODO Palo: exception handling !
-               QString start = "START\n";
-               QString end = "\nEND\n";
-               QString message = start + query.lastError().text() + end;
-                const char *data = message.toLatin1().data();
-                qDebug(data);
             throw new OrmSqlException(query.lastError().text());
         }
     }
@@ -140,6 +154,12 @@ QString OrmRecord<T>::selectQuery()
     return QString("SELECT %1 FROM %2 ").arg(columnsForSelect(), T::sTableName);
 }
 
+template <typename T>
+QString OrmRecord<T>::updateQuery()
+{
+    return QString("UPDATE %1 ").arg(T::sTableName);
+}
+
 template <typename T>
 QSqlRecord OrmRecord<T>::toRecord(const QList<QSqlField> & columnList)
 {
index a7cd626..87c245c 100644 (file)
@@ -90,16 +90,17 @@ void SqlEngine::addEventToDB(QHash<QString,QString> &aEvent)
         // The items of the Event are divided into the two tables EVENT and VIRTUAL_EVENT
         // VIRTUAL_EVENT is for Full-Text-Serach Support
         QDateTime startDateTime = QDateTime(QDate::fromString(aEvent["date"],DATE_FORMAT),QTime::fromString(aEvent["start"],TIME_FORMAT));
-        QString values = QString("'%1', '%2', '%3', '%4', '%5', '%6', '%7'") \
+        QString values = QString("'%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8'") \
                          .arg(aEvent["conference_id"]) \
                          .arg(aEvent["id"]) \
                          .arg(QString::number(startDateTime.toTime_t())) \
                          .arg(-QTime::fromString(aEvent["duration"],TIME_FORMAT).secsTo(QTime(0,0))) \
                          .arg("123456") \
                          .arg(aEvent["type"]) \
-                         .arg(aEvent["language"]);
+                         .arg(aEvent["language"]) \
+                         .arg("0"); // not favourite when added
 
-        QString query = QString("INSERT INTO EVENT (xid_conference, id, start, duration, xid_activity, type, language) VALUES (%1)").arg(values);
+        QString query = QString("INSERT INTO EVENT (xid_conference, id, start, duration, xid_activity, type, language, favourite) VALUES (%1)").arg(values);
         QSqlQuery result (query, db);
         //LOG_AUTOTEST(query);
 
@@ -226,6 +227,7 @@ bool SqlEngine::createTables(QSqlDatabase &aDatabase)
             xid_activity INTEGER NOT NULL REFERENCES ACTIVITY(id), \
             type VARCHAR, \
             language VARCHAR, \
+            favourite INTEGER DEFAULT 0, \
             PRIMARY KEY (xid_conference,id), \
             FOREIGN KEY(xid_conference) REFERENCES CONFERENCE(id) \
             FOREIGN KEY(xid_activity) REFERENCES ACTIVITY(id))");