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