dsettings.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Copyright (C) 2010 Martin Zuber, zuber@centrum.cz
  3. This application is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. This application is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with This application; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. #include "dsettings.h"
  16. #include "ui_dsettings.h"
  17. #include <QSettings>
  18. #include <QDebug>
  19. #include <QRegExp>
  20. #include <QMessageBox>
  21. DSettings::DSettings(QWidget *parent) :
  22. QDialog(parent),
  23. ui(new Ui::DSettings)
  24. {
  25. ui->setupUi(this);
  26. initializeUi();
  27. connect(ui->lstPresets, SIGNAL(currentTextChanged(QString)), this, SLOT(on_lstPresets_changed(QString)));
  28. }
  29. DSettings::~DSettings()
  30. {
  31. delete ui;
  32. }
  33. void DSettings::initializeUi()
  34. {
  35. // for mobile devices, it should be maximized
  36. setWindowState(Qt::WindowMaximized);
  37. // load settings
  38. QSettings settings;
  39. QStringList keys = settings.allKeys();
  40. if(keys.size() == 0)
  41. {
  42. qDebug() << "No presets found, the defaults will be created";
  43. settings.setValue("PresetBlack Tea", QTime::fromString("00:03:00", "HH:mm:ss"));
  44. settings.setValue("PresetFruit Tea", QTime::fromString("00:03:00", "HH:mm:ss"));
  45. settings.setValue("PresetHerbal Tea", QTime::fromString("00:03:00", "HH:mm:ss"));
  46. settings.setValue("PresetEggs", QTime::fromString("00:10:00", "HH:mm:ss"));
  47. settings.setValue ("PresetDumplings", QTime::fromString("00:22:00", "HH:mm:ss"));
  48. qDebug() << "Defaults created";
  49. // reload data
  50. keys = settings.allKeys();
  51. qDebug() << "The setting were reloaded, number of keys: " << keys.size();
  52. }
  53. // insert settings into list
  54. foreach(QString key, keys)
  55. {
  56. if(key.startsWith("Preset"))
  57. {
  58. qDebug() << "Loading data for preset: " << key;
  59. QTime presetValue = settings.value(key).toTime();
  60. QRegExp regex;
  61. ui->lstPresets->addItem(key.replace(QString("Preset"), QString("")));
  62. qDebug() << "... value is: " << presetValue;
  63. }
  64. }
  65. }
  66. void DSettings::on_lstPresets_changed(QString newText)
  67. {
  68. QSettings settings;
  69. qDebug() << "New text: " << newText;
  70. QTime presetTime = settings.value(QString("Preset").append(newText)).toTime();
  71. qDebug() << "New text: " << presetTime;
  72. if(presetTime.isNull())
  73. {
  74. QMessageBox::warning(this,
  75. tr("No data"),
  76. tr("The requested entry has no data attached!<br/><br/>It seems that this entry is invalid."),
  77. QMessageBox::Ok);
  78. return;
  79. }
  80. ui->lePresetName->setText(newText);
  81. ui->tePresetTimer->setTime(presetTime);
  82. }