f907830696c085ccc2af5229b5e9b5424ae65589
[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
61                 if(event.isFavourite())
62                 {
63                     // event has became 'favourite' and so 'conflicts' list may have changed
64                     conflicts = Event::conflictEvents(event.id(),Conference::activeConference());
65                 }
66
67                 // have to emit 'eventHasChanged' signal on all events in conflict
68                 for(int i=0; i<conflicts.count(); i++)
69                     emit(eventHasChanged(conflicts[i].id()));
70                 
71                 // since the Favourite icon has changed, update TreeViews accordingly
72                 // all TreeViews have to listen on this signal
73                 emit(eventHasChanged(event.id(),true));
74
75                 handled = true;
76             }
77             break;
78         case Delegate::AlarmControlOn:
79         case Delegate::AlarmControlOff:
80             {
81                 // handle Alarm Control clicked
82                 Event event = Event::getById(aIndex.data().toInt(),1);
83                 if(event.hasAlarm())
84                 {
85                     event.setHasAlarm(false); // update DB
86 #ifdef MAEMO
87                     // remove alarm from the 'alarmd' alrms list
88                     Alarm alarm;
89                     alarm.deleteAlarm(event.id());
90                     // TODO: test if removing was successfull
91 #endif /* MAEMO */
92                 }
93                 else
94                 {
95                     event.setHasAlarm(true);
96 #ifdef MAEMO
97                     // add alarm to the 'alarmd'
98                     Alarm alarm;
99                     int cookie = alarm.addAlarm(event.id(),QDateTime::currentDateTime().addSecs(10));
100                     qDebug() << "cookie: " << cookie;
101 #endif /* MAEMO */
102                 }
103                 event.update("alarm");
104                 qDebug() << " ALARM [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.hasAlarm();
105                 // since the Alarm icon has changed, update TreeView accordingly
106                 // all TreeViews have to listen on this signal
107                 emit(eventHasChanged(event.id()));
108                 handled = true;
109             }
110             break;
111         case Delegate::MapControl:
112             {
113                 // handle Alarm Control clicked
114                 qDebug() << "MAP CLICKED: " << qVariantValue<QString>(aIndex.data());
115                 emit(requestForMap(aIndex));
116                 handled = true;
117             }
118         break;
119         case Delegate::WarningControl:
120         {
121
122             qDebug() << "WARNING CLICKED: " << qVariantValue<QString>(aIndex.data());
123             emit(requestForConflicts(aIndex));
124             handled = true;
125         }
126         break;
127         case Delegate::ControlNone:
128         default:
129             {
130                 // item was clicked, but not a control
131                 handled = false;
132             }
133     };
134
135     return handled;
136 }
137
138 void TreeView::handleItemClicked(const QModelIndex &index)
139 {
140     if(!index.parent().isValid()) // time-group
141     {
142         if(isExpanded(index)) 
143             setExpanded(index, false);
144         else
145             setExpanded(index, true);
146     }
147 }
148
149 void TreeView::setAllExpanded(bool aExpanded)
150 {
151     for(int i=0; i<model()->rowCount(QModelIndex()); i++)
152     {
153         setExpanded(model()->index(i,0,QModelIndex()),aExpanded);
154     }
155 }
156