Merge "Added style names for images and text labels of Slider" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / slider / slider-impl.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_SLIDER_H__
2 #define __DALI_TOOLKIT_INTERNAL_SLIDER_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/adaptor-framework/timer.h>
23 #include <dali/public-api/object/property-array.h>
24 #include <dali/public-api/object/property-map.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/public-api/controls/control-impl.h>
28 #include <dali-toolkit/public-api/controls/slider/slider.h>
29 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
30 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
31
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 class Button;
39
40 namespace Internal
41 {
42
43 class Slider;
44
45 typedef Dali::IntrusivePtr< Slider > SliderPtr;
46
47 /**
48  * @copydoc Toolkit::Slider
49  */
50 class Slider : public Control
51 {
52 public:
53
54   typedef Property::Array MarkList;
55
56   /**
57    * Create a new Slider.
58    *
59    * @return A public handle to the newly allocated Slider.
60    */
61   static Dali::Toolkit::Slider New();
62
63 public:
64
65   // Properties
66
67   /**
68    * Set marks from a list
69    *
70    * @param[in] marks The list of marks to set
71    */
72   void SetMarks( const MarkList& marks );
73
74   /**
75    * Get the list of marks
76    *
77    * @return The marks list
78    */
79   const MarkList& GetMarks() const;
80
81   /**
82    * Set if should snap to marks or not
83    *
84    * @param[in] snap Flag to snap to marks or not
85    */
86   void SetSnapToMarks( bool snap );
87
88   /**
89    * Return if snap to marks is set or not
90    *
91    * @return If snap to marks is set
92    */
93   bool GetSnapToMarks() const;
94
95   /**
96    * Set the value of the slider
97    *
98    * @param[in] value The value to set. Will be clamped to [lowerBound .. upperBound]
99    */
100   void SetValue( float value );
101
102   /**
103    * Get the value of the slider
104    *
105    * @return The current value of the slider
106    */
107   float GetValue() const;
108
109   /**
110    * Set hit region
111    *
112    * @param[in] region The hit region
113    */
114   void SetHitRegion( const Vector2& region );
115
116   /**
117    * Get hit region
118    *
119    * @return The hit region
120    */
121   const Vector2& GetHitRegion() const;
122
123   /**
124    * Set the track region
125    *
126    * @param[in] region The track region
127    */
128   void SetTrackRegion( const Vector2& region );
129
130   /**
131    * Get the track region
132    *
133    * @return The track region
134    */
135   const Vector2& GetTrackRegion() const;
136
137   /**
138    * @brief Set the disabled color.
139    *
140    * @param[in] color The disabled color.
141    */
142   void SetDisabledColor( const Vector4& color );
143
144   /**
145    * @brief Get disabled color
146    *
147    * @return The disabled color
148    */
149   Vector4 GetDisabledColor() const;
150
151   /**
152    * Set the value precision to be used for numbers in the slider
153    *
154    * @param[in] precision The number of decimal places to use for printing numbers
155    */
156   void SetValuePrecision( int precision );
157
158   /**
159    * Get value precision
160    *
161    * @return The value precision
162    */
163   int GetValuePrecision() const;
164
165   /**
166    * Show the popup
167    *
168    * @param[in] showPopup The show popup flag
169    */
170   void SetShowPopup( bool showPopup );
171
172   /**
173    * Get show value in popup
174    *
175    * @return The show value flag
176    */
177   bool GetShowPopup() const;
178
179   /**
180    * Set show value on handle
181    *
182    * @param[in] showValue The show value flag
183    */
184   void SetShowValue( bool showValue );
185
186   /**
187    * Get show value on handle
188    *
189    * @return The show value flag
190    */
191   bool GetShowValue() const;
192
193   /**
194    * Set enabled
195    *
196    * param[in] enabled Set the enabled flag
197    */
198   void SetEnabled( bool enabled );
199
200   /**
201    * Return if enabled or not
202    *
203    * @return If enabled
204    */
205   bool IsEnabled() const;
206
207   /**
208    * @brief Set the mark tolerance
209    *
210    * The tolerance is the percentage of the slider width for which snapping to
211    * marks occurs
212    *
213    * @param[in] tolerance The percentage of width for which to snap
214    */
215   void SetMarkTolerance( float tolerance );
216
217   /**
218    * Return the mark tolerance
219    *
220    * @return The tolerance set for snapping to marks
221    */
222   float GetMarkTolerance() const;
223
224 public:
225   //Signals
226
227   /**
228    * @copydoc Toolkit::Slider::ValueChangedSignal()
229    */
230   Toolkit::Slider::ValueChangedSignalType& ValueChangedSignal();
231
232   /**
233    * copydoc Toolkit::Slider::SlidingFinishedSignal()
234    */
235   Toolkit::Slider::ValueChangedSignalType& SlidingFinishedSignal();
236
237   /**
238    * @copydoc Toolkit::Slider::MarkReachedSignal()
239    */
240   Toolkit::Slider::MarkReachedSignalType& MarkReachedSignal();
241
242   /**
243    * Connects a callback function with the object's signals.
244    * @param[in] object The object providing the signal.
245    * @param[in] tracker Used to disconnect the signal.
246    * @param[in] signalName The signal to connect to.
247    * @param[in] functor A newly allocated FunctorDelegate.
248    * @return True if the signal was connected.
249    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
250    */
251   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName,
252                                FunctorDelegate* functor );
253
254   // Properties
255
256   /**
257    * Called when a property of an object of this type is set.
258    * @param[in] object The object whose property is set.
259    * @param[in] index The property index.
260    * @param[in] value The new property value.
261    */
262   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
263
264   /**
265    * Called to retrieve a property of an object of this type.
266    * @param[in] object The object whose property is to be retrieved.
267    * @param[in] index The property index.
268    * @return The current value of the property.
269    */
270   static Property::Value GetProperty( BaseObject* object, Property::Index propertyIndex );
271
272 protected:
273
274   /**
275    * Construct a new Slider.
276    */
277   Slider();
278
279   /**
280    * A reference counted object may only be deleted by calling Unreference()
281    */
282   virtual ~Slider();
283
284   /**
285    * @copydoc CustomActorImpl::OnSizeSet( const Vector3& size )
286    */
287   virtual void OnSizeSet( const Vector3& size );
288
289   /**
290    * @copydoc CustomActorImpl::OnRelayout
291    */
292   virtual void OnRelayout( const Vector2& size, RelayoutContainer& container );
293
294
295 private:
296
297   /**
298    * Domain is a from/to pair
299    */
300   struct Domain
301   {
302     Vector2 from;
303     Vector2 to;
304
305     Domain()
306     {
307     }
308     Domain( Vector2 fromVal, Vector2 toVal )
309         : from( fromVal ), to( toVal )
310     {
311     }
312   };
313
314   /**
315    * Slider states
316    */
317   enum SliderState
318   {
319     NORMAL,
320     DISABLED,
321     PRESSED,
322     FOCUSED
323   };
324
325 private:
326
327   /**
328    * @copydoc Toolkit::Control::OnInitialize()
329    */
330   virtual void OnInitialize();
331
332   /**
333    * Hit region touch
334    *
335    * @param[in] actor The actor the event is raised for
336    * @param[in] touch The touch info
337    * @return If touch is handled or not
338    */
339   bool OnTouch( Actor actor, const TouchData& touch );
340
341   /**
342    * Pan gesture event
343    *
344    * @param[in] actor The actor the event is raised for
345    * @param[in] gesture The pan event info
346    */
347   void OnPan( Actor actor, const PanGesture& gesture );
348
349   /**
350    * Map a position onto a domain and return the result as a percentage
351    *
352    * @param[in] point The point to map onto the domain
353    * @return The result as a percentage [0..1]
354    */
355   float MapPercentage( const Vector2& point );
356
357   /**
358    * Map a value in the range to a percentage
359    *
360    * @param[in] point The value in range [lowerBound..upperBound]
361    * @return The result as a percentage [0..1]
362    */
363   float MapValuePercentage( float value );
364
365   /**
366    * Convert a point in local hit space into domain space
367    *
368    * @param[in] x The x position to convert
369    * @return The x position in domain space
370    */
371   float HitSpaceToDomain( float x );
372
373   /**
374    * Map a percentage onto the slider's bounds
375    *
376    * @param[in] percent The current value of slider in percent
377    * @param[in] lowerBound The lower bound to map onto
378    * @param[in] upperBound The upper bound to map onto
379    * @return The value of percent mapped from lowerBound to upperBound
380    */
381   float MapBounds( float percent, float lowerBound, float upperBound );
382
383   /**
384    * Get the range of the valid values the slider handle can move between
385    *
386    * @param[in] currentSize The current size of the slider
387    * @return The range as a domain pair
388    */
389   Domain CalcDomain( const Vector2& currentSize );
390
391   /**
392    * Create the hit region
393    *
394    * @return The hit region actor
395    */
396   Actor CreateHitRegion();
397
398   /**
399    * Create the track for the slider
400    *
401    * @return The track actor
402    */
403   Toolkit::ImageView CreateTrack();
404
405   /**
406    * Create the progress track for the slider
407    *
408    * @return The track actor
409    */
410   Toolkit::ImageView CreateProgress();
411
412   /**
413    * Create the handle for the slider
414    *
415    * @return The created image handle
416    */
417   Toolkit::ImageView CreateHandle();
418
419   /**
420    * Create the popup arrow
421    *
422    * @return The created image handle
423    */
424   Toolkit::ImageView CreatePopupArrow();
425
426   /**
427    * Create the popup
428    *
429    * @return The created image handle
430    */
431   Toolkit::ImageView CreatePopup();
432
433   /**
434    * Create the textview for the popup
435    *
436    * @return The textview created for the popup
437    */
438   Toolkit::TextLabel CreatePopupText();
439
440   /**
441    * Create the value display for the slider
442    *
443    * @return The created root actor of the display
444    */
445   Actor CreateValueDisplay();
446
447   /**
448    * Set the skin based on the current state
449    */
450   void UpdateSkin();
451
452   /**
453    * Create all the children
454    */
455   void CreateChildren();
456
457   /**
458    * Create value popup
459    */
460   void AddPopup();
461
462   /**
463    * Remove value popup
464    */
465   void RemovePopup();
466
467   /**
468    * Display the popup for a set duration with the given value
469    *
470    * @param[in] value The value to display in the popup
471    */
472   void DisplayPopup( float value );
473
474   /**
475    * If there are marks present, filter the incoming percent based on snapping to any nearby marks
476    *
477    * @param[in] value The incoming value on the slider to filter
478    * @return The filtered percentage snapped to any nearby marks
479    */
480   float MarkFilter( float value );
481
482   /**
483    * If there are marks present, snap the incoming percent to the nearest mark
484    *
485    * @param[in] value The incoming value on the slider to snap
486    * @return The filtered percentage snapped to the nearest mark
487    */
488   float SnapToMark( float value );
489
490   /**
491    * Search for if a mark has been reached
492    *
493    * @param[in] value The value to search against
494    * @param[out] outIndex The index of the mark if found
495    * @return If a mark has been found to match percent
496    */
497   bool MarkReached( float value, int& outIndex );
498
499   /**
500    * Handler for when the value view needs to be hidden
501    *
502    * @return If handled or not
503    */
504   bool HideValueView();
505
506   /**
507    * Set value choosing whether to fire signals or not
508    *
509    * @paramp[in] value The value to set
510    * @param[in] raiseSignals Configure signals to be raised or not.
511    */
512   void DisplayValue( float value, bool raiseSignals );
513
514   /**
515    * Create the image for the track
516    *
517    * @param[in] filename The track image
518    */
519   void SetTrackVisual( const std::string& filename );
520
521   /**
522    * @brief Set the track visual from an Dali::Property::Map
523    *
524    * @param[in] map The Dali::Property::Map to use for to display
525    */
526   void SetTrackVisual( Dali::Property::Map map );
527
528   /**
529    * @brief Return the track image.
530    *
531    * @return The track image.
532    */
533   std::string GetTrackVisual();
534
535   /**
536    * Create the image for the progress bar
537    *
538    * @param[in] filename The progress bar image
539    */
540   void SetProgressVisual( const std::string& filename );
541
542   /**
543    * @brief Set the progress visual from an Dali::Property::Map
544    *
545    * @param[in] map The Dali::Property::Map to use for to display
546    */
547   void SetProgressVisual( Dali::Property::Map map );
548
549   /**
550    * @brief Return the progress bar image.
551    *
552    * @return The progress bar image if it exists.
553    */
554   std::string GetProgressVisual();
555
556   /**
557    * @brief Create the image for the popup
558    *
559    * @param[in] filename The popup image
560    */
561   void CreatePopupImage( const std::string& filename );
562
563   /**
564    * @brief Set the popup image
565    *
566    * @param[in] filename The popup image to set
567    */
568   void SetPopupVisual( const std::string& filename );
569
570   /**
571    * @brief Set the popup from an Dali::Property::Map
572    *
573    * @param[in] map The Dali::Property::Map to use for to display
574    */
575   void SetPopupVisual( Dali::Property::Map map );
576
577   /**
578    * @brief Return the popup image.
579    *
580    * @return The popup image if it exists.
581    */
582   std::string GetPopupVisual();
583
584   /**
585    * @brief Set the popup arrow image
586    *
587    * @param[in] filename The popup arrow image to set
588    */
589   void SetPopupArrowVisual( const std::string& filename );
590
591   /**
592    * @brief Set the popup arrow from an Dali::Property::Map
593    *
594    * @param[in] map The Dali::Property::Map to use for to display
595    */
596   void SetPopupArrowVisual( Dali::Property::Map map );
597
598   /**
599    * @brief Return the popup arrow image.
600    *
601    * @return The popup arrow image if it exists.
602    */
603   std::string GetPopupArrowVisual();
604
605   /**
606    * Create the image for the popup arrow
607    *
608    * @param[in] filename The popup arrow image to load and set
609    */
610   void CreatePopupArrowImage( const std::string& filename );
611
612   /**
613    * Set the size of the progress bar region
614    *
615    * @param[in] region The size of the region to set
616    */
617   void ResizeProgressRegion( const Vector2& region );
618
619   /**
620    * Create the image for the handle
621    *
622    * @param[in] filename The handle image
623    */
624   void SetHandleVisual( const std::string& filename );
625
626   /**
627    * @brief Set the handle visual from an Dali::Property::Map
628    *
629    * @param[in] map The Dali::Property::Map to use for to display
630    */
631   void SetHandleVisual( Property::Map map );
632
633   /**
634    * @brief Return the handle image.
635    *
636    * @return The handle image if it exists.
637    */
638   std::string GetHandleVisual();
639
640   /**
641    * Reset the size of the handle
642    *
643    * @param[in] size The size of the handle to set
644    */
645   void ResizeHandleSize( const Vector2& size );
646
647   /**
648    * Create and display the value on the handle
649    */
650   void CreateHandleValueDisplay();
651
652   /**
653    * Remove and destroy the handle value display
654    */
655   void DestroyHandleValueDisplay();
656
657   /**
658    * Set the size of the handle
659    *
660    * @param[in] size The handle size
661    */
662   void SetHandleSize( const Vector2& size );
663
664   /**
665    * Get the size of the handle
666    *
667    * @return The handle size
668    */
669   const Vector2& GetHandleSize() const;
670
671   /**
672    * Set the lower bound of the slider's value
673    *
674    * @param[in] bound The value to set for the lower bound
675    */
676   void SetLowerBound( float bound );
677
678   /**
679    * Get the lower bound of the slider's value
680    *
681    * @return The lower bound value
682    */
683   float GetLowerBound() const;
684
685   /**
686    * Set the upper bound of the slider's value
687    *
688    * @param[in] bound The value to set for the upper bound
689    */
690   void SetUpperBound( float bound );
691
692   /**
693    * Get the upper bound of the slider's value
694    *
695    * @return The upper bound value
696    */
697   float GetUpperBound() const;
698
699 private:
700
701   // Undefined
702   Slider( const Slider& );
703
704   // Undefined
705   Slider& operator=( const Slider& rhs );
706
707 private:
708
709   Domain mDomain;                           ///< Current domain of the handle
710
711   Actor mHitArea;                           ///< The input handler
712   Actor mValueDisplay;                      ///< Display of the value
713   Toolkit::ImageView mTrack;                ///< Track image
714   Toolkit::ImageView mHandle;               ///< Slider handle
715   Toolkit::ImageView mProgress;             ///< Progress bar
716   Toolkit::ImageView mPopup;                ///< Popup backing
717   Toolkit::ImageView mPopupArrow;           ///< Popup arrow backing
718
719   Toolkit::TextLabel mValueTextLabel;       //< The text value in popup
720   Toolkit::TextLabel mHandleValueTextLabel; ///< The text value on handle
721   Vector2 mHandleLastTouchPoint;            ///< The last touch point for the handle
722   Timer mValueTimer;                        ///< Timer used to hide value view
723
724   Toolkit::Slider::ValueChangedSignalType mValueChangedSignal;       ///< Signal emitted when the value is changed
725   Toolkit::Slider::ValueChangedSignalType mSlidingFinishedSignal;    ///< Signal emitted when a sliding is finished
726   Toolkit::Slider::MarkReachedSignalType mMarkReachedSignal;         ///< Signal emitted when a mark is reached
727
728   SliderState mState;                 ///< The state of the slider
729
730   PanGestureDetector mPanDetector;    ///< Hit region pan detector
731
732   MarkList mMarks;                    ///< List of discreet marks
733
734   std::string mPopupVisual;           ///< Image for popup image
735   std::string mPopupArrowVisual;      ///< Image for popup arrow image
736   std::string mTrackVisual;           ///< Image for track image
737   std::string mHandleVisual;          ///< Image for handle image
738   std::string mProgressVisual;        ///< Image for progress bar image
739
740   Property::Map mPopupMap;         ///< the Property::Map if the image came from a Property::Map, empty otherwise
741   Property::Map mTrackMap;         ///< the Property::Map if the image came from a Property::Map, empty otherwise
742   Property::Map mHandleMap;        ///< the Property::Map if the image came from a Property::Map, empty otherwise
743   Property::Map mProgressMap;      ///< the Property::Map if the image came from a Property::Map, empty otherwise
744   Property::Map mPopupArrowMap;    ///< the Property::Map if the image came from a Property::Map, empty otherwise
745
746   Vector4 mDisabledColor;    ///< The color to tint the slider when disabled
747
748   Vector2 mHitRegion;     ///< Size of hit region
749   Vector2 mTrackRegion;   ///< Size of track region
750   Vector2 mHandleSize;    ///< Size of the handle
751
752   float mLowerBound;        ///< Lower bound on value
753   float mUpperBound;        ///< Upper bound on value
754   float mValue;             ///< Current value of slider
755
756   float mMarkTolerance;     ///< Tolerance in percentage of slider width for which to snap to marks
757
758   int mValuePrecision;      ///< The precision to use for outputting the value
759
760   bool mShowPopup   : 1,      ///< Show the popup or not
761        mShowValue   : 1,      ///< Whether to display the value number or not on the handle
762        mSnapToMarks : 1;      ///< Turn on or off snapping to marks
763 };
764
765 } // namespace Internal
766
767 // Helpers for public-api forwarding methods
768
769 inline Toolkit::Internal::Slider& GetImpl( Toolkit::Slider& pub )
770 {
771   DALI_ASSERT_ALWAYS( pub );
772
773   Dali::RefObject& handle = pub.GetImplementation();
774
775   return static_cast< Toolkit::Internal::Slider& >( handle );
776 }
777
778 inline const Toolkit::Internal::Slider& GetImpl( const Toolkit::Slider& pub )
779 {
780   DALI_ASSERT_ALWAYS( pub );
781
782   const Dali::RefObject& handle = pub.GetImplementation();
783
784   return static_cast< const Toolkit::Internal::Slider& >( handle );
785 }
786
787 } // namespace Toolkit
788
789 } // namespace Dali
790
791 #endif // __DALI_TOOLKIT_INTERNAL_SLIDER_H__