EventModel signaling changed
[toast/confclerk.git] / src / mvc / treeview.cpp
1 #include <QMouseEvent>
2
3 #include "treeview.h"
4 #include "delegate.h"
5 #include "event.h"
6 #include "eventmodel.h"
7
8 #ifdef MAEMO
9 #include <alarm.h>
10 #endif
11
12 #include <QDebug>
13
14 TreeView::TreeView(QWidget *aParent)
15     : QTreeView(aParent)
16 {
17     connect(this, SIGNAL(clicked(QModelIndex)), SLOT(handleItemClicked(QModelIndex)));
18 }
19
20 void TreeView::mouseReleaseEvent(QMouseEvent *aEvent)
21 {
22     QModelIndex index = currentIndex();
23     QPoint point = aEvent->pos();
24
25     // test whether we have handled the mouse event
26     if(!testForControlClicked(index,point))
27     {
28         // pass the event to the Base class, so item clicks/events are handled correctly
29         QTreeView::mouseReleaseEvent(aEvent);
30     }
31 }
32
33 // returns bool if some Control was clicked
34 bool TreeView::testForControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) 
35 {
36     bool handled = false;
37
38     if(!aIndex.isValid())
39         return handled;
40
41     QRect rect = visualRect(aIndex); // visual QRect of selected/clicked item in the list
42     Delegate *delegate = static_cast<Delegate*>(itemDelegate(aIndex));
43     switch(delegate->whichControlClicked(aIndex,aPoint))
44     {
45         case Delegate::FavouriteControlOn:
46         case Delegate::FavouriteControlOff:
47             {
48                 // handle Favourite Control clicked
49                 Event event = Event::getById(aIndex.data().toInt(),1);
50                 if(event.isFavourite())
51                     event.setFavourite(false);
52                 else
53                     event.setFavourite(true);
54                 event.update("favourite");
55                 qDebug() << " FAVOURITE [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.isFavourite();
56                 // since the Favourite icon has changed, update TreeViews accordingly
57                 // all TreeViews have to listen on this signal
58                 emit(eventHasChanged(event.id()));
59                 handled = true;
60             }
61             break;
62         case Delegate::AlarmControlOn:
63         case Delegate::AlarmControlOff:
64             {
65                 // handle Alarm Control clicked
66                 Event event = Event::getById(aIndex.data().toInt(),1);
67                 if(event.hasAlarm())
68                 {
69                     event.setHasAlarm(false); // update DB
70 #ifdef MAEMO
71                     // remove alarm from the 'alarmd' alrms list
72                     Alarm alarm;
73                     alarm.deleteAlarm(event.id());
74                     // TODO: test if removing was successfull
75 #endif /* MAEMO */
76                 }
77                 else
78                 {
79                     event.setHasAlarm(true);
80 #ifdef MAEMO
81                     // add alarm to the 'alarmd'
82                     Alarm alarm;
83                     int cookie = alarm.addAlarm(event.id(),QDateTime::currentDateTime().addSecs(10));
84                     qDebug() << "cookie: " << cookie;
85 #endif /* MAEMO */
86                 }
87                 event.update("alarm");
88                 qDebug() << " ALARM [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.hasAlarm();
89                 // since the Alarm icon has changed, update TreeView accordingly
90                 // all TreeViews have to listen on this signal
91                 emit(eventHasChanged(event.id()));
92                 handled = true;
93             }
94             break;
95         case Delegate::MapControl:
96             {
97                 // handle Alarm Control clicked
98                 qDebug() << "MAP CLICKED: " << qVariantValue<QString>(aIndex.data());
99                 emit(requestForMap(aIndex));
100                 handled = true;
101             }
102         break;
103         case Delegate::WarningControlOff:
104         case Delegate::WarningControlOn:
105         {
106
107             qDebug() << "WARNING CLICKED: " << qVariantValue<QString>(aIndex.data());
108             // TODO: implement
109             emit(requestForWarning(aIndex));
110             handled = true;
111         }
112         break;
113         case Delegate::ControlNone:
114         default:
115             {
116                 // item was clicked, but not a control
117                 handled = false;
118             }
119     };
120
121     return handled;
122 }
123
124 void TreeView::handleItemClicked(const QModelIndex &index)
125 {
126     if(!index.parent().isValid()) // time-group
127     {
128         if(isExpanded(index)) 
129             setExpanded(index, false);
130         else
131             setExpanded(index, true);
132     }
133 }
134