feggclockwidget.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 "feggclockwidget.h"
  16. #include "ui_feggclockwidget.h"
  17. #include <QDebug>
  18. #include <QSettings>
  19. FEggClockWidget::FEggClockWidget(QWidget *parent) :
  20. QWidget(parent),
  21. ui(new Ui::FEggClockWidget)
  22. {
  23. ui->setupUi(this);
  24. loadPresets();
  25. // set up timers
  26. m_timer = new QTimer();
  27. m_timer->setInterval(1000);
  28. m_alertTimer = new QTimer();
  29. m_alertTimer->setInterval(300);
  30. // set up status flags
  31. m_isRunning = false;
  32. m_isAlert = false;
  33. // connect timers' signals to slots
  34. connect(m_timer, SIGNAL(timeout()), this, SLOT(onTimerTimeOut()));
  35. connect(m_alertTimer, SIGNAL(timeout()), this, SLOT(onAlertTimerTimeOut()));
  36. }
  37. FEggClockWidget::~FEggClockWidget()
  38. {
  39. // delete timers
  40. if(m_timer->isActive())
  41. {
  42. m_timer->stop();
  43. delete m_timer;
  44. }
  45. if(m_alertTimer->isActive())
  46. {
  47. m_alertTimer->stop();
  48. delete m_alertTimer;
  49. }
  50. delete ui;
  51. }
  52. // one second decrement, if the time reaches 00:00:00 the alert is turn on
  53. void FEggClockWidget::onTimerTimeOut()
  54. {
  55. qDebug() << "The timer has expired";
  56. QTime curTime = ui->timeEdit->time();
  57. qDebug() << "Timer event - minut second";
  58. curTime = curTime.addSecs(-1);
  59. ui->timeEdit->setTime(curTime);
  60. // is it time to alert?
  61. if(curTime.hour() == 0 && curTime.minute() == 0 && curTime.second() == 0)
  62. {
  63. m_isRunning = false;
  64. qDebug() << "ALERT";
  65. ui->pbClear->setEnabled(true);
  66. ui->pbStart->setText(tr("Snooze"));
  67. m_timer->stop();
  68. m_alertTimer->start();
  69. m_isAlert = true;
  70. }
  71. }
  72. /**
  73. The button `pbStart` can have three stages:
  74. <ul>
  75. <li>Start - after click the counter is started</li>
  76. <li>Stop - after click the counter is stopped</li>
  77. <li>Snooze - after click the alert is snoozed</li>
  78. </ul>
  79. */
  80. void FEggClockWidget::on_pbStart_clicked()
  81. {
  82. if(m_isAlert) {
  83. ui->pbStart->setStyleSheet("");
  84. ui->pbStart->setText("Start");
  85. m_alertTimer->stop();
  86. m_isAlert = false;
  87. return;
  88. }
  89. if(m_isRunning)
  90. {
  91. // the timer is running
  92. m_timer->stop();
  93. ui->pbStart->setText(tr("Start"));
  94. m_isRunning = false;
  95. qDebug() << "Timer has been stopped";
  96. ui->pbClear->setEnabled(true);
  97. }
  98. else
  99. {
  100. m_lastTime = ui->timeEdit->time();
  101. m_timer->start();
  102. ui->pbStart->setText(tr("Stop"));
  103. m_isRunning = true;
  104. qDebug() << "Timer has been started";
  105. ui->pbClear->setEnabled(false);
  106. }
  107. }
  108. /**
  109. Only sets the clock to zero time
  110. */
  111. void FEggClockWidget::on_pbClear_clicked()
  112. {
  113. ui->timeEdit->setTime(QTime::fromString("00:00:00", "HH:mm:ss"));
  114. }
  115. /**
  116. Returns the last set time to widget, this can be done during the counting
  117. */
  118. void FEggClockWidget::on_pbReset_clicked()
  119. {
  120. ui->timeEdit->setTime(m_lastTime);
  121. }
  122. /**
  123. Very simple effects (just now only visual) for alert
  124. */
  125. void FEggClockWidget::onAlertTimerTimeOut()
  126. {
  127. if(m_alertStyleSwitch)
  128. {
  129. ui->pbStart->setStyleSheet("background: red; color: white;");
  130. qDebug() << "R";
  131. m_alertStyleSwitch = false;
  132. }
  133. else
  134. {
  135. ui->pbStart->setStyleSheet("background: yellow; color: black;");
  136. qDebug() << "Y";
  137. m_alertStyleSwitch = true;
  138. }
  139. qDebug() << "Alert screen updated" << m_alertStyleSwitch;
  140. }
  141. /**
  142. Stops and resets the clock, primary usage is for application resize
  143. @see MainWindow
  144. */
  145. void FEggClockWidget::stopAlertAndReset()
  146. {
  147. if(m_timer->isActive()) m_timer->stop();
  148. if(m_alertTimer->isActive()) m_alertTimer->stop();
  149. m_isAlert = false;
  150. m_isRunning = false;
  151. ui->pbStart->setStyleSheet("");
  152. ui->pbStart->setText(tr("Start"));
  153. }
  154. /**
  155. Loads presets from application settings
  156. @todo replace hardcoded settings to customizable
  157. */
  158. void FEggClockWidget::loadPresets()
  159. {
  160. QSettings settings;
  161. QStringList keys = settings.allKeys();
  162. if(keys.size() == 0)
  163. {
  164. qDebug() << "No presets found";
  165. }
  166. ui->cbPreset->addItem(tr("- none -"));
  167. foreach(QString key, keys)
  168. {
  169. if(key.startsWith("Preset"))
  170. {
  171. qDebug() << "Loading data for preset: " << key;
  172. QTime presetValue = settings.value(key).toTime();
  173. ui->cbPreset->addItem(key.replace(QString("Preset"), QString("")), presetValue);
  174. qDebug() << "... value is: " << presetValue;
  175. }
  176. }
  177. }
  178. void FEggClockWidget::on_cbPreset_currentIndexChanged(int index)
  179. {
  180. QVariant timeData = ui->cbPreset->itemData(index);
  181. if(timeData.isNull()) return;
  182. ui->timeEdit->setTime(timeData.toTime());
  183. }