group items (time/track/...) are expanded on single-click
[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                 {
52                     static_cast<Event*>(aIndex.internalPointer())->setFavourite(false); // list of events
53                     event.setFavourite(false); // update DB
54                 }
55                 else
56                 {
57                     static_cast<Event*>(aIndex.internalPointer())->setFavourite(true); // list of events
58                     event.setFavourite(true);
59                 }
60                 qDebug() << " FAVOURITE [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.isFavourite();
61                 event.update("favourite");
62                 // since the Favourite icon has changed, update TreeView accordingly
63                 static_cast<EventModel*>(model())->emitDataChangedSignal(aIndex,aIndex);
64                 handled = true;
65             }
66             break;
67         case Delegate::AlarmControlOn:
68         case Delegate::AlarmControlOff:
69             {
70                 // handle Alarm Control clicked
71                 Event event = Event::getById(aIndex.data().toInt(),1);
72                 if(event.hasAlarm())
73                 {
74                     static_cast<Event*>(aIndex.internalPointer())->setHasAlarm(false); // list of events
75                     event.setHasAlarm(false); // update DB
76 #ifdef MAEMO
77                     // remove alarm from the 'alarmd' alrms list
78                     Alarm alarm;
79                     alarm.deleteAlarm(event.id());
80                     // TODO: test if removing was successfull
81 #endif /* MAEMO */
82                 }
83                 else
84                 {
85                     static_cast<Event*>(aIndex.internalPointer())->setHasAlarm(true); // list of events
86                     event.setHasAlarm(true);
87 #ifdef MAEMO
88                     // add alarm to the 'alarmd'
89                     Alarm alarm;
90                     int cookie = alarm.addAlarm(event.id(),QDateTime::currentDateTime().addSecs(10));
91                     qDebug() << "cookie: " << cookie;
92 #endif /* MAEMO */
93                 }
94                 qDebug() << " ALARM [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.hasAlarm();
95                 event.update("alarm");
96                 // since the Alarm icon has changed, update TreeView accordingly
97                 static_cast<EventModel*>(model())->emitDataChangedSignal(aIndex,aIndex);
98                 handled = true;
99             }
100             break;
101         case Delegate::MapControl:
102             {
103                 // handle Alarm Control clicked
104                 qDebug() << "MAP CLICKED: " << qVariantValue<QString>(aIndex.data());
105                 emit(requestForMap(aIndex));
106                 handled = true;
107             }
108         break;
109         case Delegate::WarningControlOn:
110         case Delegate::WarningControlOff:
111         {
112             // TODO: implement
113             handled = true;
114         }
115         break;
116         case Delegate::ControlNone:
117         default:
118             {
119                 // item was clicked, but not a control
120                 handled = false;
121             }
122     };
123
124     return handled;
125 }
126
127 void TreeView::handleItemClicked(const QModelIndex &index)
128 {
129     if(!index.parent().isValid()) // time-group
130     {
131         if(isExpanded(index)) 
132             setExpanded(index, false);
133         else
134             setExpanded(index, true);
135     }
136 }
137