File indexing completed on 2024-12-22 04:40:21

0001 /*
0002     SPDX-FileCopyrightText: 2007-2009 Sergio Pistone <sergio_pistone@yahoo.com.ar>
0003     SPDX-FileCopyrightText: 2010-2022 Mladen Milinkovic <max@smoothware.net>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "timeedit.h"
0009 
0010 #include <QTime>
0011 #include <QEvent>
0012 #include <QKeyEvent>
0013 
0014 #include <QDebug>
0015 
0016 TimeEdit::TimeEdit(QWidget *parent) :
0017     QTimeEdit(parent),
0018     m_secsStep(100)
0019 {
0020     setDisplayFormat("hh:mm:ss.zzz");
0021     setTimeRange(QTime(0, 0, 0, 0), QTime(23, 59, 59, 999));
0022     setWrapping(false);
0023     setAlignment(Qt::AlignHCenter);
0024     setCorrectionMode(QAbstractSpinBox::CorrectToNearestValue);
0025     setCurrentSection(QDateTimeEdit::MSecSection);
0026 
0027     connect(this, &QDateTimeEdit::timeChanged, this, &TimeEdit::onTimeChanged);
0028 }
0029 
0030 int
0031 TimeEdit::msecsStep() const
0032 {
0033     return m_secsStep;
0034 }
0035 
0036 void
0037 TimeEdit::setMSecsStep(int msecs)
0038 {
0039     m_secsStep = msecs;
0040 }
0041 
0042 int
0043 TimeEdit::value() const
0044 {
0045     return QTime(0, 0, 0, 0).msecsTo(time());
0046 }
0047 
0048 void
0049 TimeEdit::setValue(int value)
0050 {
0051     if(wrapping()) {
0052         setTime(QTime(0, 0, 0, 0).addMSecs(value));
0053     } else {
0054         if(value < QTime(0, 0, 0, 0).msecsTo(minimumTime()))
0055             setTime(minimumTime());
0056         else if(value >= QTime(0, 0, 0, 0).msecsTo(maximumTime()))
0057             setTime(maximumTime());
0058         else
0059             setTime(QTime(0, 0, 0, 0).addMSecs(value));
0060     }
0061 }
0062 
0063 void
0064 TimeEdit::onTimeChanged(const QTime &time)
0065 {
0066     emit valueChanged(QTime(0, 0, 0, 0).msecsTo(time));
0067 }
0068 
0069 void
0070 TimeEdit::stepBy(int steps)
0071 {
0072     int oldValue = value();
0073     int newValue;
0074 
0075     setSelectedSection(currentSection());
0076 
0077     switch(currentSection()) {
0078     case QDateTimeEdit::MSecSection:
0079         newValue = oldValue + m_secsStep * steps;
0080         break;
0081     case QDateTimeEdit::SecondSection:
0082         newValue = oldValue + 1000 * steps;
0083         break;
0084     case QDateTimeEdit::MinuteSection:
0085         newValue = oldValue + 60000 * steps;
0086         break;
0087     case QDateTimeEdit::HourSection:
0088         newValue = oldValue + 3600000 * steps;
0089         break;
0090     default:
0091         return;
0092     }
0093 
0094     if(oldValue != newValue)
0095         setValue(newValue);
0096 }
0097 
0098 TimeEdit::StepEnabled
0099 TimeEdit::stepEnabled() const
0100 {
0101     if(wrapping()) {
0102         return QAbstractSpinBox::StepUpEnabled | QAbstractSpinBox::StepDownEnabled;
0103     } else {
0104         QTime time = this->time();
0105         if(time == minimumTime())
0106             return QAbstractSpinBox::StepUpEnabled;
0107         else if(time == maximumTime())
0108             return QAbstractSpinBox::StepDownEnabled;
0109         else
0110             return QAbstractSpinBox::StepUpEnabled | QAbstractSpinBox::StepDownEnabled;
0111     }
0112 }
0113 
0114 void
0115 TimeEdit::keyPressEvent(QKeyEvent *event)
0116 {
0117     int key = event->key();
0118     if(key == Qt::Key_Return) {
0119         emit valueEntered(QTime(0, 0, 0, 0).msecsTo(time()));
0120     } else if(key == Qt::Key_Up) {
0121         stepUp();
0122     } else if(key == Qt::Key_Down) {
0123         stepDown();
0124     } else if(key == Qt::Key_PageUp) {
0125         setCurrentSection(QDateTimeEdit::MSecSection);
0126         stepUp();
0127     } else if(key == Qt::Key_PageDown) {
0128         setCurrentSection(QDateTimeEdit::MSecSection);
0129         stepDown();
0130     } else {
0131         QTimeEdit::keyPressEvent(event);
0132     }
0133 }
0134 
0135