mainwindow.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QSettings>
  4. mainwindow::mainwindow(QWidget *parent)
  5. : QMainWindow(parent)
  6. , ui(new Ui::mainwindow)
  7. {
  8. ui->setupUi(this);
  9. timer = new QTimer();
  10. connect(timer, SIGNAL(timeout()), this, SLOT(timer_slot()));
  11. m = 25;
  12. s = ms = 0;
  13. ui->spinBoxStudyTime->setValue(25);
  14. ui->spinBoxRestTime->setValue(5);
  15. ui->spinBoxSession->setValue(3);
  16. readsettings();
  17. }
  18. mainwindow::~mainwindow()
  19. {
  20. writesettings();
  21. delete ui;
  22. }
  23. void mainwindow::timer_slot()
  24. {
  25. if(m != 0 || s != 0 || ms != 0)
  26. {
  27. ms--;
  28. if(ms < 0)
  29. {
  30. ms = 99;
  31. s--;
  32. if(s < 0)
  33. {
  34. s = 59;
  35. if(m > 0)
  36. m--;
  37. }
  38. }
  39. }
  40. else if(ui->actionAutostart_next_session->isChecked() && pastSession != countSession)
  41. {
  42. if(studyStatus == 2)
  43. {
  44. studyStatus = 1;
  45. ++pastSession;
  46. m = ui->spinBoxRestTime->value();
  47. }
  48. else if(studyStatus == 1)
  49. {
  50. studyStatus = 2;
  51. m = ui->spinBoxStudyTime->value();
  52. }
  53. }
  54. else if(pastSession != countSession)
  55. {
  56. on_pushBtnStart_clicked();
  57. ui->pushBtnNext->show();
  58. if(studyStatus == 2)
  59. ui->pushBtnNext->setText("Start rest");
  60. else
  61. ui->pushBtnNext->setText("Start next session");
  62. }
  63. else
  64. {
  65. flagbtn = !flagbtn;
  66. studyStatus = 0;
  67. ui->pushBtnStart->setText("Start");
  68. timer->stop();
  69. ui->pushBtnSetTime->show();
  70. ui->spinBoxStudyTime->setEnabled(true);
  71. ui->spinBoxRestTime->setEnabled(true);
  72. ui->spinBoxSession->setEnabled(true);
  73. }
  74. ui->statusBar->showMessage("Session " + QString::number(pastSession) + "/" + QString::number(countSession));
  75. if(ui->actionShow_seconds->isChecked())
  76. ui->labelTimer->setText(QString("%1:%2").arg(m, 2, 10, QChar('0')).arg(s, 2, 10, QChar('0')));
  77. else
  78. ui->labelTimer->setText(QString("%1").arg(m + 1, 3));
  79. }
  80. void mainwindow::on_pushBtnStart_clicked()
  81. {
  82. flagbtn = !flagbtn;
  83. if(flagbtn)
  84. {
  85. ui->pushBtnStart->setText("Start");
  86. timer->stop();
  87. ui->pushBtnSetTime->show();
  88. ui->spinBoxStudyTime->setEnabled(true);
  89. ui->spinBoxRestTime->setEnabled(true);
  90. ui->spinBoxSession->setEnabled(true);
  91. }
  92. else
  93. {
  94. ui->pushBtnStart->setText("Stop");
  95. timer->start(10);
  96. ui->pushBtnSetTime->hide();
  97. ui->spinBoxStudyTime->setEnabled(false);
  98. ui->spinBoxRestTime->setEnabled(false);
  99. ui->spinBoxSession->setEnabled(false);
  100. }
  101. if (studyStatus == 0)
  102. studyStatus = 2;
  103. }
  104. void mainwindow::on_pushBtnSetTime_clicked()
  105. {
  106. m = ui->spinBoxStudyTime->value();
  107. s = ms = 0;
  108. countSession = ui->spinBoxSession->value();
  109. studyStatus = 2;
  110. if(ui->actionShow_seconds->isChecked())
  111. ui->labelTimer->setText(QString("%1:%2").arg(m, 2, 10, QChar('0')).arg(s, 2, 10, QChar('0')));
  112. else
  113. ui->labelTimer->setText(QString("%1").arg(m + 1, 3));
  114. }
  115. void mainwindow::on_pushBtnNext_clicked()
  116. {
  117. ui->pushBtnNext->hide();
  118. if(studyStatus == 2)
  119. {
  120. studyStatus = 1;
  121. ++pastSession;
  122. m = ui->spinBoxRestTime->value();
  123. }
  124. else if(studyStatus == 1)
  125. {
  126. studyStatus = 2;
  127. m = ui->spinBoxStudyTime->value();
  128. }
  129. on_pushBtnStart_clicked();
  130. }
  131. void mainwindow::readsettings()
  132. {
  133. QSettings settings("settings.ini", QSettings::IniFormat);
  134. m = settings.value("studyTime").toInt();
  135. s = ms = 0;
  136. if(int i = settings.value("studyTime").toInt(); i == 0)
  137. ui->spinBoxStudyTime->setValue(25);
  138. else
  139. ui->spinBoxStudyTime->setValue(i);
  140. if(int i = settings.value("restTime").toInt(); i == 0)
  141. ui->spinBoxRestTime->setValue(5);
  142. else
  143. ui->spinBoxRestTime->setValue(i);
  144. if(int i = settings.value("countSessions").toInt(); i == 0)
  145. ui->spinBoxSession->setValue(3);
  146. else
  147. ui->spinBoxSession->setValue(i);
  148. countSession = ui->spinBoxSession->value();
  149. ui->actionShow_seconds->setChecked(settings.value("showSeconds").toBool());
  150. ui->actionAutostart_next_session->setChecked(settings.value("autostartCheck").toBool());
  151. pastSession = 0;
  152. studyStatus = 0;
  153. ui->pushBtnNext->hide();
  154. if(ui->actionShow_seconds->isChecked())
  155. ui->labelTimer->setText(QString("%1:%2").arg(m, 2, 10, QChar('0')).arg(s, 2, 10, QChar('0')));
  156. else
  157. ui->labelTimer->setText(QString("%1").arg(m + 1, 3));
  158. }
  159. void mainwindow::writesettings()
  160. {
  161. QSettings settings("settings.ini", QSettings::IniFormat);
  162. settings.setValue("studyTime", ui->spinBoxStudyTime->value());
  163. settings.setValue("restTime", ui->spinBoxRestTime->value());
  164. settings.setValue("countSessions", ui->spinBoxSession->value());
  165. settings.setValue("showSeconds", ui->actionShow_seconds->isChecked());
  166. settings.setValue("autostastCheck", ui->actionAutostart_next_session->isChecked());
  167. }