1 // --------------------------------------------------------------------
2 // timelabel.cpp
3 // --------------------------------------------------------------------
4 /*
5 
6     This file is part of the extensible drawing editor Ipe.
7     Copyright (c) 1993-2020 Otfried Cheong
8 
9     Ipe is free software; you can redistribute it and/or modify it
10     under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 3 of the License, or
12     (at your option) any later version.
13 
14     As a special exception, you have permission to link Ipe with the
15     CGAL library and distribute executables, as long as you follow the
16     requirements of the Gnu General Public License in regard to all of
17     the software in the executable aside from CGAL.
18 
19     Ipe is distributed in the hope that it will be useful, but WITHOUT
20     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
22     License for more details.
23 
24     You should have received a copy of the GNU General Public License
25     along with Ipe; if not, you can find it at
26     "http://www.gnu.org/copyleft/gpl.html", or write to the Free
27     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 
29 */
30 
31 #include "timelabel_qt.h"
32 
33 #include <QInputDialog>
34 
TimeLabel(QWidget * parent)35 TimeLabel::TimeLabel(QWidget* parent):
36   QLabel(parent), time(0,0,0), counting(false), countingDown(false)
37 {
38   timer = new QTimer(this);
39   connect(timer, SIGNAL(timeout()), this, SLOT(countTime()));
40   timer->start(1000);       // one second
41 }
42 
countTime()43 void TimeLabel::countTime()
44 {
45   if (!counting) return;
46 
47   if (countingDown && !(time.hour() == 0 && time.minute() == 0 && time.second() == 0))
48     time = time.addSecs(-1);
49 
50   if (!countingDown)
51     time = time.addSecs(1);
52 
53   setText(time.toString("hh:mm:ss"));
54 }
55 
mouseDoubleClickEvent(QMouseEvent * event)56 void TimeLabel::mouseDoubleClickEvent(QMouseEvent* event)
57 {
58   setTime();
59 }
60 
setTime()61 void TimeLabel::setTime()
62 {
63   bool counting_state = counting;
64   counting = false;
65 
66   bool ok;
67   int minutes = QInputDialog::getInt(this, tr("Minutes"),
68 				     tr("Minutes to count down:"),
69 				     0, 0, 10000, 1, &ok);
70   if (ok && minutes >= 0)
71     time.setHMS(minutes/60,minutes%60,0);
72 
73   counting = counting_state;
74 
75   setText(time.toString("hh:mm:ss"));
76 }
77 
resetTime()78 void TimeLabel::resetTime()
79 {
80   time.setHMS(0, 0, 0);
81   setText(time.toString("hh:mm:ss"));
82 }
83 
toggleCounting()84 void TimeLabel::toggleCounting()
85 {
86   counting = !counting;
87 }
88 
toggleCountdown()89 void TimeLabel::toggleCountdown()
90 {
91   countingDown = !countingDown;
92 }
93 
94 // --------------------------------------------------------------------
95