00001
00002 #include "mythuistatetype.h"
00003 #include "mythuibuttonlist.h"
00004
00005
00006 #include <QDomDocument>
00007
00008
00009 #include "mythuiimage.h"
00010 #include "mythuigroup.h"
00011 #include "mythpainter.h"
00012 #include "mythmainwindow.h"
00013
00014 MythUIStateType::MythUIStateType(MythUIType *parent, const QString &name)
00015 : MythUIType(parent, name)
00016 {
00017 m_CurrentState = NULL;
00018 m_ShowEmpty = true;
00019 }
00020
00021 MythUIStateType::~MythUIStateType()
00022 {
00023 }
00024
00025 bool MythUIStateType::AddImage(const QString &name, MythImage *image)
00026 {
00027 QString key = name.toLower();
00028
00029 if (m_ObjectsByName.contains(key) || !image)
00030 return false;
00031
00032
00033
00034 MythUIImage *imType = new MythUIImage(this, name);
00035 imType->SetImage(image);
00036
00037 return AddObject(key, imType);
00038 }
00039
00040 bool MythUIStateType::AddObject(const QString &name, MythUIType *object)
00041 {
00042 QString key = name.toLower();
00043
00044 if (m_ObjectsByName.contains(key) || !object)
00045 return false;
00046
00047 object->SetVisible(false);
00048 m_ObjectsByName[key] = object;
00049
00050 MythRect objectArea = object->GetArea();
00051 objectArea.CalculateArea(m_ParentArea);
00052
00053 ExpandArea(objectArea);
00054
00055 return true;
00056 }
00057
00058 bool MythUIStateType::AddImage(StateType type, MythImage *image)
00059 {
00060 if (m_ObjectsByState.contains((int)type) || !image)
00061 return false;
00062
00063 QString name = QString("stateimage%1").arg(type);
00064
00065 MythUIImage *imType = new MythUIImage(this, name);
00066 imType->SetImage(image);
00067
00068 return AddObject(type, imType);
00069 }
00070
00071 bool MythUIStateType::AddObject(StateType type, MythUIType *object)
00072 {
00073 if (m_ObjectsByState.contains((int)type) || !object)
00074 return false;
00075
00076 object->SetVisible(false);
00077 m_ObjectsByState[(int)type] = object;
00078
00079 MythRect objectArea = object->GetArea();
00080 objectArea.CalculateArea(m_ParentArea);
00081
00082 ExpandArea(objectArea);
00083
00084 return true;
00085 }
00086
00087 bool MythUIStateType::DisplayState(const QString &name)
00088 {
00089 if (name.isEmpty())
00090 return false;
00091
00092 MythUIType *old = m_CurrentState;
00093
00094 QMap<QString, MythUIType *>::Iterator i = m_ObjectsByName.find(name.toLower());
00095
00096 if (i != m_ObjectsByName.end())
00097 m_CurrentState = i.value();
00098 else
00099 m_CurrentState = NULL;
00100
00101 if (m_CurrentState != old)
00102 {
00103 if (m_ShowEmpty || m_CurrentState)
00104 {
00105 if (m_deferload && m_CurrentState)
00106 m_CurrentState->LoadNow();
00107
00108 if (old)
00109 old->SetVisible(false);
00110
00111 if (m_CurrentState)
00112 m_CurrentState->SetVisible(true);
00113 }
00114 }
00115
00116 return (m_CurrentState != NULL);
00117 }
00118
00119 bool MythUIStateType::DisplayState(StateType type)
00120 {
00121 MythUIType *old = m_CurrentState;
00122
00123 QMap<int, MythUIType *>::Iterator i = m_ObjectsByState.find((int)type);
00124
00125 if (i != m_ObjectsByState.end())
00126 m_CurrentState = i.value();
00127 else
00128 m_CurrentState = NULL;
00129
00130 if (m_CurrentState != old)
00131 {
00132 if (m_ShowEmpty || m_CurrentState)
00133 {
00134 if (m_deferload && m_CurrentState)
00135 m_CurrentState->LoadNow();
00136
00137 if (old)
00138 old->SetVisible(false);
00139
00140 if (m_CurrentState)
00141 m_CurrentState->SetVisible(true);
00142 }
00143 }
00144
00145 return (m_CurrentState != NULL);
00146 }
00147
00148 MythUIType *MythUIStateType::GetState(const QString &name)
00149 {
00150 QString lcname = name.toLower();
00151
00152 if (m_ObjectsByName.contains(lcname))
00153 return m_ObjectsByName[lcname];
00154
00155 return NULL;
00156 }
00157
00158 MythUIType *MythUIStateType::GetState(StateType state)
00159 {
00160 if (m_ObjectsByState.contains(state))
00161 return m_ObjectsByState[state];
00162
00163 return NULL;
00164 }
00165
00169 void MythUIStateType::Clear()
00170 {
00171 if (m_ObjectsByName.isEmpty() && m_ObjectsByState.isEmpty())
00172 return;
00173
00174 QMap<QString, MythUIType *>::Iterator i;
00175
00176 for (i = m_ObjectsByName.begin(); i != m_ObjectsByName.end(); ++i)
00177 {
00178 DeleteChild(i.value());
00179 }
00180
00181 QMap<int, MythUIType *>::Iterator j;
00182
00183 for (j = m_ObjectsByState.begin(); j != m_ObjectsByState.end(); ++j)
00184 {
00185 DeleteChild(j.value());
00186 }
00187
00188 m_ObjectsByName.clear();
00189 m_ObjectsByState.clear();
00190
00191 m_CurrentState = NULL;
00192 SetRedraw();
00193 }
00194
00198 void MythUIStateType::Reset()
00199 {
00200 if (!DisplayState("default"))
00201 {
00202 if (!DisplayState(None))
00203 {
00204 if (m_CurrentState)
00205 m_CurrentState->SetVisible(false);
00206
00207 m_CurrentState = NULL;
00208 }
00209 }
00210
00211 MythUIType::Reset();
00212 }
00213
00214 bool MythUIStateType::ParseElement(
00215 const QString &filename, QDomElement &element, bool showWarnings)
00216 {
00217 QMap<QString, QString> dependsMap;
00218 if (element.tagName() == "showempty")
00219 m_ShowEmpty = parseBool(element);
00220 else if (element.tagName() == "state")
00221 {
00222 QString name = element.attribute("name", "");
00223 QString type = element.attribute("type", "");
00224
00225 QString statename;
00226
00227 if (!type.isEmpty())
00228 statename = type;
00229 else
00230 statename = name;
00231
00232 element.setAttribute("name", statename);
00233
00234 MythUIGroup *uitype = dynamic_cast<MythUIGroup *>
00235 (ParseUIType(filename, element, "group", this, NULL, showWarnings, dependsMap));
00236
00237 if (!type.isEmpty())
00238 {
00239 StateType stype = None;
00240
00241 if (type == "off")
00242 stype = Off;
00243 else if (type == "half")
00244 stype = Half;
00245 else if (type == "full")
00246 stype = Full;
00247
00248 if (uitype && !m_ObjectsByState.contains((int)stype))
00249 AddObject(stype, uitype);
00250 }
00251 else if (!name.isEmpty())
00252 {
00253 if (uitype && !m_ObjectsByName.contains(name))
00254 AddObject(name, uitype);
00255 }
00256 }
00257 else
00258 {
00259 return MythUIType::ParseElement(filename, element, showWarnings);
00260 }
00261
00262 return true;
00263 }
00264
00265 void MythUIStateType::CopyFrom(MythUIType *base)
00266 {
00267 MythUIStateType *st = dynamic_cast<MythUIStateType *>(base);
00268
00269 if (!st)
00270 return;
00271
00272 m_ShowEmpty = st->m_ShowEmpty;
00273
00274 MythUIType::CopyFrom(base);
00275
00276 QMap<QString, MythUIType *>::iterator i;
00277
00278 for (i = st->m_ObjectsByName.begin(); i != st->m_ObjectsByName.end(); ++i)
00279 {
00280 MythUIType *other = i.value();
00281 QString key = i.key();
00282
00283 MythUIType *newtype = GetChild(other->objectName());
00284 AddObject(key, newtype);
00285 newtype->SetVisible(false);
00286 }
00287
00288 QMap<int, MythUIType *>::iterator j;
00289
00290 for (j = st->m_ObjectsByState.begin(); j != st->m_ObjectsByState.end(); ++j)
00291 {
00292 MythUIType *other = j.value();
00293 int key = j.key();
00294
00295 MythUIType *newtype = GetChild(other->objectName());
00296 AddObject((StateType)key, newtype);
00297 newtype->SetVisible(false);
00298 }
00299 }
00300
00301 void MythUIStateType::CreateCopy(MythUIType *parent)
00302 {
00303 MythUIStateType *st = new MythUIStateType(parent, objectName());
00304 st->CopyFrom(this);
00305 }
00306
00307 void MythUIStateType::Finalize(void)
00308 {
00309 if (!DisplayState("default"))
00310 DisplayState(None);
00311 }
00312
00313 void MythUIStateType::EnsureStateLoaded(const QString &name)
00314 {
00315 if (name.isEmpty())
00316 return;
00317
00318 QMap<QString, MythUIType *>::Iterator i = m_ObjectsByName.find(name);
00319
00320 if (i != m_ObjectsByName.end())
00321 i.value()->LoadNow();
00322 }
00323
00324 void MythUIStateType::EnsureStateLoaded(StateType type)
00325 {
00326 QMap<int, MythUIType *>::Iterator i = m_ObjectsByState.find((int)type);
00327
00328 if (i != m_ObjectsByState.end())
00329 i.value()->LoadNow();
00330 }
00331
00332 void MythUIStateType::LoadNow(void)
00333 {
00334 if (!m_deferload)
00335 MythUIType::LoadNow();
00336 }
00337
00338 void MythUIStateType::RecalculateArea(bool recurse)
00339 {
00340 if (m_Parent)
00341 {
00342 if (objectName().startsWith("buttonlist button"))
00343 {
00344 MythUIButtonList *list = static_cast<MythUIButtonList *>(m_Parent);
00345 m_ParentArea = list->GetButtonArea();
00346 }
00347 else
00348 m_ParentArea = m_Parent->GetFullArea();
00349 }
00350 else
00351 m_ParentArea = GetMythMainWindow()->GetUIScreenRect();
00352
00353 m_Area.Reset();
00354 m_Area.CalculateArea(m_ParentArea);
00355
00356 if (recurse)
00357 {
00358 QList<MythUIType *>::iterator it;
00359
00360 for (it = m_ChildrenList.begin(); it != m_ChildrenList.end(); ++it)
00361 {
00362 (*it)->RecalculateArea(recurse);
00363 }
00364 }
00365 }
00366