Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_DateTimeBarModel.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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_DateTimeBarModel.cpp
20  * @brief               This is the implementation file for the _DateTimeBarModel class.
21  */
22
23 #include "FUiCtrl_DateTimeBarModel.h"
24
25 using namespace Tizen::Graphics;
26
27 namespace Tizen { namespace Ui { namespace Controls
28 {
29 const int CACHING_SIZE = 50;
30
31 _DateTimeBarModel::_DateTimeBarModel(void)
32         : __selectedItemIndex(-1)
33         , __firstDrawnItemIndex(0)
34         , __minValue(-1)
35         , __maxValue(-1)
36         , __maxCachingSize(CACHING_SIZE)
37         , __itemStartPositionX(0)
38 {
39 }
40
41 _DateTimeBarModel::~_DateTimeBarModel(void)
42 {
43         __dateTimeBarItems.RemoveAll(true);
44 }
45
46 _DateTimeBarModel*
47 _DateTimeBarModel::CreateInstanceN(void)
48 {
49         _DateTimeBarModel* pDateTimeBarModel = new (std::nothrow) _DateTimeBarModel;
50         SysTryReturn(NID_UI_CTRL, pDateTimeBarModel, null, E_OUT_OF_MEMORY,
51                         "[E_OUT_OF_MEMORY] Memory allocation failed.");
52
53         result r = E_SUCCESS;
54         r = pDateTimeBarModel->__dateTimeBarItems.Construct();
55         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
56
57         return pDateTimeBarModel;
58
59 CATCH:
60         delete pDateTimeBarModel;
61         return null;
62 }
63
64 void
65 _DateTimeBarModel::SetSelectedItemIndex(int index)
66 {
67         __selectedItemIndex = index;
68         return;
69 }
70
71 int
72 _DateTimeBarModel::GetSelectedItemIndex(void) const
73 {
74         return __selectedItemIndex;
75 }
76
77 void
78 _DateTimeBarModel::SetFirstDrawnItemIndex(int index)
79 {
80         __firstDrawnItemIndex = index;
81         return;
82 }
83
84 int
85 _DateTimeBarModel::GetFirstDrawnItemIndex(void) const
86 {
87         return __firstDrawnItemIndex;
88 }
89
90 int
91 _DateTimeBarModel::GetItemCount(void) const
92 {
93         return __dateTimeBarItems.GetCount();
94 }
95
96 result
97 _DateTimeBarModel::AddItem(const Tizen::Base::String& text, int actionId, _DateTimeBarAlignment alignment, int itemWidth, int margin)
98 {
99         _DateTimeBarItem* pItem = _DateTimeBarItem::CreateInstanceN(alignment, itemWidth);
100
101         result r = GetLastResult();
102         SysTryReturn(NID_UI_CTRL, pItem != null, r, r, "[%s] Propagating.", GetErrorMessage(r));
103
104         pItem->SetText(text);
105         pItem->SetActionId(actionId);
106
107         int itemCount = 0;
108         _DateTimeBarItem* pPrevItem = null;
109
110         itemCount = GetItemCount();
111
112         if (itemCount > 0)
113         {
114                 pPrevItem = GetItemAt(itemCount - 1);
115         }
116         else
117         {
118                 Rectangle itemBounds = pItem->GetBounds();
119
120                 itemBounds.x = __itemStartPositionX;
121
122                 pItem->SetBounds(itemBounds);
123         }
124
125         if (pPrevItem != null)
126         {
127                 Rectangle prevItemBounds = pPrevItem->GetBounds();
128                 Rectangle itemBounds = pItem->GetBounds();
129                 itemBounds.x = prevItemBounds.x + prevItemBounds.width + margin;
130
131                 pItem->SetBounds(itemBounds);
132         }
133
134         return __dateTimeBarItems.Add(*pItem);
135 }
136
137 result
138 _DateTimeBarModel::InsertItemAt(int index, const Tizen::Base::String& text, int actionId, _DateTimeBarAlignment alignment, int itemWidth, int margin)
139 {
140         _DateTimeBarItem* pItem = _DateTimeBarItem::CreateInstanceN(alignment, itemWidth);
141
142         result r = GetLastResult();
143         SysTryReturn(NID_UI_CTRL, pItem != null, r, r, "[%s] Propagating.", GetErrorMessage(r));
144
145         pItem->SetText(text);
146         pItem->SetActionId(actionId);
147
148         _DateTimeBarItem* pPrevItem = GetItemAt(index);
149         if (pPrevItem != null)
150         {
151                 Rectangle prevItemBounds = pPrevItem->GetBounds();
152                 Rectangle itemBounds = pItem->GetBounds();
153                 itemBounds.x = prevItemBounds.x - itemBounds.width - margin;
154
155                 pItem->SetBounds(itemBounds);
156         }
157
158         return __dateTimeBarItems.InsertAt(*pItem, index);
159 }
160
161 result
162 _DateTimeBarModel::RemoveItemAt(int index)
163 {
164         if (GetFirstDrawnItemIndex() == index)
165         {
166                 if (GetItemAt(index + 1) == null)
167                 {
168                         SetFirstDrawnItemIndex(-1);
169                 }
170         }
171
172         return __dateTimeBarItems.RemoveAt(index, true);
173 }
174
175 void
176 _DateTimeBarModel::RemoveAllItems()
177 {
178         __dateTimeBarItems.RemoveAll(true);
179         SetFirstDrawnItemIndex(0);
180         SetSelectedItemIndex(-1);
181         return;
182 }
183
184 const _DateTimeBarItem*
185 _DateTimeBarModel::GetItemAt(int index) const
186 {
187         return dynamic_cast <const _DateTimeBarItem*>(__dateTimeBarItems.GetAt(index));
188 }
189
190 _DateTimeBarItem*
191 _DateTimeBarModel::GetItemAt(int index)
192 {
193         return dynamic_cast <_DateTimeBarItem*>(__dateTimeBarItems.GetAt(index));
194 }
195
196 int
197 _DateTimeBarModel::GetMinimumValue(void) const
198 {
199         return __minValue;
200 }
201
202 void
203 _DateTimeBarModel::SetMinimumValue(int minValue)
204 {
205         __minValue = minValue;
206         return;
207 }
208
209 int
210 _DateTimeBarModel::GetMaximumValue(void) const
211 {
212         return __maxValue;
213 }
214
215 void
216 _DateTimeBarModel::SetMaximumValue(int maxValue)
217 {
218         __maxValue = maxValue;
219         return;
220 }
221
222 int
223 _DateTimeBarModel::GetMaxCachingSize(void) const
224 {
225         return __maxCachingSize;
226 }
227
228 result
229 _DateTimeBarModel::SetItemStartPosition(int positionX)
230 {
231         __itemStartPositionX = positionX;
232         return E_SUCCESS;
233 }
234 }}} // Tizen::Ui::Controls