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