File: HMSTimeWidget.cpp

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (136 lines) | stat: -rw-r--r-- 4,479 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*************************************************************************
    HMSTimeWidget.cpp  -  widget for setting a time in hours, minutes, seconds
                             -------------------
    begin                : Sat Sep 06 2003
    copyright            : (C) 2003 by Thomas Eschenbacher
    email                : Thomas.Eschenbacher@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/


#include <limits>

#include "libkwave/Utils.h"

#include "libgui/HMSTimeWidget.h"

//***************************************************************************
Kwave::HMSTimeWidget::HMSTimeWidget(QWidget *parent)
    :QWidget(parent), Ui::HMSTimeWidgetBase(),
     m_time(0), m_limit(std::numeric_limits<int>::max())
{
    setupUi(this);
    setValue(m_time);
    connect();
}

//***************************************************************************
Kwave::HMSTimeWidget::~HMSTimeWidget()
{
}

//***************************************************************************
int Kwave::HMSTimeWidget::value()
{
    return m_time;
}

//***************************************************************************
void Kwave::HMSTimeWidget::setValue(int value)
{
    if (value < 0) value = 0;
    if (Kwave::toUint(value) > m_limit) value = m_limit;
    m_time = value;

    const int seconds = (m_time % 60);
    const int minutes = (m_time / 60) % 60;
    const int hours   = (m_time / (60*60));

    sbHours->setValue(hours);
    sbMinutes->setValue(minutes);
    sbSeconds->setValue(seconds);
}

//***************************************************************************
void Kwave::HMSTimeWidget::setLimit(unsigned int limit)
{
    Q_ASSERT(limit <= SAMPLE_INDEX_MAX);
    if (limit > SAMPLE_INDEX_MAX)
        limit = SAMPLE_INDEX_MAX;
    if (limit < m_limit) {
        m_limit = limit;
        setValue(m_limit);
    } else {
        m_limit = limit;
    }
}

//***************************************************************************
void Kwave::HMSTimeWidget::timeChanged(int)
{
    // get current time and correct wrap-overs
    int seconds = sbSeconds->value();
    int minutes = sbMinutes->value();
    int hours   = sbHours->value();

    if (seconds < 0) {
        seconds = 59;
        minutes--;
    }
    if (minutes < 0) {
        minutes = 59;
        hours--;
    }
    if (hours < 0) {
        hours = 0;
        minutes = 0;
        seconds = 0;
    }

    Q_ASSERT((hours >= 0) && (minutes >= 0) && (seconds >= 0));
    unsigned int time = seconds + (minutes + (hours * 60L)) * 60L;

    bool changed = (time != m_time);

    disconnect();
    setValue(time);
    connect();

    if (changed) emit valueChanged(m_time); // emit the change
}

//***************************************************************************
void Kwave::HMSTimeWidget::connect()
{
    QObject::connect(sbSeconds, SIGNAL(valueChanged(int)),
                     this, SLOT(timeChanged(int)));
    QObject::connect(sbMinutes, SIGNAL(valueChanged(int)),
                     this, SLOT(timeChanged(int)));
    QObject::connect(sbHours, SIGNAL(valueChanged(int)),
                     this, SLOT(timeChanged(int)));
}

//***************************************************************************
void Kwave::HMSTimeWidget::disconnect()
{
    // disconnect the time controls
    QObject::disconnect(sbSeconds, SIGNAL(valueChanged(int)),
                        this, SLOT(timeChanged(int)));
    QObject::disconnect(sbMinutes, SIGNAL(valueChanged(int)),
                        this, SLOT(timeChanged(int)));
    QObject::disconnect(sbHours, SIGNAL(valueChanged(int)),
                        this, SLOT(timeChanged(int)));
}

//***************************************************************************
//***************************************************************************

#include "moc_HMSTimeWidget.cpp"