ee6419fefb3a9694f8948d34e4d52e8cf646e525
[toast/confclerk.git] / src / mvc / treeview.cpp
1 /*
2  * Copyright (C) 2010 Ixonos Plc.
3  *
4  * This file is part of fosdem-schedule.
5  *
6  * fosdem-schedule is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation, either version 2 of the License, or (at your option)
9  * any later version.
10  *
11  * fosdem-schedule is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * fosdem-schedule.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include <QMouseEvent>
20
21 #include "treeview.h"
22 #include "delegate.h"
23 #include "event.h"
24 #include "conference.h"
25 #include "eventmodel.h"
26
27 #ifdef MAEMO
28 #include <alarm.h>
29 #endif
30
31 #include <QDebug>
32
33 TreeView::TreeView(QWidget *aParent)
34     : QTreeView(aParent)
35 {
36     connect(this, SIGNAL(clicked(QModelIndex)), SLOT(handleItemClicked(QModelIndex)));
37 }
38
39 void TreeView::mouseReleaseEvent(QMouseEvent *aEvent)
40 {
41     QModelIndex index = currentIndex();
42     QPoint point = aEvent->pos();
43
44     // test whether we have handled the mouse event
45     if(!testForControlClicked(index,point))
46     {
47         // pass the event to the Base class, so item clicks/events are handled correctly
48         QTreeView::mouseReleaseEvent(aEvent);
49     }
50 }
51
52 // returns bool if some Control was clicked
53 bool TreeView::testForControlClicked(const QModelIndex &aIndex, const QPoint &aPoint)
54 {
55     bool handled = false;
56
57     if(!aIndex.isValid())
58         return handled;
59
60     int confId = Conference::activeConference();
61     QRect rect = visualRect(aIndex); // visual QRect of selected/clicked item in the list
62     Delegate *delegate = static_cast<Delegate*>(itemDelegate(aIndex));
63     switch(delegate->whichControlClicked(aIndex,aPoint))
64     {
65         case Delegate::FavouriteControlOn:
66         case Delegate::FavouriteControlOff:
67             {
68                 // handle Favourite Control clicked
69                 Event event = Event::getById(aIndex.data().toInt(),confId);
70
71                 QList<Event> conflicts = Event::conflictEvents(event.id(),Conference::activeConference());
72                 if(event.isFavourite())
73                     event.setFavourite(false);
74                 else
75                     event.setFavourite(true);
76                 event.update("favourite");
77
78                 qDebug() << " FAVOURITE [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.isFavourite();
79
80                 if(event.isFavourite())
81                 {
82                     // event has became 'favourite' and so 'conflicts' list may have changed
83                     conflicts = Event::conflictEvents(event.id(),Conference::activeConference());
84                 }
85
86                 // have to emit 'eventHasChanged' signal on all events in conflict
87                 for(int i=0; i<conflicts.count(); i++)
88                     emit(eventHasChanged(conflicts[i].id()));
89
90                 // since the Favourite icon has changed, update TreeViews accordingly
91                 // all TreeViews have to listen on this signal
92                 emit(eventHasChanged(event.id(),true));
93
94                 handled = true;
95             }
96             break;
97         case Delegate::AlarmControlOn:
98         case Delegate::AlarmControlOff:
99             {
100                 // handle Alarm Control clicked
101                 Event event = Event::getById(aIndex.data().toInt(),confId);
102                 if(event.hasAlarm())
103                 {
104                     event.setHasAlarm(false); // update DB
105 #ifdef MAEMO
106                     // remove alarm from the 'alarmd' alrms list
107                     Alarm alarm;
108                     alarm.deleteAlarm(event.id());
109                     // TODO: test if removing was successfull
110 #endif /* MAEMO */
111                 }
112                 else
113                 {
114                     event.setHasAlarm(true);
115 #ifdef MAEMO
116                     // add alarm to the 'alarmd'
117                     Alarm alarm;
118                     //int cookie = alarm.addAlarm(event.id(),QDateTime::currentDateTime().addSecs(10)); // testing
119                     int cookie = alarm.addAlarm(event.id(),event.start().addSecs(-15*60)); // 15 minutes before real start
120                     qDebug() << "cookie: " << cookie;
121 #endif /* MAEMO */
122                 }
123                 event.update("alarm");
124                 qDebug() << " ALARM [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.hasAlarm();
125                 // since the Alarm icon has changed, update TreeView accordingly
126                 // all TreeViews have to listen on this signal
127                 emit(eventHasChanged(event.id()));
128                 handled = true;
129             }
130             break;
131         case Delegate::MapControl:
132             {
133                 // handle Alarm Control clicked
134                 qDebug() << "MAP CLICKED: " << qVariantValue<QString>(aIndex.data());
135                 emit(requestForMap(aIndex));
136                 handled = true;
137             }
138         break;
139         case Delegate::WarningControl:
140         {
141
142             qDebug() << "WARNING CLICKED: " << qVariantValue<QString>(aIndex.data());
143             emit(requestForConflicts(aIndex));
144             handled = true;
145         }
146         break;
147         case Delegate::ControlNone:
148         default:
149             {
150                 // item was clicked, but not a control
151                 handled = false;
152             }
153     };
154
155     return handled;
156 }
157
158 void TreeView::handleItemClicked(const QModelIndex &index)
159 {
160     if(!index.parent().isValid()) // time-group
161     {
162         if(isExpanded(index))
163             setExpanded(index, false);
164         else
165             setExpanded(index, true);
166     }
167 }
168
169 void TreeView::setAllExpanded(bool aExpanded)
170 {
171     for(int i=0; i<model()->rowCount(QModelIndex()); i++)
172     {
173         setExpanded(model()->index(i,0,QModelIndex()),aExpanded);
174     }
175 }
176