00001
00002
00003 #include "mythuiprogressbar.h"
00004
00005
00006 #include <QCoreApplication>
00007 #include <QDomDocument>
00008
00009
00010 #include "mythlogging.h"
00011
00012
00013
00014
00015 MythUIProgressBar::MythUIProgressBar(MythUIType *parent, const QString &name)
00016 : MythUIType(parent, name),
00017 m_layout(LayoutHorizontal), m_effect(EffectReveal),
00018 m_total(0), m_start(0),
00019 m_current(0)
00020 {
00021 }
00022
00023 void MythUIProgressBar::Reset()
00024 {
00025 m_total = m_start = m_current = 0;
00026 CalculatePosition();
00027 MythUIType::Reset();
00028 }
00029
00030 bool MythUIProgressBar::ParseElement(
00031 const QString &filename, QDomElement &element, bool showWarnings)
00032 {
00033 if (element.tagName() == "layout")
00034 {
00035 QString layout = getFirstText(element).toLower();
00036
00037 if (layout == "vertical")
00038 m_layout = LayoutVertical;
00039 else
00040 m_layout = LayoutHorizontal;
00041 }
00042 else if (element.tagName() == "style")
00043 {
00044 QString effect = getFirstText(element).toLower();
00045
00046 if (effect == "slide")
00047 m_effect = EffectSlide;
00048 else
00049 m_effect = EffectReveal;
00050 }
00051 else
00052 {
00053 return MythUIType::ParseElement(filename, element, showWarnings);
00054 }
00055
00056 return true;
00057 }
00058
00059 void MythUIProgressBar::SetStart(int value)
00060 {
00061 m_start = value;
00062 CalculatePosition();
00063 }
00064
00065 void MythUIProgressBar::SetUsed(int value)
00066 {
00067 if (value < m_start)
00068 value = m_start;
00069
00070 if (value > m_total)
00071 value = m_total;
00072
00073 m_current = value;
00074 CalculatePosition();
00075 }
00076
00077 void MythUIProgressBar::SetTotal(int value)
00078 {
00079 m_total = value;
00080 CalculatePosition();
00081 }
00082
00083 void MythUIProgressBar::CalculatePosition(void)
00084 {
00085 MythUIType *progressType = GetChild("progressimage");
00086
00087 if (!progressType)
00088 {
00089 LOG(VB_GENERAL, LOG_ERR, "Progress image doesn't exist");
00090 return;
00091 }
00092
00093 progressType->SetVisible(false);
00094
00095 int total = m_total - m_start;
00096 int current = m_current - m_start;
00097 float percentage = 0.0;
00098
00099 if (total <= 0 || current <= 0 || current > total)
00100 return;
00101
00102 percentage = (float)current / (float)total;
00103 progressType->SetVisible(true);
00104
00105 QRect fillArea = progressType->GetArea();
00106
00107 int height = fillArea.height();
00108 int width = fillArea.width();
00109 int x = fillArea.x();
00110 int y = fillArea.y();
00111
00112 switch (m_effect)
00113 {
00114 case EffectReveal :
00115
00116 if (m_layout == LayoutHorizontal)
00117 {
00118 width = (int)((float)fillArea.width() * percentage);
00119 }
00120 else
00121 {
00122 height = (int)((float)fillArea.height() * percentage);
00123 }
00124
00125 break;
00126 case EffectSlide :
00127
00128 if (m_layout == LayoutHorizontal)
00129 {
00130 int newwidth = (int)((float)fillArea.width() * percentage);
00131 x = width - newwidth;
00132 width = newwidth;
00133 }
00134 else
00135 {
00136 int newheight = (int)((float)fillArea.height() * percentage);
00137 y = height - newheight;
00138 height = newheight;
00139 }
00140
00141 break;
00142 case EffectAnimate :
00143
00144 break;
00145 }
00146
00147 MythUIImage *progressImage = dynamic_cast<MythUIImage *>(progressType);
00148 MythUIShape *progressShape = dynamic_cast<MythUIShape *>(progressType);
00149
00150 if (width <= 0)
00151 width = 1;
00152
00153 if (height <= 0)
00154 height = 1;
00155
00156 if (progressImage)
00157 progressImage->SetCropRect(x, y, width, height);
00158 else if (progressShape)
00159 progressShape->SetCropRect(x, y, width, height);
00160
00161 SetRedraw();
00162 }
00163
00164 void MythUIProgressBar::Finalize()
00165 {
00166 CalculatePosition();
00167 }
00168
00169 void MythUIProgressBar::CopyFrom(MythUIType *base)
00170 {
00171 MythUIProgressBar *progressbar = dynamic_cast<MythUIProgressBar *>(base);
00172
00173 if (!progressbar)
00174 return;
00175
00176 m_layout = progressbar->m_layout;
00177 m_effect = progressbar->m_effect;
00178
00179 m_total = progressbar->m_total;
00180 m_start = progressbar->m_start;
00181 m_current = progressbar->m_current;
00182
00183 MythUIType::CopyFrom(base);
00184 }
00185
00186 void MythUIProgressBar::CreateCopy(MythUIType *parent)
00187 {
00188 MythUIProgressBar *progressbar = new MythUIProgressBar(parent, objectName());
00189 progressbar->CopyFrom(this);
00190 }