single-click is used to open event dialog
[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 }
18
19 void TreeView::mouseReleaseEvent(QMouseEvent *aEvent)
20 {
21     QModelIndex index = currentIndex();
22     QPoint point = aEvent->pos();
23
24     // test whether we have handled the mouse event
25     if(!testForControlClicked(index,point))
26     {
27         // pass the event to the Base class, so item clicks/events are handled correctly
28         QTreeView::mouseReleaseEvent(aEvent);
29     }
30 }
31
32 // returns bool if some Control was clicked
33 bool TreeView::testForControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) 
34 {
35     bool handled = false;
36
37     if(!aIndex.isValid())
38         return handled;
39
40     QRect rect = visualRect(aIndex); // visual QRect of selected/clicked item in the list
41     Delegate *delegate = static_cast<Delegate*>(itemDelegate(aIndex));
42     switch(delegate->whichControlClicked(aIndex,aPoint))
43     {
44         case Delegate::FavouriteControlOn:
45         case Delegate::FavouriteControlOff:
46             {
47                 // handle Favourite Control clicked
48                 Event event = Event::getById(aIndex.data().toInt(),1);
49                 if(event.isFavourite())
50                 {
51                     static_cast<Event*>(aIndex.internalPointer())->setFavourite(false); // list of events
52                     event.setFavourite(false); // update DB
53                 }
54                 else
55                 {
56                     static_cast<Event*>(aIndex.internalPointer())->setFavourite(true); // list of events
57                     event.setFavourite(true);
58                 }
59                 qDebug() << " FAVOURITE [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.isFavourite();
60                 event.update("favourite");
61                 // since the Favourite icon has changed, update TreeView accordingly
62                 static_cast<EventModel*>(model())->emitDataChangedSignal(aIndex,aIndex);
63                 handled = true;
64             }
65             break;
66         case Delegate::AlarmControlOn:
67         case Delegate::AlarmControlOff:
68             {
69                 // handle Alarm Control clicked
70                 Event event = Event::getById(aIndex.data().toInt(),1);
71                 if(event.hasAlarm())
72                 {
73                     static_cast<Event*>(aIndex.internalPointer())->setHasAlarm(false); // list of events
74                     event.setHasAlarm(false); // update DB
75 #ifdef MAEMO
76                     // remove alarm from the 'alarmd' alrms list
77                     Alarm alarm;
78                     alarm.deleteAlarm(event.id());
79                     // TODO: test if removing was successfull
80 #endif /* MAEMO */
81                 }
82                 else
83                 {
84                     static_cast<Event*>(aIndex.internalPointer())->setHasAlarm(true); // list of events
85                     event.setHasAlarm(true);
86 #ifdef MAEMO
87                     // add alarm to the 'alarmd'
88                     Alarm alarm;
89                     int cookie = alarm.addAlarm(event.id(),QDateTime::currentDateTime().addSecs(10));
90                     qDebug() << "cookie: " << cookie;
91 #endif /* MAEMO */
92                 }
93                 qDebug() << " ALARM [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.hasAlarm();
94                 event.update("alarm");
95                 // since the Alarm icon has changed, update TreeView accordingly
96                 static_cast<EventModel*>(model())->emitDataChangedSignal(aIndex,aIndex);
97                 handled = true;
98             }
99             break;
100         case Delegate::MapControl:
101             {
102                 // handle Alarm Control clicked
103                 qDebug() << "MAP CLICKED: " << qVariantValue<QString>(aIndex.data());
104                 emit(requestForMap(aIndex));
105                 handled = true;
106             }
107         break;
108         case Delegate::ControlNone:
109         default:
110             {
111                 // item was clicked, but not a control
112                 handled = false;
113             }
114     };
115
116     return handled;
117 }
118