MythTV  master
mythuiscrollbar.cpp
Go to the documentation of this file.
1 // Own Header
2 #include "mythuiscrollbar.h"
3 
4 // C++
5 #include <cmath>
6 #include <algorithm>
7 
8 // QT
9 #include <QCoreApplication>
10 #include <QDomDocument>
11 
12 // myth
14 
16 {
18  m_sliderPosition = 0;
20 
22 
24 }
25 
27  const QString &filename, QDomElement &element, bool showWarnings)
28 {
29  if (element.tagName() == "layout")
30  {
31  QString layout = getFirstText(element).toLower();
32 
33  if (layout == "vertical")
35  else
37  }
38  else if (element.tagName() == "hidedelay")
39  {
40  m_hideDelay = std::chrono::milliseconds(getFirstText(element).toInt());
41  }
42  else
43  {
44  return MythUIType::ParseElement(filename, element, showWarnings);
45  }
46 
47  return true;
48 }
49 
51 {
52  if (value == m_pageStep)
53  return;
54 
55  if (value < 1)
56  value = kDefaultPageStep;
57 
58  m_pageStep = value;
60 }
61 
63 {
64  if (value - 1 == m_maximum)
65  return;
66 
67  value = std::max(value, 1);
68 
69  m_maximum = value - 1;
71 }
72 
74 {
75  if (value == m_sliderPosition)
76  return;
77 
78  value = std::clamp(value, 0, m_maximum);
79 
80  m_sliderPosition = value;
82 }
83 
85 {
86  if (m_maximum > 0)
87  Show();
88  else
89  {
90  Hide();
91  return;
92  }
93 
94  MythUIType *slider = GetChild("slider");
95 
96  if (!slider)
97  {
98  LOG(VB_GENERAL, LOG_ERR, "Slider element doesn't exist");
99  return;
100  }
101 
102  float percentage = (float)m_sliderPosition / m_maximum;
103  float relativeSize = (float)m_pageStep / (m_maximum + m_pageStep);
104 
105  MythRect newSliderArea = slider->GetArea();
106  MythRect fillArea = GetArea();
107  QPoint endPos(newSliderArea.left(), newSliderArea.top());
108 
109  if (m_layout == LayoutHorizontal)
110  {
111  int width = std::max((int)lroundf(fillArea.width() * relativeSize),
112  m_sliderArea.width());
113  newSliderArea.setWidth(width);
114  endPos.setX(lroundf((fillArea.width() - width) * percentage));
115  }
116  else
117  {
118  int height = std::max((int)lroundf(fillArea.height() * relativeSize),
119  m_sliderArea.height());
120  newSliderArea.setHeight(height);
121  endPos.setY(lroundf((fillArea.height() - height) * percentage));
122  }
123 
124  slider->SetArea(newSliderArea);
125  slider->SetPosition(endPos);
126 
127  if (m_hideDelay > 0ms)
128  {
129  if (m_timerId)
130  killTimer(m_timerId);
131  m_timerId = startTimer(m_hideDelay);
132 
133  AdjustAlpha(1, 10, 0, 255);
134  }
135 }
136 
138 {
139  MythUIType *slider = GetChild("slider");
140  if (slider)
141  m_sliderArea = slider->GetArea();
142 
144 }
145 
147 {
148  auto *scrollbar = dynamic_cast<MythUIScrollBar *>(base);
149  if (!scrollbar)
150  return;
151 
152  m_layout = scrollbar->m_layout;
153  m_sliderArea = scrollbar->m_sliderArea;
154  m_hideDelay = scrollbar->m_hideDelay;
155 
156  MythUIType::CopyFrom(base);
157 }
158 
160 {
161  auto *scrollbar = new MythUIScrollBar(parent, objectName());
162  scrollbar->CopyFrom(this);
163 }
164 
165 void MythUIScrollBar::timerEvent(QTimerEvent * /*event*/)
166 {
167  if (m_timerId)
168  killTimer(m_timerId);
169  m_timerId = 0;
170  AdjustAlpha(1, -10, 0, 255);
172 }
173 
175 {
176  disconnect(this, &MythUIType::FinishedFading, nullptr, nullptr);
177  Hide();
178 }
MythUIScrollBar::CalculatePosition
void CalculatePosition(void)
Definition: mythuiscrollbar.cpp:84
MythUIScrollBar::m_sliderPosition
int m_sliderPosition
Definition: mythuiscrollbar.h:49
MythUIScrollBar::CreateCopy
void CreateCopy(MythUIType *parent) override
Copy the state of this widget to the one given, it must be of the same type.
Definition: mythuiscrollbar.cpp:159
MythUIScrollBar::m_timerId
int m_timerId
Definition: mythuiscrollbar.h:53
MythUIScrollBar::DoneFading
void DoneFading(void)
Definition: mythuiscrollbar.cpp:174
MythUIType::GetChild
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:138
MythUIScrollBar::ParseElement
bool ParseElement(const QString &filename, QDomElement &element, bool showWarnings) override
Parse the xml definition of this widget setting the state of the object accordingly.
Definition: mythuiscrollbar.cpp:26
MythUIScrollBar::m_maximum
int m_maximum
Definition: mythuiscrollbar.h:50
MythUIScrollBar::m_hideDelay
std::chrono::milliseconds m_hideDelay
Definition: mythuiscrollbar.h:52
MythUIScrollBar::LayoutVertical
@ LayoutVertical
Definition: mythuiscrollbar.h:26
mythuiscrollbar.h
MythUIType::SetArea
virtual void SetArea(const MythRect &rect)
Definition: mythuitype.cpp:610
MythUIScrollBar::CopyFrom
void CopyFrom(MythUIType *base) override
Copy this widgets state from another.
Definition: mythuiscrollbar.cpp:146
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythRect
Wrapper around QRect allowing us to handle percentage and other relative values for areas in mythui.
Definition: mythrect.h:17
MythUIType::FinishedFading
void FinishedFading()
kDefaultPageStep
const int kDefaultPageStep
Definition: mythuiscrollbar.h:7
MythUIType::GetArea
virtual MythRect GetArea(void) const
If the object has a minimum area defined, return it, other wise return the default area.
Definition: mythuitype.cpp:885
MythUIType::Show
void Show(void)
Definition: mythuitype.cpp:1144
mythlogging.h
MythUIType::SetPosition
void SetPosition(int x, int y)
Convenience method, calls SetPosition(const MythPoint&) Override that instead to change functionality...
Definition: mythuitype.cpp:533
MythUIScrollBar::SetSliderPosition
void SetSliderPosition(int value)
Definition: mythuiscrollbar.cpp:73
MythUIType::AdjustAlpha
void AdjustAlpha(int mode, int alphachange, int minalpha=0, int maxalpha=255)
Definition: mythuitype.cpp:928
XMLParseBase::getFirstText
static QString getFirstText(QDomElement &element)
Definition: xmlparsebase.cpp:52
MythUIScrollBar::MythUIScrollBar
MythUIScrollBar(MythUIType *parent, const QString &name)
Definition: mythuiscrollbar.h:20
MythRect::setHeight
void setHeight(const QString &sHeight)
Definition: mythrect.cpp:277
MythUIType::CopyFrom
virtual void CopyFrom(MythUIType *base)
Copy this widgets state from another.
Definition: mythuitype.cpp:1171
MythUIScrollBar
Scroll bar widget.
Definition: mythuiscrollbar.h:15
clamp
static eu8 clamp(eu8 value, eu8 low, eu8 high)
Definition: pxsup2dast.c:204
MythUIScrollBar::LayoutHorizontal
@ LayoutHorizontal
Definition: mythuiscrollbar.h:26
MythUIType::Reset
virtual void Reset(void)
Reset the widget to it's original state, should not reset changes made by the theme.
Definition: mythuitype.cpp:73
MythUIType::Hide
void Hide(void)
Definition: mythuitype.cpp:1139
MythUIType
The base class on which all widgets and screens are based.
Definition: mythuitype.h:85
MythUIScrollBar::m_pageStep
int m_pageStep
Definition: mythuiscrollbar.h:48
MythUIScrollBar::SetPageStep
void SetPageStep(int value)
Definition: mythuiscrollbar.cpp:50
MythRect::setWidth
void setWidth(const QString &sWidth)
Definition: mythrect.cpp:266
MythUIScrollBar::SetMaximum
void SetMaximum(int value)
Definition: mythuiscrollbar.cpp:62
MythUIScrollBar::Finalize
void Finalize(void) override
Perform any post-xml parsing initialisation tasks.
Definition: mythuiscrollbar.cpp:137
MythUIScrollBar::m_sliderArea
MythRect m_sliderArea
Definition: mythuiscrollbar.h:47
MythUIScrollBar::m_layout
LayoutType m_layout
Definition: mythuiscrollbar.h:45
MythUIScrollBar::timerEvent
void timerEvent(QTimerEvent *event) override
Definition: mythuiscrollbar.cpp:165
MythUIType::ParseElement
virtual bool ParseElement(const QString &filename, QDomElement &element, bool showWarnings)
Parse the xml definition of this widget setting the state of the object accordingly.
Definition: mythuitype.cpp:1237
build_compdb.filename
filename
Definition: build_compdb.py:21
kDefaultMaxValue
const int kDefaultMaxValue
Definition: mythuiscrollbar.h:6
MythUIScrollBar::Reset
void Reset(void) override
Reset the widget to it's original state, should not reset changes made by the theme.
Definition: mythuiscrollbar.cpp:15