Fix for N_SE-47107
[apps/osp/Calendar.git] / src / ClTaskItem.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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  * @file        ClTaskItem.cpp
19  * @brief       This is the implementation file for the TaskItem class.
20  */
21
22 #include "ClTaskItem.h"
23 #include "ClResourceManager.h"
24
25 using namespace Tizen::Base;
26 using namespace Tizen::Graphics;
27 using namespace Tizen::Locales;
28 using namespace Tizen::Ui;
29 using namespace Tizen::Ui::Controls;
30
31 enum TaskItemElementId
32 {
33         ID_TASK_ITEM_ELEMENT_COLOR_BAR,
34         ID_TASK_ITEM_ELEMENT_TITLE,
35         ID_TASK_ITEM_ELEMENT_LOCATION,
36         ID_TASK_ITEM_ELEMENT_DUE_DATE,
37         ID_TASK_ITEM_ELEMENT_PRIORITY,
38         ID_TASK_ITEM_ELEMENT_REMINDER,
39         ID_TASK_ITEM_ELEMENT_FACEBOOK
40 };
41
42 static const int H_ITEM = 140;
43 static const unsigned int COLOR_ITEM_BACKGROUND = Color32<248, 246, 239>::Value;
44 static const int X_ITEM_MAIN_CONTENT = 26;
45 static const int Y_ITEM_MAIN_CONTENT = 0;
46 static const int W_ITEM_MAIN_CONTENT_MARGIN = 16;
47 static const int H_ITEM_MAIN_CONTENT = 54;
48 static const int FONT_SIZE_ITEM_MAIN_CONTENT = 44;
49 static const unsigned int COLOR_ITEM_MAIN_CONTENT = Color32<0, 0, 0>::Value;
50 static const unsigned int COLOR_ITEM_MAIN_CONTENT_HIGH_PRIORITY = Color32<194, 71, 71>::Value;
51 static const unsigned int COLOR_ITEM_MAIN_CONTENT_FOCUS = Color32<255, 255, 255>::Value;
52 static const int X_ITEM_SUB_CONTENT = 26;
53 static const int Y_ITEM_SUB_CONTENT = 85;
54 static const int W_ITEM_SUB_CONTENT_MARGIN = 26;
55 static const int H_ITEM_SUB_CONTENT = 38;
56 static const int FONT_SIZE_ITEM_SUB_CONTENT = 32;
57 static const unsigned int COLOR_ITEM_SUB_CONTENT = Color32<128, 128, 128>::Value;
58 static const unsigned int COLOR_ITEM_SUB_CONTENT_FOCUS = Color32<255, 255, 255>::Value;
59 static const int Y_EVENT_COLOR_BAR = 4;
60 static const int W_EVENT_COLOR_BAR = 8;
61 static const int H_EVENT_COLOR_BAR = 132;
62 static const int W_ICON = 45;
63 static const int H_ICON = 45;
64 static const int W_RANGE_TO_LOCATION_MARGIN = 26;
65 static const int LIST_ANNEX_MARGIN = 30;
66 static const byte COLOR_COMPLETE_TASK_ALPHA = 77;                       // 30% alpha
67 static const int W_MIN_CHAR = 20;
68 static const int H_SPARE = 14;
69 static const int H_APPROX = 17;
70
71
72 class TaskTitleElement
73         : public virtual ICustomElement
74 {
75 public:
76         TaskTitleElement(const String& title, TaskPriority priority, float fontsize, bool isCompleted = false);
77         virtual ~TaskTitleElement(void);
78
79         virtual bool OnDraw(Canvas& canvas, const Rectangle& rect, ListItemDrawingStatus status);
80
81 private:
82         const String& __title;
83         TaskPriority __priority;
84         bool __isCompleted;
85         float __fontSize;
86 }; // TitleElement
87
88 TaskTitleElement::TaskTitleElement(const String& title, TaskPriority priority, float fontsize, bool isCompleted)
89         : __title(title)
90         , __priority(priority)
91         , __isCompleted(isCompleted)
92         , __fontSize(fontsize)
93 {
94 }
95
96 TaskTitleElement::~TaskTitleElement(void)
97 {
98 }
99
100 bool
101 TaskTitleElement::OnDraw(Canvas& canvas, const Rectangle& rect, ListItemDrawingStatus status)
102 {
103         EnrichedText text;
104         text.Construct(Dimension(rect.width, rect.height));
105         text.SetVerticalAlignment(TEXT_ALIGNMENT_MIDDLE);
106         text.SetHorizontalAlignment(TEXT_ALIGNMENT_LEFT);
107         text.SetTextAbbreviationEnabled(true);
108
109         Color titleColor;
110         if (status == LIST_ITEM_DRAWING_STATUS_PRESSED || status == LIST_ITEM_DRAWING_STATUS_HIGHLIGHTED)
111         {
112                 titleColor = Color(COLOR_ITEM_MAIN_CONTENT_FOCUS);
113         }
114         else if (__priority == TASK_PRIORITY_HIGH)
115         {
116                 titleColor = Color(COLOR_ITEM_MAIN_CONTENT_HIGH_PRIORITY);
117         }
118         else
119         {
120                 titleColor = Color(COLOR_ITEM_MAIN_CONTENT);
121         }
122
123         Font font;
124         font.Construct(FONT_STYLE_PLAIN, __fontSize);
125
126         if (__isCompleted == true)
127         {
128                 font.SetStrikeOut(true);
129                 titleColor.SetAlpha(COLOR_COMPLETE_TASK_ALPHA);
130         }
131
132         TextElement* pTextElement = new (std::nothrow) TextElement();
133         pTextElement->Construct(__title);
134         pTextElement->SetFont(font);
135         pTextElement->SetTextColor(Color(titleColor));
136         text.Add(*pTextElement);
137
138         canvas.DrawText(Point(rect.x, rect.y), text);
139         text.RemoveAll(true);
140         return true;
141 }
142
143
144 TaskItem::TaskItem(void)
145         : __style()
146         , __width(-1)
147         , __pTitleElement(null)
148         , __priority(TASK_PRIORITY_LOW)
149         , __hasReminder(false)
150         , __isCanceled(false)
151         , __isCompleted(false)
152         , __isFacebook(false)
153 {
154 }
155
156 TaskItem::~TaskItem(void)
157 {
158         delete __pTitleElement;
159 }
160
161 const Color
162 TaskItem::GetCalendarColor(void) const
163 {
164         return __calendarColor;
165 }
166
167 const String&
168 TaskItem::GetDueDateText(void) const
169 {
170         return __dueDate;
171 }
172
173 const String&
174 TaskItem::GetLocation(void) const
175 {
176         return __location;
177 }
178
179 float
180 TaskItem::GetMainContentHeight() const
181 {
182         return (__itemHeight - (H_APPROX*2) - H_SPARE);
183 }
184
185 TaskPriority
186 TaskItem::GetPriority(void) const
187 {
188         return __priority;
189 }
190
191 float
192 TaskItem::GetSubItemY() const
193 {
194         return (__itemHeight -  H_APPROX - H_ITEM_SUB_CONTENT );
195 }
196
197 const String&
198 TaskItem::GetTitle(void) const
199 {
200         return __title;
201 }
202
203 bool
204 TaskItem::HasReminder(void) const
205 {
206         return __hasReminder;
207 }
208
209 void
210 TaskItem::Initialize(TaskItemStyle style, int width, float height)
211 {
212         if (style == TASK_ITEM_STYLE_NORMAL)
213         {
214                 Construct(Dimension(width, height), LIST_ANNEX_STYLE_NORMAL);
215                 __width = width;
216                 __itemHeight = height;
217         }
218         else
219         {
220                 Construct(Dimension(width, height), LIST_ANNEX_STYLE_MARK);
221                 __width = width - GetAnnexWidth(LIST_ANNEX_STYLE_MARK) - LIST_ANNEX_MARGIN;
222                 __itemHeight = height;
223         }
224         __style = style;
225 }
226
227 bool
228 TaskItem::IsCanceledTask(void) const
229 {
230         return __isCanceled;
231 }
232
233 bool
234 TaskItem::IsCompletedTask(void) const
235 {
236         return __isCompleted;
237 }
238
239 bool
240 TaskItem::IsFacebookTask(void) const
241 {
242         return __isFacebook;
243 }
244
245 void
246 TaskItem::SetCalendarColor(const Color& calendarColor)
247 {
248         __calendarColor = calendarColor;
249 }
250
251 void
252 TaskItem::SetCanceled(bool isCanceled)
253 {
254         __isCanceled = isCanceled;
255 }
256
257 void
258 TaskItem::SetCompleted(bool isCompleted)
259 {
260         __isCompleted = isCompleted;
261 }
262
263 void
264 TaskItem::SetDueDate(const DateTime& dueDate, DateTimeFormatter* pTimeFormatter)
265 {
266         bool deleteRequired = false;
267         if (pTimeFormatter == null)
268         {
269                 pTimeFormatter = ResourceManager::CreateDateTimeFormatterN(DATE_TIME_STYLE_SHORT);
270                 deleteRequired = true;
271         }
272
273         String dueDateText;
274         pTimeFormatter->Format(dueDate, dueDateText);
275         if (deleteRequired == true)
276         {
277                 delete pTimeFormatter;
278         }
279         SetDueDateText(dueDateText);
280 }
281
282 void
283 TaskItem::SetDueDateText(const String& range)
284 {
285         __dueDate = range;
286 }
287
288 void
289 TaskItem::SetFacebookEvent(bool isFacebookEvent)
290 {
291         __isFacebook = isFacebookEvent;
292 }
293
294 void
295 TaskItem::SetFontSize(const float& size)
296 {
297         __fontSize = size;
298 }
299
300 void
301 TaskItem::SetLocation(const String& location)
302 {
303         __location = location;
304 }
305
306 void
307 TaskItem::SetPriority(TaskPriority priority)
308 {
309         __priority = priority;
310 }
311
312 void
313 TaskItem::SetReminder(bool hasReminder)
314 {
315         __hasReminder = hasReminder;
316 }
317
318 void
319 TaskItem::SetTitle(const String& title)
320 {
321         __title = title;
322 }
323
324 void
325 TaskItem::UpdateElements(void)
326 {
327         RemoveAllElements();
328
329         Bitmap* pBitmap = new (std::nothrow) Bitmap();
330         pBitmap->Construct(Dimension(W_EVENT_COLOR_BAR, __itemHeight), BITMAP_PIXEL_FORMAT_ARGB8888);
331
332         BufferInfo bufferinfo;
333         pBitmap->Lock(bufferinfo);
334
335         Canvas canvas;
336         canvas.Construct(bufferinfo);
337         canvas.Clear();
338         pBitmap->Unlock();
339
340         AddElement(Rectangle(0, Y_EVENT_COLOR_BAR, W_EVENT_COLOR_BAR, __itemHeight), ID_TASK_ITEM_ELEMENT_COLOR_BAR, *pBitmap);
341         delete pBitmap;
342
343         int titleWidth = __width - X_ITEM_MAIN_CONTENT;
344         if (__hasReminder == true)
345         {
346                 Bitmap* pBitmap = ResourceManager::GetBitmapN(IDB_LIST_ICON_REMINDER);
347                 if (pBitmap != null)
348                 {
349                         AddElement(Rectangle(titleWidth - W_ICON, Y_ITEM_MAIN_CONTENT + ((GetMainContentHeight() - H_ICON) / 2), W_ICON, H_ICON),
350                                         ID_TASK_ITEM_ELEMENT_REMINDER, *pBitmap);
351                         titleWidth -= W_ICON;
352                         delete pBitmap;
353                 }
354         }
355
356         if (__priority == TASK_PRIORITY_LOW)
357         {
358                 Bitmap* pBitmap = ResourceManager::GetBitmapN(IDB_LIST_ICON_PRIORITY_LOW);
359                 if (pBitmap != null)
360                 {
361                         AddElement(Rectangle(titleWidth - W_ICON, Y_ITEM_MAIN_CONTENT + ((GetMainContentHeight() - H_ICON) / 2), W_ICON, H_ICON),
362                                         ID_TASK_ITEM_ELEMENT_PRIORITY, *pBitmap);
363                         titleWidth -= W_ICON;
364                         delete pBitmap;
365                 }
366         }
367         else if (__priority == TASK_PRIORITY_HIGH)
368         {
369                 Bitmap* pBitmap = ResourceManager::GetBitmapN(IDB_LIST_ICON_PRIORITY);
370                 if (pBitmap != null)
371                 {
372                         AddElement(Rectangle(titleWidth - W_ICON, Y_ITEM_MAIN_CONTENT + ((GetMainContentHeight() - H_ICON) / 2), W_ICON, H_ICON),
373                                         ID_TASK_ITEM_ELEMENT_PRIORITY, *pBitmap);
374                         titleWidth -= W_ICON;
375                         delete pBitmap;
376                 }
377         }
378
379         if (__isFacebook == true)
380         {
381                 Bitmap* pBitmap = ResourceManager::GetBitmapN(IDB_LIST_ICON_FACEBOOK);
382                 if (pBitmap != null)
383                 {
384                         AddElement(Rectangle(titleWidth - W_ICON, Y_ITEM_MAIN_CONTENT + ((GetMainContentHeight() - H_ICON) / 2), W_ICON, H_ICON),
385                                         ID_TASK_ITEM_ELEMENT_FACEBOOK, *pBitmap);
386                         titleWidth -= W_ICON;
387                         delete pBitmap;
388                 }
389         }
390
391         if (__title.IsEmpty() == false)
392         {
393                 delete __pTitleElement;
394                 __pTitleElement = new (std::nothrow) TaskTitleElement(__title, __priority, __fontSize, __isCompleted);
395
396                 if (titleWidth < __width - X_ITEM_MAIN_CONTENT)
397                 {
398                         titleWidth -= W_ITEM_MAIN_CONTENT_MARGIN;
399                 }
400
401                 AddElement(Rectangle(X_ITEM_MAIN_CONTENT, Y_ITEM_MAIN_CONTENT, titleWidth - X_ITEM_MAIN_CONTENT, GetMainContentHeight()),
402                                 ID_TASK_ITEM_ELEMENT_TITLE, *__pTitleElement);
403         }
404
405         int dueDateWidth = 0;
406         if (__dueDate.IsEmpty() == false)
407         {
408                 Dimension extent;
409                 Font font;
410                 font.Construct(FONT_STYLE_PLAIN, FONT_SIZE_ITEM_SUB_CONTENT);
411                 font.GetTextExtent(__dueDate, __dueDate.GetLength(), extent);
412                 dueDateWidth = extent.width;
413
414                 AddElement(Rectangle(X_ITEM_SUB_CONTENT, GetSubItemY(), dueDateWidth, H_ITEM_SUB_CONTENT),
415                                 ID_TASK_ITEM_ELEMENT_DUE_DATE, __dueDate, FONT_SIZE_ITEM_SUB_CONTENT,
416                                 Color(COLOR_ITEM_SUB_CONTENT), Color(COLOR_ITEM_SUB_CONTENT_FOCUS), Color(COLOR_ITEM_SUB_CONTENT_FOCUS));
417         }
418
419         int locationWidth = __width - X_ITEM_SUB_CONTENT - W_ITEM_SUB_CONTENT_MARGIN - W_RANGE_TO_LOCATION_MARGIN - dueDateWidth;
420         if (__location.IsEmpty() == false)
421         {
422                 Dimension extent;
423                 Font font;
424                 font.Construct(FONT_STYLE_PLAIN, FONT_SIZE_ITEM_SUB_CONTENT);
425                 font.GetTextExtent(__location, __location.GetLength(), extent);
426                 if (locationWidth > extent.width)
427                 {
428                         locationWidth = extent.width;
429                 }
430
431                 AddElement(Rectangle(__width - locationWidth - W_ITEM_SUB_CONTENT_MARGIN, GetSubItemY(), locationWidth, H_ITEM_SUB_CONTENT),
432                                 ID_TASK_ITEM_ELEMENT_LOCATION, __location, FONT_SIZE_ITEM_SUB_CONTENT,
433                                 Color(COLOR_ITEM_SUB_CONTENT), Color(COLOR_ITEM_SUB_CONTENT_FOCUS), Color(COLOR_ITEM_SUB_CONTENT_FOCUS));
434                 SetElementTextHorizontalAlignment(ID_TASK_ITEM_ELEMENT_LOCATION, ALIGNMENT_RIGHT);
435         }
436 }