eliot/qt/timer_widget.cpp

245 lines
5.8 KiB
C++
Raw Normal View History

2012-01-20 21:27:53 +01:00
/*****************************************************************************
* Eliot
* Copyright (C) 2012 Olivier Teulière
* Authors: Olivier Teulière <ipkiss @@ gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*****************************************************************************/
2013-09-25 22:23:01 +02:00
#include <QTimer>
#include <QMouseEvent>
2012-01-20 21:27:53 +01:00
#include "timer_widget.h"
2012-11-07 01:08:27 +01:00
#include "debug.h"
2012-01-20 21:27:53 +01:00
2012-02-18 22:26:52 +01:00
INIT_LOGGER(qt, TimerModel);
2012-01-20 21:27:53 +01:00
INIT_LOGGER(qt, TimerWidget);
TimerModel::TimerModel(int iTotalDuration, int iAlertDuration)
: m_totalDuration(0), m_alertDuration(0), m_remaining(m_totalDuration),
m_chronoMode(false)
2012-01-20 21:27:53 +01:00
{
m_timer = new QTimer(this);
setTotalDuration(iTotalDuration);
setAlertDuration(iAlertDuration);
m_alertTriggered = false;
2012-01-20 21:27:53 +01:00
QObject::connect(m_timer, SIGNAL(timeout()), this, SLOT(updateTime()));
}
void TimerModel::setValue(int iNewValue)
{
if (iNewValue != m_remaining)
{
m_remaining = iNewValue;
emit valueChanged(m_chronoMode ? m_totalDuration - iNewValue : iNewValue);
}
}
void TimerModel::updateTime()
{
setValue(m_remaining - 1);
if (m_remaining <= 0)
{
// We reached the end of the countdown
m_timer->stop();
emit expired();
return;
}
if (m_remaining == m_alertDuration)
{
m_alertTriggered = true;
emit alert(m_alertDuration);
}
}
void TimerModel::startTimer()
2012-01-20 21:27:53 +01:00
{
if (!m_timer->isActive())
{
// Timeout every second
m_timer->start(1000);
}
2012-01-20 21:27:53 +01:00
}
void TimerModel::pauseTimer()
2012-01-20 21:27:53 +01:00
{
m_timer->stop();
}
2012-01-20 21:27:53 +01:00
void TimerModel::resetTimer()
{
pauseTimer();
m_alertTriggered = false;
setValue(m_totalDuration);
emit timerReset();
2012-01-20 21:27:53 +01:00
}
bool TimerModel::isActiveTimer() const
2012-01-20 21:27:53 +01:00
{
return m_timer->isActive();
2012-01-20 21:27:53 +01:00
}
void TimerModel::setTotalDuration(int iSeconds)
2012-01-20 21:27:53 +01:00
{
2012-11-07 01:08:27 +01:00
ASSERT(iSeconds > 0, "Invalid total duration");
if (iSeconds == m_totalDuration)
return;
2012-01-20 21:27:53 +01:00
m_totalDuration = iSeconds;
emit newTotalDuration(m_totalDuration);
resetTimer();
2012-01-20 21:27:53 +01:00
}
void TimerModel::setAlertDuration(int iSeconds)
2012-01-20 21:27:53 +01:00
{
2012-11-07 01:08:27 +01:00
// The duration can be negative (to deactivate the alert)
if (iSeconds == m_alertDuration)
return;
2012-01-20 21:27:53 +01:00
m_alertDuration = iSeconds;
resetTimer();
2012-01-20 21:27:53 +01:00
}
void TimerModel::setChronoMode(bool iChrono)
{
m_chronoMode = iChrono;
// Refresh the value immediately, instead of waiting for the next timeout
emit valueChanged(m_chronoMode ? m_totalDuration - m_remaining : m_remaining);
}
// ---------------------------------------
2012-01-20 21:27:53 +01:00
TimerWidget::TimerWidget(QWidget *parent, TimerModel &iTimerModel)
: QLCDNumber(parent), m_model(iTimerModel)
2012-01-20 21:27:53 +01:00
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
2012-01-20 21:27:53 +01:00
QObject::connect(&m_model, SIGNAL(valueChanged(int)),
this, SLOT(displayTime(int)));
QObject::connect(&m_model, SIGNAL(alert(int)),
this, SLOT(alertTriggered()));
QObject::connect(&m_model, SIGNAL(timerReset()),
this, SLOT(timerReset()));
QObject::connect(&m_model, SIGNAL(newTotalDuration(int)),
this, SLOT(newTotalDuration(int)));
// Initialize the display
newTotalDuration(m_model.getTotalDuration());
if (m_model.wasAlertTriggered())
alertTriggered();
displayTime(m_model.getValue());
2012-01-20 21:27:53 +01:00
}
void TimerWidget::displayTime(int iSeconds)
{
display(QString("%1:%2")
.arg(iSeconds / 60, 2)
.arg(iSeconds % 60, 2, 10, QChar('0')));
}
void TimerWidget::newTotalDuration(int iNewTotal)
{
// Adapt the number of digits dynamically
int minutesLength = QString("%1").arg(iNewTotal / 60).length();
setDigitCount(minutesLength + 3);
2012-01-29 19:40:27 +01:00
setMinimumSize(QSize(60 + 20 * minutesLength, 40));
}
void TimerWidget::alertTriggered()
{
QPalette pal = palette();
pal.setColor(QPalette::Foreground, Qt::red);
setPalette(pal);
}
void TimerWidget::timerReset()
{
// Restore the default color
QPalette pal = palette();
pal.setColor(QPalette::Foreground, Qt::black);
setPalette(pal);
}
2012-01-20 21:27:53 +01:00
void TimerWidget::mousePressEvent(QMouseEvent *iEvent)
{
if (iEvent->button() == Qt::LeftButton)
{
if (iEvent->modifiers() == Qt::ControlModifier)
2012-01-20 21:27:53 +01:00
{
// Ctrl+click -> switch between timer mode and chrono mode
m_model.setChronoMode(!m_model.isChronoMode());
2012-01-20 21:27:53 +01:00
}
else if (iEvent->modifiers() == Qt::NoModifier)
2012-01-20 21:27:53 +01:00
{
if (m_model.isActiveTimer())
{
// Pause execution
m_model.pauseTimer();
}
else if (m_model.getValue() > 0)
{
// Resume execution
m_model.startTimer();
}
else
{
// Restart timer
m_model.resetTimer();
m_model.startTimer();
}
2012-01-20 21:27:53 +01:00
}
}
else if (iEvent->button() == Qt::RightButton)
{
m_model.resetTimer();
2012-01-20 21:27:53 +01:00
}
}
void TimerWidget::mouseDoubleClickEvent(QMouseEvent *iEvent)
2012-01-20 21:27:53 +01:00
{
if (iEvent->modifiers() == Qt::NoModifier)
{
m_model.resetTimer();
m_model.startTimer();
}
2012-01-20 21:27:53 +01:00
}
QSize TimerWidget::sizeHint() const
{
return QSize(340, 170);
}