[dali_1.1.34] Merge branch 'devel/master'
[platform/core/uifw/dali-demo.git] / examples / item-view / item-view-example.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <sstream>
19 #include "shared/view.h"
20
21 #include <dali/dali.h>
22 #include <dali-toolkit/dali-toolkit.h>
23
24 using namespace Dali;
25 using namespace Dali::Toolkit;
26
27 namespace
28 {
29
30 enum AllImagesLayouts
31 {
32   SPIRAL_LAYOUT,
33   DEPTH_LAYOUT,
34   GRID_LAYOUT,
35
36   NUMBER_OF_LAYOUTS
37 };
38
39 const char* IMAGE_PATHS[] = {
40     DEMO_IMAGE_DIR "gallery-medium-1.jpg",
41     DEMO_IMAGE_DIR "gallery-medium-2.jpg",
42     DEMO_IMAGE_DIR "gallery-medium-3.jpg",
43     DEMO_IMAGE_DIR "gallery-medium-4.jpg",
44     DEMO_IMAGE_DIR "gallery-medium-5.jpg",
45     DEMO_IMAGE_DIR "gallery-medium-6.jpg",
46     DEMO_IMAGE_DIR "gallery-medium-7.jpg",
47     DEMO_IMAGE_DIR "gallery-medium-8.jpg",
48     DEMO_IMAGE_DIR "gallery-medium-9.jpg",
49     DEMO_IMAGE_DIR "gallery-medium-10.jpg",
50     DEMO_IMAGE_DIR "gallery-medium-11.jpg",
51     DEMO_IMAGE_DIR "gallery-medium-12.jpg",
52     DEMO_IMAGE_DIR "gallery-medium-13.jpg",
53     DEMO_IMAGE_DIR "gallery-medium-14.jpg",
54     DEMO_IMAGE_DIR "gallery-medium-15.jpg",
55     DEMO_IMAGE_DIR "gallery-medium-16.jpg",
56     DEMO_IMAGE_DIR "gallery-medium-17.jpg",
57     DEMO_IMAGE_DIR "gallery-medium-18.jpg",
58     DEMO_IMAGE_DIR "gallery-medium-19.jpg",
59     DEMO_IMAGE_DIR "gallery-medium-20.jpg",
60     DEMO_IMAGE_DIR "gallery-medium-21.jpg",
61     DEMO_IMAGE_DIR "gallery-medium-22.jpg",
62     DEMO_IMAGE_DIR "gallery-medium-23.jpg",
63     DEMO_IMAGE_DIR "gallery-medium-24.jpg",
64     DEMO_IMAGE_DIR "gallery-medium-25.jpg",
65     DEMO_IMAGE_DIR "gallery-medium-26.jpg",
66     DEMO_IMAGE_DIR "gallery-medium-27.jpg",
67     DEMO_IMAGE_DIR "gallery-medium-28.jpg",
68     DEMO_IMAGE_DIR "gallery-medium-29.jpg",
69     DEMO_IMAGE_DIR "gallery-medium-30.jpg",
70     DEMO_IMAGE_DIR "gallery-medium-31.jpg",
71     DEMO_IMAGE_DIR "gallery-medium-32.jpg",
72     DEMO_IMAGE_DIR "gallery-medium-33.jpg",
73     DEMO_IMAGE_DIR "gallery-medium-34.jpg",
74     DEMO_IMAGE_DIR "gallery-medium-35.jpg",
75     DEMO_IMAGE_DIR "gallery-medium-36.jpg",
76     DEMO_IMAGE_DIR "gallery-medium-37.jpg",
77     DEMO_IMAGE_DIR "gallery-medium-38.jpg",
78     DEMO_IMAGE_DIR "gallery-medium-39.jpg",
79     DEMO_IMAGE_DIR "gallery-medium-40.jpg",
80     DEMO_IMAGE_DIR "gallery-medium-41.jpg",
81     DEMO_IMAGE_DIR "gallery-medium-42.jpg",
82     DEMO_IMAGE_DIR "gallery-medium-43.jpg",
83     DEMO_IMAGE_DIR "gallery-medium-44.jpg",
84     DEMO_IMAGE_DIR "gallery-medium-45.jpg",
85     DEMO_IMAGE_DIR "gallery-medium-46.jpg",
86     DEMO_IMAGE_DIR "gallery-medium-47.jpg",
87     DEMO_IMAGE_DIR "gallery-medium-48.jpg",
88     DEMO_IMAGE_DIR "gallery-medium-49.jpg",
89     DEMO_IMAGE_DIR "gallery-medium-50.jpg",
90     DEMO_IMAGE_DIR "gallery-medium-51.jpg",
91     DEMO_IMAGE_DIR "gallery-medium-52.jpg",
92     DEMO_IMAGE_DIR "gallery-medium-53.jpg",
93 };
94
95 const unsigned int NUM_IMAGES = sizeof(IMAGE_PATHS) / sizeof(char*);
96
97 const char* BACKGROUND_IMAGE( "" );
98 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
99 const char* EDIT_IMAGE( DEMO_IMAGE_DIR "icon-edit.png" );
100 const char* EDIT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-edit-selected.png" );
101 const char* SPIRAL_LAYOUT_IMAGE( DEMO_IMAGE_DIR "icon-item-view-layout-spiral.png" );
102 const char* SPIRAL_LAYOUT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-item-view-layout-spiral-selected.png" );
103 const char* GRID_LAYOUT_IMAGE( DEMO_IMAGE_DIR "icon-item-view-layout-grid.png" );
104 const char* GRID_LAYOUT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-item-view-layout-grid-selected.png" );
105 const char* DEPTH_LAYOUT_IMAGE( DEMO_IMAGE_DIR "icon-item-view-layout-depth.png" );
106 const char* DEPTH_LAYOUT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-item-view-layout-depth-selected.png" );
107 const char* DELETE_IMAGE( DEMO_IMAGE_DIR "icon-delete.png" );
108 const char* DELETE_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-delete-selected.png" );
109 const char* REPLACE_IMAGE( DEMO_IMAGE_DIR "icon-replace.png" );
110 const char* REPLACE_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-replace-selected.png" );
111 const char* INSERT_IMAGE( DEMO_IMAGE_DIR "icon-insert.png" );
112 const char* INSERT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-insert-selected.png" );
113 const char* SELECTED_IMAGE( DEMO_IMAGE_DIR "item-select-check.png" );
114 const char* APPLICATION_TITLE( "ItemView" );
115
116 const char* SPIRAL_LABEL("Spiral");
117 const char* GRID_LABEL("Grid");
118 const char* DEPTH_LABEL("Depth");
119
120 const float ITEM_BORDER_SIZE = 2.0f;
121
122 const float DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT = 1.0f;
123 const float DEPTH_LAYOUT_ITEM_SIZE_FACTOR_LANDSCAPE = 0.8f;
124 const float DEPTH_LAYOUT_COLUMNS = 3.0f;
125
126 const float MIN_SWIPE_DISTANCE = 15.0f;
127 const float MIN_SWIPE_SPEED = 5.0f;
128
129 const float SELECTION_BORDER_WIDTH = 3.0f;
130 const float BUTTON_BORDER = -10.0f;
131 const float MENU_OPTION_HEIGHT(140.0f);
132 const float LABEL_TEXT_SIZE_Y = 20.0f;
133
134 const Vector3 INITIAL_OFFSCREEN_POSITION( 1000.0f, 0, -1000.0f );
135
136 const float SCROLL_TO_ITEM_ANIMATION_TIME = 5.f;
137
138 static Vector3 DepthLayoutItemSizeFunctionPortrait( float layoutWidth )
139 {
140   float width = ( layoutWidth / ( DEPTH_LAYOUT_COLUMNS + 1.0f ) ) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT;
141
142   // 1x1 aspect ratio
143   return Vector3(width, width, width);
144 }
145
146 static Vector3 DepthLayoutItemSizeFunctionLandscape( float layoutWidth )
147 {
148   float width = ( layoutWidth / ( DEPTH_LAYOUT_COLUMNS + 1.0f ) ) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_LANDSCAPE;
149
150   // 1x1 aspect ratio
151   return Vector3(width, width, width);
152 }
153
154 } // unnamed namespace
155
156 /**
157  * This example shows how to use ItemView UI control.
158  * There are three layouts created for ItemView, i.e., Spiral, Depth and Grid.
159  * There is one button in the upper-left corner for quitting the application and
160  * another button in the upper-right corner for switching between different layouts.
161  */
162 class ItemViewExample : public ConnectionTracker, public ItemFactory
163 {
164 public:
165
166   enum Mode
167   {
168     MODE_NORMAL,
169     MODE_REMOVE,
170     MODE_REMOVE_MANY,
171     MODE_INSERT,
172     MODE_INSERT_MANY,
173     MODE_REPLACE,
174     MODE_REPLACE_MANY,
175     MODE_LAST
176   };
177
178   /**
179    * Constructor
180    * @param application class, stored as reference
181    */
182   ItemViewExample( Application& application )
183   : mApplication( application ),
184     mMode( MODE_NORMAL ),
185     mOrientation( 0 ),
186     mCurrentLayout( SPIRAL_LAYOUT ),
187     mDurationSeconds( 0.25f )
188   {
189     // Connect to the Application's Init signal
190     mApplication.InitSignal().Connect(this, &ItemViewExample::OnInit);
191   }
192
193   /**
194    * This method gets called once the main loop of application is up and running
195    */
196   void OnInit(Application& app)
197   {
198     Stage stage = Dali::Stage::GetCurrent();
199     stage.KeyEventSignal().Connect(this, &ItemViewExample::OnKeyEvent);
200     stage.GetRootLayer().SetBehavior(Layer::LAYER_3D);
201
202     Vector2 stageSize = Stage::GetCurrent().GetSize();
203
204     // Creates a default view with a default tool bar.
205     // The view is added to the stage.
206
207     Layer contents = DemoHelper::CreateView( mApplication,
208                                              mView,
209                                              mToolBar,
210                                              BACKGROUND_IMAGE,
211                                              TOOLBAR_IMAGE,
212                                              "" );
213
214     // Create an edit mode button. (left of toolbar)
215     Toolkit::PushButton editButton = Toolkit::PushButton::New();
216     editButton.SetUnselectedImage( EDIT_IMAGE );
217     editButton.SetSelectedImage( EDIT_IMAGE_SELECTED );
218     editButton.ClickedSignal().Connect( this, &ItemViewExample::OnModeButtonClicked);
219     editButton.SetLeaveRequired( true );
220     mToolBar.AddControl( editButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalLeft, DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
221
222     // Create a layout toggle button. (right of toolbar)
223     mLayoutButton = Toolkit::PushButton::New();
224     mLayoutButton.SetUnselectedImage( SPIRAL_LAYOUT_IMAGE );
225     mLayoutButton.SetSelectedImage(SPIRAL_LAYOUT_IMAGE_SELECTED );
226     mLayoutButton.ClickedSignal().Connect( this, &ItemViewExample::OnLayoutButtonClicked);
227     mLayoutButton.SetLeaveRequired( true );
228     mToolBar.AddControl( mLayoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
229
230     // Create a delete button (bottom right of screen)
231     mDeleteButton = Toolkit::PushButton::New();
232     mDeleteButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
233     mDeleteButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
234     mDeleteButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
235     mDeleteButton.SetDrawMode( DrawMode::OVERLAY_2D );
236     mDeleteButton.SetUnselectedImage( DELETE_IMAGE );
237     mDeleteButton.SetSelectedImage( DELETE_IMAGE_SELECTED );
238     mDeleteButton.SetBackgroundImage( TOOLBAR_IMAGE );
239     mDeleteButton.SetSize( Vector2( stageSize.width * 0.15f, stageSize.width * 0.15f ) );
240     mDeleteButton.ClickedSignal().Connect( this, &ItemViewExample::OnDeleteButtonClicked);
241     mDeleteButton.SetLeaveRequired( true );
242     mDeleteButton.SetVisible( false );
243     stage.Add( mDeleteButton );
244
245     // Create an insert button (bottom right of screen)
246     mInsertButton = Toolkit::PushButton::New();
247     mInsertButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
248     mInsertButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
249     mInsertButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
250     mInsertButton.SetDrawMode( DrawMode::OVERLAY_2D );
251     mInsertButton.SetUnselectedImage( INSERT_IMAGE );
252     mInsertButton.SetSelectedImage( INSERT_IMAGE_SELECTED );
253     mInsertButton.SetBackgroundImage( TOOLBAR_IMAGE );
254     mInsertButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
255     mInsertButton.ClickedSignal().Connect( this, &ItemViewExample::OnInsertButtonClicked);
256     mInsertButton.SetLeaveRequired( true );
257     mInsertButton.SetVisible( false );
258     stage.Add( mInsertButton );
259
260     // Create an replace button (bottom right of screen)
261     mReplaceButton = Toolkit::PushButton::New();
262     mReplaceButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
263     mReplaceButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
264     mReplaceButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
265     mReplaceButton.SetDrawMode( DrawMode::OVERLAY_2D );
266     mReplaceButton.SetUnselectedImage( REPLACE_IMAGE );
267     mReplaceButton.SetSelectedImage( REPLACE_IMAGE_SELECTED );
268     mReplaceButton.SetBackgroundImage( TOOLBAR_IMAGE );
269     mReplaceButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
270     mReplaceButton.ClickedSignal().Connect( this, &ItemViewExample::OnReplaceButtonClicked);
271     mReplaceButton.SetLeaveRequired( true );
272     mReplaceButton.SetVisible( false );
273     stage.Add( mReplaceButton );
274
275     // Create the item view actor
276     mItemView = ItemView::New(*this);
277     mItemView.SetParentOrigin(ParentOrigin::CENTER);
278     mItemView.SetAnchorPoint(AnchorPoint::CENTER);
279
280     // Display item view on the stage
281     stage.Add( mItemView );
282     stage.GetRootLayer().SetBehavior( Layer::LAYER_3D );
283
284     // Create the layouts
285     mSpiralLayout = DefaultItemLayout::New( DefaultItemLayout::SPIRAL );
286     mDepthLayout = DefaultItemLayout::New( DefaultItemLayout::DEPTH );
287     mGridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
288
289     // Add the layouts to item view
290     mItemView.AddLayout(*mSpiralLayout);
291     mItemView.AddLayout(*mDepthLayout);
292     mItemView.AddLayout(*mGridLayout);
293
294     mItemView.SetMinimumSwipeDistance(MIN_SWIPE_DISTANCE);
295     mItemView.SetMinimumSwipeSpeed(MIN_SWIPE_SPEED);
296
297     // Activate the spiral layout
298     SetLayout( mCurrentLayout );
299     mItemView.SetKeyboardFocusable( true );
300     KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &ItemViewExample::OnKeyboardPreFocusChange );
301
302     // Set the title and icon to the current layout
303     SetLayoutTitle();
304     SetLayoutImage();
305
306     mLongPressDetector = LongPressGestureDetector::New();
307     mLongPressDetector.Attach( mItemView );
308     mLongPressDetector.DetectedSignal().Connect( this, &ItemViewExample::OnLongPress );
309   }
310
311   Actor OnKeyboardPreFocusChange( Actor current, Actor proposed, Control::KeyboardFocus::Direction direction )
312   {
313     if ( !current && !proposed  )
314     {
315       return mItemView;
316     }
317
318     return proposed;
319   }
320
321   /**
322    * Animate to a different layout
323    */
324   void ChangeLayout()
325   {
326     Animation animation = Animation::New( mDurationSeconds );
327     animation.FinishedSignal().Connect( this, &ItemViewExample::AnimationFinished );
328     animation.AnimateTo( Property( mItemView, Actor::Property::COLOR_ALPHA ), 0.0f );
329     animation.Play();
330   }
331
332   void AnimationFinished( Animation& )
333   {
334     SetLayout( mCurrentLayout );
335
336     Animation animation = Animation::New( mDurationSeconds );
337     animation.AnimateTo( Property( mItemView, Actor::Property::COLOR_ALPHA ), 1.0f );
338     animation.Play();
339   }
340
341   /**
342    * Switch to a different item view layout
343    */
344   void SetLayout( int layoutId )
345   {
346     Stage stage = Dali::Stage::GetCurrent();
347     switch( mCurrentLayout )
348     {
349       case SPIRAL_LAYOUT:
350       case DEPTH_LAYOUT:
351       {
352         stage.GetRootLayer().SetBehavior(Layer::LAYER_3D);
353         break;
354       }
355       case GRID_LAYOUT:
356       {
357         stage.GetRootLayer().SetBehavior(Layer::LAYER_2D);
358         break;
359       }
360     }
361
362     // Set the new orientation to the layout
363     mItemView.GetLayout(layoutId)->SetOrientation(static_cast<ControlOrientation::Type>(mOrientation / 90));
364
365     Vector2 stageSize = Stage::GetCurrent().GetSize();
366
367     if(layoutId == DEPTH_LAYOUT)
368     {
369       // Set up the depth layout according to the new orientation
370       if(Toolkit::IsVertical(mDepthLayout->GetOrientation()))
371       {
372         mDepthLayout->SetItemSize( DepthLayoutItemSizeFunctionPortrait( stageSize.width ) );
373       }
374       else
375       {
376         mDepthLayout->SetItemSize( DepthLayoutItemSizeFunctionLandscape( stageSize.height ) );
377       }
378     }
379
380     // Enable anchoring for depth layout only
381     mItemView.SetAnchoring(layoutId == DEPTH_LAYOUT);
382
383     // Activate the layout
384     mItemView.ActivateLayout( layoutId, Vector3(stageSize.x, stageSize.y, stageSize.x), 0.0f );
385   }
386
387   bool OnLayoutButtonClicked( Toolkit::Button button )
388   {
389     // Switch to the next layout
390     mCurrentLayout = (mCurrentLayout + 1) % mItemView.GetLayoutCount();
391
392     ChangeLayout();
393
394     SetLayoutTitle();
395     SetLayoutImage();
396
397     return true;
398   }
399
400   bool OnModeButtonClicked( Toolkit::Button button )
401   {
402     SwitchToNextMode();
403
404     return true;
405   }
406
407   void SwitchToNextMode()
408   {
409     switch( mMode )
410     {
411       case MODE_REMOVE:
412       {
413         ExitRemoveMode();
414         mMode = MODE_REMOVE_MANY;
415         EnterRemoveManyMode();
416         break;
417       }
418
419       case MODE_REMOVE_MANY:
420       {
421         ExitRemoveManyMode();
422         mMode = MODE_INSERT;
423         EnterInsertMode();
424         break;
425       }
426
427       case MODE_INSERT:
428       {
429         ExitInsertMode();
430         mMode = MODE_INSERT_MANY;
431         EnterInsertManyMode();
432         break;
433       }
434
435       case MODE_INSERT_MANY:
436       {
437         ExitInsertManyMode();
438         mMode = MODE_REPLACE;
439         EnterReplaceMode();
440         break;
441       }
442
443       case MODE_REPLACE:
444       {
445         ExitReplaceMode();
446         mMode = MODE_REPLACE_MANY;
447         EnterReplaceManyMode();
448         break;
449       }
450
451       case MODE_REPLACE_MANY:
452       {
453         ExitReplaceManyMode();
454         mMode = MODE_NORMAL;
455         SetLayoutTitle();
456         break;
457       }
458
459       case MODE_NORMAL:
460       default:
461       {
462         mMode = MODE_REMOVE;
463         EnterRemoveMode();
464         break;
465       }
466     }
467   }
468
469   void EnterRemoveMode()
470   {
471     SetTitle("Edit: Remove");
472
473     mTapDetector = TapGestureDetector::New();
474
475     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
476     {
477       mTapDetector.Attach(mItemView.GetChildAt(i));
478     }
479
480     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::RemoveOnTap );
481   }
482
483   void ExitRemoveMode()
484   {
485     mTapDetector.Reset();
486   }
487
488   void RemoveOnTap( Actor actor, const TapGesture& tap )
489   {
490     mItemView.RemoveItem( mItemView.GetItemId(actor), 0.5f );
491   }
492
493   void EnterRemoveManyMode()
494   {
495     SetTitle("Edit: Remove Many");
496
497     mDeleteButton.SetVisible( true );
498
499     mTapDetector = TapGestureDetector::New();
500
501     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
502     {
503       Actor child = mItemView.GetChildAt( i );
504       Actor box = child.FindChildByName( "CheckBox" );
505
506       if( box )
507       {
508         mTapDetector.Attach( child );
509         box.SetVisible( true );
510       }
511     }
512
513     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
514   }
515
516   void ExitRemoveManyMode()
517   {
518     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
519     {
520       Actor child = mItemView.GetChildAt( i );
521       Actor box = child.FindChildByName( "CheckBox" );
522
523       if( box )
524       {
525         box.SetVisible( false );
526
527         Actor tick = box.FindChildByName( "Tick" );
528         if( tick )
529         {
530           tick.SetVisible( false );
531         }
532       }
533     }
534
535     mTapDetector.Reset();
536
537     mDeleteButton.SetVisible( false );
538   }
539
540   void SelectOnTap( Actor actor, const TapGesture& tap )
541   {
542     Actor tick = actor.FindChildByName( "Tick" );
543     if( tick )
544     {
545       tick.SetVisible( !tick.IsVisible() );
546     }
547   }
548
549   void OnLongPress( Actor actor, const LongPressGesture& gesture )
550   {
551     switch( gesture.state )
552     {
553       case Gesture::Started:
554       {
555         const Size& size = Stage::GetCurrent().GetSize();
556
557         ItemRange range( 0u, 0u );
558         mItemView.GetItemsRange( range );
559
560         const unsigned int item = ( gesture.screenPoint.y < 0.5f * size.height ) ? range.begin : range.end;
561         mItemView.ScrollToItem( item, SCROLL_TO_ITEM_ANIMATION_TIME );
562
563         break;
564       }
565       case Gesture::Finished:
566       {
567         Property::Map attributes;
568         mItemView.DoAction( "stopScrolling", attributes );
569         break;
570       }
571       default:
572       {
573         break;
574       }
575     }
576   }
577
578   bool OnDeleteButtonClicked( Toolkit::Button button )
579   {
580     ItemIdContainer removeList;
581
582     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
583     {
584       Actor child = mItemView.GetChildAt( i );
585       Actor tick = child.FindChildByName( "Tick" );
586
587       if( tick && tick.IsVisible() )
588       {
589         removeList.push_back( mItemView.GetItemId(child) );
590       }
591     }
592
593     if( ! removeList.empty() )
594     {
595       mItemView.RemoveItems( removeList, 0.5f );
596     }
597
598     return true;
599   }
600
601   void EnterInsertMode()
602   {
603     SetTitle("Edit: Insert");
604
605     mTapDetector = TapGestureDetector::New();
606
607     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
608     {
609       mTapDetector.Attach( mItemView.GetChildAt(i) );
610     }
611
612     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::InsertOnTap );
613   }
614
615   void ExitInsertMode()
616   {
617     mTapDetector.Reset();
618   }
619
620   void InsertOnTap( Actor actor, const TapGesture& tap )
621   {
622     ItemId id = mItemView.GetItemId( actor );
623
624     Actor newActor = NewItem( rand() );
625
626     mItemView.InsertItem( Item(id,newActor), 0.5f );
627   }
628
629   void EnterInsertManyMode()
630   {
631     SetTitle("Edit: Insert Many");
632
633     mInsertButton.SetVisible( true );
634
635     mTapDetector = TapGestureDetector::New();
636
637     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
638     {
639       Actor child = mItemView.GetChildAt( i );
640       Actor box = child.FindChildByName( "CheckBox" );
641
642       if( box )
643       {
644         mTapDetector.Attach( child );
645         box.SetVisible( true );
646       }
647     }
648
649     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
650   }
651
652   void ExitInsertManyMode()
653   {
654     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
655     {
656       Actor child = mItemView.GetChildAt( i );
657       Actor box = child.FindChildByName( "CheckBox" );
658
659       if( box )
660       {
661         box.SetVisible( false );
662
663         Actor tick = box.FindChildByName( "Tick" );
664         if( tick )
665         {
666           tick.SetVisible( false );
667         }
668       }
669     }
670
671     mTapDetector.Reset();
672
673     mInsertButton.SetVisible( false );
674   }
675
676   bool OnInsertButtonClicked( Toolkit::Button button )
677   {
678     ItemContainer insertList;
679
680     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
681     {
682       Actor child = mItemView.GetChildAt( i );
683       Actor tick = child.FindChildByName( "Tick" );
684
685       if( tick && tick.IsVisible() )
686       {
687         insertList.push_back( Item( mItemView.GetItemId(child), NewItem(rand()) ) );
688       }
689     }
690
691     if( ! insertList.empty() )
692     {
693       mItemView.InsertItems( insertList, 0.5f );
694     }
695
696     return true;
697   }
698
699   void EnterReplaceMode()
700   {
701     SetTitle("Edit: Replace");
702
703     mTapDetector = TapGestureDetector::New();
704
705     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
706     {
707       mTapDetector.Attach(mItemView.GetChildAt(i));
708     }
709
710     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::ReplaceOnTap );
711   }
712
713   void ReplaceOnTap( Actor actor, const TapGesture& tap )
714   {
715     mItemView.ReplaceItem( Item( mItemView.GetItemId(actor), NewItem(rand()) ), 0.5f );
716   }
717
718   void ExitReplaceMode()
719   {
720     mTapDetector.Reset();
721   }
722
723   void EnterReplaceManyMode()
724   {
725     SetTitle("Edit: Replace Many");
726
727     mReplaceButton.SetVisible( true );
728
729     mTapDetector = TapGestureDetector::New();
730
731     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
732     {
733       Actor child = mItemView.GetChildAt( i );
734       Actor box = child.FindChildByName( "CheckBox" );
735
736       if( box )
737       {
738         mTapDetector.Attach( child );
739         box.SetVisible( true );
740       }
741     }
742
743     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
744   }
745
746   void ExitReplaceManyMode()
747   {
748     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
749     {
750       Actor child = mItemView.GetChildAt( i );
751       Actor box = child.FindChildByName( "CheckBox" );
752
753       if( box )
754       {
755         box.SetVisible( false );
756
757         Actor tick = box.FindChildByName( "Tick" );
758         if( tick )
759         {
760           tick.SetVisible( false );
761         }
762       }
763     }
764
765     mTapDetector.Reset();
766
767     mReplaceButton.SetVisible( false );
768   }
769
770   bool OnReplaceButtonClicked( Toolkit::Button button )
771   {
772     ItemContainer replaceList;
773
774     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
775     {
776       Actor child = mItemView.GetChildAt( i );
777       Actor tick = child.FindChildByName( "Tick" );
778
779       if( tick && tick.IsVisible() )
780       {
781         replaceList.push_back( Item( mItemView.GetItemId(child), NewItem(rand()) ) );
782       }
783     }
784
785     if( ! replaceList.empty() )
786     {
787       mItemView.ReplaceItems( replaceList, 0.5f );
788     }
789
790     return true;
791   }
792
793   void SetLayoutTitle()
794   {
795     if( MODE_NORMAL == mMode )
796     {
797       std::stringstream ss(APPLICATION_TITLE);
798       switch(mCurrentLayout)
799       {
800       case SPIRAL_LAYOUT:
801         ss << APPLICATION_TITLE << ": " << SPIRAL_LABEL;
802         break;
803       case GRID_LAYOUT:
804         ss << APPLICATION_TITLE << ": " << GRID_LABEL;
805         break;
806       case DEPTH_LAYOUT:
807         ss << APPLICATION_TITLE << ": " << DEPTH_LABEL;
808         break;
809       default:
810         break;
811       }
812       SetTitle(ss.str());
813     }
814   }
815
816   void SetLayoutImage()
817   {
818     if( mLayoutButton )
819     {
820       switch( mCurrentLayout )
821       {
822         case SPIRAL_LAYOUT:
823         {
824           mLayoutButton.SetUnselectedImage( SPIRAL_LAYOUT_IMAGE );
825           mLayoutButton.SetSelectedImage( SPIRAL_LAYOUT_IMAGE_SELECTED );
826           break;
827         }
828
829         case GRID_LAYOUT:
830         {
831           mLayoutButton.SetUnselectedImage( GRID_LAYOUT_IMAGE );
832           mLayoutButton.SetSelectedImage( GRID_LAYOUT_IMAGE_SELECTED );
833           break;
834         }
835
836         case DEPTH_LAYOUT:
837         {
838           mLayoutButton.SetUnselectedImage( DEPTH_LAYOUT_IMAGE );
839           mLayoutButton.SetSelectedImage( DEPTH_LAYOUT_IMAGE_SELECTED );
840           break;
841         }
842
843         default:
844           break;
845       }
846     }
847   }
848
849 public: // From ItemFactory
850
851   /**
852    * Query the number of items available from the factory.
853    * The maximum available item has an ID of GetNumberOfItems() - 1.
854    */
855   virtual unsigned int GetNumberOfItems()
856   {
857     return NUM_IMAGES * 10;
858   }
859
860   /**
861    * Create an Actor to represent a visible item.
862    * @param itemId
863    * @return the created actor.
864    */
865   virtual Actor NewItem(unsigned int itemId)
866   {
867     // Create an image view for this item
868     ImageView actor = ImageView::New( IMAGE_PATHS[ itemId % NUM_IMAGES ] );
869     actor.SetZ( 0.0f );
870     actor.SetPosition( INITIAL_OFFSCREEN_POSITION );
871
872     // Add a border image child actor
873     ImageView borderActor = ImageView::New();
874     borderActor.SetParentOrigin( ParentOrigin::CENTER );
875     borderActor.SetAnchorPoint( AnchorPoint::CENTER );
876     borderActor.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS );
877     borderActor.SetSizeModeFactor( Vector3( 2.0f * ITEM_BORDER_SIZE, 2.0f * ITEM_BORDER_SIZE, 0.0f ) );
878     borderActor.SetColorMode( USE_PARENT_COLOR );
879
880     Property::Map borderProperty;
881     borderProperty.Insert( "rendererType", "border" );
882     borderProperty.Insert( "borderColor", Color::WHITE );
883     borderProperty.Insert( "borderSize", ITEM_BORDER_SIZE );
884     borderProperty.Insert( "antiAliasing", true );
885     borderActor.SetProperty( ImageView::Property::IMAGE, borderProperty );
886
887     actor.Add(borderActor);
888
889     actor.SetKeyboardFocusable( true );
890
891     Vector3 spiralItemSize;
892     static_cast<ItemLayout&>(*mSpiralLayout).GetItemSize( 0u, Vector3( Stage::GetCurrent().GetSize() ), spiralItemSize );
893
894     // Add a checkbox child actor; invisible until edit-mode is enabled
895     ImageView checkbox = ImageView::New();
896     checkbox.SetName( "CheckBox" );
897     checkbox.SetColorMode( USE_PARENT_COLOR );
898     checkbox.SetParentOrigin( ParentOrigin::TOP_RIGHT );
899     checkbox.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
900     checkbox.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
901     checkbox.SetPosition( -SELECTION_BORDER_WIDTH, SELECTION_BORDER_WIDTH );
902     checkbox.SetZ( 0.1f );
903
904     Property::Map solidColorProperty;
905     solidColorProperty.Insert( "rendererType", "color" );
906     solidColorProperty.Insert( "mixColor", Vector4(0.f, 0.f, 0.f, 0.6f) );
907     checkbox.SetProperty( ImageView::Property::IMAGE, solidColorProperty );
908
909     if( MODE_REMOVE_MANY  != mMode &&
910         MODE_INSERT_MANY  != mMode &&
911         MODE_REPLACE_MANY != mMode )
912     {
913       checkbox.SetVisible( false );
914     }
915     borderActor.Add( checkbox );
916
917     ImageView tick = ImageView::New( SELECTED_IMAGE );
918     tick.SetName( "Tick" );
919     tick.SetColorMode( USE_PARENT_COLOR );
920     tick.SetParentOrigin( ParentOrigin::TOP_RIGHT );
921     tick.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
922     tick.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
923     tick.SetZ( 0.2f );
924     tick.SetVisible( false );
925     checkbox.Add( tick );
926
927     // Connect new items for various editing modes
928     if( mTapDetector )
929     {
930       mTapDetector.Attach( actor );
931     }
932
933     return actor;
934   }
935
936 private:
937
938   /**
939    * Sets/Updates the title of the View
940    * @param[in] title The new title for the view.
941    */
942   void SetTitle(const std::string& title)
943   {
944     if(!mTitleActor)
945     {
946       mTitleActor = DemoHelper::CreateToolBarLabel( "" );
947       // Add title to the tool bar.
948       mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HorizontalCenter );
949     }
950
951     mTitleActor.SetProperty( TextLabel::Property::TEXT, title );
952   }
953
954   /**
955    * Main key event handler
956    */
957   void OnKeyEvent(const KeyEvent& event)
958   {
959     if(event.state == KeyEvent::Down)
960     {
961       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
962       {
963         mApplication.Quit();
964       }
965     }
966   }
967
968 private:
969
970   Application& mApplication;
971   Mode mMode;
972
973   Toolkit::Control mView;
974   unsigned int mOrientation;
975
976   Toolkit::ToolBar mToolBar;
977   TextLabel mTitleActor;             ///< The Toolbar's Title.
978
979   ItemView mItemView;
980   unsigned int mCurrentLayout;
981   float mDurationSeconds;
982
983   ItemLayoutPtr mSpiralLayout;
984   ItemLayoutPtr mDepthLayout;
985   ItemLayoutPtr mGridLayout;
986
987   TapGestureDetector mTapDetector;
988   Toolkit::PushButton mLayoutButton;
989   Toolkit::PushButton mDeleteButton;
990   Toolkit::PushButton mInsertButton;
991   Toolkit::PushButton mReplaceButton;
992
993   LongPressGestureDetector mLongPressDetector;
994 };
995
996 void RunTest(Application& app)
997 {
998   ItemViewExample test(app);
999
1000   app.MainLoop();
1001 }
1002
1003 int DALI_EXPORT_API main(int argc, char **argv)
1004 {
1005   Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
1006
1007   RunTest(app);
1008
1009   return 0;
1010 }