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  m_hideDelay = std::chrono::milliseconds(getFirstText(element).toInt());
40  else
41  return MythUIType::ParseElement(filename, element, showWarnings);
42 
43  return true;
44 }
45 
47 {
48  if (value == m_pageStep)
49  return;
50 
51  if (value < 1)
52  value = kDefaultPageStep;
53 
54  m_pageStep = value;
56 }
57 
59 {
60  if (value - 1 == m_maximum)
61  return;
62 
63  if (value < 1)
64  value = 1;
65 
66  m_maximum = value - 1;
68 }
69 
71 {
72  if (value == m_sliderPosition)
73  return;
74 
75  if (value < 0)
76  value = 0;
77 
78  if (value > m_maximum)
79  value = m_maximum;
80 
81  m_sliderPosition = value;
83 }
84 
86 {
87  if (m_maximum > 0)
88  Show();
89  else
90  {
91  Hide();
92  return;
93  }
94 
95  MythUIType *slider = GetChild("slider");
96 
97  if (!slider)
98  {
99  LOG(VB_GENERAL, LOG_ERR, "Slider element doesn't exist");
100  return;
101  }
102 
103  float percentage = (float)m_sliderPosition / m_maximum;
104  float relativeSize = (float)m_pageStep / (m_maximum + m_pageStep);
105 
106  MythRect newSliderArea = slider->GetArea();
107  MythRect fillArea = GetArea();
108  QPoint endPos(newSliderArea.left(), newSliderArea.top());
109 
110  if (m_layout == LayoutHorizontal)
111  {
112  int width = std::max((int)lroundf(fillArea.width() * relativeSize),
113  m_sliderArea.width());
114  newSliderArea.setWidth(width);
115  endPos.setX(lroundf((fillArea.width() - width) * percentage));
116  }
117  else
118  {
119  int height = std::max((int)lroundf(fillArea.height() * relativeSize),
120  m_sliderArea.height());
121  newSliderArea.setHeight(height);
122  endPos.setY(lroundf((fillArea.height() - height) * percentage));
123  }
124 
125  slider->SetArea(newSliderArea);
126  slider->SetPosition(endPos);
127 
128  if (m_hideDelay > 0ms)
129  {
130  if (m_timerId)
131  killTimer(m_timerId);
132  m_timerId = startTimer(m_hideDelay);
133 
134  AdjustAlpha(1, 10, 0, 255);
135  }
136 }
137 
139 {
140  MythUIType *slider = GetChild("slider");
141  if (slider)
142  m_sliderArea = slider->GetArea();
143 
145 }
146 
148 {
149  auto *scrollbar = dynamic_cast<MythUIScrollBar *>(base);
150  if (!scrollbar)
151  return;
152 
153  m_layout = scrollbar->m_layout;
154  m_sliderArea = scrollbar->m_sliderArea;
155  m_hideDelay = scrollbar->m_hideDelay;
156 
157  MythUIType::CopyFrom(base);
158 }
159 
161 {
162  auto *scrollbar = new MythUIScrollBar(parent, objectName());
163  scrollbar->CopyFrom(this);
164 }
165 
166 void MythUIScrollBar::timerEvent(QTimerEvent * /*event*/)
167 {
168  if (m_timerId)
169  killTimer(m_timerId);
170  m_timerId = 0;
171  AdjustAlpha(1, -10, 0, 255);
173 }
174 
176 {
177  disconnect(this, &MythUIType::FinishedFading, nullptr, nullptr);
178  Hide();
179 }
MythUIScrollBar::CalculatePosition
void CalculatePosition(void)
Definition: mythuiscrollbar.cpp:85
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:160
MythUIScrollBar::m_timerId
int m_timerId
Definition: mythuiscrollbar.h:53
MythUIScrollBar::DoneFading
void DoneFading(void)
Definition: mythuiscrollbar.cpp:175
MythUIType::GetChild
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:133
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.h
MythUIType::SetArea
virtual void SetArea(const MythRect &rect)
Definition: mythuitype.cpp:609
MythUIScrollBar::CopyFrom
void CopyFrom(MythUIType *base) override
Copy this widgets state from another.
Definition: mythuiscrollbar.cpp:147
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:884
MythUIType::Show
void Show(void)
Definition: mythuitype.cpp:1147
mythlogging.h
MythUIScrollBar::LayoutVertical
@ LayoutVertical
Definition: mythuiscrollbar.h:26
MythUIType::SetPosition
void SetPosition(int x, int y)
Convenience method, calls SetPosition(const MythPoint&) Override that instead to change functionality...
Definition: mythuitype.cpp:532
MythUIScrollBar::SetSliderPosition
void SetSliderPosition(int value)
Definition: mythuiscrollbar.cpp:70
MythUIType::AdjustAlpha
void AdjustAlpha(int mode, int alphachange, int minalpha=0, int maxalpha=255)
Definition: mythuitype.cpp:927
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:1174
MythUIScrollBar
Scroll bar widget.
Definition: mythuiscrollbar.h:15
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:72
MythUIType::Hide
void Hide(void)
Definition: mythuitype.cpp:1142
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:46
MythUIScrollBar::LayoutHorizontal
@ LayoutHorizontal
Definition: mythuiscrollbar.h:26
MythRect::setWidth
void setWidth(const QString &sWidth)
Definition: mythrect.cpp:266
MythUIScrollBar::SetMaximum
void SetMaximum(int value)
Definition: mythuiscrollbar.cpp:58
MythUIScrollBar::Finalize
void Finalize(void) override
Perform any post-xml parsing initialisation tasks.
Definition: mythuiscrollbar.cpp:138
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:166
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:1240
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