|
| 1 | + |
| 2 | +#include "mythcontext.h" |
| 3 | +#include "mythverbose.h" |
| 4 | +#include "mythuibutton.h" |
| 5 | +#include "mythuibuttonlist.h" |
| 6 | +#include "mythuitext.h" |
| 7 | +#include "mythuitextedit.h" |
| 8 | +#include "rawsettingseditor.h" |
| 9 | +#include "remoteutil.h" |
| 10 | + |
| 11 | +/** \fn RawSettingsEditor::RawSettingsEditor(MythScreenStack *parent, |
| 12 | + const char name) |
| 13 | + * \brief Raw Settings Editor constructor |
| 14 | + * |
| 15 | + * Initializes necessary variables. |
| 16 | + * |
| 17 | + * \param parent Parent screen stack for this window |
| 18 | + * \param name Name of this window |
| 19 | + */ |
| 20 | +RawSettingsEditor::RawSettingsEditor(MythScreenStack *parent, const char *name) |
| 21 | + : MythScreenType(parent, name), |
| 22 | + m_title(tr("Settings Editor")), |
| 23 | + // Settings widgets |
| 24 | + m_settingsList(NULL), m_settingValue(NULL), |
| 25 | + // Action buttons |
| 26 | + m_saveButton(NULL), m_cancelButton(NULL), |
| 27 | + // Labels |
| 28 | + m_textLabel(NULL) |
| 29 | +{ |
| 30 | +} |
| 31 | + |
| 32 | +/** \fn RawSettingsEditor::~RawSettingsEditor() |
| 33 | + * \brief Raw Settings Editor destructor |
| 34 | + */ |
| 35 | +RawSettingsEditor::~RawSettingsEditor() |
| 36 | +{ |
| 37 | +} |
| 38 | + |
| 39 | +/** \fn RawSettingsEditor::Create(void) |
| 40 | + * \brief Creates the UI screen. |
| 41 | + */ |
| 42 | +bool RawSettingsEditor::Create(void) |
| 43 | +{ |
| 44 | + if (!LoadWindowFromXML("settings-ui.xml", "rawsettingseditor", this)) |
| 45 | + return false; |
| 46 | + |
| 47 | + m_settingsList = dynamic_cast<MythUIButtonList *> (GetChild("settings")); |
| 48 | + |
| 49 | + m_saveButton = dynamic_cast<MythUIButton *> (GetChild("save")); |
| 50 | + m_cancelButton = dynamic_cast<MythUIButton *> (GetChild("cancel")); |
| 51 | + |
| 52 | + if (!m_saveButton || !m_cancelButton) |
| 53 | + { |
| 54 | + VERBOSE(VB_IMPORTANT, "Theme is missing critical theme elements."); |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + if (!BuildFocusList()) |
| 59 | + VERBOSE(VB_IMPORTANT, "Failed to build a focuslist. Something is wrong"); |
| 60 | + |
| 61 | + MythUIText *text = dynamic_cast<MythUIText *> (GetChild("heading")); |
| 62 | + text->SetText(m_title); |
| 63 | + |
| 64 | + m_textLabel = dynamic_cast<MythUIText *> (GetChild("label-text")); |
| 65 | + |
| 66 | + MythUIShape *shape = NULL; |
| 67 | + |
| 68 | + for (int i = -8; i <= 8; i++) |
| 69 | + { |
| 70 | + text = dynamic_cast<MythUIText *> |
| 71 | + (GetChild(QString("value%1%2").arg(i >= 0? "+" : "").arg(i))); |
| 72 | + if (text) |
| 73 | + m_prevNextTexts[i] = text; |
| 74 | + |
| 75 | + shape = dynamic_cast<MythUIShape *> |
| 76 | + (GetChild(QString("shape%1%2").arg(i >= 0? "+" : "").arg(i))); |
| 77 | + if (shape) |
| 78 | + m_prevNextShapes[i] = shape; |
| 79 | + } |
| 80 | + |
| 81 | + m_settingValue = dynamic_cast<MythUITextEdit *> (GetChild("settingvalue")); |
| 82 | + |
| 83 | + connect(m_settingsList, SIGNAL(itemSelected(MythUIButtonListItem*)), |
| 84 | + SLOT(selectionChanged(MythUIButtonListItem*))); |
| 85 | + connect(m_settingValue, SIGNAL(LosingFocus()), SLOT(valueChanged())); |
| 86 | + |
| 87 | + connect(m_saveButton, SIGNAL(Clicked()), this, SLOT(Save())); |
| 88 | + connect(m_cancelButton, SIGNAL(Clicked()), this, SLOT(Close())); |
| 89 | + |
| 90 | + LoadInBackground(); |
| 91 | + |
| 92 | + return true; |
| 93 | +} |
| 94 | + |
| 95 | +/** \fn RawSettingsEditor::Load(void) |
| 96 | + * \brief Loads the current values for the specified settings list |
| 97 | + */ |
| 98 | +void RawSettingsEditor::Load(void) |
| 99 | +{ |
| 100 | + QList<QString>settingsList = m_settings.keys(); |
| 101 | + QList<QString>::iterator it = settingsList.begin(); |
| 102 | + |
| 103 | + // FIXME, optimize this using gContext->GetSettings() |
| 104 | + // QMap<QString,QString> kv; |
| 105 | + |
| 106 | + while (it != settingsList.end()) |
| 107 | + { |
| 108 | + QString value = gContext->GetSetting(*it); |
| 109 | + m_settingValues[*it] = value; |
| 110 | + m_origValues[*it] = value; |
| 111 | + |
| 112 | + ++it; |
| 113 | + } |
| 114 | + m_settingValues.detach(); |
| 115 | + m_origValues.detach(); |
| 116 | +} |
| 117 | + |
| 118 | +/** \fn RawSettingsEditor::Init(void) |
| 119 | + * \brief Initialize the settings screen with the loaded data |
| 120 | + */ |
| 121 | +void RawSettingsEditor::Init(void) |
| 122 | +{ |
| 123 | + QList<QString>settingsList = m_settings.keys(); |
| 124 | + QList<QString>::iterator it = settingsList.begin(); |
| 125 | + |
| 126 | + while (it != settingsList.end()) |
| 127 | + { |
| 128 | + MythUIButtonListItem *item = new MythUIButtonListItem(m_settingsList, |
| 129 | + "", qVariantFromValue(*it)); |
| 130 | + |
| 131 | + if (m_settings[*it].isEmpty()) |
| 132 | + item->SetText(*it); |
| 133 | + else |
| 134 | + item->SetText(m_settings[*it]); |
| 135 | + |
| 136 | + ++it; |
| 137 | + } |
| 138 | + |
| 139 | + m_settingsList->SetItemCurrent(0); |
| 140 | + m_textLabel->SetText(m_settingsList->GetItemFirst()->GetText()); |
| 141 | + updatePrevNextTexts(); |
| 142 | +} |
| 143 | + |
| 144 | +/** \fn RawSettingsEditor::Save(void) |
| 145 | + * \brief Save editted values and clear settings cache if necessary |
| 146 | + */ |
| 147 | +void RawSettingsEditor::Save(void) |
| 148 | +{ |
| 149 | + bool changed = false; |
| 150 | + |
| 151 | + QHash <QString, QString>::const_iterator it = m_settingValues.constBegin(); |
| 152 | + while (it != m_settingValues.constEnd()) |
| 153 | + { |
| 154 | + if ((!it.value().isEmpty()) || |
| 155 | + ((m_origValues.contains(it.key())) && |
| 156 | + (!m_origValues.value(it.key()).isEmpty()))) |
| 157 | + { |
| 158 | + gContext->SaveSetting(it.key(), it.value()); |
| 159 | + changed = true; |
| 160 | + } |
| 161 | + |
| 162 | + ++it; |
| 163 | + } |
| 164 | + |
| 165 | + if (changed && (!gContext->IsMasterHost() || gContext->BackendIsRunning())) |
| 166 | + RemoteSendMessage("CLEAR_SETTINGS_CACHE"); |
| 167 | + |
| 168 | + Close(); |
| 169 | +} |
| 170 | + |
| 171 | +/** \fn RawSettingsEditor::selectionChanged(MythUIButtonListItem *item) |
| 172 | + * \brief Slot handler for buttonlist current item changes |
| 173 | + * |
| 174 | + * Updates the text edit area with the current value of the setting that |
| 175 | + * is currently selected in the button list. |
| 176 | + * |
| 177 | + * \param item The currently selected item in the buttonlist |
| 178 | + */ |
| 179 | +void RawSettingsEditor::selectionChanged(MythUIButtonListItem *item) |
| 180 | +{ |
| 181 | + if (!item) |
| 182 | + return; |
| 183 | + |
| 184 | + m_settingValue->SetText(m_settingValues[item->GetData().toString()]); |
| 185 | + m_textLabel->SetText(item->GetText()); |
| 186 | + |
| 187 | + updatePrevNextTexts(); |
| 188 | +} |
| 189 | + |
| 190 | +/** \fn RawSettingsEditor::updatePrevNextTexts(void) |
| 191 | + * \brief Updates previous and next text areas |
| 192 | + * |
| 193 | + * Updates previous and next text areas to show values of other non-current |
| 194 | + * settings in the button list. |
| 195 | + */ |
| 196 | +void RawSettingsEditor::updatePrevNextTexts(void) |
| 197 | +{ |
| 198 | + MythUIButtonListItem *tmpitem; |
| 199 | + int curPos = m_settingsList->GetCurrentPos(); |
| 200 | + int recs = m_settingsList->GetCount(); |
| 201 | + |
| 202 | + if (!recs) |
| 203 | + return; |
| 204 | + |
| 205 | + for (int i = -8; i <= 8; i++) |
| 206 | + { |
| 207 | + if (m_prevNextTexts.contains(i)) |
| 208 | + { |
| 209 | + if (((i < 0) && ((curPos + i) >= 0)) || |
| 210 | + ((i > 0) && (((recs-1) - i) >= curPos))) |
| 211 | + { |
| 212 | + if (m_prevNextShapes.contains(i)) |
| 213 | + m_prevNextShapes[i]->Show(); |
| 214 | + |
| 215 | + tmpitem = m_settingsList->GetItemAt(curPos + i); |
| 216 | + m_prevNextTexts[i]->SetText( |
| 217 | + m_settingValues[tmpitem->GetData().toString()]); |
| 218 | + } |
| 219 | + else |
| 220 | + { |
| 221 | + if (m_prevNextShapes.contains(i)) |
| 222 | + m_prevNextShapes[i]->Hide(); |
| 223 | + |
| 224 | + m_prevNextTexts[i]->SetText(QString()); |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +/** \fn RawSettingsEditor::valueChanged(void) |
| 231 | + * \brief Tracks current value for a setting when the value is editted |
| 232 | + * |
| 233 | + * Updates the local in-memory settings value cache when the value for a |
| 234 | + * setting in the list is updated. |
| 235 | + */ |
| 236 | +void RawSettingsEditor::valueChanged(void) |
| 237 | +{ |
| 238 | + m_settingValues[m_settingsList->GetItemCurrent()->GetData().toString()] = |
| 239 | + m_settingValue->GetText(); |
| 240 | +} |
| 241 | + |
| 242 | +/* vim: set expandtab tabstop=4 shiftwidth=4: */ |
0 commit comments