Merge "modify Klockwork bug" into tizen_2.2
[platform/framework/native/uifw.git] / src / ui / inc / FUiCtrl_TableViewPresenter.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FUiCtrl_TableViewPresenter.h
20  * @brief       This is the implementation file for _TableViewPresenter class.
21  *
22  * This file contains the implementation of _TableViewPresenter class.
23  */
24
25
26 #ifndef _FUI_CTRL_INTERNAL_TABLE_VIEW_PRESENTER_H_
27 #define _FUI_CTRL_INTERNAL_TABLE_VIEW_PRESENTER_H_
28
29 #include <FUiCtrlTableView.h>
30 #include <FBaseColArrayListT.h>
31 #include "FUiCtrl_TableViewItem.h"
32 #include "FUiCtrl_ListViewModel.h"
33 #include "FUiCtrl_ScrollPanelPresenter.h"
34
35 namespace Tizen { namespace Ui { namespace Animations
36 {
37 class _VisualElement;
38 }}}
39
40 namespace Tizen { namespace Ui { namespace Controls
41 {
42
43 enum ItemAlignment
44 {
45         TABLE_VIEW_ITEM_ALIGNMENT_TOP = 0,
46         TABLE_VIEW_ITEM_ALIGNMENT_BOTTOM
47 };
48
49 enum ItemTouchReleasedEventState
50 {
51         TABLE_VIEW_ITEM_TOUCH_RELEASED_EVENT_NORMAL = 0,
52         TABLE_VIEW_ITEM_TOUCH_RELEASED_EVENT_FIRE,
53         TABLE_VIEW_ITEM_TOUCH_RELEASED_EVENT_UPDATE_TABLE_VIEW
54 };
55
56 struct _TableViewReorderInfo
57 {
58         int groupIndex;
59         int itemIndex;
60         int originGroupIndex;
61         int originItemIndex;
62         float touchPressedPositionY;
63         float itemBasisPositionY;
64         int isScrollDirectionUp;
65         float nonScrollableLimitArea;
66         bool blockedTouchReleaseState;
67         bool blockedScroll;
68         long long touchPressedTick;
69         Tizen::Graphics::FloatRectangle itemBounds;
70
71         _TableViewReorderInfo(void)
72                 : groupIndex(-1)
73                 , itemIndex(-1)
74                 , originGroupIndex(-1)
75                 , originItemIndex(-1)
76                 , touchPressedPositionY(0.0f)
77                 , itemBasisPositionY(0.0f)
78                 , isScrollDirectionUp(false)
79                 , nonScrollableLimitArea(0.0f)
80                 , blockedTouchReleaseState(false)
81                 , blockedScroll(false)
82                 , touchPressedTick(0)
83                 , itemBounds()
84         {
85
86         }
87 };
88
89 struct _TableViewSectionStringAlignment
90 {
91         HorizontalAlignment headerHorizontalAlignment;
92         HorizontalAlignment footerHorizontalAlignment;
93
94         _TableViewSectionStringAlignment(void)
95                 : headerHorizontalAlignment(ALIGNMENT_LEFT)
96                 , footerHorizontalAlignment(ALIGNMENT_RIGHT)
97         {
98         }
99
100         bool operator== (const _TableViewSectionStringAlignment& rhs) const;
101         bool operator!= (const _TableViewSectionStringAlignment& rhs) const;
102 };
103
104 struct _TableViewItemHeight
105 {
106         int groupIndex;
107         int itemIndex;
108         float itemHeight;
109         bool isFooterItem;
110
111         _TableViewItemHeight(void)
112                 : groupIndex(0)
113                 , itemIndex(-1)
114                 , itemHeight(0)
115                 , isFooterItem(false)
116         {
117         }
118
119         bool operator== (const _TableViewItemHeight& rhs) const;
120         bool operator!= (const _TableViewItemHeight& rhs) const;
121 };
122
123 struct _TableViewScrollToItemTag
124 {
125         int groupIndex;
126         int itemIndex;
127         TableViewScrollItemAlignment itemAlignment;
128         float shiftingDistance;
129         bool startedAnimation;
130
131         _TableViewScrollToItemTag(void)
132                 : groupIndex(-1)
133                 , itemIndex(-1)
134                 , itemAlignment(TABLE_VIEW_SCROLL_ITEM_ALIGNMENT_TOP)
135                 , shiftingDistance(0)
136                 , startedAnimation(false)
137         {
138
139         }
140 };
141
142 class _TableView;
143 class _ListViewModel;
144 class _TableViewItemProviderAdaptor;
145
146 class _TableViewPresenter
147         : public _ScrollPanelPresenter
148         , public _IListViewModelDelegate
149         , public Tizen::Ui::Animations::IVisualElementAnimationTickEventListener
150         , public Tizen::Ui::Animations::IVisualElementAnimationStatusEventListener
151         , virtual public Tizen::Base::Runtime::IEventListener
152 {
153 public:
154         _TableViewPresenter();
155         virtual ~_TableViewPresenter(void);
156
157         virtual result Construct(_TableView* pTableView);
158
159         bool Initialize(_TableView* pTableView);
160
161         _TableView* GetView(void) const;
162
163         _ListViewModel* GetModel(void) const;
164
165         // set ItemProvider
166         result SetItemProvider(const _TableViewItemProvider* pProvider);
167
168         void SetTableViewItemProviderAdaptor(_TableViewItemProviderAdaptor* pProviderAdaptor);
169
170         _TableViewItemProviderAdaptor* GetTableViewItemProviderAdaptor(void) const;
171
172         // event handler for window event
173         virtual void OnBoundsChanged(void);
174         virtual result Draw(void);
175         virtual void OnChangeLayout(_ControlOrientation orientation);
176
177         // Touch Event Previewer
178         virtual _UiTouchEventDelivery OnPreviewTouchPressed(const _Control& source, const _TouchInfo& touchInfo);
179         virtual _UiTouchEventDelivery OnPreviewTouchMoved(const _Control& source, const _TouchInfo& touchInfo);
180         virtual _UiTouchEventDelivery OnPreviewTouchReleased(const _Control& source, const _TouchInfo& touchInfo);
181         virtual _UiTouchEventDelivery OnPreviewTouchCanceled(const _Control& source, const _TouchInfo& touchInfo);
182
183         // Touch Event Listener
184         virtual bool OnTouchPressed(const _Control& source, const _TouchInfo& touchInfo);
185         virtual bool OnTouchMoved(const _Control& source, const _TouchInfo& touchInfo);
186         virtual bool OnTouchReleased(const _Control& source, const _TouchInfo& touchInfo);
187         virtual bool OnTouchCanceled(const _Control& source, const _TouchInfo& touchInfo);
188
189         // gestureListener Callback API
190         virtual bool OnFlickGestureDetected(_TouchFlickGestureDetector& gesture);
191         virtual bool OnFlickGestureCanceled(_TouchFlickGestureDetector& gesture);
192         virtual bool OnPreviewFlickGestureDetected(_TouchFlickGestureDetector& gesture);
193
194         virtual void OnVisualElementAnimationStarted(const Tizen::Ui::Animations::VisualElementAnimation& animation, const Tizen::Base::String& keyName, Tizen::Ui::Animations::VisualElement& target);
195         virtual void OnVisualElementAnimationRepeated(const Tizen::Ui::Animations::VisualElementAnimation& animation, const Tizen::Base::String& keyName, Tizen::Ui::Animations::VisualElement& target, long currentRepeatCount);
196         virtual void OnVisualElementAnimationFinished(const Tizen::Ui::Animations::VisualElementAnimation& animation, const Tizen::Base::String& keyName, Tizen::Ui::Animations::VisualElement& target, bool completedNormally);
197         virtual void OnTickOccurred(const Tizen::Ui::Animations::VisualElementAnimation& animation, const Tizen::Base::String& keyName, Tizen::Ui::Animations::VisualElement& target, const Tizen::Ui::Variant& currentValue);
198
199         // Timer Event
200         virtual void OnTimerExpired(Tizen::Base::Runtime::Timer& timer);
201
202         // accessibility
203         virtual bool OnAccessibilityItemRefreshed(const _AccessibilityContainer& control, const _AccessibilityElement& element, _AccessibilityFocusDirection direction);
204
205         // List model delegate
206         virtual bool OnOcurredOverflowItems(const int currentCashSize, const int overflowCount);
207
208         // Focus Ui
209         virtual bool OnKeyPressed(const _Control& source, const _KeyInfo& keyInfo);
210         virtual bool OnKeyReleased(const _Control& source, const _KeyInfo& keyInfo);
211         virtual void OnDrawFocus(void);
212         virtual void OnFocusModeStateChanged(void);
213
214         //Enable/Visible state change
215         void OnVisibleStateChanged(void);
216         void OnAncestorEnableStateChanged(const _Control& control);
217         void OnAncestorVisibleStateChanged(const _Control& control);
218         void OnFocusableStateChanged(bool focusableState);
219
220         void Dispose(void);
221
222         result RefreshTableView(int groupIndex, int itemIndex, TableViewRefreshType type, bool animation);
223         result RefreshAllItems(void);
224         result UpdateTableView(bool isRestoreAnnexFocusValue);
225
226         result GetItemIndexFromPosition(const Tizen::Graphics::FloatPoint& position, int& groupIndex, int& itemIndex) const;
227         result GetItemFromPosition(const Tizen::Graphics::FloatPoint& position, TableViewItemTag& itemPos) const;
228
229         result GetTopDrawnItem(TableViewItemTag& itemPos) const;
230         result SetTopDrawnItem(TableViewItemTag& itemPos);
231
232         result GetBottomDrawnItem(TableViewItemTag& itemPos) const;
233
234         result GetTopDrawnItemIndex(int& groupIndex, int& itemIndex) const;
235         result SetTopDrawnItemIndex(int groupIndex, int itemIndex);
236
237         result GetBottomDrawnItemIndex(int& groupIndex, int& itemIndex) const;
238         result SetBottomDrawnItemIndex(int groupIndex, int itemIndex);
239
240         int GetGroupCount(void) const;
241         int GetItemCountAt(int groupIndex) const;
242         bool HasSectionHeader(int groupIndex) const;
243         bool HasSectionFooter(int groupIndex) const;
244
245         float GetTopMargin(void) const;
246         result SetTopMargin(float topMargin);
247
248         float GetBottomMargin(void) const;
249         result SetBottomMargin(float bottomMargin);
250
251         float GetLeftMargin(void) const;
252         result SetLeftMargin(float leftMargin);
253
254         //Restore Focus
255         void SetAnnexFocused(bool isAnnexFocused);
256         bool IsAnnexFocused(void);
257         void SetTableViewFocused(bool focusStatus);
258         bool IsTableViewFocused(void);
259         result GetFocusedItemIndex(int& groupIndex, int& itemIndex) const;
260
261         bool IsItemChecked(int groupIndex, int itemIndex) const;
262         result SetItemChecked(int groupIndex, int itemIndex, bool checked);
263
264         bool IsItemEnabled(int groupIndex, int itemIndex) const;
265         result SetItemEnabled(int groupIndex, int itemIndex, bool enabled);
266
267         _TableViewItem* FindItem(TableViewItemTag& itemTag);
268         void UnloadItem(int groupIndex, int itemIndex);
269         _TableViewItem* LoadItem(int groupIndex, int itemIndex);
270         void RefreshItemLayout(int groupIndex, int itemIndex);
271         void GetFirstLoadedItemIndex(int& groupIndex, int& itemIndex) const;
272         void GetLastLoadedItemIndex(int& groupIndex, int& itemIndex) const;
273
274         result SetStatusChanged(bool changed);
275         bool IsStatusChanged(void) const;
276
277         result ExpandGroup(int groupIndex, bool withAnimation);
278         result CollapseGroup(int groupIndex, bool withAnimation);
279         bool IsGroupExpanded(int groupIndex) const;
280         void StopExpandCollapseAnimation(void);
281
282         result SetReorderMode(bool enabled);
283
284         bool CheckItemHeightAndRefreshLayout(TableViewItemTag itemTag, bool downScroll);
285
286         result ScrollByPixel(float scrollDistance);
287         void SetScrollEnabled(bool enable);
288         bool IsScrollEnabled(void) const;
289
290         bool GetFirstDrawnFlag(void) const;
291         int GetMaxItemCachingSize(void) const;
292         void ResetSweepItem(void);
293         void GetSweepItemIndex(int& groupIndex, int& itemIndex) const;
294         int GetPressedItemCount(void);
295
296         result OpenContextItem(int groupIndex, int itemIndex);
297         result CloseContextItem(int groupIndex, int itemIndex);
298         bool IsContextItemOpened(int groupIndex, int itemIndex) const;
299
300         result SetSectionHeaderTextHorizontalAlignment(int sectionIndex, HorizontalAlignment alignment);
301         HorizontalAlignment GetSectionHeaderTextHorizontalAlignment(int sectionIndex) const;
302         result SetSectionFooterTextHorizontalAlignment(int sectionIndex, HorizontalAlignment alignment);
303         HorizontalAlignment GetSectionFooterTextHorizontalAlignment(int sectionIndex) const;
304
305         void BlockAnimationCallback(bool blocked);
306
307         void FireItemTouchReleasedEventDuringPressing(int groupIndex, int itemIndex);
308
309         void SetFocusDuringOrientationChange(void);
310         bool OnTraversalControlFocusGained(void);
311         bool OnTraversalControlFocusLost(void);
312         void SetFocusItemOnPressedState(int groupIndex, int itemIndex);
313         void SetItemFocus(_TableViewItem* pItem, TableViewItemTag itemPos);
314         void RestoreEditCopyPasteManager(void);
315
316 protected:
317         virtual float ScrollToInternal(float targetPosition);
318         virtual void FadeInScrollBar(void);
319         virtual void FadeOutScrollBar(void);
320
321         // Update Layout
322         virtual void UpdateLayout(void);
323
324 private:
325         _TableViewPresenter(const _TableViewPresenter& rhs);
326         _TableViewPresenter& operator =(const _TableViewPresenter& rhs);
327
328         _TableView* __pTableView;
329         _ListViewModel* __pListModel;
330         _TableViewItemProviderAdaptor* __pProviderAdaptor;
331
332         bool PreloadItem(void);
333         bool PreloadItem(int topDrawnGroupIndex, int topDrawnItemIndex, float shiftingDistance);
334
335         bool GetFirstItem(TableViewItemTag& firstItem) const;
336         bool GetLastItem(TableViewItemTag& lastItem) const;
337         bool GetPreviousItemPosition(const TableViewItemTag& currentItem, TableViewItemTag& prevItem) const;
338         bool GetNextItemPosition(const TableViewItemTag& currentItem, TableViewItemTag& nextItem) const;
339         int GetItemCount(void) const;
340         float GetHeightOfAllItems(void) const;
341
342         void SetItemLayout(_TableViewItem* pItem, TableViewItemTag& itemPos);
343         float CalculateItemPositionY(int groupIndex, int itemIndex);
344         void AdjustLayoutItems(float scrollPosition);
345         bool IsCachingItemsTotalHeightLessThanViewHeight(void);
346
347         void ResetItemLayout(TableViewItemTag& topDrawnItemTag, float shiftingDistance);
348         void RefreshItemLayout(TableViewItemTag& topDrawnItemTag, TableViewItemTag& refreshItemTag, TableViewRefreshType type, bool animation);
349         void RefreshItemBounds(_TableViewItem* pItem, TableViewItemTag& itemPos);
350         void ScrollToItem(int groupIndex, int itemIndex, TableViewScrollItemAlignment itemAlignment, float shiftingDistance = 0.0f);
351
352         _TableViewItem* GetTableViewItemFromControl(const _Control& source);
353
354         void UnloadItem(TableViewItemTag& itemTag);
355         void DetachItem(TableViewItemTag& itemTag);
356         void DetachContextItem(TableViewItemTag& itemTag);
357         void DetachAllItem(bool removeItem);
358         void SetItemDrawingProperty(void);
359         void SetItemType(_TableViewItem* pItem, TableViewItemTag itemPosition);
360
361         result StartFastScrollTimer(void);
362         void StopFastScrollTimer(void);
363
364         bool IsEmpty(void) const;
365         result DrawEmptyTableView(void);
366
367         void LoadItemWithScrollPosition(float previousScrollPosition, float currentScrollPos);
368         bool GetTableViewItemPosition(_TableViewItem& item, TableViewItemTag& itemTag); // item must exist between from tag and end tag
369         int LoadAllItemsInGroup(int groupIndex, bool downward);
370         void SetLoadedItemsVisibleInGroup(int groupIndex, bool visible);
371         TableViewItemTag LoadItemsToBeVisible(const TableViewItemTag& from);
372         void AttachNextItemsToBottom(const TableViewItemTag& anchor);
373         bool IsAnyItemInGroupLoaded(int groupIndex) const;
374         bool IsAnyItemInLoadedIndex(int groupIndex, int itemIndex);
375         void ScrollToHideNonClientArea(TableViewItemTag& bottomTag);
376         void AdjustClientAreaBounds(bool reset, float dist = 0.0f);
377         void SetClientAreaHeight(float height);
378         void UnloadInvisibleItems(void);
379
380         // reordering
381         bool SelectReorderItem(int groupIndex, int itemIndex, bool directSelection);
382         bool ResetReorderItem(int groupIndex, int itemIndex);
383         bool DragSelectedItem(float distance, bool relativeCoordinate);
384         bool CheckReorderItemScrollAnimation(_TableViewItem *pItem);
385         bool CheckReorderItemPosition(_TableViewItem* pItem, TableViewItemTag& reorderItemTag);
386         void AdjustReorderItemLimitPositionY(float& positionY);
387         bool ReorderItem(int destinationGroupIndex, int destinationItemIndex);
388         void StartReorderScrollTimer(void);
389         void StopReorderScrollTimer(void);
390
391         // item height
392         float GetItemHeight(TableViewItemTag itemTag) const;
393         float SetItemHeight(TableViewItemTag itemTag, float height);
394         bool CreateItemHeightList(float defaultGroupItemHeight, float defaultItemHeight);
395         bool RestoreItemHeightList(float defaultGroupItemHeight, float defaultItemHeight);
396         void DeleteItemHeightList(void);
397         void PresetItemHeightList(void);
398         bool RefreshItemHeightList(int groupIndex, int itemIndex, TableViewRefreshType refreshType);
399
400         bool CreateSectionAlignmentList(void);
401         void DeleteSectionAlignmentList(void);
402         bool RefreshSectionAlignmentList(int groupIndex, TableViewRefreshType refreshType);
403
404         void CaptureAndStartRemoveItemAnimation(int groupIndex, int itemIndex);
405
406         void ShowTableViewCapturedItem(int groupIndex, int itemIndex, float height);
407         void SetLoadedItemsVisibleFromPosition(float position, bool visible);
408         void MoveLoadedItemsFromPosition(float position);
409         void ExpandGroupAnimationFinished(bool completedNormally);
410         void CollapseGroupAnimationFinished(bool completedNormally);
411
412
413         void SweepItem(float x);
414         void ResetSweptItem(void);
415         void AdjustSweptItemPosition(bool withAnimation = false);
416         void AdjustLoadedItemWidth(void);
417
418         bool IsValidDrawnItem(int groupIndex, int itemIndex);
419         void StopAllItemAnimation(void);
420
421         bool GetAccessibilityElementFocusedState(void);
422
423 private:
424         float __topMargin;
425         float __bottomMargin;
426         float __leftMargin;
427
428         bool __modelInitialized;
429         bool __firstDrawnFlag;
430         bool __statusChangedFlag;
431         bool __scrolling;
432
433         _ItemDrawingProperty* __pItemDrawingProperty;
434
435         Tizen::Ui::Animations::_VisualElement* __pBaseVisualElement;
436
437         bool __sweepOccured;
438         Tizen::Graphics::FloatPoint __sweptItemPosition;
439         TableViewItemTag __sweptItemTag;
440
441         TableViewItemTag __focusItemTag;
442         TableViewItemTag __focusItemTagOnTraversal;
443
444         TableViewItemTag __reservedScrollItemIndex;
445         TableViewScrollItemAlignment __reservedScrollItemAlignment;
446
447         bool __firstTouchMoved;
448
449         Tizen::Base::Runtime::Timer* __pReorderScrollTimer;
450
451         _TableViewReorderInfo __reorderInfo;
452
453         Tizen::Base::Collection::ArrayListT<_TableViewItemHeight> __itemHeightList;
454         Tizen::Base::Collection::ArrayListT<_TableViewSectionStringAlignment> __sectionAlignmentList;
455         float __itemTotalHeight;
456
457         TableViewItemTag __expandableItemTag;
458         Tizen::Ui::Animations::_VisualElement* __pCapturedItemVisualElement;
459
460         Tizen::Base::Runtime::Timer* __pFastScrollTimer;
461         bool __isFastScrollTimerEnabled;
462
463         float __scrollHeightOnFlickStarted;
464         float __scrollPositionOnFlickStarted;
465
466         bool __isAnimationCallbackBlocked;
467         bool __lockLoadItemWithScroll;
468
469         bool __isRestoreFocus;
470         bool __isAnnexFocused;
471         bool __isTableViewFocused;
472
473         int __itemTouchReleasedEventState;
474
475         _TableViewScrollToItemTag __scrollToItemTag;
476
477         bool __isOrientationChanged;
478         bool __isFocusedDuringOrientationChange;
479         bool __isUpdatingTableView;
480
481         static const int TABLEVIEW_MAX_ITEM_COUNT = 30;
482         static const int REORDER_SCROLL_ANIMATION_TIMER_DURATION = 10;
483         static const float REORDER_SCROLL_ANIMATION_DISTANCE;
484         static const int REORDER_TOUCH_UIACTIVATE_DURATION = 100;
485         static const int FAST_SCROLL_FADE_OUT_DURATION = 500;
486         static const float TABLEVIEW_DEFAULT_ITEM_HEIGHT_VALUE;
487
488         //animation
489         static const int REORDER_ITEM_MOVE_ANIMATION_DURATION = 113;
490         static const int ADD_ITEM_ANIMATION_DURATION = 333;
491         static const int REMOVE_ITEM_MOVE_ANIMATION_DURATION = 267;
492         static const int REFRESH_ITEM_ANIMATION_DELAY = 100;
493         static const int EXPAND_GROUP_ANIMATION_DURATION = 335;
494         static const int COLLAPSE_GROUP_ANIMATION_DURATION = 329;
495         static const int SWEEP_ITEM_ANIMATION_DURATION = 300;
496 };
497
498 }}} // Tizen::Ui::Controls
499
500 #endif // _FUI_CTRL_INTERNAL_TABLE_VIEW_PRESENTER_H_
501