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 | emit(requestForConflicts(aIndex)); |
---|
110 | handled = true; |
---|
111 | } |
---|
112 | break; |
---|
113 | case Delegate::ControlNone: |
---|
114 | default: |
---|
115 | { |
---|
116 | // item was clicked, but not a control |
---|
117 | handled = false; |
---|
118 | } |
---|
119 | }; |
---|
120 | |
---|
121 | return handled; |
---|
122 | } |
---|
123 | |
---|
124 | void TreeView::handleItemClicked(const QModelIndex &index) |
---|
125 | { |
---|
126 | if(!index.parent().isValid()) // time-group |
---|
127 | { |
---|
128 | if(isExpanded(index)) |
---|
129 | setExpanded(index, false); |
---|
130 | else |
---|
131 | setExpanded(index, true); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | void TreeView::setAllExpanded(bool aExpanded) |
---|
136 | { |
---|
137 | for(int i=0; i<model()->rowCount(QModelIndex()); i++) |
---|
138 | { |
---|
139 | setExpanded(model()->index(i,0,QModelIndex()),aExpanded); |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|