conflicts updated correctly
[toast/confclerk.git] / src / mvc / treeview.cpp
index 7abe943e0ac3562fda85b28de12fda72ce8d7526..0b3b632c6c3cd5aa8951f98c9052a0bc585baaba 100644 (file)
@@ -3,6 +3,7 @@
 #include "treeview.h"
 #include "delegate.h"
 #include "event.h"
+#include "conference.h"
 #include "eventmodel.h"
 
 #ifdef MAEMO
@@ -14,6 +15,7 @@
 TreeView::TreeView(QWidget *aParent)
     : QTreeView(aParent)
 {
+    connect(this, SIGNAL(clicked(QModelIndex)), SLOT(handleItemClicked(QModelIndex)));
 }
 
 void TreeView::mouseReleaseEvent(QMouseEvent *aEvent)
@@ -46,20 +48,31 @@ bool TreeView::testForControlClicked(const QModelIndex &aIndex, const QPoint &aP
             {
                 // handle Favourite Control clicked
                 Event event = Event::getById(aIndex.data().toInt(),1);
+
+                QList<Event> conflicts = Event::conflictEvents(event.id(),Conference::activeConference());
                 if(event.isFavourite())
-                {
-                    static_cast<Event*>(aIndex.internalPointer())->setFavourite(false); // list of events
-                    event.setFavourite(false); // update DB
-                }
+                    event.setFavourite(false);
                 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");
-                // since the Favourite icon has changed, update TreeView accordingly
-                static_cast<EventModel*>(model())->emitDataChangedSignal(aIndex,aIndex);
+
+                qDebug() << " FAVOURITE [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.isFavourite();
+                // update EVENT_CONFLICT table
+                event.updateConflicts();
+                if(event.isFavourite())
+                {
+                    // event has became 'favourite' and so 'conflicts' list may have changed
+                    conflicts = Event::conflictEvents(event.id(),Conference::activeConference());
+                }
+
+                // since the Favourite icon has changed, update TreeViews accordingly
+                // all TreeViews have to listen on this signal
+                emit(eventHasChanged(event.id()));
+
+                // have to emit 'eventHasChanged' signal on all events in conflict
+                for(int i=0; i<conflicts.count(); i++)
+                    emit(eventHasChanged(conflicts[i].id()));
+
                 handled = true;
             }
             break;
@@ -70,7 +83,6 @@ bool TreeView::testForControlClicked(const QModelIndex &aIndex, const QPoint &aP
                 Event event = Event::getById(aIndex.data().toInt(),1);
                 if(event.hasAlarm())
                 {
-                    static_cast<Event*>(aIndex.internalPointer())->setHasAlarm(false); // list of events
                     event.setHasAlarm(false); // update DB
 #ifdef MAEMO
                     // remove alarm from the 'alarmd' alrms list
@@ -81,7 +93,6 @@ bool TreeView::testForControlClicked(const QModelIndex &aIndex, const QPoint &aP
                 }
                 else
                 {
-                    static_cast<Event*>(aIndex.internalPointer())->setHasAlarm(true); // list of events
                     event.setHasAlarm(true);
 #ifdef MAEMO
                     // add alarm to the 'alarmd'
@@ -90,10 +101,11 @@ bool TreeView::testForControlClicked(const QModelIndex &aIndex, const QPoint &aP
                     qDebug() << "cookie: " << cookie;
 #endif /* MAEMO */
                 }
-                qDebug() << " ALARM [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.hasAlarm();
                 event.update("alarm");
+                qDebug() << " ALARM [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.hasAlarm();
                 // since the Alarm icon has changed, update TreeView accordingly
-                static_cast<EventModel*>(model())->emitDataChangedSignal(aIndex,aIndex);
+                // all TreeViews have to listen on this signal
+                emit(eventHasChanged(event.id()));
                 handled = true;
             }
             break;
@@ -105,6 +117,14 @@ bool TreeView::testForControlClicked(const QModelIndex &aIndex, const QPoint &aP
                 handled = true;
             }
         break;
+        case Delegate::WarningControl:
+        {
+
+            qDebug() << "WARNING CLICKED: " << qVariantValue<QString>(aIndex.data());
+            emit(requestForConflicts(aIndex));
+            handled = true;
+        }
+        break;
         case Delegate::ControlNone:
         default:
             {
@@ -116,3 +136,22 @@ bool TreeView::testForControlClicked(const QModelIndex &aIndex, const QPoint &aP
     return handled;
 }
 
+void TreeView::handleItemClicked(const QModelIndex &index)
+{
+    if(!index.parent().isValid()) // time-group
+    {
+        if(isExpanded(index)) 
+            setExpanded(index, false);
+        else
+            setExpanded(index, true);
+    }
+}
+
+void TreeView::setAllExpanded(bool aExpanded)
+{
+    for(int i=0; i<model()->rowCount(QModelIndex()); i++)
+    {
+        setExpanded(model()->index(i,0,QModelIndex()),aExpanded);
+    }
+}
+