N9profile
[n9profile] / profildialog.cpp
1 #include "profildialog.h"
2 #include "ui_profildialog.h"
3
4 #include <QMaemo5ValueButton>
5 #include <QMaemo5ListPickSelector>
6 #include <QMaemo5InformationBox>
7 #include "profil.h"
8 #include <QtCore/QDebug> //Debug pro informace
9 #include <QtGui/QStandardItemModel>
10 #include <QtCore/QStringList>
11 #include <QtCore/QFileInfo>
12 #include <QtGui/QFileDialog>
13 #include <QtGui/QListView>
14 #include <QtCore/QModelIndex>
15 #include <QtCore/QVariant>
16 #include <QtCore/Qt>
17 #include <QtCore/QString>
18
19 /** Constructor.
20 Setup ui and create new objects for item model and maemo5
21 value button and setup slider, check boxes atc. if dialog is use
22 for update profile
23 */
24 ProfilDialog::ProfilDialog(QStringList  list_of_profiles_n, QWidget *parent, Profil * profil, QStandardItemModel *  model) :
25         QDialog(parent),
26         ui(new Ui::ProfilDialog)
27 {
28     ui->setupUi(this);
29
30     list_of_profiles_names = list_of_profiles_n; // save pointer
31     model_of_files = model;
32     profile = profil;
33     //edit profile or new profile
34     if(profil->GetName().isEmpty()) {
35         update = false;
36         setWindowTitle(tr("New profile"));
37         ui->slider_ringing_volume->setValue(0);
38         ui->slider_sms_volume->setValue(0);
39         ui->slider_im_volume->setValue(0);
40         ui->slider_email_volume->setValue(0);
41     } else {
42         update = true;
43         setWindowTitle(tr("Edit profile"));
44         ui->lineEdit_nameprofile->setText(profile->GetName());
45         ui->lineEdit_nameprofile->setDisabled(true);
46         ui->slider_ringing_volume->setValue(profile->GetRingingAlertVolume().toInt());
47         ui->slider_sms_volume->setValue(profile->GetSmsAlertVolume().toInt());
48         ui->slider_im_volume->setValue(profile->GetImAlertVolume().toInt());
49         ui->slider_email_volume->setValue(profile->GetEmailAlertVolume().toInt());
50     }
51     createMeamoButtons();
52     createRadioID();
53 }
54
55 /** destructor
56 Delete ui
57 */
58 ProfilDialog::~ProfilDialog()
59 {
60     delete ui;
61 }
62
63 /** createMeamoButtons()
64 Method will create Meamo5 buttons and sets
65 pick selector for them
66 */
67 void ProfilDialog::createMeamoButtons()
68 {
69     selModelMobil = new QItemSelectionModel(model_of_files); //create new selection model
70     QListView * list_view = createListView(selModelMobil); //method will create QListview
71     QMaemo5ListPickSelector *listpicker1 = createMeamoListPick(list_view); //method will create QMaemo5ListPickSelector
72     if(update) //if update then select ringtone
73     {
74         setSelectedItem(listpicker1,selModelMobil,profile->GetRingingAlertTone().toString());
75     }
76     v1 = new QMaemo5ValueButton(tr("Ringing tone"), this); //create button
77     //  v1->setIcon(QIcon::fromTheme("chat_smiley_happy"));
78     v1->setValueLayout(QMaemo5ValueButton::ValueUnderText);
79     v1->setPickSelector(listpicker1);//set list pick
80     ui->gridLayout->addWidget(v1,13,0,1,-1); // add to layout
81     //===========================================================================================================================
82     selModelSMS = new QItemSelectionModel(model_of_files);
83     list_view = createListView(selModelSMS);
84     QMaemo5ListPickSelector *listpicker2 = createMeamoListPick(list_view);
85     if(update)
86     {
87         setSelectedItem(listpicker2,selModelSMS,profile->GetSmsAlertTone().toString());
88     }
89     v2 = new QMaemo5ValueButton(tr("SMS tone"), this);
90     // v2->setIcon(QIcon::fromTheme("chat_smiley_happy"));
91     v2->setValueLayout(QMaemo5ValueButton::ValueUnderText);
92     v2->setPickSelector(listpicker2);
93     ui->gridLayout->addWidget(v2,14,0,1,-1);
94     //===========================================================================================================================
95     selModelIM = new QItemSelectionModel(model_of_files);
96     list_view = createListView(selModelIM);
97     QMaemo5ListPickSelector *listpicker3 = createMeamoListPick(list_view);
98     if(update)
99     {
100         setSelectedItem(listpicker3,selModelIM,profile->GetImAlertTone().toString());
101     }
102     v3 = new QMaemo5ValueButton(tr("IM tone"), this);
103     // v3->setIcon(QIcon::fromTheme("chat_smiley_happy"));
104     v3->setValueLayout(QMaemo5ValueButton::ValueUnderText);
105     v3->setPickSelector(listpicker3);
106     ui->gridLayout->addWidget(v3,15,0,1,-1);
107     //===========================================================================
108     selModelEmail = new QItemSelectionModel(model_of_files);
109     list_view = createListView(selModelEmail);
110     QMaemo5ListPickSelector *listpicker4 = createMeamoListPick(list_view);
111     if(update)
112     {
113         setSelectedItem(listpicker4,selModelEmail,profile->GetEmailAlertTone().toString());
114     }
115     v4 = new QMaemo5ValueButton(tr("E-mail tone"), this);
116     // v4->setIcon(QIcon::fromTheme("chat_smiley_happy"));
117     v4->setValueLayout(QMaemo5ValueButton::ValueUnderText);
118     v4->setPickSelector(listpicker4);
119     ui->gridLayout->addWidget(v4,16,0,1,-1);
120 }
121
122 /** createListView
123 Method will create QListView and set edit triggers to NoEditTriggers,
124 selection model, selection behavior to select rows and selection mode to single selection
125 */
126 QListView * ProfilDialog::createListView(QItemSelectionModel * select_model)
127 {
128     QListView * list_view = new QListView(this); //QListView::selectedItem() will return the selected QListViewItem*.
129     list_view->setEditTriggers(QAbstractItemView::NoEditTriggers);
130     /*
131      MOdel má
132      QList<QStandardItem *> QStandardItemModel::findItems ( const QString & text, Qt::MatchFlags flags = Qt::MatchExactly, int column = 0 ) const
133
134      item má
135      QModelIndex QStandardItem::index () const
136
137       Model selected má
138       void QItemSelectionModel::select ( const QModelIndex & index, QItemSelectionModel::SelectionFlags command )   [virtual slot]
139
140       */
141     list_view->setModel(model_of_files);
142     list_view->setSelectionModel(select_model);
143     list_view->setSelectionBehavior(QAbstractItemView::SelectRows);
144     list_view->setSelectionMode(QAbstractItemView::SingleSelection);
145     return list_view;
146 }
147
148 /** createMeamoListPick
149 Method will create QMaemo5ListPickSelector and set list pick model, model column and view
150 */
151 QMaemo5ListPickSelector * ProfilDialog::createMeamoListPick(QListView * list_view)
152 {
153     QMaemo5ListPickSelector *listpicker = new QMaemo5ListPickSelector(this);
154     listpicker->setModel(model_of_files);
155     listpicker->setModelColumn(0);
156     listpicker->setView(list_view);
157     return listpicker;
158 }
159
160 /** setSelectedItem
161 Method will select right row for selction model and list picker
162 */
163 void ProfilDialog::setSelectedItem(QMaemo5ListPickSelector * listpicker, QItemSelectionModel * selModel, QString find )
164 {
165     QList<QStandardItem *> seznam_nalezenych;
166     //qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << " co ma najit " << find;
167     seznam_nalezenych = model_of_files->findItems(find, Qt::MatchExactly, 1);//find itemm
168     //qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << " Co nasel" << seznam_nalezenych;
169     if(!seznam_nalezenych.empty()){
170         selModel->select(seznam_nalezenych.at(0)->index(),QItemSelectionModel::Select | QItemSelectionModel::Rows);
171         listpicker->setCurrentIndex(seznam_nalezenych.at(0)->row());
172     }
173 }
174
175 /** accept()
176 Slot for signal when user click on save button.
177 Check is everyting is OK and save values into profile object
178 */
179 void ProfilDialog::accept()
180 {
181     //qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << " tisk všech dat";
182
183     if(ui->lineEdit_nameprofile->text().isEmpty()){
184         QMaemo5InformationBox::information(this, tr("Profile must have name"), QMaemo5InformationBox::DefaultTimeout);
185         return;
186     }
187     if(update == false){
188         if(list_of_profiles_names.contains(ui->lineEdit_nameprofile->text()))
189         {
190             QMaemo5InformationBox::information(this, tr("This profile elready exist"), QMaemo5InformationBox::DefaultTimeout);
191             return;
192         }
193     }
194
195     if(ui->lineEdit_nameprofile->text().split(QRegExp("\\s+"),QString::SkipEmptyParts).size() > 1)
196     {
197         QMaemo5InformationBox::information(this, tr("One word name please"), QMaemo5InformationBox::DefaultTimeout);
198         return;
199     }
200
201     if(ui->lineEdit_nameprofile->text().contains(QRegExp("^\\d\\D*")))
202     {
203         QMaemo5InformationBox::information(this, tr("You can't start name with digit"), QMaemo5InformationBox::DefaultTimeout);
204         return;
205     }
206
207     QModelIndexList list_of_Mobil_tone = selModelMobil->selectedIndexes();
208     if(list_of_Mobil_tone.empty())
209     {
210         QMaemo5InformationBox::information(this, tr("Select ringtone for mobile"), QMaemo5InformationBox::DefaultTimeout);
211         return;
212     }
213
214     QModelIndexList list_of_SMS_tone = selModelSMS->selectedIndexes();
215     if(list_of_SMS_tone.empty())
216     {
217         QMaemo5InformationBox::information(this, tr("Select ringtone for SMS"), QMaemo5InformationBox::DefaultTimeout);
218         return;
219     }
220
221     QModelIndexList list_of_IM_tone = selModelIM->selectedIndexes();
222     if(list_of_IM_tone.empty())
223     {
224         QMaemo5InformationBox::information(this, tr("Select ringtone for IM"), QMaemo5InformationBox::DefaultTimeout);
225         return;
226     }
227
228     QModelIndexList list_of_Email_tone = selModelEmail->selectedIndexes();
229     if(list_of_Email_tone.empty())
230     {
231         QMaemo5InformationBox::information(this, tr("Select ringtone for E-mail"), QMaemo5InformationBox::DefaultTimeout);
232         return;
233     }
234
235     // QModelIndexList list =  selModelMobil->selectedIndexes();
236     //
237     // for(int i = 0; i < list.size(); i++) {
238     // //qDebug() <<  __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << list.at(i).data().toString();
239     ////}
240
241     //    //qDebug() <<  __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << " tisk dat z dialogu";
242     //    //qDebug() << "text je " << ui->lineEdit_nameprofile->text();
243     //    //qDebug() << "email volume je " << ui->slider_email_volume->value();
244     //    //qDebug() << "im volume je " << ui->slider_im_volume->value();
245     //    //qDebug() << "ring volume je " << ui->slider_ringing_volume->value();
246     //    //qDebug() << "sms volume je " << ui->slider_sms_volume->value();
247     //    //qDebug() << "key board je " << ui->buttonGroup_keyboard->checkedId();
248     //    //qDebug() << "screen je " << ui->buttonGroup_screen->checkedId();
249     //    //qDebug() << "system je " << ui->buttonGroup_system->checkedId();
250     //    //qDebug() << list_of_Mobil_tone.at(1).data().toString() ;
251     //    //qDebug() << "zvuk SMS " << list_of_SMS_tone.at(1).data().toString() ;
252     //    //qDebug() << "zvuk Im " << list_of_IM_tone.at(1).data().toString() ;
253     //    //qDebug() << "zvuk email " << list_of_Email_tone.at(1).data().toString() ;
254     //    //qDebug() <<  __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "konec tisku dat z dialogu";
255     //m_settings->setValue("Settings/rememberGeometry", geometryCb->isChecked());
256
257     profile->SetName(ui->lineEdit_nameprofile->text());
258
259     profile->SetRingingAlertVolume(QVariant(ui->slider_ringing_volume->value()));
260     profile->SetSmsAlertVolume(QVariant(ui->slider_im_volume->value()));
261     profile->SetImAlertVolume(QVariant(ui->slider_im_volume->value()));
262     profile->SetEmailAlertVolume(QVariant(ui->slider_email_volume->value()));
263
264     profile->SetKeypadSoundLevel(QVariant(ui->buttonGroup_keyboard->checkedId()));
265     profile->SetTouchscreenSoundLevel(QVariant(ui->buttonGroup_screen->checkedId()));
266     profile->SetSystemSoundLevel(QVariant(ui->buttonGroup_system->checkedId()));
267
268     profile->SetRingingAlertTone(list_of_Mobil_tone.at(1).data());
269     profile->SetSmsAlertTone(list_of_SMS_tone.at(1).data());
270     profile->SetImAlertTone(list_of_IM_tone.at(1).data());
271     profile->SetEmailAlertTone(list_of_Email_tone.at(1).data());
272
273     profile->SetVibratingAlertEnabled(QVariant((int)ui->check_vibration->isChecked()));
274     profile->SetRingingAlertType(QVariant(QString("ringing")));
275
276     switch (ui->buttonGroup_ringing->checkedId())
277     {
278     case 0:
279         profile->SetRingingAlertType(QVariant(QString("ringing")));
280         break;
281     case 1:
282         profile->SetRingingAlertType(QVariant(QString("silent")));
283         break;
284     default:
285         profile->SetRingingAlertType(QVariant(QString("ringing")));
286     }
287
288     profile->SetCalendarAlarmEnabled(QVariant((int)ui->checkCalendarAlarm->isChecked()));
289     profile->SetClockAlarmEnabled(QVariant((int)ui->checkClockAlarm->isChecked()));
290
291     done(QDialog::Accepted);
292 }
293
294 /** createRadioID()
295 Method setup Radio buttons
296 If updating profile setradio buttons to profile values
297 */
298 void ProfilDialog::createRadioID()
299 {
300     ui->buttonGroup_system->setId(ui->radioB_system1, 0);
301     ui->buttonGroup_system->setId(ui->radioB_system2, 1);
302     ui->buttonGroup_system->setId(ui->radioB_system3, 2);
303
304     ui->buttonGroup_keyboard->setId(ui->radioB_keyboard_1,0);
305     ui->buttonGroup_keyboard->setId(ui->radioB_keyboard_2,1);
306     ui->buttonGroup_keyboard->setId(ui->radioB_keyboard_3,2);
307
308     ui->buttonGroup_screen->setId(ui->radioB_screen_1,0);
309     ui->buttonGroup_screen->setId(ui->radioB_screen_2,1);
310     ui->buttonGroup_screen->setId(ui->radioB_screen_3,2);
311
312     ui->buttonGroup_ringing->setId(ui->radioButton_ringing,0);
313     ui->buttonGroup_ringing->setId(ui->radioButton_silent,1);
314
315     if(update)
316     {
317         if(profile->GetRingingAlertType().toString().contains(QString("ringing")))
318         {
319             ui->radioButton_ringing->setChecked(true);
320         } else {
321             ui->radioButton_silent->setChecked(true);
322         }
323         ui->check_vibration->setChecked(profile->GetVibratingAlertEnabled().toBool());
324         ui->checkCalendarAlarm->setChecked(profile->GetCalendarAlarmEnabled().toBool());
325         ui->checkClockAlarm->setChecked(profile->GetClockAlarmEnabled().toBool());
326     } else {
327         ui->radioButton_ringing->setChecked(true);
328     }
329
330     switch (profile->GetSystemSoundLevel().toInt())
331     {
332     case 0:
333         ui->radioB_system1->setChecked(true);
334         break;
335     case 1:
336         ui->radioB_system2->setChecked(true);
337         break;
338     case 2:
339         ui->radioB_system3->setChecked(true);
340         break;
341     default:
342         ui->radioB_system1->setChecked(true);
343     }
344
345     switch (profile->GetKeypadSoundLevel().toInt())
346     {
347     case 0:
348         ui->radioB_keyboard_1->setChecked(true);
349         break;
350     case 1:
351         ui->radioB_keyboard_2->setChecked(true);
352         break;
353     case 2:
354         ui->radioB_keyboard_3->setChecked(true);
355         break;
356     default:
357         ui->radioB_keyboard_1->setChecked(true);
358     }
359
360     switch (profile->GetKeypadSoundLevel().toInt())
361     {
362     case 0:
363         ui->radioB_screen_1->setChecked(true);
364         break;
365     case 1:
366         ui->radioB_screen_2->setChecked(true);
367         break;
368     case 2:
369         ui->radioB_screen_3->setChecked(true);
370         break;
371     default:
372         ui->radioB_screen_1->setChecked(true);
373     }
374 }
375
376 /** on_ButtonLoadSound_clicked()
377 Slot for loading music files
378 */
379 void ProfilDialog::on_ButtonLoadSound_clicked()
380 {
381     QString fileName = QFileDialog::getOpenFileName(this, tr("Choose sound file"),"/home/user",tr("Sound files (*.wav *.mp3 *.AAC *.eAAC* *.acc *.wma *.m4a)"));
382
383     if (fileName.isEmpty())
384         return;
385
386     QFileInfo file(fileName);
387
388     QList<QStandardItem *>  items;
389
390     if(model_of_files != NULL)
391         if(checkFileInModelAndExist(file)){
392         //qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << " bude se pridavat do modelu toto" << file.absoluteFilePath();
393         items.append(new QStandardItem(file.fileName()));
394         items.append(new QStandardItem(file.absoluteFilePath()));
395         model_of_files->appendRow(items);
396     }
397 }
398
399 /** checkFileInModelAndExist()
400 Check if this audio file is already added
401 */
402 bool ProfilDialog::checkFileInModelAndExist(QFileInfo inf)
403 {
404     if(!inf.exists()) return false;
405     if(!model_of_files->findItems(inf.absoluteFilePath(),Qt::MatchExactly, 1).empty()) return false;
406     return true;
407 }
408
409 /** on_slider_ringing_volume_valueChanged
410 Slot for changing volume in text label
411 */
412 void ProfilDialog::on_slider_ringing_volume_valueChanged(int value)
413 {
414     QString str;
415     str = "%1 %2";
416     ui->label_ringing_volume->setText(str.arg(tr("Ringing:"),QString().setNum(value)));
417 }
418
419 /** on_slider_sms_volume_valueChanged
420 Slot for changing volume in text label
421 */
422 void ProfilDialog::on_slider_sms_volume_valueChanged(int value)
423 {
424     QString str;
425     str = "%1 %2";
426     ui->label_sms_volume->setText(str.arg(QString("SMS:"),QString().setNum(value)));
427 }
428
429 /** on_slider_im_volume_valueChanged
430 Slot for changing volume in text label
431 */
432 void ProfilDialog::on_slider_im_volume_valueChanged(int value)
433 {
434     QString str;
435     str = "%1 %2";
436     ui->label_im_volume->setText( str.arg(QString("IM:"),QString().setNum(value)));
437 }
438
439 /** on_slider_email_volume_valueChanged
440 Slot for changing volume in text label
441 */
442 void ProfilDialog::on_slider_email_volume_valueChanged(int value)
443 {
444     QString str;
445     str = "%1 %2";
446     ui->label_email_volume->setText( str.arg(QString("E-mail:"),QString().setNum(value)));
447 }
448
449 /** changeEvent(QEvent *e)
450
451 */
452 void ProfilDialog::changeEvent(QEvent *e)
453 {
454     QDialog::changeEvent(e);
455     switch (e->type()) {
456     case QEvent::LanguageChange:
457         ui->retranslateUi(this);
458         break;
459     default:
460         break;
461     }
462 }
463