00001
00002
00003 #include "mythuicheckbox.h"
00004
00005
00006 #include "mythlogging.h"
00007
00008
00009 #include "mythmainwindow.h"
00010 #include "mythgesture.h"
00011
00012 MythUICheckBox::MythUICheckBox(MythUIType *parent, const QString &name)
00013 : MythUIType(parent, name)
00014 {
00015 m_currentCheckState = MythUIStateType::Off;
00016 m_state = "active";
00017
00018 m_BackgroundState = m_CheckState = NULL;
00019
00020 connect(this, SIGNAL(TakingFocus()), this, SLOT(Select()));
00021 connect(this, SIGNAL(LosingFocus()), this, SLOT(Deselect()));
00022 connect(this, SIGNAL(Enabling()), this, SLOT(Enable()));
00023 connect(this, SIGNAL(Disabling()), this, SLOT(Disable()));
00024
00025 SetCanTakeFocus(true);
00026 }
00027
00028 MythUICheckBox::~MythUICheckBox()
00029 {
00030 }
00031
00032 void MythUICheckBox::SetInitialStates()
00033 {
00034 m_BackgroundState = dynamic_cast<MythUIStateType *>(GetChild("background"));
00035 m_CheckState = dynamic_cast<MythUIStateType *>(GetChild("checkstate"));
00036
00037 if (!m_CheckState || !m_BackgroundState)
00038 LOG(VB_GENERAL, LOG_ERR,
00039 QString("Checkbox %1 is missing required elements")
00040 .arg(objectName()));
00041
00042 if (m_CheckState)
00043 m_CheckState->DisplayState(m_currentCheckState);
00044
00045 if (m_BackgroundState)
00046 m_BackgroundState->DisplayState(m_state);
00047 }
00048
00049
00050 void MythUICheckBox::toggleCheckState()
00051 {
00052 bool onOff = false;
00053
00054 if (m_currentCheckState != MythUIStateType::Full)
00055 {
00056 m_currentCheckState = MythUIStateType::Full;
00057 onOff = true;
00058 }
00059 else
00060 {
00061 m_currentCheckState = MythUIStateType::Off;
00062 onOff = false;
00063 }
00064
00065 if (m_CheckState)
00066 m_CheckState->DisplayState(m_currentCheckState);
00067
00068 emit DependChanged(!onOff);
00069 emit toggled(onOff);
00070 emit valueChanged();
00071 }
00072
00073 void MythUICheckBox::SetCheckState(MythUIStateType::StateType state)
00074 {
00075 m_currentCheckState = state;
00076 if (m_CheckState)
00077 m_CheckState->DisplayState(state);
00078
00079 if (state == MythUIStateType::Off)
00080 emit DependChanged(true);
00081 else
00082 emit DependChanged(false);
00083 emit valueChanged();
00084 }
00085
00086 void MythUICheckBox::SetCheckState(bool onOff)
00087 {
00088 if (onOff)
00089 {
00090 m_currentCheckState = MythUIStateType::Full;
00091 }
00092 else
00093 {
00094 m_currentCheckState = MythUIStateType::Off;
00095 }
00096
00097 if (m_CheckState)
00098 m_CheckState->DisplayState(m_currentCheckState);
00099
00100 emit toggled(onOff);
00101 emit DependChanged(!onOff);
00102 emit valueChanged();
00103 }
00104
00105 MythUIStateType::StateType MythUICheckBox::GetCheckState() const
00106 {
00107 return m_currentCheckState;
00108 }
00109
00110 bool MythUICheckBox::GetBooleanCheckState() const
00111 {
00112 return m_currentCheckState == MythUIStateType::Full;
00113 }
00114
00115 void MythUICheckBox::Select()
00116 {
00117 if (!IsEnabled())
00118 return;
00119
00120 m_state = "selected";
00121 if (m_BackgroundState)
00122 m_BackgroundState->DisplayState(m_state);
00123 }
00124
00125 void MythUICheckBox::Deselect()
00126 {
00127 if (IsEnabled())
00128 m_state = "active";
00129 else
00130 m_state = "disabled";
00131
00132 if (m_BackgroundState)
00133 m_BackgroundState->DisplayState(m_state);
00134 }
00135
00136 void MythUICheckBox::Enable()
00137 {
00138 m_state = "active";
00139 if (m_BackgroundState)
00140 m_BackgroundState->DisplayState(m_state);
00141 }
00142
00143 void MythUICheckBox::Disable()
00144 {
00145 m_state = "disabled";
00146 if (m_BackgroundState)
00147 m_BackgroundState->DisplayState(m_state);
00148 }
00149
00156 bool MythUICheckBox::gestureEvent(MythGestureEvent *event)
00157 {
00158 if (event->gesture() == MythGestureEvent::Click)
00159 {
00160 if (IsEnabled())
00161 {
00162 toggleCheckState();
00163 return true;
00164 }
00165 }
00166
00167 return false;
00168 }
00169
00174 bool MythUICheckBox::keyPressEvent(QKeyEvent *event)
00175 {
00176 QStringList actions;
00177 bool handled = false;
00178 handled = GetMythMainWindow()->TranslateKeyPress("Global", event, actions);
00179
00180 for (int i = 0; i < actions.size() && !handled; i++)
00181 {
00182 QString action = actions[i];
00183 handled = true;
00184
00185 if (action == "SELECT")
00186 toggleCheckState();
00187 else
00188 handled = false;
00189 }
00190
00191 return handled;
00192 }
00193
00197 void MythUICheckBox::CreateCopy(MythUIType *parent)
00198 {
00199 MythUICheckBox *checkbox = new MythUICheckBox(parent, objectName());
00200 checkbox->CopyFrom(this);
00201 }
00202
00206 void MythUICheckBox::CopyFrom(MythUIType *base)
00207 {
00208 MythUICheckBox *button = dynamic_cast<MythUICheckBox *>(base);
00209
00210 if (!button)
00211 {
00212 LOG(VB_GENERAL, LOG_ERR, "Dynamic cast of base failed");
00213 return;
00214 }
00215
00216 MythUIType::CopyFrom(base);
00217
00218 SetInitialStates();
00219 }
00220
00224 void MythUICheckBox::Finalize()
00225 {
00226 SetInitialStates();
00227 }