|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * Definition file for class ClockRegionalSettingsView. |
|
16 * |
|
17 */ |
|
18 |
|
19 // System includes |
|
20 #include <HbView> |
|
21 #include <HbAction> |
|
22 #include <HbDataForm> |
|
23 #include <HbDataFormModel> |
|
24 #include <HbDataFormModelItem> |
|
25 #include <HbDataFormViewItem> |
|
26 #include <HbMainWindow> |
|
27 #include <HbInstance> |
|
28 #include <HbPushButton> |
|
29 #include <HbExtendedLocale> |
|
30 #include <HbListWidget> |
|
31 #include <HbComboBox> |
|
32 #include <HBListWidgetItem> |
|
33 #include <HbApplication> |
|
34 #include <QTranslator> |
|
35 |
|
36 // User includes |
|
37 #include "clockregionalsettingsview.h" |
|
38 #include "clocksettingsdocloader.h" |
|
39 #include "clocksettingsdefines.h" |
|
40 #include "settingsdatatypes.h" |
|
41 #include "settingscustomitem.h" |
|
42 |
|
43 /*! |
|
44 \class ClockRegionalSettingsView |
|
45 |
|
46 The view for regional setting items in clock applications settings. |
|
47 */ |
|
48 |
|
49 /*! |
|
50 Default constructor. |
|
51 */ |
|
52 ClockRegionalSettingsView::ClockRegionalSettingsView( |
|
53 SettingsUtility &utility, QObject *parent) |
|
54 :QObject(parent), |
|
55 mView(0), |
|
56 mBackAction(0), |
|
57 mLoader(0), |
|
58 mSettingsUtility(utility) |
|
59 { |
|
60 // Construct the document loader. |
|
61 mLoader = new ClockSettingsDocLoader; |
|
62 |
|
63 // Load the translation file and install the editor specific translator |
|
64 mTranslator = new QTranslator; |
|
65 //QString lang = QLocale::system().name(); |
|
66 //QString path = "Z:/resource/qt/translations/"; |
|
67 mTranslator->load("clocksettingsview",":/translations"); |
|
68 // TODO: Load the appropriate .qm file based on locale |
|
69 //bool loaded = mTranslator->load("caleneditor_" + lang, path); |
|
70 HbApplication::instance()->installTranslator(mTranslator); |
|
71 // Create the custom prototype. |
|
72 mCustomPrototype = new SettingsCustomItem(); |
|
73 } |
|
74 |
|
75 /*! |
|
76 Destructor. |
|
77 */ |
|
78 ClockRegionalSettingsView::~ClockRegionalSettingsView() |
|
79 { |
|
80 if (mLoader) { |
|
81 delete mLoader; |
|
82 mLoader = 0; |
|
83 } |
|
84 // Remove the translator |
|
85 HbApplication::instance()->removeTranslator(mTranslator); |
|
86 if (mTranslator) { |
|
87 delete mTranslator; |
|
88 mTranslator = 0; |
|
89 } |
|
90 } |
|
91 |
|
92 /*! |
|
93 Shows the regional settings view. |
|
94 */ |
|
95 void ClockRegionalSettingsView::showView() |
|
96 { |
|
97 bool success; |
|
98 |
|
99 // Load the application xml. |
|
100 mLoader->load(CLOCK_REG_SETTINGS_VIEW_DOCML, &success); |
|
101 if (!success) { |
|
102 qFatal("Unable to load the docml."); |
|
103 } |
|
104 |
|
105 // Find the main view. |
|
106 mView = static_cast<HbView *> ( |
|
107 mLoader->findWidget(CLOCK_REG_SETTINGS_VIEW)); |
|
108 if (!mView) { |
|
109 qFatal("Unable to find view"); |
|
110 } |
|
111 |
|
112 // Find the data form. |
|
113 mForm = static_cast<HbDataForm *> ( |
|
114 mLoader->findWidget(CLOCK_REG_SETTINGS_DATA_FORM)); |
|
115 if (!mForm) { |
|
116 qFatal("Unable to find the form"); |
|
117 } |
|
118 connect( |
|
119 mForm, SIGNAL(itemShown(QModelIndex)), |
|
120 this, SLOT(handleItemDisplayed(QModelIndex))); |
|
121 |
|
122 |
|
123 QList <HbAbstractViewItem*> prototypes = mForm->itemPrototypes(); |
|
124 prototypes.append(mCustomPrototype); |
|
125 mForm->setItemPrototypes(prototypes); |
|
126 mCustomPrototype->setWeekdaysList(weekdayList()); |
|
127 |
|
128 // Create the form model. |
|
129 createModel(); |
|
130 |
|
131 // Set the view as the current view. |
|
132 HbMainWindow *window = hbInstance->allMainWindows().first(); |
|
133 window->addView(mView); |
|
134 window->setCurrentView(mView); |
|
135 |
|
136 // Add the back softkey. |
|
137 mBackAction = new HbAction(Hb::BackAction); |
|
138 mView->setNavigationAction(mBackAction); |
|
139 connect( |
|
140 mBackAction, SIGNAL(triggered()), |
|
141 this, SLOT(handleBackAction())); |
|
142 } |
|
143 |
|
144 /*! |
|
145 The view is removed from main window and a deleteLater is called on `this'. |
|
146 */ |
|
147 void ClockRegionalSettingsView::handleBackAction() |
|
148 { |
|
149 HbExtendedLocale locale = HbExtendedLocale::system(); |
|
150 HbExtendedLocale::WeekDay startOfWeekIndex = |
|
151 HbExtendedLocale::system().startOfWeek(); |
|
152 // TODO: Save workdays settings. |
|
153 QItemSelectionModel *model = 0; |
|
154 model = mWorkdaysItem->selectionModel(); |
|
155 QModelIndexList selectedModelIndex = model->selectedIndexes(); |
|
156 int count = selectedModelIndex.count(); |
|
157 QModelIndex index; |
|
158 for(int i = 0 ; i < count ; i++){ |
|
159 index = selectedModelIndex[i]; |
|
160 } |
|
161 |
|
162 QString workdayString("0000000"); |
|
163 for (int i = 0; i < workdayString.count(); ++i) { |
|
164 if (model->isSelected(model->model()->index(i, 0))) { |
|
165 workdayString.replace(i, 1, '1'); |
|
166 } |
|
167 } |
|
168 |
|
169 QString workdaysSetting = workdayString; |
|
170 int max = workdayString.size() - 1; |
|
171 for (int index = max - startOfWeekIndex, i = 0; i <= max; i++) { |
|
172 QChar ch = workdayString.at(index); |
|
173 workdaysSetting.replace(i, 1, ch); |
|
174 if (index == 0) { |
|
175 index = max; |
|
176 } else { |
|
177 index--; |
|
178 } |
|
179 } |
|
180 locale.setWorkDays(workdaysSetting); |
|
181 |
|
182 HbMainWindow *window = hbInstance->allMainWindows().first(); |
|
183 // Cleanup. |
|
184 window->removeView(mView); |
|
185 deleteLater(); |
|
186 } |
|
187 |
|
188 /*! |
|
189 Called when each of the form items are displayed. Using this slot, we |
|
190 connect to SIGNALS of the items that have been added to the form. |
|
191 |
|
192 \param index QModelIndex of the row that was just displayed. |
|
193 */ |
|
194 void ClockRegionalSettingsView::handleItemDisplayed(const QModelIndex &index) |
|
195 { |
|
196 if (!index.isValid()) { |
|
197 return; |
|
198 } |
|
199 |
|
200 HbDataFormViewItem *item = |
|
201 static_cast<HbDataFormViewItem*>(mForm->itemByIndex(index)); |
|
202 HbWidget *widget = item->dataItemContentWidget(); |
|
203 |
|
204 switch (index.row()) { |
|
205 case 0: |
|
206 |
|
207 break; |
|
208 case 1: |
|
209 |
|
210 break; |
|
211 |
|
212 case 2: |
|
213 |
|
214 break; |
|
215 |
|
216 case 3: |
|
217 |
|
218 break; |
|
219 |
|
220 case 4: |
|
221 mWorkdaysItem = static_cast<HbListWidget *> (widget); |
|
222 case 5: |
|
223 mStartOfWeekItem = static_cast<HbComboBox *> (widget); |
|
224 break; |
|
225 default: |
|
226 break; |
|
227 } |
|
228 } |
|
229 |
|
230 /*! |
|
231 Called when the time format toggle item is clicked. Here we update the |
|
232 value in the locale. |
|
233 */ |
|
234 void ClockRegionalSettingsView::handleTimeFormatChange() |
|
235 { |
|
236 // mSettingsUtility.setTimeFormat(mTimeFormatItem->text()); |
|
237 mSettingsUtility.setTimeFormat( |
|
238 mTimeFormatItem->contentWidgetData("text").toString()); |
|
239 } |
|
240 |
|
241 /*! |
|
242 Called when the time separator toggle item is clicked. Here we update the |
|
243 value in the locale. |
|
244 */ |
|
245 void ClockRegionalSettingsView::handleTimeSeparatorChange() |
|
246 { |
|
247 // mSettingsUtility.setTimeSeparator(mTimeSeparatorItem->text()); |
|
248 mSettingsUtility.setTimeSeparator( |
|
249 mTimeSeparatorItem->contentWidgetData("text").toString()); |
|
250 } |
|
251 |
|
252 /*! |
|
253 */ |
|
254 void ClockRegionalSettingsView::handleDateFormatChange(QString text) |
|
255 { |
|
256 mSettingsUtility.setDateFormat(text); |
|
257 } |
|
258 |
|
259 /*! |
|
260 Called when the date separator item is changed. Here we update the |
|
261 value in the locale. |
|
262 */ |
|
263 void ClockRegionalSettingsView::handleDateSeparatorChange(QString text) |
|
264 { |
|
265 mSettingsUtility.setDateSeparator(text); |
|
266 } |
|
267 |
|
268 /*! |
|
269 This slot is called any item in the data form is changed. |
|
270 */ |
|
271 void ClockRegionalSettingsView::handleDataChanged( |
|
272 const QModelIndex& topLeft, const QModelIndex& bottomRight) |
|
273 { |
|
274 qDebug("clock: ClockRegionalSettingsView::handleDataChanged() -->"); |
|
275 |
|
276 Q_UNUSED(bottomRight) |
|
277 |
|
278 |
|
279 switch (topLeft.row()) { |
|
280 /* case 4: |
|
281 { |
|
282 // Weekdays item. |
|
283 HbDataFormModelItem *workdayItem = mFormModel->itemFromIndex(topLeft); |
|
284 QList<QVariant> items = workdayItem |
|
285 ->contentWidgetData("selectedItems").toList(); |
|
286 int temp = items.count(); |
|
287 for (int var = 0; var < temp; ++var) { |
|
288 int temp1 = items.at(var).toInt(); |
|
289 } |
|
290 } |
|
291 break;*/ |
|
292 case 5: |
|
293 { |
|
294 // The Start of week item. |
|
295 if (mStartOfWeekItem != 0) { |
|
296 int index = mStartOfWeekItem->currentIndex(); |
|
297 mSettingsUtility.setStartOfWeek(index); |
|
298 |
|
299 QStringList weekdays = weekdayList(); |
|
300 QString workdays = mCustomPrototype->workdaysSetting(); |
|
301 QItemSelectionModel *model = 0; |
|
302 model = mWorkdaysItem->selectionModel(); |
|
303 |
|
304 for (int i = 0, index = workdays.size() - 1; |
|
305 i < mWorkdaysItem->count(); ++i, index--) { |
|
306 QString str = weekdays[i]; |
|
307 mWorkdaysItem->item(i)->setText(str); |
|
308 |
|
309 QChar ch = workdays.at(index); |
|
310 if ( ch == QChar('0')) { |
|
311 // Not a workday. |
|
312 model->select( |
|
313 model->model()->index(i,0), |
|
314 QItemSelectionModel::Deselect); |
|
315 } else { |
|
316 // Workday. |
|
317 model->select( |
|
318 model->model()->index(i,0), |
|
319 QItemSelectionModel::Select); |
|
320 } |
|
321 } |
|
322 } |
|
323 } |
|
324 break; |
|
325 |
|
326 default: |
|
327 break; |
|
328 } |
|
329 } |
|
330 |
|
331 /*! |
|
332 Here we create the form model. |
|
333 */ |
|
334 void ClockRegionalSettingsView::createModel() |
|
335 { |
|
336 if (mForm->model()) { |
|
337 delete mForm->model(); |
|
338 mForm->setModel(0); |
|
339 } |
|
340 |
|
341 mFormModel = new HbDataFormModel(this); |
|
342 |
|
343 // Populate the form model. |
|
344 populateFormModel(); |
|
345 |
|
346 mForm->setModel(mFormModel); |
|
347 |
|
348 connect( |
|
349 mFormModel, |
|
350 SIGNAL(dataChanged(const QModelIndex, const QModelIndex)), |
|
351 this, |
|
352 SLOT(handleDataChanged(const QModelIndex, const QModelIndex))); |
|
353 } |
|
354 |
|
355 /*! |
|
356 Function in which we populate the form model. |
|
357 */ |
|
358 void ClockRegionalSettingsView::populateFormModel() |
|
359 { |
|
360 if (!mFormModel) { |
|
361 createModel(); |
|
362 } |
|
363 |
|
364 // Get the locale. |
|
365 HbExtendedLocale locale = HbExtendedLocale::system(); |
|
366 |
|
367 // Time format item. |
|
368 mTimeFormatItem = mFormModel->appendDataFormItem( |
|
369 HbDataFormModelItem::ToggleValueItem, |
|
370 hbTrId("txt_clock_setlabel_time_format")); |
|
371 int index = mSettingsUtility.timeFormat(mTimeFormatStringList); |
|
372 if (0 == index) { |
|
373 mTimeFormatItem->setContentWidgetData("text", mTimeFormatStringList[0]); |
|
374 mTimeFormatItem->setContentWidgetData("additionalText", mTimeFormatStringList[1]); |
|
375 } else { |
|
376 mTimeFormatItem->setContentWidgetData("text", mTimeFormatStringList[1]); |
|
377 mTimeFormatItem->setContentWidgetData("additionalText", mTimeFormatStringList[0]); |
|
378 } |
|
379 mForm->addConnection( |
|
380 mTimeFormatItem, SIGNAL(clicked()), |
|
381 this, SLOT(handleTimeFormatChange())); |
|
382 |
|
383 // Time separator item. |
|
384 mTimeSeparatorItem = mFormModel->appendDataFormItem( |
|
385 HbDataFormModelItem::ToggleValueItem, |
|
386 hbTrId("txt_clock_setlabel_time_separator")); |
|
387 index = mSettingsUtility.timeSeparator(mTimeSeparatorStringList); |
|
388 if (0 == index) { |
|
389 mTimeSeparatorItem->setContentWidgetData("text", mTimeSeparatorStringList[0]); |
|
390 mTimeSeparatorItem->setContentWidgetData( |
|
391 "additionalText", mTimeSeparatorStringList[1]); |
|
392 } else { |
|
393 mTimeSeparatorItem->setContentWidgetData("text", mTimeSeparatorStringList[1]); |
|
394 mTimeSeparatorItem->setContentWidgetData( |
|
395 "additionalText", mTimeSeparatorStringList[0]); |
|
396 } |
|
397 mForm->addConnection( |
|
398 mTimeSeparatorItem, SIGNAL(clicked()), |
|
399 this, SLOT(handleTimeSeparatorChange())); |
|
400 |
|
401 // Date format. |
|
402 mDateFormatItem = mFormModel->appendDataFormItem( |
|
403 HbDataFormModelItem::ComboBoxItem, |
|
404 hbTrId("txt_clock_setlabel_date_format")); |
|
405 index = mSettingsUtility.dateFormat(mDateFormatStringList); |
|
406 |
|
407 mDateFormatItem->setContentWidgetData("items", mDateFormatStringList); |
|
408 mDateFormatItem->setContentWidgetData("currentIndex",index); |
|
409 mForm->addConnection( |
|
410 mDateFormatItem, SIGNAL(currentIndexChanged(QString)), |
|
411 this, SLOT(handleDateFormatChange(QString))); |
|
412 |
|
413 // Date separator. |
|
414 mDateSeparatorItem = mFormModel->appendDataFormItem( |
|
415 HbDataFormModelItem::ComboBoxItem, |
|
416 hbTrId("txt_clock_setlabel_date_separator")); |
|
417 index = mSettingsUtility.dateSeparator(mDateSeparatorStringList); |
|
418 |
|
419 mDateSeparatorItem->setContentWidgetData("items", mDateSeparatorStringList); |
|
420 mDateSeparatorItem->setContentWidgetData("currentIndex",index); |
|
421 mForm->addConnection( |
|
422 mDateSeparatorItem, SIGNAL(currentIndexChanged(QString)), |
|
423 this, SLOT(handleDateSeparatorChange(QString))); |
|
424 |
|
425 // Workdays. |
|
426 HbDataFormModelItem *item = 0; |
|
427 // Create the weekday list based on start of week. |
|
428 QStringList weekdaysList; |
|
429 weekdaysList |
|
430 << hbTrId("txt_clk_setlabel_val_monday") |
|
431 << hbTrId("txt_clk_setlabel_val_tuesday") |
|
432 << hbTrId("txt_clk_setlabel_val_wednesday") |
|
433 << hbTrId("txt_clk_setlabel_val_thursday") |
|
434 << hbTrId("txt_clk_setlabel_val_friday") |
|
435 << hbTrId("txt_clk_setlabel_val_saturday") |
|
436 << hbTrId("txt_clk_setlabel_val_sunday"); |
|
437 |
|
438 HbDataFormModelItem::DataItemType workdaysItemType = |
|
439 static_cast<HbDataFormModelItem::DataItemType> |
|
440 (HbDataFormModelItem::CustomItemBase + 50); |
|
441 item = new HbDataFormModelItem(workdaysItemType, |
|
442 hbTrId("txt_clock_setlabel_workdays")); |
|
443 mFormModel->appendDataFormItem(item); |
|
444 |
|
445 // Start of week item. |
|
446 item = 0; |
|
447 item = mFormModel->appendDataFormItem(HbDataFormModelItem::ComboBoxItem, |
|
448 hbTrId("txt_clock_setlabel_week_starts_on")); |
|
449 HbExtendedLocale::WeekDay startOfWeek = locale.startOfWeek(); |
|
450 item->setContentWidgetData("items", weekdaysList); |
|
451 item->setContentWidgetData("currentIndex", startOfWeek); |
|
452 } |
|
453 |
|
454 /*! |
|
455 Returns the weekday list ordered based on start of week. |
|
456 */ |
|
457 |
|
458 QStringList ClockRegionalSettingsView::weekdayList() |
|
459 { |
|
460 QStringList weekDays; |
|
461 QStringList daysList; |
|
462 daysList |
|
463 << hbTrId("txt_clk_setlabel_val_monday") |
|
464 << hbTrId("txt_clk_setlabel_val_tuesday") |
|
465 << hbTrId("txt_clk_setlabel_val_wednesday") |
|
466 << hbTrId("txt_clk_setlabel_val_thursday") |
|
467 << hbTrId("txt_clk_setlabel_val_friday") |
|
468 << hbTrId("txt_clk_setlabel_val_saturday") |
|
469 << hbTrId("txt_clk_setlabel_val_sunday"); |
|
470 |
|
471 HbExtendedLocale::WeekDay startOfWeekIndex = |
|
472 HbExtendedLocale::system().startOfWeek(); |
|
473 |
|
474 for (int i = 0, index = startOfWeekIndex; i < daysList.count(); ++i) { |
|
475 weekDays.append(daysList.at(index)); |
|
476 if (6 == index) { |
|
477 index = 0; |
|
478 } else { |
|
479 index++; |
|
480 } |
|
481 } |
|
482 |
|
483 return weekDays; |
|
484 } |
|
485 |
|
486 // End of file --Don't remove this. |