[66327a0] | 1 | #include <QMouseEvent> |
---|
| 2 | |
---|
| 3 | #include "treeview.h" |
---|
| 4 | #include "delegate.h" |
---|
[680a4da] | 5 | #include "event.h" |
---|
[67c59a7] | 6 | #include "eventmodel.h" |
---|
[66327a0] | 7 | |
---|
[6bd729b] | 8 | #ifdef MAEMO |
---|
| 9 | #include <alarm.h> |
---|
| 10 | #endif |
---|
| 11 | |
---|
[66327a0] | 12 | #include <QDebug> |
---|
| 13 | |
---|
| 14 | TreeView::TreeView(QWidget *aParent) |
---|
| 15 | : QTreeView(aParent) |
---|
| 16 | { |
---|
[8fe9bd2] | 17 | connect(this, SIGNAL(clicked(QModelIndex)), SLOT(handleItemClicked(QModelIndex))); |
---|
[66327a0] | 18 | } |
---|
| 19 | |
---|
| 20 | void TreeView::mouseReleaseEvent(QMouseEvent *aEvent) |
---|
| 21 | { |
---|
| 22 | QModelIndex index = currentIndex(); |
---|
| 23 | QPoint point = aEvent->pos(); |
---|
| 24 | |
---|
[c53a3f4] | 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 | } |
---|
[66327a0] | 31 | } |
---|
| 32 | |
---|
[c53a3f4] | 33 | // returns bool if some Control was clicked |
---|
| 34 | bool TreeView::testForControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) |
---|
[66327a0] | 35 | { |
---|
[c53a3f4] | 36 | bool handled = false; |
---|
| 37 | |
---|
[66327a0] | 38 | if(!aIndex.isValid()) |
---|
[c53a3f4] | 39 | return handled; |
---|
[66327a0] | 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 | { |
---|
[680a4da] | 45 | case Delegate::FavouriteControlOn: |
---|
| 46 | case Delegate::FavouriteControlOff: |
---|
[66327a0] | 47 | { |
---|
| 48 | // handle Favourite Control clicked |
---|
[680a4da] | 49 | Event event = Event::getById(aIndex.data().toInt(),1); |
---|
| 50 | if(event.isFavourite()) |
---|
[c718a77] | 51 | event.setFavourite(false); |
---|
[680a4da] | 52 | else |
---|
| 53 | event.setFavourite(true); |
---|
| 54 | event.update("favourite"); |
---|
[c718a77] | 55 | qDebug() << " FAVOURITE [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.isFavourite(); |
---|
| 56 | // since the Favourite icon has changed, update TreeViews accordingly |
---|
| 57 | // all TreeViews have to listen on this signal |
---|
| 58 | emit(eventHasChanged(event.id())); |
---|
[c53a3f4] | 59 | handled = true; |
---|
[66327a0] | 60 | } |
---|
| 61 | break; |
---|
[680a4da] | 62 | case Delegate::AlarmControlOn: |
---|
| 63 | case Delegate::AlarmControlOff: |
---|
[66327a0] | 64 | { |
---|
| 65 | // handle Alarm Control clicked |
---|
[b6cd05c] | 66 | Event event = Event::getById(aIndex.data().toInt(),1); |
---|
| 67 | if(event.hasAlarm()) |
---|
| 68 | { |
---|
| 69 | event.setHasAlarm(false); // update DB |
---|
[6bd729b] | 70 | #ifdef MAEMO |
---|
| 71 | // remove alarm from the 'alarmd' alrms list |
---|
| 72 | Alarm alarm; |
---|
| 73 | alarm.deleteAlarm(event.id()); |
---|
| 74 | // TODO: test if removing was successfull |
---|
| 75 | #endif /* MAEMO */ |
---|
[b6cd05c] | 76 | } |
---|
| 77 | else |
---|
| 78 | { |
---|
| 79 | event.setHasAlarm(true); |
---|
[6bd729b] | 80 | #ifdef MAEMO |
---|
| 81 | // add alarm to the 'alarmd' |
---|
| 82 | Alarm alarm; |
---|
| 83 | int cookie = alarm.addAlarm(event.id(),QDateTime::currentDateTime().addSecs(10)); |
---|
| 84 | qDebug() << "cookie: " << cookie; |
---|
| 85 | #endif /* MAEMO */ |
---|
[b6cd05c] | 86 | } |
---|
| 87 | event.update("alarm"); |
---|
[c718a77] | 88 | qDebug() << " ALARM [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.hasAlarm(); |
---|
[b6cd05c] | 89 | // since the Alarm icon has changed, update TreeView accordingly |
---|
[c718a77] | 90 | // all TreeViews have to listen on this signal |
---|
| 91 | emit(eventHasChanged(event.id())); |
---|
[c53a3f4] | 92 | handled = true; |
---|
[66327a0] | 93 | } |
---|
| 94 | break; |
---|
[03ff157] | 95 | case Delegate::MapControl: |
---|
| 96 | { |
---|
| 97 | // handle Alarm Control clicked |
---|
| 98 | qDebug() << "MAP CLICKED: " << qVariantValue<QString>(aIndex.data()); |
---|
[59c6cfe] | 99 | emit(requestForMap(aIndex)); |
---|
[c53a3f4] | 100 | handled = true; |
---|
[03ff157] | 101 | } |
---|
| 102 | break; |
---|
[a5c1179] | 103 | case Delegate::WarningControlOff: |
---|
[3f3e22d] | 104 | case Delegate::WarningControlOn: |
---|
[a5c1179] | 105 | { |
---|
[3f3e22d] | 106 | |
---|
| 107 | qDebug() << "WARNING CLICKED: " << qVariantValue<QString>(aIndex.data()); |
---|
[a5c1179] | 108 | // TODO: implement |
---|
[3f3e22d] | 109 | emit(requestForWarning(aIndex)); |
---|
[a5c1179] | 110 | handled = true; |
---|
| 111 | } |
---|
| 112 | break; |
---|
[66327a0] | 113 | case Delegate::ControlNone: |
---|
| 114 | default: |
---|
| 115 | { |
---|
| 116 | // item was clicked, but not a control |
---|
[c53a3f4] | 117 | handled = false; |
---|
[66327a0] | 118 | } |
---|
| 119 | }; |
---|
[c53a3f4] | 120 | |
---|
| 121 | return handled; |
---|
[66327a0] | 122 | } |
---|
| 123 | |
---|
[8fe9bd2] | 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 | |
---|
[b8a3ad1] | 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 | |
---|