TimerWidget: new "chrono" mode, triggered with Ctrl+click

This commit is contained in:
Olivier Teulière 2013-01-10 01:55:41 +01:00
parent 2db1732f7a
commit ce3da4adbb
2 changed files with 43 additions and 17 deletions

View file

@ -30,7 +30,8 @@ INIT_LOGGER(qt, TimerWidget);
TimerModel::TimerModel(int iTotalDuration, int iAlertDuration) TimerModel::TimerModel(int iTotalDuration, int iAlertDuration)
: m_totalDuration(0), m_alertDuration(0), m_remaining(m_totalDuration) : m_totalDuration(0), m_alertDuration(0), m_remaining(m_totalDuration),
m_chronoMode(false)
{ {
m_timer = new QTimer(this); m_timer = new QTimer(this);
@ -47,7 +48,7 @@ void TimerModel::setValue(int iNewValue)
if (iNewValue != m_remaining) if (iNewValue != m_remaining)
{ {
m_remaining = iNewValue; m_remaining = iNewValue;
emit valueChanged(iNewValue); emit valueChanged(m_chronoMode ? m_totalDuration - iNewValue : iNewValue);
} }
} }
@ -123,6 +124,14 @@ void TimerModel::setAlertDuration(int iSeconds)
resetTimer(); resetTimer();
} }
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);
}
// --------------------------------------- // ---------------------------------------
TimerWidget::TimerWidget(QWidget *parent, TimerModel &iTimerModel) TimerWidget::TimerWidget(QWidget *parent, TimerModel &iTimerModel)
@ -185,21 +194,29 @@ void TimerWidget::mousePressEvent(QMouseEvent *iEvent)
{ {
if (iEvent->button() == Qt::LeftButton) if (iEvent->button() == Qt::LeftButton)
{ {
if (m_model.isActiveTimer()) if (iEvent->modifiers() == Qt::ControlModifier)
{ {
// Pause execution // Ctrl+click -> switch between timer mode and chrono mode
m_model.pauseTimer(); m_model.setChronoMode(!m_model.isChronoMode());
} }
else if (m_model.getValue() > 0) else if (iEvent->modifiers() == Qt::NoModifier)
{ {
// Resume execution if (m_model.isActiveTimer())
m_model.startTimer(); {
} // Pause execution
else m_model.pauseTimer();
{ }
// Restart timer else if (m_model.getValue() > 0)
m_model.resetTimer(); {
m_model.startTimer(); // Resume execution
m_model.startTimer();
}
else
{
// Restart timer
m_model.resetTimer();
m_model.startTimer();
}
} }
} }
else if (iEvent->button() == Qt::RightButton) else if (iEvent->button() == Qt::RightButton)
@ -209,10 +226,13 @@ void TimerWidget::mousePressEvent(QMouseEvent *iEvent)
} }
void TimerWidget::mouseDoubleClickEvent(QMouseEvent*) void TimerWidget::mouseDoubleClickEvent(QMouseEvent *iEvent)
{ {
m_model.resetTimer(); if (iEvent->modifiers() == Qt::NoModifier)
m_model.startTimer(); {
m_model.resetTimer();
m_model.startTimer();
}
} }

View file

@ -51,6 +51,9 @@ public:
bool wasAlertTriggered() const { return m_alertTriggered; } bool wasAlertTriggered() const { return m_alertTriggered; }
bool isExpired() const { return m_remaining == 0; } bool isExpired() const { return m_remaining == 0; }
bool isChronoMode() const { return m_chronoMode; }
void setChronoMode(bool iChrono);
bool isActiveTimer() const; bool isActiveTimer() const;
public slots: public slots:
@ -82,6 +85,9 @@ private:
/// Indicate whether we triggered an alert /// Indicate whether we triggered an alert
bool m_alertTriggered; bool m_alertTriggered;
/// Indicate whether to show the remaining time or elapsed time
bool m_chronoMode;
/// Timer used for the countdown /// Timer used for the countdown
QTimer *m_timer; QTimer *m_timer;
}; };