source: src/mvc/treeview.cpp @ 3f3e22d

qt5
Last change on this file since 3f3e22d was 3f3e22d, checked in by fortefr <fortefr@…>, 12 years ago

Warning handling

  • Property mode set to 100644
File size: 4.7 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    connect(this, SIGNAL(clicked(QModelIndex)), SLOT(handleItemClicked(QModelIndex)));
18}
19
20void 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
34bool 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                {
52                    static_cast<Event*>(aIndex.internalPointer())->setFavourite(false); // list of events
53                    event.setFavourite(false); // update DB
54                }
55                else
56                {
57                    static_cast<Event*>(aIndex.internalPointer())->setFavourite(true); // list of events
58                    event.setFavourite(true);
59                }
60                qDebug() << " FAVOURITE [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.isFavourite();
61                event.update("favourite");
62                // since the Favourite icon has changed, update TreeView accordingly
63                static_cast<EventModel*>(model())->emitDataChangedSignal(aIndex,aIndex);
64                handled = true;
65            }
66            break;
67        case Delegate::AlarmControlOn:
68        case Delegate::AlarmControlOff:
69            {
70                // handle Alarm Control clicked
71                Event event = Event::getById(aIndex.data().toInt(),1);
72                if(event.hasAlarm())
73                {
74                    static_cast<Event*>(aIndex.internalPointer())->setHasAlarm(false); // list of events
75                    event.setHasAlarm(false); // update DB
76#ifdef MAEMO
77                    // remove alarm from the 'alarmd' alrms list
78                    Alarm alarm;
79                    alarm.deleteAlarm(event.id());
80                    // TODO: test if removing was successfull
81#endif /* MAEMO */
82                }
83                else
84                {
85                    static_cast<Event*>(aIndex.internalPointer())->setHasAlarm(true); // list of events
86                    event.setHasAlarm(true);
87#ifdef MAEMO
88                    // add alarm to the 'alarmd'
89                    Alarm alarm;
90                    int cookie = alarm.addAlarm(event.id(),QDateTime::currentDateTime().addSecs(10));
91                    qDebug() << "cookie: " << cookie;
92#endif /* MAEMO */
93                }
94                qDebug() << " ALARM [" << qVariantValue<QString>(aIndex.data()) << "] -> " << event.hasAlarm();
95                event.update("alarm");
96                // since the Alarm icon has changed, update TreeView accordingly
97                static_cast<EventModel*>(model())->emitDataChangedSignal(aIndex,aIndex);
98                handled = true;
99            }
100            break;
101        case Delegate::MapControl:
102            {
103                // handle Alarm Control clicked
104                qDebug() << "MAP CLICKED: " << qVariantValue<QString>(aIndex.data());
105                emit(requestForMap(aIndex));
106                handled = true;
107            }
108        break;
109        case Delegate::WarningControlOff:
110        case Delegate::WarningControlOn:
111        {
112
113            qDebug() << "WARNING CLICKED: " << qVariantValue<QString>(aIndex.data());
114            // TODO: implement
115            emit(requestForWarning(aIndex));
116            handled = true;
117        }
118        break;
119        case Delegate::ControlNone:
120        default:
121            {
122                // item was clicked, but not a control
123                handled = false;
124            }
125    };
126
127    return handled;
128}
129
130void TreeView::handleItemClicked(const QModelIndex &index)
131{
132    if(!index.parent().isValid()) // time-group
133    {
134        if(isExpanded(index))
135            setExpanded(index, false);
136        else
137            setExpanded(index, true);
138    }
139}
140
Note: See TracBrowser for help on using the repository browser.