'conflicts' modifications
authorpavelpa <pavelpa@localhost>
Wed, 27 Jan 2010 06:41:47 +0000 (06:41 +0000)
committerpavelpa <pavelpa@localhost>
Wed, 27 Jan 2010 06:41:47 +0000 (06:41 +0000)
 - preparing for the dialog showing also list of events in the conflict
 - created 'EVENT_CONFLICT' for flaging events in conflict state
 - TODO: not finished

src/mvc/delegate.cpp
src/mvc/delegate.h
src/mvc/event.cpp
src/mvc/event.h
src/mvc/treeview.cpp
src/sql/sqlengine.cpp

index 79d94db..d7c6db1 100644 (file)
@@ -65,7 +65,8 @@ void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, cons
         //int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
 
         //Time conflicts are colored differently
-        if(hasTimeConflict(index, index.parent()))
+        if(static_cast<Event*>(index.internalPointer())->isFavourite())
+        if(static_cast<Event*>(index.internalPointer())->hasTimeConflict())
         {
             bkgrColor = Qt::yellow;
         }
@@ -132,9 +133,7 @@ void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, cons
         // map
         painter->drawImage(mControls[MapControl]->drawPoint(option.rect),*mControls[MapControl]->image());
         // Time conflict
-        //if(static_cast<Event*>(index.internalPointer())->hasTimeConflict())
-
-        if(hasTimeConflict(index, index.parent()))
+        if(static_cast<Event*>(index.internalPointer())->hasTimeConflict())
             painter->drawImage(mControls[WarningControl]->drawPoint(option.rect),*mControls[WarningControl]->image());
 
         // draw texts
@@ -311,7 +310,7 @@ Delegate::ControlId Delegate::whichControlClicked(const QModelIndex &aIndex, con
         {
             if(id == WarningControl)
             {
-                if(mControls[id]->hasConflict)
+                if(static_cast<Event*>(aIndex.internalPointer())->hasTimeConflict())
                     return id;
             }
             else
@@ -373,7 +372,6 @@ void Delegate::defineControls()
     // WARNING ICON
     control = new Control(WarningControl,QString(":icons/exclamation.png"));
     p = mControls[MapControl]->drawPoint();
-    control->hasConflict = false;
     p.setX(p.x()-control->image()->width()-SPACER);
     control->setDrawPoint(p);
     mControls.insert(WarningControl,control);
@@ -413,29 +411,3 @@ int Delegate::numberOfAlarms(const QModelIndex &index) const
     return nrofAlarms;
 }
 
-bool Delegate::hasTimeConflict(const QModelIndex &index, const QModelIndex &parent) const
-{
-    Event *event = static_cast<Event*>(index.internalPointer());
-    QTime start = event->start().time();
-    QTime end = start.addSecs(event->duration());
-    for(int i=0; i<parent.model()->rowCount(parent); i++)
-    {
-        if((event->id()!=static_cast<Event*>(parent.child(i,0).internalPointer())->id())
-        &&
-        (static_cast<Event*>(parent.child(i,0).internalPointer())->isFavourite()))
-        {
-            if (((start >= static_cast<Event*>(parent.child(i,0).internalPointer())->start().time())
-            &&
-            (start < static_cast<Event*>(parent.child(i,0).internalPointer())->start().time().addSecs(static_cast<Event*>(parent.child(i,0).internalPointer())->duration())))
-            ||
-            ((end > static_cast<Event*>(parent.child(i,0).internalPointer())->start().time())
-            &&
-            (end <= static_cast<Event*>(parent.child(i,0).internalPointer())->start().time().addSecs(static_cast<Event*>(parent.child(i,0).internalPointer())->duration()))))
-            {
-                return true;
-            }
-        }
-    }
-    return false;
-}
-
index 42cce94..3f544f7 100644 (file)
@@ -43,7 +43,6 @@ class Delegate : public QItemDelegate
                 {
                     return QRect(drawPoint(aRect), drawPoint(aRect)+QPoint(mImage->size().width(),mImage->size().height()));
                 }
-                bool hasConflict;
             private:
                 ControlId mId;
                 QImage *mImage;
@@ -71,7 +70,6 @@ class Delegate : public QItemDelegate
         // every time it requires them
         int numberOfFavourities(const QModelIndex &index) const;
         int numberOfAlarms(const QModelIndex &index) const;
-        bool hasTimeConflict(const QModelIndex &index, const QModelIndex &parent) const;
 
     private:
         QPointer<QTreeView> mViewPtr;
index 1af20d5..1dc3af7 100644 (file)
@@ -124,6 +124,97 @@ QStringList Event::persons() const
     return persons;
 }
 
+QList<int> Event::conflicts() const
+{
+    QSqlQuery query;
+    // TODO: conference ID isn't used here
+    query.prepare("SELECT conflict_event FROM event_conflict WHERE xid_event = :id AND xid_conference = :conf");
+    query.bindValue(":id", id());
+    query.bindValue(":conf", conferenceId());
+    query.exec();
+    // TODO: handle qeury error
+    //qDebug() << query.lastError();
+
+    QList<int> conflicts;
+    while(query.next())
+        conflicts.append(query.record().value("conflict_event").toInt());
+
+    return conflicts;
+}
+
+bool Event::hasTimeConflict() const
+{
+    return conflicts().count() > 0 ? true : false;
+}
+
+void Event::updateConflicts()
+{
+    qDebug() << "updating conflicts";
+    QSqlQuery query;
+    query.prepare("SELECT id FROM event WHERE xid_conference = :conf AND ( \
+           ( start <= :start1 AND ( start + duration ) >= :start2 ) \
+        OR ( start >= :start3 AND ( start + duration ) <= :end1 ) \
+        OR ( start <= :end2  AND ( start + duration ) >= :end3 ) ) AND favourite = 1 ORDER BY start");
+    query.bindValue(":conf", conferenceId());
+    query.bindValue(":start1", convertToDb(start(), QVariant::DateTime));
+    query.bindValue(":start2", convertToDb(start(), QVariant::DateTime));
+    query.bindValue(":start3", convertToDb(start(), QVariant::DateTime));
+    query.bindValue(":end1", convertToDb(start().toTime_t()+duration(), QVariant::DateTime));
+    query.bindValue(":end2", convertToDb(start().toTime_t()+duration(), QVariant::DateTime));
+    query.bindValue(":end3", convertToDb(start().toTime_t()+duration(), QVariant::DateTime));
+    query.exec();
+
+    QList<int> conflicts;
+    while(query.next())
+    {
+        int idx = query.record().value("id").toInt();
+        if(idx != id())
+            conflicts.append(idx);
+    }
+
+    if(isFavourite()) // event became favourite
+    {
+        for(int i=0; i<conflicts.count(); i++)
+        {
+            QSqlQuery query;
+            query.prepare("INSERT INTO event_conflict (xid_conference, xid_event, conflict_event) VALUES ( ? , ? , ? )");
+            query.bindValue(0, conferenceId());
+            query.bindValue(1, id());
+            query.bindValue(2, conflicts[i]);
+            query.exec();
+
+            QSqlQuery query2;
+            query2.prepare("INSERT INTO event_conflict (xid_conference, xid_event, conflict_event) VALUES ( ? , ? , ? )");
+            query2.bindValue(0, conferenceId());
+            query2.bindValue(1, conflicts[i]);
+            query2.bindValue(2, id());
+            query2.exec();
+        }
+    }
+    else // event removed from favourities
+    {
+        qDebug() << "removing";
+
+        QSqlQuery queryRemove;
+        queryRemove.prepare("DELETE FROM event_conflict WHERE xid_event = :id AND xid_conference = :conf");
+        queryRemove.bindValue(":id",id());
+        queryRemove.bindValue(":conf",conferenceId());
+        queryRemove.exec();
+
+        for(int i=0; i<conflicts.count(); i++)
+        {
+            qDebug() << "removing: " << id() << " -> " << conflicts[i];
+
+            QSqlQuery queryRemove;
+            queryRemove.prepare("DELETE FROM event_conflict WHERE xid_event = :id1 AND xid_conference = :conf AND conflict_event = :id2");
+            queryRemove.bindValue(":id1",conflicts[i]);
+            queryRemove.bindValue(":conf",conferenceId());
+            queryRemove.bindValue(":id2",id());
+            queryRemove.exec();
+        }
+    }
+}
+
 void Event::setRoom(const QString &room)
 {
     Q_UNUSED(room);
index 6c82a2b..a96ed19 100644 (file)
@@ -37,7 +37,7 @@ public:
     QString language() const { return value("language").toString(); }
     bool isFavourite() const { return value("favourite").toBool(); }
     bool hasAlarm() const { return value("alarm").toBool(); }
-    bool hasTimeConflict() const { return true; /*return value("warning").toBool()*/; } //TODO
+    bool hasTimeConflict() const;
     QString tag() const { return value("tag").toString(); }
     QString title() const { return value("title").toString(); }
     QString subtitle() const { return value("subtitle").toString(); }
@@ -47,6 +47,7 @@ public:
     QString room() const;
     int roomId() const;
     QStringList persons() const;
+    QList<int> conflicts() const;
 
     void setId(int id) { setValue("id", id); }
     void setConferenceId(int conferenceId) { setValue("xid_conference", conferenceId); }
@@ -65,6 +66,7 @@ public:
     // records from other tables associated with 'id'
     void setRoom(const QString& room);
     void setPersons(const QStringList &persons);
+    void updateConflicts();
 
 friend class EventTest;
 };
index d18c873..e1dc9b7 100644 (file)
@@ -53,6 +53,8 @@ bool TreeView::testForControlClicked(const QModelIndex &aIndex, const QPoint &aP
                     event.setFavourite(true);
                 event.update("favourite");
                 qDebug() << " FAVOURITE [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.isFavourite();
+                // update EVENT_CONFLICT table
+                event.updateConflicts();
                 // since the Favourite icon has changed, update TreeViews accordingly
                 // all TreeViews have to listen on this signal
                 emit(eventHasChanged(event.id()));
index faed167..14c68fc 100644 (file)
@@ -291,6 +291,14 @@ bool SqlEngine::createTables(QSqlDatabase &aDatabase)
             "FOREIGN KEY(xid_event) REFERENCES EVENT(id), "
             "FOREIGN KEY(xid_room) REFERENCES ROOM(id));");
 
+        query.exec("CREATE TABLE EVENT_CONFLICT ( "
+            "xid_conference INTEGER NOT NULL, "
+            "xid_event INTEGER NOT NULL, "
+            "conflict_event INTEGER NOT NULL, "
+            "UNIQUE ( xid_conference, xid_event, conflict_event ) ON CONFLICT IGNORE, "
+            "FOREIGN KEY(xid_conference) REFERENCES CONFERENCE(id), "
+            "FOREIGN KEY(xid_event) REFERENCES EVENT(id));");
+
         query.exec("CREATE TABLE LINK ( "
             "id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
             "xid_conference INTEGER NOT NULL, "
@@ -362,3 +370,4 @@ bool SqlEngine::execQuery(QSqlDatabase &aDatabase, const QString &aQuery)
        return true;
     }
 }
+