|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Day view control of calendar |
|
15 * |
|
16 */ |
|
17 |
|
18 //System includes |
|
19 #include <QGesture> |
|
20 #include <QGraphicsLinearLayout> |
|
21 #include <hbabstractviewitem.h> |
|
22 #include <hbinstance.h> |
|
23 #include <hbaction.h> |
|
24 |
|
25 //User includes |
|
26 #include "calendayitemview.h" |
|
27 #include "calendayutils.h" |
|
28 #include "calendaycontainer.h" |
|
29 #include "calendayinfo.h" |
|
30 #include "calendaymodel.h" |
|
31 #include "calenservices.h" |
|
32 #include "calencontext.h" |
|
33 #include "CalenUid.h" |
|
34 |
|
35 |
|
36 // ----------------------------------------------------------------------------- |
|
37 // CalenDayItemView() |
|
38 // Constructor |
|
39 // ----------------------------------------------------------------------------- |
|
40 // |
|
41 CalenDayItemView::CalenDayItemView( MCalenServices &services, HbModelIterator *iterator, QGraphicsItem *parent ) |
|
42 : HbAbstractItemView(mContainer = new CalenDayContainer(), iterator, parent), |
|
43 mServices( services ) |
|
44 { |
|
45 mInfo = new CalenDayInfo( CalenDayInfo::ETwo ); |
|
46 mContainer->setDayInfo( mInfo ); |
|
47 |
|
48 //setup Selection Model |
|
49 QItemSelectionModel* selModel = new QItemSelectionModel(this->model()); |
|
50 if ( selModel ) { |
|
51 setSelectionMode( HbAbstractItemView::SingleSelection ); |
|
52 setSelectionModel( selModel ); |
|
53 } |
|
54 |
|
55 setScrollDirections(Qt::Vertical); |
|
56 setScrollingStyle(HbScrollArea::PanWithFollowOn); |
|
57 setVerticalScrollBarPolicy(HbScrollArea::ScrollBarAlwaysOff); |
|
58 |
|
59 grabGesture(Qt::PanGesture); |
|
60 ungrabGesture(Qt::SwipeGesture); |
|
61 |
|
62 //TODO: KG: Animate item pressed?! HbAbstractItemView::TouchDown |
|
63 setEnabledAnimations( HbAbstractItemView::None ); |
|
64 |
|
65 // Get the width of content and fix its size |
|
66 qreal contentWidth = CalenDayUtils::instance()->contentWidth(); |
|
67 setMinimumWidth(contentWidth); |
|
68 setMaximumWidth(contentWidth); |
|
69 |
|
70 setupSlots(); |
|
71 setupContextMenu(); |
|
72 } |
|
73 |
|
74 // ----------------------------------------------------------------------------- |
|
75 // ~CalenDayItemView() |
|
76 // Destructor |
|
77 // ----------------------------------------------------------------------------- |
|
78 // |
|
79 CalenDayItemView::~CalenDayItemView() |
|
80 { |
|
81 delete mInfo; |
|
82 } |
|
83 |
|
84 // ----------------------------------------------------------------------------- |
|
85 // scrollTo() |
|
86 // |
|
87 // ----------------------------------------------------------------------------- |
|
88 // |
|
89 void CalenDayItemView::scrollTo(const QModelIndex &index, HbAbstractItemView::ScrollHint hint) |
|
90 { |
|
91 HbAbstractItemView::scrollTo(index, hint); |
|
92 } |
|
93 |
|
94 // ----------------------------------------------------------------------------- |
|
95 // reset() |
|
96 // |
|
97 // ----------------------------------------------------------------------------- |
|
98 // |
|
99 void CalenDayItemView::reset() |
|
100 { |
|
101 // CalenDayItemView::reset(); |
|
102 } |
|
103 |
|
104 // ----------------------------------------------------------------------------- |
|
105 // modelAboutToBeReset() |
|
106 // Handles signal that is emitted when reset() is called, before the model's |
|
107 // internal state (e.g. persistent model indexes) has been invalidated. |
|
108 // ----------------------------------------------------------------------------- |
|
109 // |
|
110 void CalenDayItemView::modelAboutToBeReset() |
|
111 { |
|
112 |
|
113 } |
|
114 |
|
115 // ----------------------------------------------------------------------------- |
|
116 // modelReset() |
|
117 // Handles signal that is emitted when reset() is called, after the model's |
|
118 // internal state (e.g. persistent model indexes) has been invalidated. |
|
119 // ----------------------------------------------------------------------------- |
|
120 // |
|
121 void CalenDayItemView::modelReset() |
|
122 { |
|
123 if ( !mInfo ) { |
|
124 return; |
|
125 } |
|
126 //reset day info |
|
127 mInfo->Reset(); |
|
128 |
|
129 CalenDayModel* model(0); |
|
130 model = static_cast<CalenDayModel*>(this->model()); |
|
131 if ( !model ) { |
|
132 return; |
|
133 } |
|
134 int rowCount = model->rowCount( ); |
|
135 SCalenApptInfo apptInfo; |
|
136 for( int row = 0; row < rowCount; row++ ) { |
|
137 QModelIndex idx = model->index( row, 0 ); |
|
138 QVariant variant = idx.data( CalenDayEntry ); |
|
139 AgendaEntry entry = variant.value<AgendaEntry>(); |
|
140 apptInfo.iIndex = idx; |
|
141 |
|
142 QDateTime start; |
|
143 QDateTime end; |
|
144 QDateTime currDate( model->modelDate() ); |
|
145 CalenDayUtils::instance()->getEventValidStartEndTime( start, end, |
|
146 entry, currDate ); |
|
147 |
|
148 apptInfo.iStartTime = start; |
|
149 apptInfo.iEndTime = end; |
|
150 |
|
151 TCalenInstanceId id = TCalenInstanceId::nullInstanceId();//index.row() - temporary ID |
|
152 id.mEntryLocalUid = row; |
|
153 id.mInstanceTime = apptInfo.iStartTime; |
|
154 apptInfo.iId = id; |
|
155 apptInfo.iColor = 0xffff; |
|
156 |
|
157 if ( entry.isTimedEntry() ) |
|
158 { |
|
159 apptInfo.iAllDay = false; |
|
160 mInfo->InsertTimedEvent( apptInfo ); |
|
161 } |
|
162 else if( entry.type() == AgendaEntry::TypeEvent) //all-day event |
|
163 { |
|
164 apptInfo.iAllDay = true; |
|
165 mInfo->InsertAlldayEvent( apptInfo ); |
|
166 } |
|
167 } |
|
168 |
|
169 HbAbstractItemView::reset(); |
|
170 } |
|
171 |
|
172 // ----------------------------------------------------------------------------- |
|
173 // scrollVertically() |
|
174 // |
|
175 // ----------------------------------------------------------------------------- |
|
176 // |
|
177 void CalenDayItemView::scrollVertically( const QPointF &newPosition ) |
|
178 { |
|
179 QPointF currentPos = contentWidget()->pos(); |
|
180 if (abs(newPosition.y()) != abs(currentPos.y())) { |
|
181 currentPos.setY(newPosition.y()); |
|
182 scrollContentsTo(currentPos, 0); |
|
183 } |
|
184 } |
|
185 |
|
186 // ----------------------------------------------------------------------------- |
|
187 // itemPressed() |
|
188 // This function is called when a touch down event is received within Abstract view item that is representing index. |
|
189 // ----------------------------------------------------------------------------- |
|
190 // |
|
191 void CalenDayItemView::itemPressed( const QPointF &position ) |
|
192 { |
|
193 Q_UNUSED( position ) |
|
194 } |
|
195 |
|
196 // ----------------------------------------------------------------------------- |
|
197 // itemReleased() |
|
198 // This function is called when a touch release event is received within Abstract view item that is representing index. |
|
199 // ----------------------------------------------------------------------------- |
|
200 // |
|
201 void CalenDayItemView::itemReleased( const QPointF &position ) |
|
202 { |
|
203 Q_UNUSED( position ) |
|
204 } |
|
205 |
|
206 // ----------------------------------------------------------------------------- |
|
207 // itemActivated() |
|
208 // This function is called when the item specified by index is activated by the user. |
|
209 // ----------------------------------------------------------------------------- |
|
210 // |
|
211 void CalenDayItemView::itemActivated( const QPointF &position ) |
|
212 { |
|
213 Q_UNUSED( position ) |
|
214 HbAbstractViewItem *item = qobject_cast<HbAbstractViewItem *>(sender()); |
|
215 QModelIndex index = item->modelIndex(); |
|
216 QItemSelectionModel* selModel( selectionModel() ); |
|
217 if ( index.isValid() && selModel) { |
|
218 selModel->select( index, QItemSelectionModel::Current | QItemSelectionModel::Select ); |
|
219 openSelectedItem(); |
|
220 } |
|
221 } |
|
222 |
|
223 // ----------------------------------------------------------------------------- |
|
224 // itemLongPressed() |
|
225 // This function is called when long press event is received within Abstract view item viewItem. |
|
226 // ----------------------------------------------------------------------------- |
|
227 // |
|
228 void CalenDayItemView::itemLongPressed(HbAbstractViewItem* item, QPointF coords) |
|
229 { |
|
230 QModelIndex index = item->modelIndex(); |
|
231 QItemSelectionModel* selModel( selectionModel() ); |
|
232 if ( index.isValid() && selModel && mContextMenu) { |
|
233 selModel->select( index, QItemSelectionModel::Current | QItemSelectionModel::Select ); |
|
234 mContextMenu->setPreferredPos( coords, HbPopup::BottomRightCorner ); |
|
235 mContextMenu->open(); |
|
236 } |
|
237 } |
|
238 |
|
239 // ----------------------------------------------------------------------------- |
|
240 // orientationChanged() |
|
241 // Slot which is called whenever the orientation of the device changes. |
|
242 // ----------------------------------------------------------------------------- |
|
243 // |
|
244 void CalenDayItemView::orientationChanged( Qt::Orientation orientation ) |
|
245 { |
|
246 // Update the width of screen |
|
247 qreal contentWidth = CalenDayUtils::instance()->contentWidth(); |
|
248 setMinimumWidth(contentWidth); |
|
249 setMaximumWidth(contentWidth); |
|
250 if( mContainer ) { |
|
251 mContainer->orientationChanged( orientation ); |
|
252 } |
|
253 } |
|
254 |
|
255 // ----------------------------------------------------------------------------- |
|
256 // openSelectedItem() |
|
257 // Opens currently selected model item. |
|
258 // ----------------------------------------------------------------------------- |
|
259 // |
|
260 void CalenDayItemView::openSelectedItem() |
|
261 { |
|
262 issueCommandOnSelectedItem( ECalenEventView ); |
|
263 } |
|
264 |
|
265 // ----------------------------------------------------------------------------- |
|
266 // editSelectedItem() |
|
267 // Edits currently selected model item. |
|
268 // ----------------------------------------------------------------------------- |
|
269 // |
|
270 void CalenDayItemView::editSelectedItem() |
|
271 { |
|
272 issueCommandOnSelectedItem( ECalenEditCurrentEntry ); |
|
273 } |
|
274 |
|
275 // ----------------------------------------------------------------------------- |
|
276 // deleteSelectedIten() |
|
277 // Deletes currently selected model item. |
|
278 // ----------------------------------------------------------------------------- |
|
279 // |
|
280 void CalenDayItemView::deleteSelectedItem() |
|
281 { |
|
282 issueCommandOnSelectedItem( ECalenDeleteCurrentEntry ); |
|
283 } |
|
284 |
|
285 // ----------------------------------------------------------------------------- |
|
286 // setupSlots() |
|
287 // Connects item view's slots. |
|
288 // ----------------------------------------------------------------------------- |
|
289 // |
|
290 void CalenDayItemView::setupSlots() |
|
291 { |
|
292 // Connect to main window's orientationChanged signal to handle orientation |
|
293 // switching |
|
294 QObject::connect(CalenDayUtils::instance()->mainWindow(), |
|
295 SIGNAL(orientationChanged(Qt::Orientation)), this, |
|
296 SLOT(orientationChanged(Qt::Orientation))); |
|
297 |
|
298 // Connect with model signals |
|
299 if (this->model()) { |
|
300 QObject::connect(this->model(), SIGNAL(modelAboutToBeReset()), this, |
|
301 SLOT(modelAboutToBeReset())); |
|
302 QObject::connect(this->model(), SIGNAL(modelReset()), this, |
|
303 SLOT(modelReset())); |
|
304 } |
|
305 if (mContainer) { |
|
306 // Connect with base class to enable handling item press by model index |
|
307 QObject::connect(mContainer, SIGNAL(itemCreated(HbAbstractViewItem *)), |
|
308 this, SLOT(itemCreated(HbAbstractViewItem*))); |
|
309 } |
|
310 QObject::connect(this, SIGNAL(pressed(const QPointF&)), this, |
|
311 SLOT(itemPressed(const QPointF&))); |
|
312 QObject::connect(this, SIGNAL(released(const QPointF&)), this, |
|
313 SLOT(itemReleased(const QPointF&))); |
|
314 QObject::connect(this, SIGNAL(activated(const QPointF&)), this, |
|
315 SLOT(const QPointF&)); |
|
316 QObject::connect(this, SIGNAL(longPressed(HbAbstractViewItem*, QPointF)), |
|
317 this, SLOT(itemLongPressed(HbAbstractViewItem*, QPointF))); |
|
318 } |
|
319 |
|
320 // ----------------------------------------------------------------------------- |
|
321 // setupSlots() |
|
322 // Creates Context menu. |
|
323 // ----------------------------------------------------------------------------- |
|
324 // |
|
325 void CalenDayItemView::setupContextMenu() |
|
326 { |
|
327 mContextMenu = new HbMenu(); |
|
328 HbAction *openAction = mContextMenu->addAction(hbTrId("txt_common_menu_open")); |
|
329 if (openAction) { |
|
330 connect(openAction, SIGNAL(triggered()), this, SLOT(openSelectedItem())); |
|
331 } |
|
332 HbAction *editAction = mContextMenu->addAction(hbTrId("txt_common_menu_edit")); |
|
333 if (editAction) { |
|
334 connect(editAction, SIGNAL(triggered()), this, SLOT(editSelectedItem())); |
|
335 } |
|
336 HbAction *deleteAction = mContextMenu->addAction(hbTrId("txt_common_menu_delete")); |
|
337 if (deleteAction) { |
|
338 connect(deleteAction, SIGNAL(triggered()), this, |
|
339 SLOT(deleteSelectedItem())); |
|
340 } |
|
341 } |
|
342 |
|
343 // ----------------------------------------------------------------------------- |
|
344 // issueCommandOnSelectedItem() |
|
345 // Sets context to currently selected model item and issues calendar command. |
|
346 // ----------------------------------------------------------------------------- |
|
347 // |
|
348 bool CalenDayItemView::issueCommandOnSelectedItem( int command ) |
|
349 { |
|
350 bool retVal(false); |
|
351 QItemSelectionModel* selModel(selectionModel()); |
|
352 if (!selModel){ |
|
353 return retVal; |
|
354 } |
|
355 QModelIndex index( selModel->currentIndex() ); |
|
356 if( index.isValid() ) { |
|
357 retVal = true; |
|
358 QVariant variant = index.data( CalenDayEntry ); |
|
359 AgendaEntry entry = variant.value<AgendaEntry>(); |
|
360 QDateTime start; |
|
361 QDateTime end; |
|
362 CalenDayModel* model(0); |
|
363 model = static_cast<CalenDayModel*>(this->model()); |
|
364 QDateTime currDate( model->modelDate() ); |
|
365 CalenDayUtils::instance()->getEventValidStartEndTime( start, end, |
|
366 entry, currDate ); |
|
367 TRAPD(error, |
|
368 mServices.Context().setFocusDateAndTimeAndInstance(start, |
|
369 TCalenInstanceId::create(entry)); |
|
370 mServices.IssueCommandL( command ); |
|
371 ); |
|
372 if( error ) { |
|
373 retVal = false; |
|
374 } |
|
375 clearSelectionInModel(); |
|
376 } |
|
377 return retVal; |
|
378 } |
|
379 |
|
380 // ----------------------------------------------------------------------------- |
|
381 // clearSelectionInModel() |
|
382 // Clears current selection in Selection Model. |
|
383 // ----------------------------------------------------------------------------- |
|
384 // |
|
385 void CalenDayItemView::clearSelectionInModel() |
|
386 { |
|
387 QItemSelectionModel* selModel(selectionModel()); |
|
388 if (selModel) { |
|
389 selModel->clear(); |
|
390 } |
|
391 } |
|
392 |
|
393 // End of File |