00001 #include <qapplication.h>
00002
00003 #include "mythuitext.h"
00004 #include "mythpainter.h"
00005 #include "mythmainwindow.h"
00006 #include "mythfontproperties.h"
00007
00008 #include "mythcontext.h"
00009 #include "compat.h"
00010
00011 MythUIText::MythUIText(MythUIType *parent, const char *name)
00012 : MythUIType(parent, name)
00013 {
00014 m_Message = m_DefaultMessage = "";
00015
00016 m_Font = new MythFontProperties();
00017
00018 m_OrigDisplayRect = m_AltDisplayRect = m_Area = QRect();
00019
00020 m_Cutdown = true;
00021 m_CutMessage = "";
00022
00023 m_Justification = (Qt::AlignLeft | Qt::AlignTop);
00024
00025 m_colorCycling = false;
00026 }
00027
00028 MythUIText::MythUIText(const QString &text, const MythFontProperties &font,
00029 QRect displayRect, QRect altDisplayRect,
00030 MythUIType *parent, const char *name)
00031 : MythUIType(parent, name)
00032 {
00033 m_Message = text;
00034 m_DefaultMessage = text;
00035
00036 m_Font = new MythFontProperties();
00037 *m_Font = font;
00038
00039 m_OrigDisplayRect = m_Area = displayRect;
00040 m_AltDisplayRect = altDisplayRect;
00041
00042 m_Cutdown = true;
00043 m_CutMessage = "";
00044 m_Justification = (Qt::AlignLeft | Qt::AlignTop);
00045
00046 m_colorCycling = false;
00047 }
00048
00049 MythUIText::~MythUIText()
00050 {
00051 if (m_Font)
00052 {
00053 delete m_Font;
00054 m_Font = NULL;
00055 }
00056 }
00057
00058 void MythUIText::SetText(const QString &text)
00059 {
00060 if (text == m_Message)
00061 return;
00062
00063 m_Message = text;
00064 m_CutMessage = "";
00065 SetRedraw();
00066 }
00067
00068 QString MythUIText::GetText(void)
00069 {
00070 return m_Message;
00071 }
00072
00073 QString MythUIText::GetDefaultText(void)
00074 {
00075 return m_DefaultMessage;
00076 }
00077
00078 void MythUIText::SetFontProperties(const MythFontProperties &fontProps)
00079 {
00080 *m_Font = fontProps;
00081 SetRedraw();
00082 }
00083
00084 void MythUIText::UseAlternateArea(bool useAlt)
00085 {
00086 m_CutMessage = "";
00087
00088 if (useAlt && m_AltDisplayRect.width() > 1)
00089 MythUIType::SetArea(m_AltDisplayRect);
00090 else
00091 MythUIType::SetArea(m_OrigDisplayRect);
00092 }
00093
00094 void MythUIText::SetJustification(int just)
00095 {
00096 if (m_Justification != just)
00097 {
00098 m_Justification = just;
00099 m_CutMessage = "";
00100 SetRedraw();
00101 }
00102 }
00103
00104 int MythUIText::GetJustification(void)
00105 {
00106 return m_Justification;
00107 }
00108
00109 void MythUIText::SetCutDown(bool cut)
00110 {
00111 m_Cutdown = cut;
00112 m_CutMessage = "";
00113 SetRedraw();
00114 }
00115
00116 void MythUIText::SetArea(const QRect &rect)
00117 {
00118 m_CutMessage = "";
00119 MythUIType::SetArea(rect);
00120 }
00121
00122 void MythUIText::DrawSelf(MythPainter *p, int xoffset, int yoffset,
00123 int alphaMod, QRect clipRect)
00124 {
00125 QRect area = m_Area;
00126 area.moveBy(xoffset, yoffset);
00127
00128 int alpha = CalcAlpha(alphaMod);
00129
00130 if (m_CutMessage == "")
00131 {
00132 bool multiline = (m_Justification & Qt::WordBreak);
00133
00134 if (m_Cutdown)
00135 {
00136 QFont font = m_Font->face();
00137 m_CutMessage = cutDown(m_Message, &font, multiline);
00138 }
00139 else
00140 m_CutMessage = m_Message;
00141 }
00142
00143 p->DrawText(area, m_CutMessage, m_Justification, *m_Font, alpha);
00144 }
00145
00146 void MythUIText::Pulse(void)
00147 {
00148
00149
00150
00151
00152
00153 MythUIType::Pulse();
00154
00155 if (!m_colorCycling)
00156 return;
00157
00158 curR += incR;
00159 curG += incG;
00160 curB += incB;
00161
00162 m_curStep++;
00163 if (m_curStep >= m_numSteps)
00164 {
00165 m_curStep = 0;
00166 incR *= -1;
00167 incG *= -1;
00168 incB *= -1;
00169 }
00170
00171 QColor newColor = QColor((int)curR, (int)curG, (int)curB);
00172 if (newColor != m_Font->color())
00173 {
00174 m_Font->SetColor(newColor);
00175 SetRedraw();
00176 }
00177 }
00178
00179 void MythUIText::CycleColor(QColor startColor, QColor endColor, int numSteps)
00180 {
00181 if (!GetMythPainter()->SupportsAnimation())
00182 return;
00183
00184 m_startColor = startColor;
00185 m_endColor = endColor;
00186 m_numSteps = numSteps;
00187 m_curStep = 0;
00188
00189 curR = startColor.red();
00190 curG = startColor.green();
00191 curB = startColor.blue();
00192
00193 incR = (endColor.red() * 1.0 - curR) / m_numSteps;
00194 incG = (endColor.green() * 1.0 - curG) / m_numSteps;
00195 incB = (endColor.blue() * 1.0 - curB) / m_numSteps;
00196
00197 m_colorCycling = true;
00198 }
00199
00200 void MythUIText::StopCycling(void)
00201 {
00202 if (!m_colorCycling)
00203 return;
00204
00205 m_Font->SetColor(m_startColor);
00206 m_colorCycling = false;
00207 SetRedraw();
00208 }
00209
00210 bool MythUIText::ParseElement(QDomElement &element)
00211 {
00212 if (element.tagName() == "area")
00213 m_Area = m_OrigDisplayRect = parseRect(element);
00214 else if (element.tagName() == "altarea")
00215 m_AltDisplayRect = parseRect(element);
00216 else if (element.tagName() == "font")
00217 {
00218 QString fontname = getFirstText(element);
00219 MythFontProperties *fp = GetFont(fontname);
00220 if (!fp)
00221 fp = GetGlobalFontMap()->GetFont(fontname);
00222 if (fp)
00223 *m_Font = *fp;
00224 }
00225 else if (element.tagName() == "value")
00226 {
00227 if (element.attribute("lang","").isEmpty())
00228 {
00229 m_Message = qApp->translate("ThemeUI", getFirstText(element));
00230 }
00231 else if (element.attribute("lang","").lower() ==
00232 gContext->GetLanguageAndVariant())
00233 {
00234 m_Message = getFirstText(element);
00235 }
00236 else if (element.attribute("lang","").lower() ==
00237 gContext->GetLanguage())
00238 {
00239 m_Message = getFirstText(element);
00240 }
00241 }
00242 else if (element.tagName() == "cutdown")
00243 {
00244 m_Cutdown = parseBool(element);
00245 }
00246 else if (element.tagName() == "multiline")
00247 {
00248 if (parseBool(element))
00249 m_Justification |= Qt::WordBreak;
00250 else
00251 m_Justification &= ~Qt::WordBreak;
00252 }
00253 else if (element.tagName() == "align")
00254 {
00255 QString align = getFirstText(element).lower();
00256
00257
00258 m_Justification = m_Justification & Qt::WordBreak;
00259
00260 if (align == "center")
00261 m_Justification |= Qt::AlignCenter;
00262 else if (align == "right")
00263 m_Justification |= Qt::AlignRight;
00264 else if (align == "left")
00265 m_Justification |= Qt::AlignLeft;
00266 else if (align == "allcenter")
00267 m_Justification |= Qt::AlignHCenter | Qt::AlignVCenter;
00268 else if (align == "vcenter")
00269 m_Justification |= Qt::AlignVCenter;
00270 else if (align == "hcenter")
00271 m_Justification |= Qt::AlignHCenter;
00272 }
00273 else if (element.tagName() == "colorcycle")
00274 {
00275 if (GetMythPainter()->SupportsAnimation())
00276 {
00277 QString tmp = element.attribute("start");
00278 if (!tmp.isEmpty())
00279 m_startColor = QColor(tmp);
00280 tmp = element.attribute("end");
00281 if (!tmp.isEmpty())
00282 m_endColor = QColor(tmp);
00283 tmp = element.attribute("steps");
00284 if (!tmp.isEmpty())
00285 m_numSteps = tmp.toInt();
00286
00287
00288 CycleColor(m_startColor, m_endColor, m_numSteps);
00289 }
00290 else
00291 m_colorCycling = false;
00292
00293 if (!element.attribute("disable").isEmpty())
00294 m_colorCycling = false;
00295 }
00296 else
00297 return MythUIType::ParseElement(element);
00298
00299 return true;
00300 }
00301
00302 void MythUIText::CopyFrom(MythUIType *base)
00303 {
00304 MythUIText *text = dynamic_cast<MythUIText *>(base);
00305 if (!text)
00306 {
00307 VERBOSE(VB_IMPORTANT, "ERROR, bad parsing");
00308 return;
00309 }
00310
00311 m_Justification = text->m_Justification;
00312 m_OrigDisplayRect = text->m_OrigDisplayRect;
00313 m_AltDisplayRect = text->m_AltDisplayRect;
00314
00315 m_Message = text->m_Message;
00316 m_CutMessage = text->m_CutMessage;
00317 m_DefaultMessage = text->m_DefaultMessage;
00318
00319 m_Cutdown = text->m_Cutdown;
00320
00321 *m_Font = *(text->m_Font);
00322
00323 m_colorCycling = text->m_colorCycling;
00324 m_startColor = text->m_startColor;
00325 m_endColor = text->m_endColor;
00326 m_numSteps = text->m_numSteps;
00327 m_curStep = text->m_curStep;
00328 curR = text->curR;
00329 curG = text->curG;
00330 curB = text->curB;
00331 incR = text->incR;
00332 incG = text->incG;
00333 incB = text->incB;
00334
00335 MythUIType::CopyFrom(base);
00336 }
00337
00338 void MythUIText::CreateCopy(MythUIType *parent)
00339 {
00340 MythUIText *text = new MythUIText(parent, name());
00341 text->CopyFrom(this);
00342 }
00343
00344 void MythUIText::Finalize(void)
00345 {
00346 m_CutMessage = "";
00347 }
00348