Coverage Report

Created: 2025-11-09 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wt/src/Wt/WTimeEdit.h
Line
Count
Source
1
// This may look like C code, but it's really -*- C++ -*-
2
/*
3
 * Copyright (C) 2015 Emweb bv, Herent, Belgium.
4
 *
5
 * See the LICENSE file for terms of use.
6
 */
7
#ifndef WTIME_EDIT_H_
8
#define WTIME_EDIT_H_
9
10
#include <Wt/WLineEdit.h>
11
#include <Wt/WTime.h>
12
#include <Wt/WTimeValidator.h>
13
#include <Wt/WTimePicker.h>
14
15
namespace Wt {
16
17
/*! \class WTimeEdit Wt/WTimeEdit.h Wt/WTimeEdit.h
18
 *  \brief A Time field editor
19
 *
20
 * Styling through CSS is not applicable.
21
 * A native HTML5 control can be used by means of setNativeControl().
22
 *
23
 * \note Using the native HTML5 control forces the format \c HH:mm or
24
 * \c HH:mm:ss. The user will not be aware of this, since the control
25
 * offers them a view dependent on their locale.
26
 *
27
 * \sa WTime
28
 * \sa WTimeValidator
29
 * \sa setNativeControl
30
 */
31
class WT_API WTimeEdit : public WLineEdit
32
{
33
public:
34
    /*! \brief Creates a new time edit.
35
     */
36
    WTimeEdit();
37
38
    virtual ~WTimeEdit();
39
40
    /*! \brief Sets the time
41
     *
42
     *  Does nothing if the current time is \p Null.
43
     *
44
     * \sa time()
45
     */
46
    void setTime(const WTime& time);
47
48
    /*! \brief Returns the time.
49
     *
50
     * Returns an invalid time (for which WTime::isValid() returns
51
     * \c false) if the time could not be parsed using the current
52
     * format().
53
     *
54
     * \sa setTime(), WTime::fromString(), WLineEdit::text()
55
     */
56
    WTime time() const;
57
58
    /*! \brief Sets the format of the Time
59
     *
60
     * This sets the format in the validator.
61
     *
62
     * When the native HTML5 control is used, the format is limited to
63
     * \c HH:mm:ss or \c HH:mm. Changing to another format has no effect.
64
     *
65
     * \sa WTimeValidator::setFormat()
66
     * \sa setNativeControl()
67
     */
68
    void setFormat(const WT_USTRING& format);
69
70
    //! \brief Returns the format.
71
    WT_USTRING format() const;
72
73
    virtual void setHidden(bool hidden,
74
                           const WAnimation& animation = WAnimation()) override;
75
76
    /*! \brief Changes whether the native HTML5 control is used.
77
     *
78
     * When enabled the browser's native time input
79
     * (<tt><input type="time"></tt>) will be used if available.
80
     * This should provide a better experience on mobile browsers.
81
     * This option is set to false by default.
82
     *
83
     * Calling native control after the widget is rendered is
84
     * not supported.
85
     *
86
     * Setting native control to true requires both \c HH:mm:ss and
87
     * \c HH:mm to be valid formats. The format can be set by either the
88
     * validator, or directly with setFormat().
89
     *
90
     * Once the format is set, the step will be automatically
91
     * calculated. This indicates the minimum increment in seconds or
92
     * minutes that is valid input.
93
     *
94
     * When setting native control to true the setters for the steps
95
     * will no longer do anything.
96
     *
97
     * \sa nativeControl()
98
     * \sa setFormat()
99
     * \sa setHourStep()
100
     * \sa setMinuteStep()
101
     * \sa setSecondStep()
102
     * \sa setMillisecondStep()
103
     * \sa setWrapAroundEnabled()
104
     */
105
    void setNativeControl(bool nativeControl);
106
107
    /*! \brief Returns whether a native HTML5 control is used.
108
     *
109
     * When active, the format of the input it limited to \c HH:mm or
110
     * \c HH:mm:ss. The step is set to \c 60, or \c 1 respectively,
111
     * specifying the granularity of the input to a minute or a second.
112
     *
113
     * \sa setNativeControl()
114
     */
115
0
    WT_NODISCARD bool nativeControl() const noexcept { return nativeControl_; }
116
117
    /*! \brief Sets the lower limit of the valid time range
118
     */
119
    void setBottom(const WTime &bottom);
120
121
    /*! \brief Returns the lower limit of the valid time range
122
     */
123
    WTime bottom() const;
124
125
    /*! \brief Sets the upper limit of the valid time range
126
     */
127
    void setTop(const WTime &top);
128
129
    /*! \brief Returns the upper limit of the valid time range
130
     */
131
    WTime top() const;
132
133
    /*! \brief Sets the step size for the hours
134
     *
135
     * It has no effect if a native HTML5 control is used.
136
     *
137
     * \sa setNativeControl()
138
     */
139
    void setHourStep(int step);
140
141
    /*! \brief Returns the step size for the hours
142
     */
143
    int hourStep() const;
144
145
    /*! \brief Sets the step size for the minutes
146
     *
147
     * It has no effect if a native HTML5 control is used.
148
     *
149
     * \sa setNativeControl()
150
     */
151
    void setMinuteStep(int step);
152
153
    /*! \brief Returns the step size for the minutes
154
     */
155
    int minuteStep() const;
156
157
    /*! \brief Sets the step size for the seconds
158
     *
159
     * It has no effect if a native HTML5 control is used.
160
     *
161
     * \sa setNativeControl()
162
     */
163
    void setSecondStep(int step);
164
165
    /*! \brief Returns the step size for the seconds
166
     */
167
    int secondStep() const;
168
169
    /*! \brief Sets the step size for the milliseconds
170
     *
171
     * It has no effect if a native HTML5 control is used.
172
     *
173
     * \sa setNativeControl()
174
     */
175
    void setMillisecondStep(int step);
176
177
    /*! \brief Returns the step size for the milliseconds
178
     */
179
    int millisecondStep() const;
180
181
    /*! \brief Enables or disables wraparound
182
     *
183
     * It has no effect if a native HTML5 control is used.
184
     *
185
     * Wraparound is enabled by default
186
     */
187
    void setWrapAroundEnabled(bool rollover);
188
189
    /*! \brief Returns whether wraparound is enabled
190
     */
191
    bool wrapAroundEnabled() const;
192
193
    virtual void load() override;
194
195
    /*! \brief Returns the validator
196
     *
197
     * \note Using the validator to change the format while
198
     * a native control is being used will break the
199
     * native control. If a native control is used, do not call
200
     * WTimeValidator()::setFormat(), instead use
201
     * WTimeEdit::setFormat().
202
     *
203
     * \sa WTimeValidator::WTimeValidator()
204
     * \sa setFormat()
205
     */
206
    virtual std::shared_ptr<WTimeValidator> timeValidator() const;
207
208
protected:
209
  void render(WFlags<RenderFlag> flags) override;
210
  void propagateSetEnabled(bool enabled) override;
211
  void updateDom(DomElement& element, bool all) override;
212
  void validatorChanged() override;
213
  WT_NODISCARD std::string type() const noexcept override;
214
215
private:
216
  std::unique_ptr<WPopupWidget> popup_;
217
  std::unique_ptr<WTimePicker> uTimePicker_;
218
  Wt::Core::observing_ptr<WTimePicker> oTimePicker_;
219
  bool nativeControl_;
220
221
  void init();
222
  void setFromTimePicker();
223
  void setFromLineEdit();
224
  void defineJavaScript();
225
  void connectJavaScript(Wt::EventSignalBase& s,
226
                         const std::string& methodName);
227
  WT_NODISCARD const char* step() const noexcept;
228
};
229
230
}
231
232
#endif // WTIME_EDIT_H_