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 | Event event = Event::getById(aIndex.data().toInt(),1); |
---|
61 | if(event.hasAlarm()) |
---|
62 | { |
---|
63 | static_cast<Event*>(aIndex.internalPointer())->setHasAlarm(false); // list of events |
---|
64 | event.setHasAlarm(false); // update DB |
---|
65 | } |
---|
66 | else |
---|
67 | { |
---|
68 | static_cast<Event*>(aIndex.internalPointer())->setHasAlarm(true); // list of events |
---|
69 | event.setHasAlarm(true); |
---|
70 | } |
---|
71 | qDebug() << " ALARM [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.hasAlarm(); |
---|
72 | event.update("alarm"); |
---|
73 | // since the Alarm icon has changed, update TreeView accordingly |
---|
74 | static_cast<EventModel*>(model())->emitDataChangedSignal(aIndex,aIndex); |
---|
75 | |
---|
76 | } |
---|
77 | break; |
---|
78 | case Delegate::MapControl: |
---|
79 | { |
---|
80 | // handle Alarm Control clicked |
---|
81 | qDebug() << "MAP CLICKED: " << qVariantValue<QString>(aIndex.data()); |
---|
82 | } |
---|
83 | break; |
---|
84 | case Delegate::ControlNone: |
---|
85 | default: |
---|
86 | { |
---|
87 | // item was clicked, but not a control |
---|
88 | } |
---|
89 | }; |
---|
90 | } |
---|
91 | |
---|