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