00001
00002 #include "mythuibutton.h"
00003
00004
00005 #include <QTimer>
00006 #include <QDomDocument>
00007 #include <QCoreApplication>
00008
00009
00010 #include "mythlogging.h"
00011
00012
00013 #include "mythmainwindow.h"
00014 #include "mythuigroup.h"
00015
00016 MythUIButton::MythUIButton(MythUIType *parent, const QString &name)
00017 : MythUIType(parent, name)
00018 {
00019 m_clickTimer = new QTimer();
00020 m_clickTimer->setSingleShot(true);
00021
00022 m_Pushed = false;
00023 m_Lockable = false;
00024
00025 m_Text = NULL;
00026 m_BackgroundState = NULL;
00027
00028 connect(m_clickTimer, SIGNAL(timeout()), SLOT(UnPush()));
00029
00030 connect(this, SIGNAL(TakingFocus()), SLOT(Select()));
00031 connect(this, SIGNAL(LosingFocus()), SLOT(Deselect()));
00032 connect(this, SIGNAL(Enabling()), SLOT(Enable()));
00033 connect(this, SIGNAL(Disabling()), SLOT(Disable()));
00034
00035 SetCanTakeFocus(true);
00036 }
00037
00038 MythUIButton::~MythUIButton()
00039 {
00040 if (m_clickTimer)
00041 m_clickTimer->deleteLater();
00042 }
00043
00044 void MythUIButton::SetInitialStates()
00045 {
00046 m_BackgroundState = dynamic_cast<MythUIStateType *>(GetChild("buttonstate"));
00047
00048 if (!m_BackgroundState)
00049 LOG(VB_GENERAL, LOG_ERR, QString("Button %1 is missing required "
00050 "elements").arg(objectName()));
00051
00052 SetState("active");
00053
00054 if (m_Text && m_Message.isEmpty())
00055 m_Message = m_Text->GetDefaultText();
00056 }
00057
00061 void MythUIButton::Reset()
00062 {
00063 MythUIType::Reset();
00064 }
00065
00066 void MythUIButton::Select()
00067 {
00068 if (!IsEnabled() || m_Pushed)
00069 return;
00070
00071 SetState("selected");
00072 }
00073
00074 void MythUIButton::Deselect()
00075 {
00076 if (m_Pushed)
00077 return;
00078
00079 if (IsEnabled())
00080 SetState("active");
00081 else
00082 SetState("disabled");
00083 }
00084
00085 void MythUIButton::Enable()
00086 {
00087 SetState("active");
00088 }
00089
00090 void MythUIButton::Disable()
00091 {
00092 SetState("disabled");
00093 }
00094
00095 void MythUIButton::SetState(QString state)
00096 {
00097 if (m_state == state)
00098 return;
00099
00100 if (m_Pushed && state != "pushed")
00101 UnPush();
00102
00103 m_state = state;
00104
00105 if (!m_BackgroundState)
00106 return;
00107
00108 m_BackgroundState->DisplayState(m_state);
00109
00110 MythUIGroup *activeState = dynamic_cast<MythUIGroup *>
00111 (m_BackgroundState->GetCurrentState());
00112
00113 if (activeState)
00114 m_Text = dynamic_cast<MythUIText *>(activeState->GetChild("text"));
00115
00116 if (m_Text)
00117 {
00118 m_Text->SetFontState(m_state);
00119 m_Text->SetText(m_Message);
00120 }
00121 }
00122
00126 bool MythUIButton::keyPressEvent(QKeyEvent *e)
00127 {
00128 QStringList actions;
00129 bool handled = false;
00130 handled = GetMythMainWindow()->TranslateKeyPress("Global", e, actions);
00131
00132 for (int i = 0; i < actions.size() && !handled; i++)
00133 {
00134 QString action = actions[i];
00135 handled = true;
00136
00137 if (action == "SELECT")
00138 {
00139 if (IsEnabled())
00140 {
00141 if (m_Pushed)
00142 UnPush();
00143 else
00144 Push();
00145 }
00146 }
00147 else
00148 handled = false;
00149 }
00150
00151 return handled;
00152 }
00153
00157 bool MythUIButton::gestureEvent(MythGestureEvent *event)
00158 {
00159 if (event->gesture() == MythGestureEvent::Click)
00160 {
00161 if (IsEnabled())
00162 {
00163 if (m_Pushed)
00164 UnPush();
00165 else
00166 Push();
00167
00168 return true;
00169 }
00170 }
00171
00172 return false;
00173 }
00174
00175 void MythUIButton::Push(bool lock)
00176 {
00177 m_Pushed = true;
00178 SetState("pushed");
00179
00180 if (!lock && !m_Lockable)
00181 m_clickTimer->start(500);
00182
00183 emit Clicked();
00184 }
00185
00186 void MythUIButton::UnPush()
00187 {
00188 if (!m_Pushed)
00189 return;
00190
00191 m_clickTimer->stop();
00192
00193 m_Pushed = false;
00194
00195 if (m_HasFocus)
00196 SetState("selected");
00197 else if (m_Enabled)
00198 SetState("active");
00199 else
00200 SetState("disabled");
00201
00202 if (m_Lockable)
00203 emit Clicked();
00204 }
00205
00206 void MythUIButton::SetLocked(bool locked)
00207 {
00208 if (!m_Lockable)
00209 return;
00210
00211 if (locked)
00212 {
00213 m_Pushed = true;
00214 SetState("pushed");
00215 }
00216 else
00217 {
00218 m_Pushed = false;
00219
00220 if (m_HasFocus)
00221 SetState("selected");
00222 else if (m_Enabled)
00223 SetState("active");
00224 else
00225 SetState("disabled");
00226 }
00227 }
00228
00229 void MythUIButton::SetText(const QString &msg)
00230 {
00231 if (m_Message == msg)
00232 return;
00233
00234 m_Message = msg;
00235
00236 MythUIGroup *activeState = dynamic_cast<MythUIGroup *>
00237 (m_BackgroundState->GetCurrentState());
00238
00239 if (activeState)
00240 m_Text = dynamic_cast<MythUIText *>(activeState->GetChild("text"));
00241
00242 if (m_Text)
00243 m_Text->SetText(msg);
00244 }
00245
00246 QString MythUIButton::GetText() const
00247 {
00248 return m_Message;
00249 }
00250
00251 QString MythUIButton::GetDefaultText() const
00252 {
00253 return m_Text->GetDefaultText();
00254 }
00255
00259 bool MythUIButton::ParseElement(
00260 const QString &filename, QDomElement &element, bool showWarnings)
00261 {
00262 if (element.tagName() == "value")
00263 {
00264 m_ValueText = qApp->translate("ThemeUI",
00265 parseText(element).toUtf8(), NULL,
00266 QCoreApplication::UnicodeUTF8);
00267 }
00268 else
00269 {
00270 return MythUIType::ParseElement(filename, element, showWarnings);
00271 }
00272
00273 return true;
00274 }
00275
00279 void MythUIButton::CreateCopy(MythUIType *parent)
00280 {
00281 MythUIButton *button = new MythUIButton(parent, objectName());
00282 button->CopyFrom(this);
00283 }
00284
00288 void MythUIButton::CopyFrom(MythUIType *base)
00289 {
00290 MythUIButton *button = dynamic_cast<MythUIButton *>(base);
00291
00292 if (!button)
00293 {
00294 LOG(VB_GENERAL, LOG_ERR, "Dynamic cast of base failed");
00295 return;
00296 }
00297
00298 m_Message = button->m_Message;
00299 m_ValueText = button->m_ValueText;
00300 m_Lockable = button->m_Lockable;
00301
00302 MythUIType::CopyFrom(base);
00303
00304 SetInitialStates();
00305 }
00306
00310 void MythUIButton::Finalize()
00311 {
00312 SetInitialStates();
00313 SetText(m_ValueText);
00314 }