|
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: CalenDayContentWidget implementation. |
|
15 * |
|
16 */ |
|
17 |
|
18 // System includes |
|
19 #include <QGraphicsLinearLayout> |
|
20 #include <hbmodeliterator.h> |
|
21 |
|
22 // User includes |
|
23 #include "calendaymodelmanager.h" |
|
24 #include "calendaycontentwidget.h" |
|
25 #include "calendayitemview.h" |
|
26 #include "calendaycontentscrollarea.h" |
|
27 |
|
28 /*! |
|
29 \class CalenDayContentWidget |
|
30 \brief Content widget is a container class for horizontal layouted widgets. |
|
31 */ |
|
32 |
|
33 /*! |
|
34 \brief Constructor |
|
35 |
|
36 \param parent The parent of central widget |
|
37 */ |
|
38 CalenDayContentWidget::CalenDayContentWidget( |
|
39 CalenDayModelManager &modelManager, |
|
40 QGraphicsItem *parent) : |
|
41 HbWidget(parent), mLayout(NULL), mModelManager(modelManager) |
|
42 { |
|
43 mWidgets.clear(); |
|
44 initializeViews(parent); |
|
45 } |
|
46 |
|
47 /*! |
|
48 \brief Destructor |
|
49 */ |
|
50 CalenDayContentWidget::~CalenDayContentWidget() |
|
51 { |
|
52 // Just clear widget list - parent object will destroy its children |
|
53 mWidgets.clear(); |
|
54 } |
|
55 |
|
56 /*! |
|
57 \brief Adds the widget to central widget and layouts it horizontally. |
|
58 |
|
59 Ownership of item is passed to CalenDayContentWidget. |
|
60 |
|
61 \param item Widget to be added |
|
62 \param where Place where widget should be added (as first/last item) |
|
63 */ |
|
64 void CalenDayContentWidget::add(HbWidget* item, CalenWidgetPosition where) |
|
65 { |
|
66 // Create linear, horizontal layout if not exist |
|
67 if (!mLayout) { |
|
68 mLayout = new QGraphicsLinearLayout(Qt::Horizontal, NULL); |
|
69 mLayout->setContentsMargins(0.0, 0.0, 0.0, 0.0); |
|
70 mLayout->setSpacing(0.0); |
|
71 } |
|
72 |
|
73 switch (where) { |
|
74 case ECalenFirstWidget: { |
|
75 mLayout->insertItem(0, item); |
|
76 mWidgets.insert(0, item); |
|
77 break; |
|
78 } |
|
79 case ECalenLastWidget: |
|
80 default: { |
|
81 mLayout->addItem(item); |
|
82 mWidgets.append(item); |
|
83 } |
|
84 } |
|
85 |
|
86 // If layout has no parent - apply it to central widget |
|
87 if (mLayout && !mLayout->parentLayoutItem()) { |
|
88 setLayout(mLayout); |
|
89 } |
|
90 } |
|
91 |
|
92 /*! |
|
93 \brief Takes the widget and removes it from central widget. |
|
94 |
|
95 Ownership of item is passed to caller. |
|
96 |
|
97 \param which Indicates which widget should be taken (first/last) |
|
98 \return Pointer to HbWidget or NULL if widget was not found |
|
99 */ |
|
100 HbWidget* CalenDayContentWidget::take(CalenWidgetPosition which) |
|
101 { |
|
102 Q_ASSERT(mLayout); |
|
103 |
|
104 HbWidget* itemToTake = NULL; |
|
105 switch (which) { |
|
106 case ECalenFirstWidget: { |
|
107 itemToTake = mWidgets.takeFirst(); |
|
108 break; |
|
109 } |
|
110 case ECalenLastWidget: |
|
111 default: { |
|
112 itemToTake = mWidgets.takeLast(); |
|
113 } |
|
114 } |
|
115 |
|
116 if (itemToTake) { |
|
117 mLayout->removeItem(itemToTake); |
|
118 } |
|
119 |
|
120 return itemToTake; |
|
121 } |
|
122 |
|
123 /*! |
|
124 \brief Removed the widget from central widget. |
|
125 |
|
126 Widget is removed and deleted. |
|
127 |
|
128 \param which Indicates which widget should be removed (first/last) |
|
129 */ |
|
130 void CalenDayContentWidget::remove(CalenWidgetPosition which) |
|
131 { |
|
132 Q_ASSERT(mLayout); |
|
133 |
|
134 HbWidget* itemToRemove = NULL; |
|
135 switch (which) { |
|
136 case ECalenFirstWidget: { |
|
137 itemToRemove = mWidgets.takeFirst(); |
|
138 break; |
|
139 } |
|
140 case ECalenLastWidget: |
|
141 default: { |
|
142 itemToRemove = mWidgets.takeLast(); |
|
143 } |
|
144 } |
|
145 |
|
146 if (itemToRemove) { |
|
147 mLayout->removeItem(itemToRemove); |
|
148 delete itemToRemove; |
|
149 } |
|
150 } |
|
151 |
|
152 /*! |
|
153 \brief Performs relayout of widgets when it is needed. |
|
154 |
|
155 Relayout is done depending on last scroll move: |
|
156 - if widget was scrolled to next: first widget is taken and added as last |
|
157 - if widget was scrolled to prev: last widget is taken and inserted as first |
|
158 |
|
159 Emits widgetsRelayoutFinished SIGNAL when relayout is over. |
|
160 |
|
161 \param scrollTo Direction of last movement |
|
162 */ |
|
163 void CalenDayContentWidget::relayoutWidgets(CalenScrollDirection scrollTo) |
|
164 { |
|
165 HbWidget* widget = NULL; |
|
166 switch (scrollTo) { |
|
167 case ECalenScrollToNext: { |
|
168 widget = take(ECalenFirstWidget); |
|
169 add(widget, ECalenLastWidget); |
|
170 break; |
|
171 } |
|
172 case ECalenScrollToPrev: |
|
173 default: { |
|
174 widget = take(ECalenLastWidget); |
|
175 add(widget, ECalenFirstWidget); |
|
176 } |
|
177 } |
|
178 emit widgetsRelayoutFinished(scrollTo); |
|
179 } |
|
180 |
|
181 /*! |
|
182 \brief SLOT handles simultanous vertical scrolling of owned widgets. |
|
183 |
|
184 \param newPos New scroll position |
|
185 */ |
|
186 void CalenDayContentWidget::widgetScrolled(const QPointF &newPos) |
|
187 { |
|
188 for (int i = 0; i < mWidgets.count(); i++) { |
|
189 CalenDayItemView *view = |
|
190 static_cast<CalenDayItemView *> (mWidgets[i]); |
|
191 view->scrollVertically(newPos); |
|
192 } |
|
193 emit scrollPositionChanged(newPos); |
|
194 } |
|
195 |
|
196 /*! |
|
197 \brief Initializes content widgets and adds them to layout. |
|
198 |
|
199 \param parent Parent object |
|
200 */ |
|
201 void CalenDayContentWidget::initializeViews(QGraphicsItem *parent) |
|
202 { |
|
203 // Create item views |
|
204 HbModelIterator *iterator(0); |
|
205 |
|
206 iterator = new HbModelIterator(&mModelManager.getModel( |
|
207 CalenDayModelManager::PreviousDay)); |
|
208 CalenDayItemView *prevItemView = new CalenDayItemView( |
|
209 mModelManager.getServices(), iterator, 0); |
|
210 |
|
211 iterator = new HbModelIterator(&mModelManager.getModel( |
|
212 CalenDayModelManager::CurrentDay)); |
|
213 CalenDayItemView *currItemView = new CalenDayItemView( |
|
214 mModelManager.getServices(), iterator, 0); |
|
215 |
|
216 iterator = new HbModelIterator(&mModelManager.getModel( |
|
217 CalenDayModelManager::NextDay)); |
|
218 CalenDayItemView *nextItemView = new CalenDayItemView( |
|
219 mModelManager.getServices(), iterator, 0); |
|
220 |
|
221 // Connecting views with widgetScrolled SLOT |
|
222 // to support simultanous vertical scrolling |
|
223 connect(prevItemView, SIGNAL(scrollPositionChanged(const QPointF&)), this, |
|
224 SLOT(widgetScrolled(const QPointF&))); |
|
225 connect(currItemView, SIGNAL(scrollPositionChanged(const QPointF&)), this, |
|
226 SLOT(widgetScrolled(const QPointF&))); |
|
227 connect(nextItemView, SIGNAL(scrollPositionChanged(const QPointF&)), this, |
|
228 SLOT(widgetScrolled(const QPointF&))); |
|
229 |
|
230 // Install event filters to receive events necessary for gesture handling |
|
231 CalenDayContentScrollArea* scrollArea = |
|
232 static_cast<CalenDayContentScrollArea*> (parent); |
|
233 prevItemView->installEventFilter(scrollArea); |
|
234 currItemView->installEventFilter(scrollArea); |
|
235 nextItemView->installEventFilter(scrollArea); |
|
236 |
|
237 // Add views to layout |
|
238 add(prevItemView); |
|
239 add(currItemView); |
|
240 add(nextItemView); |
|
241 } |
|
242 |
|
243 // End of File |