source: src/mvc/treeview.cpp @ 02c1e09

qt5
Last change on this file since 02c1e09 was 6bd729b, checked in by pavelpa <pavelpa@…>, 12 years ago

work on alarm

  • Property mode set to 100644
File size: 3.8 KB
Line 
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
14TreeView::TreeView(QWidget *aParent)
15    : QTreeView(aParent)
16{
17}
18
19void TreeView::mouseReleaseEvent(QMouseEvent *aEvent)
20{
21    QModelIndex index = currentIndex();
22    QPoint point = aEvent->pos();
23
24    testForControlClicked(index,point);
25
26    // pass the event to the Base class, so item clicks/events are handled correctly
27    QTreeView::mouseReleaseEvent(aEvent);
28}
29
30void TreeView::testForControlClicked(const QModelIndex &aIndex, const QPoint &aPoint)
31{
32    if(!aIndex.isValid())
33        return;
34
35    QRect rect = visualRect(aIndex); // visual QRect of selected/clicked item in the list
36    Delegate *delegate = static_cast<Delegate*>(itemDelegate(aIndex));
37    switch(delegate->whichControlClicked(aIndex,aPoint))
38    {
39        case Delegate::FavouriteControlOn:
40        case Delegate::FavouriteControlOff:
41            {
42                // handle Favourite Control clicked
43                Event event = Event::getById(aIndex.data().toInt(),1);
44                if(event.isFavourite())
45                {
46                    static_cast<Event*>(aIndex.internalPointer())->setFavourite(false); // list of events
47                    event.setFavourite(false); // update DB
48                }
49                else
50                {
51                    static_cast<Event*>(aIndex.internalPointer())->setFavourite(true); // list of events
52                    event.setFavourite(true);
53                }
54                qDebug() << " FAVOURITE [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.isFavourite();
55                event.update("favourite");
56                // since the Favourite icon has changed, update TreeView accordingly
57                static_cast<EventModel*>(model())->emitDataChangedSignal(aIndex,aIndex);
58            }
59            break;
60        case Delegate::AlarmControlOn:
61        case Delegate::AlarmControlOff:
62            {
63                // handle Alarm Control clicked
64                Event event = Event::getById(aIndex.data().toInt(),1);
65                if(event.hasAlarm())
66                {
67                    static_cast<Event*>(aIndex.internalPointer())->setHasAlarm(false); // list of events
68                    event.setHasAlarm(false); // update DB
69#ifdef MAEMO
70                    // remove alarm from the 'alarmd' alrms list
71                    Alarm alarm;
72                    alarm.deleteAlarm(event.id());
73                    // TODO: test if removing was successfull
74#endif /* MAEMO */
75                }
76                else
77                {
78                    static_cast<Event*>(aIndex.internalPointer())->setHasAlarm(true); // list of events
79                    event.setHasAlarm(true);
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 */
86                }
87                qDebug() << " ALARM [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.hasAlarm();
88                event.update("alarm");
89                // since the Alarm icon has changed, update TreeView accordingly
90                static_cast<EventModel*>(model())->emitDataChangedSignal(aIndex,aIndex);
91
92            }
93            break;
94        case Delegate::MapControl:
95            {
96                // handle Alarm Control clicked
97                qDebug() << "MAP CLICKED: " << qVariantValue<QString>(aIndex.data());
98                emit(requestForMap(aIndex));
99            }
100        break;
101        case Delegate::ControlNone:
102        default:
103            {
104                // item was clicked, but not a control
105            }
106    };
107}
108
Note: See TracBrowser for help on using the repository browser.