Coverage Report

Created: 2026-11-25 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wt/src/Wt/WProgressBar.C
Line
Count
Source
1
/*
2
 * Copyright (C) 2010 Thomas Suckow.
3
 * Copyright (C) 2008 Emweb bv, Herent, Belgium.
4
 *
5
 *   progressCompleted() and valueChanged() contributed by Omer Katz.
6
 *
7
 * See the LICENSE file for terms of use.
8
 */
9
10
#include <Wt/WApplication.h>
11
#include <Wt/WBootstrap5Theme.h>
12
#include <Wt/WProgressBar.h>
13
#include <Wt/WTheme.h>
14
15
#include "DomElement.h"
16
#include "WebUtils.h"
17
18
using namespace Wt;
19
20
namespace Wt {
21
22
WProgressBar::WProgressBar()
23
0
  : min_(0),
24
0
    max_(100),
25
0
    value_(0),
26
0
    changed_(false),
27
0
    valueStyleClassChanged_(false)
28
0
{
29
0
  format_ = WString::fromUTF8("%.0f %%");
30
0
  setFlexBox(true);
31
0
  setInline(true);
32
0
}
33
34
void WProgressBar::setValueStyleClass(const std::string& valueStyleClass)
35
0
{
36
0
  valueStyleClass_ = valueStyleClass;
37
0
  valueStyleClassChanged_ = true;
38
0
  repaint();
39
0
}
40
41
void WProgressBar::setValue(double value)
42
0
{
43
0
  value_ = value;
44
45
0
  valueChanged_.emit(value_);
46
47
0
  if (value_ == max_)
48
0
    progressCompleted_.emit();
49
50
0
  changed_ = true;
51
0
  repaint();
52
0
}
53
54
void WProgressBar::setMinimum(double minimum)
55
0
{
56
0
  min_ = minimum;
57
58
0
  changed_ = true;
59
0
  repaint();
60
0
}
61
62
void WProgressBar::setMaximum(double maximum)
63
0
{
64
0
  max_ = maximum;
65
66
0
  changed_ = true;
67
0
  repaint();
68
0
}
69
70
void WProgressBar::setRange(double minimum, double maximum)
71
0
{
72
0
  min_ = minimum;
73
0
  max_ = maximum;
74
75
0
  changed_ = true;
76
0
  repaint();
77
0
}
78
79
void WProgressBar::setState(double minimum, double maximum, double value)
80
0
{
81
0
  min_ = minimum;
82
0
  max_ = maximum;
83
84
0
  if (value_ != value) {
85
0
    value_ = value;
86
87
0
    if (value_ == max_)
88
0
      progressCompleted_.emit();
89
0
  }
90
0
}
91
92
void WProgressBar::setFormat(const WString& format)
93
0
{
94
0
  format_ = format;
95
0
}
96
97
WString WProgressBar::text() const
98
0
{
99
0
  return Utils::formatFloat(format_, percentage());
100
0
}
101
102
double WProgressBar::percentage() const
103
0
{
104
0
  double max = maximum(), min = minimum();
105
106
0
  if (max - min != 0)
107
0
    return (value() - min) * 100 / (max - min);
108
0
  else
109
0
    return 0;
110
0
}
111
112
DomElementType WProgressBar::domElementType() const
113
0
{
114
0
  return DomElementType::DIV; // later support DomElementType::PROGRESS
115
0
}
116
117
void WProgressBar::resize(const WLength& width, const WLength& height)
118
0
{
119
0
  WInteractWidget::resize(width, height);
120
121
0
  if (!height.isAuto())
122
0
    setAttributeValue("style", "line-height: " + height.cssText());
123
0
}
124
125
void WProgressBar::updateBar(DomElement& bar)
126
0
{
127
0
  bar.setProperty(Property::StyleWidth, std::to_string(percentage()) + "%");
128
0
}
129
130
void WProgressBar::updateDom(DomElement& element, bool all)
131
0
{
132
0
  DomElement *bar = nullptr, *label = nullptr;
133
134
0
  auto app = WApplication::instance();
135
0
  auto bs5Theme = std::dynamic_pointer_cast<Wt::WBootstrap5Theme>(app->theme());
136
137
0
  if (all) {
138
0
    bar = DomElement::createNew(DomElementType::DIV);
139
0
    bar->setId("bar" + id());
140
0
    bar->setProperty(Property::Class, valueStyleClass_);
141
0
    app->theme()->apply(this, *bar, ProgressBarBar);
142
143
0
    if (bs5Theme) {
144
0
      label = bar;
145
0
    } else {
146
0
      label = DomElement::createNew(DomElementType::DIV);
147
0
      label->setId("lbl" + id());
148
0
      app->theme()->apply(this, *label, ProgressBarLabel);
149
0
    }
150
0
  }
151
152
0
  if (changed_ || all) {
153
0
    if (!bar)
154
0
      bar = DomElement::getForUpdate("bar" + id(), DomElementType::DIV);
155
0
    if (!label) {
156
0
      if (bs5Theme) {
157
0
        label = bar;
158
0
      } else {
159
0
        label = DomElement::getForUpdate("lbl" + id(), DomElementType::DIV);
160
0
      }
161
0
    }
162
163
0
    updateBar(*bar);
164
165
0
    WString s = text();
166
0
    removeScript(s);
167
168
0
    label->setProperty(Property::InnerHTML, s.toUTF8());
169
170
0
    changed_ = false;
171
0
  }
172
173
0
  if (valueStyleClassChanged_)
174
0
  {
175
0
    if (!bar)
176
0
      bar = DomElement::getForUpdate("bar" + id(), DomElementType::DIV);
177
178
0
    bar->setProperty(Property::Class, valueStyleClass_);
179
0
    WApplication::instance()->theme()->apply(this, *bar, ProgressBarBar);
180
0
    valueStyleClassChanged_ = false;
181
0
  }
182
183
0
  if (bar)
184
0
    element.addChild(bar);
185
186
0
  if (label && !bs5Theme)
187
0
    element.addChild(label);
188
189
0
  WInteractWidget::updateDom(element, all);
190
0
}
191
192
void WProgressBar::propagateRenderOk(bool deep)
193
0
{
194
0
  changed_ = false;
195
196
0
  WInteractWidget::propagateRenderOk(deep);
197
0
}
198
}
199