00001 #include "mythuistatetype.h"
00002 #include "mythuiimage.h"
00003 #include "mythpainter.h"
00004 #include "mythmainwindow.h"
00005
00006 #include "mythcontext.h"
00007
00008 MythUIStateType::MythUIStateType(MythUIType *parent, const char *name)
00009 : MythUIType(parent, name)
00010 {
00011 m_CurrentState = NULL;
00012 m_ShowEmpty = false;
00013 }
00014
00015 MythUIStateType::~MythUIStateType()
00016 {
00017 }
00018
00019 bool MythUIStateType::AddImage(const QString &name, MythImage *image)
00020 {
00021 QString key = name.lower();
00022 if (m_ObjectsByName.contains(key) || !image)
00023 return false;
00024
00025 MythUIImage *imType = new MythUIImage(this, key);
00026 imType->SetImage(image);
00027
00028 return AddObject(name, imType);
00029 }
00030
00031 bool MythUIStateType::AddObject(const QString &name, MythUIType *object)
00032 {
00033 QString key = name.lower();
00034 if (m_ObjectsByName.contains(key) || !object)
00035 return false;
00036
00037 object->SetVisible(false);
00038 m_ObjectsByName[key] = object;
00039
00040 QSize aSize = m_Area.size();
00041 aSize = aSize.expandedTo(object->GetArea().size());
00042 m_Area.setSize(aSize);
00043
00044 return true;
00045 }
00046
00047 bool MythUIStateType::AddImage(StateType type, MythImage *image)
00048 {
00049 if (m_ObjectsByState.contains((int)type) || !image)
00050 return false;
00051
00052 MythUIImage *imType = new MythUIImage(this, "stateimage");
00053 imType->SetImage(image);
00054
00055 return AddObject(type, imType);
00056 }
00057
00058 bool MythUIStateType::AddObject(StateType type, MythUIType *object)
00059 {
00060 if (m_ObjectsByState.contains((int)type) || !object)
00061 return false;
00062
00063 object->SetVisible(false);
00064 m_ObjectsByState[(int)type] = object;
00065
00066 QSize aSize = m_Area.size();
00067 aSize = aSize.expandedTo(object->GetArea().size());
00068 m_Area.setSize(aSize);
00069
00070 return true;
00071 }
00072
00073 bool MythUIStateType::DisplayState(const QString &name)
00074 {
00075 MythUIType *old = m_CurrentState;
00076
00077 QMap<QString, MythUIType *>::Iterator i = m_ObjectsByName.find(name.lower());
00078 if (i != m_ObjectsByName.end())
00079 m_CurrentState = i.data();
00080 else
00081 m_CurrentState = NULL;
00082
00083 if (m_CurrentState != old)
00084 {
00085 if (m_ShowEmpty || m_CurrentState != NULL)
00086 {
00087 if (m_CurrentState)
00088 m_CurrentState->SetVisible(true);
00089 if (old)
00090 old->SetVisible(false);
00091 }
00092 }
00093
00094 return (m_CurrentState != NULL);
00095 }
00096
00097 bool MythUIStateType::DisplayState(StateType type)
00098 {
00099 MythUIType *old = m_CurrentState;
00100
00101 QMap<int, MythUIType *>::Iterator i = m_ObjectsByState.find((int)type);
00102 if (i != m_ObjectsByState.end())
00103 m_CurrentState = i.data();
00104 else
00105 m_CurrentState = NULL;
00106
00107 if (m_CurrentState != old)
00108 {
00109 if (m_ShowEmpty || m_CurrentState != NULL)
00110 {
00111 if (m_CurrentState)
00112 m_CurrentState->SetVisible(true);
00113 if (old)
00114 old->SetVisible(false);
00115 }
00116 }
00117
00118 return (m_CurrentState != NULL);
00119 }
00120
00121 void MythUIStateType::ClearMaps()
00122 {
00123 QMap<QString, MythUIType *>::Iterator i;
00124 for (i = m_ObjectsByName.begin(); i != m_ObjectsByName.end(); ++i)
00125 {
00126 delete i.data();
00127 }
00128
00129 QMap<int, MythUIType *>::Iterator j;
00130 for (j = m_ObjectsByState.begin(); j != m_ObjectsByState.end(); ++j)
00131 {
00132 delete j.data();
00133 }
00134
00135 m_ObjectsByName.clear();
00136 m_ObjectsByState.clear();
00137
00138 m_CurrentState = NULL;
00139 }
00140
00141 void MythUIStateType::ClearImages()
00142 {
00143 ClearMaps();
00144 SetRedraw();
00145 }
00146
00147 bool MythUIStateType::ParseElement(QDomElement &element)
00148 {
00149 if (element.tagName() == "showempty")
00150 m_ShowEmpty = parseBool(element);
00151 else if (element.tagName() == "state")
00152 {
00153 QString name = element.attribute("name", "").lower();
00154 QString type = element.attribute("type", "").lower();
00155
00156 MythUIType *uitype = ParseChildren(element, this);
00157
00158 if (!type.isEmpty())
00159 {
00160 StateType stype = None;
00161 if (type == "off")
00162 stype = Off;
00163 else if (type == "half")
00164 stype = Half;
00165 else if (type == "full")
00166 stype = Full;
00167
00168 if (uitype && m_ObjectsByState.contains((int)stype))
00169 {
00170 delete m_ObjectsByState[(int)stype];
00171 m_ObjectsByState.erase((int)stype);
00172 }
00173 AddObject(stype, uitype);
00174 }
00175 else if (!name.isEmpty())
00176 {
00177 if (uitype && m_ObjectsByName.contains(name))
00178 {
00179 delete m_ObjectsByName[name];
00180 m_ObjectsByName.erase(name);
00181 }
00182 AddObject(name, uitype);
00183 }
00184 }
00185 else
00186 return MythUIType::ParseElement(element);
00187
00188 return true;
00189 }
00190
00191 void MythUIStateType::CopyFrom(MythUIType *base)
00192 {
00193 MythUIStateType *st = dynamic_cast<MythUIStateType *>(base);
00194 if (!st)
00195 {
00196 VERBOSE(VB_IMPORTANT, "ERROR, bad parsing");
00197 return;
00198 }
00199
00200 ClearMaps();
00201
00202 m_ShowEmpty = st->m_ShowEmpty;
00203
00204 MythUIType::CopyFrom(base);
00205
00206 QMap<QString, MythUIType *>::iterator i;
00207 for (i = st->m_ObjectsByName.begin(); i != st->m_ObjectsByName.end(); ++i)
00208 {
00209 MythUIType *other = i.data();
00210 QString key = i.key();
00211
00212 MythUIType *newtype = GetChild(other->name());
00213 AddObject(key, newtype);
00214 newtype->SetVisible(other->IsVisible());
00215 }
00216
00217 QMap<int, MythUIType *>::iterator j;
00218 for (j = st->m_ObjectsByState.begin(); j != st->m_ObjectsByState.end(); ++j)
00219 {
00220 MythUIType *other = j.data();
00221 int key = j.key();
00222
00223 MythUIType *newtype = GetChild(other->name());
00224 AddObject((StateType)key, newtype);
00225 newtype->SetVisible(other->IsVisible());
00226 }
00227 }
00228
00229 void MythUIStateType::CreateCopy(MythUIType *parent)
00230 {
00231 MythUIStateType *st = new MythUIStateType(parent, name());
00232 st->CopyFrom(this);
00233 }
00234
00235 void MythUIStateType::Finalize(void)
00236 {
00237 }
00238