implemented method to force 'EventModel' emit a signal
[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 #include <QDebug>
9
10 TreeView::TreeView(QWidget *aParent)
11     : QTreeView(aParent)
12 {
13 }
14
15 void TreeView::mouseReleaseEvent(QMouseEvent *aEvent)
16 {
17     QModelIndex index = currentIndex();
18     QPoint point = aEvent->pos();
19
20     testForControlClicked(index,point);
21
22     // pass the event to the Base class, so item clicks/events are handled correctly
23     QTreeView::mouseReleaseEvent(aEvent);
24 }
25
26 void TreeView::testForControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) 
27 {
28     if(!aIndex.isValid())
29         return;
30
31     QRect rect = visualRect(aIndex); // visual QRect of selected/clicked item in the list
32     Delegate *delegate = static_cast<Delegate*>(itemDelegate(aIndex));
33     switch(delegate->whichControlClicked(aIndex,aPoint))
34     {
35         case Delegate::FavouriteControlOn:
36         case Delegate::FavouriteControlOff:
37             {
38                 // handle Favourite Control clicked
39                 Event event = Event::getById(aIndex.data().toInt(),1);
40                 if(event.isFavourite())
41                 {
42                     static_cast<Event*>(aIndex.internalPointer())->setFavourite(false); // list of events
43                     event.setFavourite(false); // update DB
44                 }
45                 else
46                 {
47                     static_cast<Event*>(aIndex.internalPointer())->setFavourite(true); // list of events
48                     event.setFavourite(true);
49                 }
50                 qDebug() << " FAVOURITE [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.isFavourite();
51                 event.update("favourite");
52                 // since the Favourite icon has changed, update TreeView accordingly
53                 static_cast<EventModel*>(model())->emitDataChangedSignal(aIndex,aIndex);
54             }
55             break;
56         case Delegate::AlarmControlOn:
57         case Delegate::AlarmControlOff:
58             {
59                 // handle Alarm Control clicked
60                 qDebug() << "ALARM CLICKED: " << qVariantValue<QString>(aIndex.data());
61             }
62             break;
63         case Delegate::MapControl:
64             {
65                 // handle Alarm Control clicked
66                 qDebug() << "MAP CLICKED: " << qVariantValue<QString>(aIndex.data());
67             }
68         break;
69         case Delegate::ControlNone:
70         default:
71             {
72                 // item was clicked, but not a control
73             }
74     };
75 }
76