possible to have multiple conferences in the DB
[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     int confId = Conference::activeConference();
43     QRect rect = visualRect(aIndex); // visual QRect of selected/clicked item in the list
44     Delegate *delegate = static_cast<Delegate*>(itemDelegate(aIndex));
45     switch(delegate->whichControlClicked(aIndex,aPoint))
46     {
47         case Delegate::FavouriteControlOn:
48         case Delegate::FavouriteControlOff:
49             {
50                 // handle Favourite Control clicked
51                 Event event = Event::getById(aIndex.data().toInt(),confId);
52
53                 QList<Event> conflicts = Event::conflictEvents(event.id(),Conference::activeConference());
54                 if(event.isFavourite())
55                     event.setFavourite(false);
56                 else
57                     event.setFavourite(true);
58                 event.update("favourite");
59
60                 qDebug() << " FAVOURITE [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.isFavourite();
61
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                 // have to emit 'eventHasChanged' signal on all events in conflict
69                 for(int i=0; i<conflicts.count(); i++)
70                     emit(eventHasChanged(conflicts[i].id()));
71                 
72                 // since the Favourite icon has changed, update TreeViews accordingly
73                 // all TreeViews have to listen on this signal
74                 emit(eventHasChanged(event.id(),true));
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(),confId);
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)); // testing
101                     int cookie = alarm.addAlarm(event.id(),event.start().addSecs(-15*60)); // 15 minutes before real start
102                     qDebug() << "cookie: " << cookie;
103 #endif /* MAEMO */
104                 }
105                 event.update("alarm");
106                 qDebug() << " ALARM [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.hasAlarm();
107                 // since the Alarm icon has changed, update TreeView accordingly
108                 // all TreeViews have to listen on this signal
109                 emit(eventHasChanged(event.id()));
110                 handled = true;
111             }
112             break;
113         case Delegate::MapControl:
114             {
115                 // handle Alarm Control clicked
116                 qDebug() << "MAP CLICKED: " << qVariantValue<QString>(aIndex.data());
117                 emit(requestForMap(aIndex));
118                 handled = true;
119             }
120         break;
121         case Delegate::WarningControl:
122         {
123
124             qDebug() << "WARNING CLICKED: " << qVariantValue<QString>(aIndex.data());
125             emit(requestForConflicts(aIndex));
126             handled = true;
127         }
128         break;
129         case Delegate::ControlNone:
130         default:
131             {
132                 // item was clicked, but not a control
133                 handled = false;
134             }
135     };
136
137     return handled;
138 }
139
140 void TreeView::handleItemClicked(const QModelIndex &index)
141 {
142     if(!index.parent().isValid()) // time-group
143     {
144         if(isExpanded(index)) 
145             setExpanded(index, false);
146         else
147             setExpanded(index, true);
148     }
149 }
150
151 void TreeView::setAllExpanded(bool aExpanded)
152 {
153     for(int i=0; i<model()->rowCount(QModelIndex()); i++)
154     {
155         setExpanded(model()->index(i,0,QModelIndex()),aExpanded);
156     }
157 }
158