Ticket #10092: settings-libmyth-2012-03-09.diff

File settings-libmyth-2012-03-09.diff, 40.5 KB (added by Xavier Hervy <xavier.hervy@…>, 14 years ago)
  • mythtv/libs/libmyth/libmyth.pro

    diff --git a/mythtv/libs/libmyth/libmyth.pro b/mythtv/libs/libmyth/libmyth.pro
    index b457360..88902aa 100644
    a b HEADERS += mythevent.h mythexp.h mythmediamonitor.h  
    2626HEADERS += mythplugin.h mythpluginapi.h
    2727HEADERS += mythwidgets.h mythwizard.h schemawizard.h
    2828HEADERS += output.h
    29 HEADERS += settings.h
     29HEADERS += settings.h standardsettings.h
    3030HEADERS += visual.h xmlparse.h
    3131HEADERS += storagegroupeditor.h
    3232HEADERS += mythterminal.h
    SOURCES += mythmediamonitor.cpp  
    5555SOURCES += mythplugin.cpp
    5656SOURCES += mythwidgets.cpp mythwizard.cpp schemawizard.cpp
    5757SOURCES += output.cpp
    58 SOURCES += settings.cpp
     58SOURCES += settings.cpp  standardsettings.cpp
    5959SOURCES += storagegroupeditor.cpp
    6060SOURCES += mythterminal.cpp
    6161SOURCES += remoteutil.cpp
    POST_TARGETDEPS += ../../external/FFmpeg/libavutil/$$avLibName(avutil)  
    100100inc.path = $${PREFIX}/include/mythtv/
    101101inc.files  = dialogbox.h mythcontext.h
    102102inc.files += mythwidgets.h remotefile.h oldsettings.h volumecontrol.h
    103 inc.files += settings.h uitypes.h  mythplugin.h mythdialogs.h
     103inc.files += standardsettings.h uitypes.h  mythplugin.h mythdialogs.h
    104104inc.files += audio/audiooutput.h audio/audiosettings.h
    105105inc.files += audio/audiooutputsettings.h
    106106inc.files += audio/volumebase.h audio/eldutils.h
  • new file mythtv/libs/libmyth/standardsettings.cpp

    diff --git a/mythtv/libs/libmyth/standardsettings.cpp b/mythtv/libs/libmyth/standardsettings.cpp
    new file mode 100755
    index 0000000..57502d3
    - +  
     1#include "standardsettings.h"
     2#include <QCoreApplication>
     3
     4#include <mythcontext.h>
     5#include <mythmainwindow.h>
     6#include <mythdialogbox.h>
     7#include <mythuispinbox.h>
     8#include <mythuitext.h>
     9#include <mythuibutton.h>
     10#include <mythuifilebrowser.h>
     11#include "mythlogging.h"
     12
     13void MythUIButtonListItemSetting::ShouldUpdate(StandardSetting *setting)
     14{
     15    if (setting)        //???
     16        LOG(VB_GENERAL, LOG_ERR, QString("MythUIButtonListItemSetting::ShouldUpdate(%1)=>%2")
     17            .arg(setting->getLabel())
     18            .arg(setting->getValue()));
     19    setting->updateButton(this);
     20}
     21
     22StandardSetting::StandardSetting(Storage *_storage) :
     23    m_enabled(true), m_label(""), m_helptext(""), m_visible(true),
     24    m_haveChanged(false),
     25    m_storage(_storage),
     26
     27
     28    m_parent(NULL)
     29
     30{
     31}
     32
     33StandardSetting::~StandardSetting()
     34{   
     35    QList<StandardSetting *>::const_iterator i;
     36    for (i = m_children.constBegin(); i != m_children.constEnd(); ++i)
     37        delete *i;
     38    m_children.clear();
     39
     40    QMap<QString, QList<StandardSetting *> >::const_iterator iMap;
     41    for (iMap = m_targets.constBegin(); iMap != m_targets.constEnd(); ++iMap)
     42    {
     43        for (i = (*iMap).constBegin(); i != (*iMap).constEnd(); ++i)
     44            delete *i;
     45    }
     46    m_targets.clear();
     47}
     48
     49MythUIButtonListItem * StandardSetting::createButton(MythUIButtonList * list)
     50{
     51    MythUIButtonListItemSetting *item = new MythUIButtonListItemSetting(list, m_label);
     52    item->SetData(qVariantFromValue(this));
     53    connect(this, SIGNAL(ShouldRedraw(StandardSetting *)), item, SLOT(ShouldUpdate(StandardSetting *)));
     54    updateButton(item);
     55    return item;
     56}
     57
     58void StandardSetting::setEnabled(bool b)
     59{
     60    m_enabled = b;
     61    emit ShouldRedraw(this);
     62}
     63
     64void StandardSetting::setParent(StandardSetting *parent)
     65{
     66    m_parent = parent;
     67}
     68
     69void StandardSetting::addChild(StandardSetting *child)
     70{
     71    if (!child)return;
     72    m_children.append(child);
     73    child->setParent(this);
     74}
     75
     76bool StandardSetting::keyPressEvent(QKeyEvent *)
     77{
     78    return false;
     79}
     80
     81/**
     82 * This method is called whenever the UI need to reflect a change
     83 * Reimplement this If you widget need a custom look
     84 * \param item is the associated MythUIButtonListItem to be updated
     85 */
     86void StandardSetting::updateButton(MythUIButtonListItem *item)
     87{
     88    item->DisplayState("standard", "widgettype");
     89    item->setEnabled(isEnabled());
     90    item->SetText(m_label);
     91    item->SetText(m_settingValue,"value");
     92    item->SetText(getHelpText(),"description");
     93    item->setDrawArrow (haveSubSettings());
     94}
     95
     96void StandardSetting::addTargetedChild(const QString &value, StandardSetting * setting)
     97{
     98    m_targets[value].append(setting);
     99    setting->setParent(this);
     100}
     101
     102
     103QList<StandardSetting *> *StandardSetting::getSubSettings()
     104{
     105    if (m_targets.contains(m_settingValue) && m_targets[m_settingValue].size()>0)
     106            return &m_targets[m_settingValue];
     107    return &m_children;
     108}
     109
     110bool StandardSetting::haveSubSettings()
     111{
     112    QList<StandardSetting *> * subSettings=getSubSettings();
     113    return (subSettings!=NULL && subSettings->size()>0);
     114}
     115
     116void StandardSetting::clearSettings()
     117{
     118    m_children.clear();
     119}
     120
     121void StandardSetting::setValue(const QString &newValue)
     122{
     123    m_settingValue = newValue;
     124    m_haveChanged=true;
     125    emit valueChanged(newValue);
     126    emit valueChanged(this);
     127    emit ShouldRedraw(this);
     128}
     129
     130
     131/**
     132 * Return true if the setting have changed or any of its children
     133 */
     134bool StandardSetting::haveChanged()
     135{
     136    if (m_haveChanged)
     137    {
     138        LOG(VB_GENERAL, LOG_ERR, QString("Is changed %1").arg(getLabel()));
     139        return true;
     140    }
     141    //else
     142        //LOG(VB_GENERAL, LOG_ERR, QString("Is not changed %1").arg(getLabel()));
     143
     144    //we check only the relevant children
     145    QList<StandardSetting *> *children = getSubSettings();
     146    if (children==NULL) return false;
     147    QList<StandardSetting *>::const_iterator i;
     148    bool haveChanged = false;
     149    for (i = children->constBegin(); !haveChanged && i != children->constEnd(); ++i)
     150        haveChanged=(*i)->haveChanged();
     151
     152    return haveChanged;
     153}
     154
     155void StandardSetting::Load(void)
     156{
     157    m_haveChanged=false;
     158
     159    QList<StandardSetting *>::const_iterator i;
     160    for (i = m_children.constBegin(); i != m_children.constEnd(); ++i)
     161        (*i)->Load();
     162
     163    QMap<QString, QList<StandardSetting *> >::const_iterator iMap;
     164    for (iMap = m_targets.constBegin(); iMap != m_targets.constEnd(); ++iMap)
     165    {
     166        for (i = (*iMap).constBegin(); i != (*iMap).constEnd(); ++i)
     167            (*i)->Load();
     168    }
     169}
     170
     171void StandardSetting::Save(void)
     172{
     173    m_haveChanged=false;
     174
     175    //we save only the relevant children
     176    QList<StandardSetting *> *children = getSubSettings();
     177    if (children==NULL) return;
     178    QList<StandardSetting *>::const_iterator i;
     179    for (i = children->constBegin(); i != children->constEnd(); ++i)
     180        (*i)->Save();
     181}
     182
     183/*******************************************************************************
     184                            Group Setting
     185********************************************************************************/
     186GroupSetting::GroupSetting():
     187    StandardSetting(this), TransientStorage()
     188{
     189}
     190
     191void GroupSetting::edit(MythScreenType * screen)
     192{
     193    if (!isEnabled())return;
     194    DialogCompletionEvent *dce =
     195            new DialogCompletionEvent("leveldown", 0, "", "");
     196    QCoreApplication::postEvent(screen, dce);
     197}
     198
     199void GroupSetting::updateButton(MythUIButtonListItem *item)
     200{
     201    item->DisplayState("group", "widgettype");
     202    item->setEnabled(isEnabled());
     203    item->SetText(m_label);
     204    item->SetText(m_settingValue,"value");
     205    item->SetText(getHelpText(),"description");
     206    item->setDrawArrow (haveSubSettings());
     207}
     208
     209
     210ButtonStandardSetting::ButtonStandardSetting(const QString &):
     211    StandardSetting(this), TransientStorage()
     212{
     213}
     214
     215void ButtonStandardSetting::edit(MythScreenType * screen)
     216{
     217    emit clicked();
     218}
     219
     220
     221/*******************************************************************************
     222                            Text Setting
     223********************************************************************************/
     224
     225MythUITextEditSetting::MythUITextEditSetting(Storage *_storage):
     226    StandardSetting(_storage)
     227{
     228}
     229
     230void MythUITextEditSetting::edit(MythScreenType * screen)
     231{
     232    if (!isEnabled())return;
     233    MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
     234
     235    MythTextInputDialog *settingdialog =
     236                                    new MythTextInputDialog(popupStack,
     237                                    getLabel(),FilterNone, false,m_settingValue);
     238
     239    if (settingdialog->Create())
     240    {
     241        settingdialog->SetReturnEvent(screen, "editsetting");
     242        popupStack->AddScreen(settingdialog);
     243    }
     244};
     245
     246void MythUITextEditSetting::resultEdit(DialogCompletionEvent *dce)
     247{
     248    if (m_settingValue!=dce->GetResultText())
     249    {
     250        setValue(dce->GetResultText());
     251    }
     252}
     253
     254
     255void MythUITextEditSetting::updateButton(MythUIButtonListItem *item)
     256{
     257    StandardSetting::updateButton(item);
     258    item->DisplayState("textedit", "widgettype");
     259}
     260
     261
     262/*******************************************************************************
     263                            Directory Setting
     264********************************************************************************/
     265
     266MythUIFileBrowserSetting::MythUIFileBrowserSetting(Storage *_storage):
     267    StandardSetting(_storage)
     268{
     269    m_typeFilter = (QDir::AllDirs | QDir::Drives | QDir::Files |
     270                    QDir::Readable | QDir::Writable | QDir::Executable);
     271    m_nameFilter.clear();
     272    m_nameFilter << "*";
     273}
     274
     275void MythUIFileBrowserSetting::edit(MythScreenType * screen)
     276{
     277    if (!isEnabled())return;
     278    MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
     279
     280    MythUIFileBrowser *settingdialog =
     281                                    new MythUIFileBrowser(popupStack,
     282                                    m_settingValue);
     283    settingdialog->SetTypeFilter(m_typeFilter);
     284    settingdialog->SetNameFilter(m_nameFilter);
     285
     286    if (settingdialog->Create())
     287    {
     288        settingdialog->SetReturnEvent(screen, "editsetting");
     289        popupStack->AddScreen(settingdialog);
     290    }
     291};
     292
     293void MythUIFileBrowserSetting::resultEdit(DialogCompletionEvent *dce)
     294{
     295    if (m_settingValue!=dce->GetResultText())
     296    {
     297        setValue(dce->GetResultText());
     298    }
     299}
     300
     301void MythUIFileBrowserSetting::updateButton(MythUIButtonListItem *item)
     302{
     303    StandardSetting::updateButton(item);
     304    item->DisplayState("filebrowser", "widgettype");
     305}
     306
     307
     308/*******************************************************************************
     309                            ComboBoxSetting
     310********************************************************************************/
     311/**
     312 * Create a Setting Widget to select the value from a list
     313 * \param rw if set to true, the user can input it's own value
     314 */
     315MythUIComboBoxSetting::MythUIComboBoxSetting(Storage *_storage, bool rw):
     316    StandardSetting(_storage),
     317    m_rewrite(rw),
     318    m_isSet(false)
     319{
     320}
     321
     322MythUIComboBoxSetting::~MythUIComboBoxSetting()
     323{
     324    m_labels.clear();
     325    m_values.clear();
     326}
     327
     328void MythUIComboBoxSetting::setValue(int value)
     329{
     330    if (value>=0 && value < m_values.size())
     331            StandardSetting::setValue(m_values.at(value));
     332}
     333
     334void MythUIComboBoxSetting::addSelection(const QString &label, QString value,
     335                                 bool select)
     336{
     337    value = (value.isEmpty()) ? label : value;
     338    m_labels.push_back(label);
     339    m_values.push_back(value);
     340
     341    if (select || !m_isSet)
     342    {
     343        StandardSetting::setValue(value);
     344        if (!m_isSet) m_isSet = true;
     345    }
     346}
     347
     348void MythUIComboBoxSetting::clearSelections()
     349{
     350    m_labels.clear();
     351    m_values.clear();
     352}
     353
     354void MythUIComboBoxSetting::updateButton(MythUIButtonListItem *item)
     355{
     356    LOG(VB_GENERAL, LOG_ERR, QString("MythUIComboBoxSetting::updateButton(%1)").arg(getLabel()));
     357    item->DisplayState("combobox", "widgettype");
     358    item->setEnabled(isEnabled());
     359    item->SetText(m_label);
     360    int indexValue = m_values.indexOf(m_settingValue);
     361    if (indexValue >=0)
     362        item->SetText(m_labels.value(indexValue),"value");
     363    else
     364        item->SetText(m_settingValue,"value");
     365    item->SetText(getHelpText(),"description");
     366    item->setDrawArrow (haveSubSettings());
     367}
     368
     369void MythUIComboBoxSetting::edit(MythScreenType * screen)
     370{
     371    if (!isEnabled())return;
     372    MythScreenStack *popupStack =
     373                            GetMythMainWindow()->GetStack("popup stack");
     374
     375    MythDialogBox * m_menuPopup =
     376            new MythDialogBox(getLabel(), popupStack, "optionmenu");
     377
     378    if (m_menuPopup->Create())
     379        popupStack->AddScreen(m_menuPopup);
     380
     381   // connect(m_menuPopup,SIGNAL(haveResult(QString)), this, SLOT(setValue(QString)));
     382
     383    m_menuPopup->SetReturnEvent(screen, "editsetting");
     384
     385    if (m_rewrite)
     386        //FIXME This look like a hack to me, what if the list a a NEWENTRY value ?
     387        m_menuPopup->AddButton(QObject::tr("New entry"),QString("NEWENTRY"),false,m_settingValue=="");
     388    for (int i = 0; i < m_labels.size() && m_values.size(); ++i)
     389    {
     390        QString value = m_values.at(i);
     391        m_menuPopup->AddButton(m_labels.at(i),value,false,value==m_settingValue);
     392    }
     393}
     394
     395void MythUIComboBoxSetting::setValue(const QString& newValue)
     396{
     397    LOG(VB_GENERAL, LOG_ERR, QString("MythUIComboBoxSetting::setValue(%1)").arg(newValue));
     398    StandardSetting::setValue(newValue);
     399}
     400
     401void MythUIComboBoxSetting::resultEdit(DialogCompletionEvent *dce)
     402{
     403    if (dce->GetResult()!=-1)
     404    {
     405        if ( m_rewrite && dce->GetData().toString()=="NEWENTRY")
     406        {
     407            MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
     408
     409            MythTextInputDialog *settingdialog =
     410                                            new MythTextInputDialog(popupStack,
     411                                            getLabel(),FilterNone, false,m_settingValue);
     412
     413            if (settingdialog->Create())
     414            {
     415                connect(settingdialog,SIGNAL(haveResult(QString)),
     416                        this, SLOT(setValue(const QString&)));
     417//                settingdialog->SetReturnEvent(screen, "editsetting");
     418                popupStack->AddScreen(settingdialog);
     419            }
     420        }
     421        else if ( m_settingValue!=dce->GetData().toString())
     422        {
     423            StandardSetting::setValue(dce->GetData().toString());
     424        }
     425    }
     426}
     427void MythUIComboBoxSetting::Load()
     428{
     429    if (m_rewrite)
     430        LOG(VB_GENERAL, LOG_ERR, QString("MythUIComboBoxSetting::MythUIComboBoxSetting: need to implement rewrite for %1").arg(getLabel()));
     431    StandardSetting::Load();
     432}
     433
     434/*******************************************************************************
     435                            SpinBox Setting
     436********************************************************************************/
     437MythUISpinBoxSetting::MythUISpinBoxSetting(Storage *_storage, int min, int max, int step, bool allow_single_step):
     438    MythUIComboBoxSetting(_storage, false),
     439    m_min(min),
     440    m_max(max),
     441    m_step(step),
     442    m_allow_single_step(allow_single_step)
     443{
     444    //we default to 0 unless 0 is out of range
     445    if (m_min >0 || m_max < 0)m_settingValue=m_min;
     446}
     447
     448void MythUISpinBoxSetting::updateButton(MythUIButtonListItem *item)
     449{
     450    LOG(VB_GENERAL, LOG_ERR, QString("MythUISpinBoxSetting::updateButton(%1)").arg(getLabel()));
     451    item->DisplayState("spinbox", "widgettype");
     452    item->setEnabled(isEnabled());
     453    item->SetText(m_label);
     454    int indexValue = m_values.indexOf(m_settingValue);
     455    if (indexValue >=0)
     456        item->SetText(m_labels.value(indexValue),"value");
     457    else
     458        item->SetText(m_settingValue,"value");
     459    item->SetText(getHelpText(),"description");
     460    item->setDrawArrow (haveSubSettings());
     461}
     462
     463int MythUISpinBoxSetting::intValue()
     464{
     465    return m_settingValue.toInt();
     466}
     467
     468void MythUISpinBoxSetting::edit(MythScreenType * screen)
     469{
     470    if (!isEnabled())return;
     471
     472    MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
     473
     474    MythSpinBoxDialog *settingdialog =
     475                                    new MythSpinBoxDialog(popupStack,
     476                                    getLabel());
     477
     478    if (settingdialog->Create())
     479    {
     480        settingdialog->SetRange(m_min, m_max, m_step);
     481        //Add custom values
     482        for (int i = 0; i < m_labels.size() && m_values.size(); ++i)
     483        {
     484            QString value = m_values.at(i);
     485            settingdialog->AddSelection(m_labels.at(i),value.toInt());
     486        }
     487        LOG(VB_GENERAL, LOG_ERR, QString("MythUISpinBoxSetting::edit(%1) SetValue(%2)").arg(getLabel()).arg(m_settingValue));
     488        settingdialog->SetValue(m_settingValue);
     489        settingdialog->SetReturnEvent(screen, "editsetting");
     490        popupStack->AddScreen(settingdialog);
     491    }
     492};
     493
     494void MythUISpinBoxSetting::resultEdit(DialogCompletionEvent *dce)
     495{
     496    LOG(VB_GENERAL, LOG_ERR, QString("MythUISpinBoxSetting::resultEdit(DialogCompletionEvent *dce) START"));
     497    if (m_settingValue!=dce->GetResultText())
     498    {
     499        MythUIComboBoxSetting::setValue(dce->GetResultText());
     500    }
     501    LOG(VB_GENERAL, LOG_ERR, QString("MythUISpinBoxSetting::resultEdit(DialogCompletionEvent *dce) END"));
     502}
     503
     504/*******************************************************************************
     505                           MythUICheckBoxSetting
     506********************************************************************************/
     507
     508MythUICheckBoxSetting::MythUICheckBoxSetting(Storage *_storage):
     509    StandardSetting(_storage)
     510{
     511}
     512
     513bool MythUICheckBoxSetting::boolValue()
     514{
     515    return m_settingValue=="1";
     516}
     517
     518void MythUICheckBoxSetting::setValue(const QString &value)
     519{
     520    StandardSetting::setValue(value);
     521    emit valueChanged(value=="1");
     522}
     523
     524void MythUICheckBoxSetting::setValue(bool value)
     525{
     526    StandardSetting::setValue(value?"1":"0");
     527    emit valueChanged(value);
     528}
     529
     530void MythUICheckBoxSetting::updateButton(MythUIButtonListItem *item)
     531{
     532    StandardSetting::updateButton(item);
     533    item->DisplayState("checkbox", "widgettype");
     534    item->setCheckable (true);
     535    item->SetText("","value");
     536    if (m_settingValue=="1")
     537        item->setChecked(MythUIButtonListItem::FullChecked);
     538    else
     539        item->setChecked(MythUIButtonListItem::NotChecked);
     540}
     541
     542void MythUICheckBoxSetting::edit(MythScreenType * screen)
     543{
     544    if (!isEnabled())return;
     545    DialogCompletionEvent *dce =
     546            new DialogCompletionEvent("editsetting", 0, "", "");
     547    QCoreApplication::postEvent(screen, dce);
     548}
     549
     550void MythUICheckBoxSetting::resultEdit(DialogCompletionEvent *dce)
     551{
     552    setValue(!boolValue());
     553}
     554
     555/*******************************************************************************
     556                           Standard setting dialog
     557********************************************************************************/
     558
     559StandardSettingDialog::StandardSettingDialog(MythScreenStack *parent, const char *name) :
     560    MythScreenType(parent, name),
     561    m_buttonList(0),
     562    m_title(0),
     563    m_groupHelp(0),
     564    m_selectedSettingHelp(0),
     565    m_menuPopup(0),
     566    m_settingsTree(0),
     567    m_currentGroupSetting(0)
     568{
     569}
     570
     571StandardSettingDialog::~StandardSettingDialog()
     572{
     573    //LOG(VB_GENERAL, LOG_ERR, "StandardSettingDialog::~StandardSettingDialog()");
     574    if (m_settingsTree!=0)
     575        m_settingsTree->deleteLater();
     576}
     577
     578bool StandardSettingDialog::Create(void)
     579{
     580    if (!LoadWindowFromXML("standardsetting-ui.xml", "settingssetup", this))
     581        return false;
     582
     583    bool error=false;
     584    UIUtilE::Assign(this, m_title, "title",&error);
     585    UIUtilW::Assign(this, m_groupHelp, "grouphelp",&error);
     586    UIUtilE::Assign(this, m_buttonList,"settingslist",&error);
     587
     588    UIUtilW::Assign(this, m_selectedSettingHelp, "selectedsettinghelp");
     589
     590    connect(m_buttonList,SIGNAL(itemSelected(MythUIButtonListItem*)),
     591        this,SLOT(settingSelected(MythUIButtonListItem*)));
     592    connect(m_buttonList,SIGNAL(itemClicked(MythUIButtonListItem*)),
     593        this,SLOT(settingClicked(MythUIButtonListItem*)));
     594
     595    if (error)
     596    {
     597        LOG(VB_GENERAL, LOG_ERR, "error.");
     598        return false;
     599    }
     600    BuildFocusList();
     601
     602    return true;
     603}
     604
     605void StandardSettingDialog::settingSelected(MythUIButtonListItem *item)
     606{
     607    if (!item)return;
     608
     609    StandardSetting * setting = qVariantValue<StandardSetting*>(item->GetData());
     610    if (setting && m_selectedSettingHelp)
     611    {
     612        m_selectedSettingHelp->SetText(setting->getHelpText());
     613        if (m_selectedSettingHelp->GetText().isEmpty()) m_selectedSettingHelp->SetText("This setting need a description");
     614    }
     615}
     616
     617void StandardSettingDialog::settingClicked(MythUIButtonListItem *item)
     618{
     619    StandardSetting* setting = item->GetData().value<StandardSetting*>();
     620    if (setting)
     621    {
     622        setting->edit(this);
     623    }
     624}
     625
     626void StandardSettingDialog::customEvent(QEvent *event)
     627{
     628    if (event->type() == DialogCompletionEvent::kEventType)
     629    {
     630        DialogCompletionEvent *dce = (DialogCompletionEvent*)(event);
     631        QString resultid  = dce->GetId();
     632
     633        if (resultid == "leveldown")
     634        {
     635            //a GroupSetting have been clicked
     636            LevelDown();
     637        }
     638        else if (resultid == "editsetting")
     639        {
     640            MythUIButtonListItem * item = m_buttonList->GetItemCurrent();
     641            if (item)
     642            {
     643                StandardSetting * ss = item->GetData().value<StandardSetting*>();
     644                if (ss)
     645                {
     646                    ss->resultEdit(dce);
     647                }
     648            }
     649        }
     650        else if (resultid == "exit")
     651        {
     652            int buttonnum = dce->GetResult();
     653            if (buttonnum == 0)
     654            {
     655                Save();
     656                MythScreenType::Close();
     657                if (m_settingsTree)
     658                    m_settingsTree->applyChange();
     659            }
     660            else if (buttonnum == 1)
     661                MythScreenType::Close();
     662        }
     663    }
     664}
     665
     666void StandardSettingDialog::loadSettings(GroupSetting * groupSettings)
     667{
     668    //TODO I  probably should do this in the background ?
     669    m_settingsTree=groupSettings;
     670    if (m_settingsTree)
     671        m_settingsTree->Load();
     672
     673    setCurrentGroupSetting(m_settingsTree);
     674}
     675
     676void StandardSettingDialog::setCurrentGroupSetting(StandardSetting * groupSettings,
     677        StandardSetting * selectedSetting )
     678{
     679    if (!groupSettings) return;
     680
     681    if (m_currentGroupSetting)
     682    {
     683        disconnect(m_currentGroupSetting, SIGNAL(settingsChanged(StandardSetting *)), 0, 0);
     684        m_currentGroupSetting->Close();
     685    }
     686
     687    m_currentGroupSetting=groupSettings;
     688
     689    m_currentGroupSetting->Open();
     690
     691
     692    m_title->SetText(m_currentGroupSetting->getLabel());
     693    if (m_title->GetText().isEmpty()) m_title->SetText("This group need a title");
     694    if (m_groupHelp)
     695    {
     696        m_groupHelp->SetText(m_currentGroupSetting->getHelpText());
     697        if (m_groupHelp->GetText().isEmpty()) m_groupHelp->SetText("This group need a description");
     698    }
     699    updateSettings(selectedSetting);
     700    connect(m_currentGroupSetting, SIGNAL(settingsChanged(StandardSetting *)), this, SLOT(updateSettings(StandardSetting *)));
     701
     702}
     703
     704void StandardSettingDialog::updateSettings(StandardSetting * selectedSetting)
     705{
     706    m_buttonList->Reset();
     707    if (!m_currentGroupSetting->haveSubSettings())return;
     708    QList<StandardSetting *> * settings = m_currentGroupSetting->getSubSettings();
     709    if (settings==NULL ) return;
     710    QList<StandardSetting *>::const_iterator i;
     711    MythUIButtonListItem *selectedItem=0;
     712    for (i = settings->constBegin(); i != settings->constEnd(); ++i)
     713    {
     714        if (selectedSetting == (*i))
     715            selectedItem =(*i)->createButton(m_buttonList);
     716        else
     717            (*i)->createButton(m_buttonList);
     718    }
     719    if (selectedItem)
     720        m_buttonList->SetItemCurrent(selectedItem);
     721    settingSelected(m_buttonList->GetItemCurrent());
     722}
     723
     724void StandardSettingDialog::Save()
     725{
     726    if (m_settingsTree)
     727        m_settingsTree->Save();
     728}
     729
     730void StandardSettingDialog::LevelUp()
     731{
     732    if (!m_currentGroupSetting) return;
     733    if (m_currentGroupSetting->getParent())
     734    {
     735        setCurrentGroupSetting(m_currentGroupSetting->getParent(),
     736                    m_currentGroupSetting);
     737    }
     738}
     739
     740void StandardSettingDialog::LevelDown()
     741{
     742    MythUIButtonListItem * item = m_buttonList->GetItemCurrent();
     743    if (item)
     744    {
     745        StandardSetting * ss = item->GetData().value<StandardSetting*>();
     746        if (ss && ss->haveSubSettings() && ss->isEnabled())
     747            setCurrentGroupSetting(ss);
     748    }
     749}
     750
     751void StandardSettingDialog::Close(void)
     752{
     753    if (m_settingsTree->haveChanged())
     754    {
     755        QString label = tr("Exit ?");
     756
     757        MythScreenStack *popupStack =
     758                                GetMythMainWindow()->GetStack("popup stack");
     759
     760        MythDialogBox * m_menuPopup =
     761                new MythDialogBox(label, popupStack, "exitmenu");
     762
     763        if (m_menuPopup->Create())
     764            popupStack->AddScreen(m_menuPopup);
     765
     766        m_menuPopup->SetReturnEvent(this, "exit");
     767
     768        m_menuPopup->AddButton(tr("Save then Exit"));
     769        m_menuPopup->AddButton(tr("Exit without saving changes"));
     770        m_menuPopup->AddButton(tr("Cancel"));
     771    }
     772    else
     773        MythScreenType::Close();
     774}
     775
     776
     777bool StandardSettingDialog::keyPressEvent(QKeyEvent *e)
     778{
     779    QStringList actions;
     780    bool handled = m_buttonList->keyPressEvent(e);
     781    if (handled) return true;
     782    handled = GetMythMainWindow()->TranslateKeyPress("Global", e, actions);
     783
     784    //send the key to the selected Item first
     785    MythUIButtonListItem * item = m_buttonList->GetItemCurrent();
     786    if (item)
     787    {
     788        StandardSetting * ss = item->GetData().value<StandardSetting*>();
     789        if (ss)
     790        {
     791            handled = ss->keyPressEvent(e);
     792        }
     793    }
     794    if (handled) return true;
     795
     796    for (int i = 0; i < actions.size() && !handled; i++)
     797    {
     798        QString action = actions[i];
     799        handled = true;
     800
     801        if (action == "LEFT")
     802        {
     803            if (m_currentGroupSetting
     804                && m_currentGroupSetting==m_settingsTree)
     805                Close();
     806            else
     807                LevelUp();
     808        }
     809        else if (action == "RIGHT")
     810            LevelDown();
     811        else
     812            handled = MythScreenType::keyPressEvent(e);
     813    }
     814
     815    return handled;
     816}
     817
     818
     819
     820
     821
  • new file mythtv/libs/libmyth/standardsettings.h

    diff --git a/mythtv/libs/libmyth/standardsettings.h b/mythtv/libs/libmyth/standardsettings.h
    new file mode 100755
    index 0000000..80a9764
    - +  
     1#ifndef STANDARDSETTINGS_H_
     2#define STANDARDSETTINGS_H_
     3
     4#include "mythuibuttonlist.h"
     5#include <mythdialogbox.h>
     6#include <mythuibuttontree.h>
     7#include <mythgenerictree.h>
     8#include <QMap>
     9#include "settings.h"
     10
     11#include "mythlogging.h"
     12
     13class StandardSetting;
     14
     15class MythUIButtonListItemSetting : public QObject, public MythUIButtonListItem
     16{
     17    Q_OBJECT
     18  public:
     19        MythUIButtonListItemSetting (MythUIButtonList *lbtype, const QString &text)
     20            :MythUIButtonListItem(lbtype,text){}
     21  public slots:
     22    void ShouldUpdate(StandardSetting *setting);
     23};
     24
     25class MPUBLIC StandardSetting : public QObject, public StorageUser
     26{
     27    Q_OBJECT
     28
     29  public:
     30    virtual void setLabel(QString str) { m_label = str; }
     31    QString getLabel(void) const { return m_label; }
     32
     33    virtual void setHelpText(const QString &str) { m_helptext = str; }
     34    QString getHelpText(void) const { return m_helptext; }
     35
     36
     37/*    bool isVisible(void) const { return m_visible; };*/
     38    bool isEnabled() const { return m_enabled; }
     39    bool haveChanged();
     40    StandardSetting * getParent() const {return m_parent;};
     41
     42    // Gets
     43    virtual QString getValue(void) const {return m_settingValue;}
     44
     45    virtual void updateButton(MythUIButtonListItem *item);
     46
     47    // StorageUser
     48    void SetDBValue(const QString &val) { setValue(val); }
     49    QString GetDBValue(void) const { return getValue(); }
     50
     51    //added by xavier
     52
     53    MythUIButtonListItem * createButton(MythUIButtonList * list);
     54    //subsettings
     55    virtual void addChild(StandardSetting *child);
     56    virtual QList<StandardSetting *> *getSubSettings();
     57    virtual bool haveSubSettings();
     58    virtual void clearSettings();
     59
     60
     61    virtual void edit(MythScreenType * screen)=0;
     62    virtual void resultEdit(DialogCompletionEvent *dce)=0;
     63
     64    virtual void Load(void);
     65    virtual void Save(void);
     66
     67    virtual void Open(void){};
     68    virtual void Close(void){};
     69
     70    Storage *GetStorage(void) const { return m_storage; }
     71
     72    void addTargetedChild(const QString &value, StandardSetting * setting);
     73
     74    //not sure I want to do that yet
     75    virtual bool keyPressEvent(QKeyEvent *);
     76
     77  public slots:
     78    virtual void setEnabled(bool b);
     79    void setVisible(bool b) { m_visible = b; };
     80    virtual void setValue(const QString &newValue);
     81    virtual void childChanged(StandardSetting *){};
     82
     83  signals:
     84    void valueChanged(const QString &);
     85    void valueChanged(StandardSetting *);
     86    void ShouldRedraw(StandardSetting *);
     87    void settingsChanged(StandardSetting * selectedSetting = NULL);
     88
     89  protected:
     90    StandardSetting(Storage *_storage);
     91    virtual ~StandardSetting();
     92    void setParent(StandardSetting *parent);
     93    QString m_settingValue;
     94    bool m_enabled;
     95    QString m_label;
     96    QString m_helptext;
     97    bool m_visible;
     98  private:
     99    bool m_haveChanged;
     100    Storage *m_storage;
     101    StandardSetting * m_parent;
     102    QList<StandardSetting *> m_children;
     103    QMap<QString, QList<StandardSetting *> > m_targets;
     104};
     105
     106
     107/*******************************************************************************
     108*                           TextEdit Setting                                   *
     109*******************************************************************************/
     110
     111
     112class MPUBLIC MythUITextEditSetting : public StandardSetting
     113{
     114  public:
     115    virtual void resultEdit(DialogCompletionEvent *dce);
     116    virtual void edit(MythScreenType * screen);
     117    virtual void updateButton(MythUIButtonListItem *item);
     118  protected:
     119    MythUITextEditSetting (Storage *_storage);
     120   
     121};
     122
     123
     124class MPUBLIC TransTextEditSetting: public MythUITextEditSetting, public TransientStorage
     125{
     126  public:
     127    TransTextEditSetting() :
     128        MythUITextEditSetting(this), TransientStorage() { }
     129
     130    //Don't want to have to do that but cannot think of a work around
     131    /*virtual void Load(){MythUITextEditSetting::Load();}
     132    virtual void Save(){MythUITextEditSetting::Save();}*/
     133};
     134
     135
     136class MPUBLIC HostTextEditSetting: public MythUITextEditSetting, public HostDBStorage
     137{
     138  public:
     139    HostTextEditSetting(const QString &name) :
     140        MythUITextEditSetting(this), HostDBStorage(this, name) { }
     141
     142    //Don't want to have to do that but cannot think of a work around
     143    virtual void Load(){HostDBStorage::Load();MythUITextEditSetting::Load();}
     144    virtual void Save(){HostDBStorage::Save();MythUITextEditSetting::Save();}
     145};
     146
     147
     148/*******************************************************************************
     149*                           Directory Setting                                  *
     150*******************************************************************************/
     151
     152
     153class MPUBLIC MythUIFileBrowserSetting : public StandardSetting
     154{
     155  public:
     156    virtual void resultEdit(DialogCompletionEvent *dce);
     157    virtual void edit(MythScreenType * screen);
     158    virtual void updateButton(MythUIButtonListItem *item);
     159    void SetTypeFilter(QDir::Filters filter) { m_typeFilter = filter; };
     160    void SetNameFilter(QStringList filter) { m_nameFilter = filter; };
     161  protected:
     162    MythUIFileBrowserSetting (Storage *_storage);
     163    QDir::Filters      m_typeFilter;
     164    QStringList        m_nameFilter;
     165   
     166};
     167
     168
     169class MPUBLIC HostFileBrowserSetting: public MythUIFileBrowserSetting, public HostDBStorage
     170{
     171  public:
     172    HostFileBrowserSetting(const QString &name) :
     173        MythUIFileBrowserSetting(this), HostDBStorage(this, name) { }
     174
     175    //Don't want to have to do that but cannot think of a work around
     176    virtual void Load(){HostDBStorage::Load();MythUIFileBrowserSetting::Load();}
     177    virtual void Save(){HostDBStorage::Save();MythUIFileBrowserSetting::Save();}
     178};
     179
     180
     181/*******************************************************************************
     182*                           ComboBox Setting                                   *
     183*******************************************************************************/
     184//TODO implement rw=true
     185class MPUBLIC MythUIComboBoxSetting : public StandardSetting
     186{
     187  Q_OBJECT
     188  public:
     189    void setValue(int value);
     190    virtual void resultEdit(DialogCompletionEvent *dce);
     191    virtual void edit(MythScreenType * screen);
     192    void addSelection(const QString &label, QString value = QString::null, bool select = false);
     193    void clearSelections();
     194    virtual void updateButton(MythUIButtonListItem *item);
     195    virtual void Load();
     196  public slots:
     197    void setValue(const QString&);
     198  protected:
     199    MythUIComboBoxSetting (Storage *_storage, bool rw = false);
     200    ~MythUIComboBoxSetting();
     201    QVector<QString> m_labels;
     202    QVector<QString> m_values;
     203
     204  private:
     205    bool m_rewrite;
     206    bool m_isSet;
     207   
     208};
     209
     210class MPUBLIC HostComboBoxSetting: public MythUIComboBoxSetting, public HostDBStorage
     211{
     212  public:
     213    HostComboBoxSetting(const QString &name, bool rw = false) :
     214        MythUIComboBoxSetting(this, rw), HostDBStorage(this, name) { }
     215
     216    //Don't want to have to do that but cannot think of a work around
     217    virtual void Load(){HostDBStorage::Load();MythUIComboBoxSetting::Load();};
     218    virtual void Save(){HostDBStorage::Save();MythUIComboBoxSetting::Save();};
     219};
     220
     221
     222class MPUBLIC GlobalComboBoxSetting: public MythUIComboBoxSetting, public GlobalDBStorage
     223{
     224  public:
     225    GlobalComboBoxSetting(const QString &name, bool rw = false) :
     226        MythUIComboBoxSetting(this, rw), GlobalDBStorage(this, name) { }
     227
     228    //Don't want to have to do that but cannot think of a work around
     229    virtual void Load(){GlobalDBStorage::Load();MythUIComboBoxSetting::Load();}
     230    virtual void Save(){GlobalDBStorage::Save();MythUIComboBoxSetting::Save();}
     231};
     232
     233/*******************************************************************************
     234*                           SpinBox Setting                                    *
     235*******************************************************************************/
     236
     237
     238class MPUBLIC MythUISpinBoxSetting : public MythUIComboBoxSetting
     239{
     240  public:
     241    //void setValue(int value);
     242    int intValue();
     243    virtual void resultEdit(DialogCompletionEvent *dce);
     244    virtual void edit(MythScreenType * screen);
     245    virtual void updateButton(MythUIButtonListItem *item);
     246  protected:
     247    MythUISpinBoxSetting (Storage *_storage, int min, int max, int step, bool allow_single_step);
     248  private:
     249    int m_min;
     250    int m_max;
     251    int m_step;
     252    bool m_allow_single_step;
     253   
     254};
     255
     256class MPUBLIC TransMythUISpinBoxSetting: public MythUISpinBoxSetting, public TransientStorage
     257{
     258  public:
     259    TransMythUISpinBoxSetting(int min, int max, int step, bool allow_single_step=false) :
     260        MythUISpinBoxSetting(this, min, max, step, allow_single_step), TransientStorage() { }
     261
     262    //Don't want to have to do that but cannot think of a work around
     263    /*virtual void Load(){MythUISpinBoxSetting::Load();}
     264    virtual void Save(){MythUISpinBoxSetting::Save();}*/
     265};
     266
     267class MPUBLIC HostSpinBoxSetting: public MythUISpinBoxSetting, public HostDBStorage
     268{
     269  public:
     270    HostSpinBoxSetting(const QString &name, int min, int max, int step, bool allow_single_step=false) :
     271        MythUISpinBoxSetting(this, min, max, step, allow_single_step), HostDBStorage(this, name) { }
     272
     273    //Don't want to have to do that but cannot think of a work around
     274    virtual void Load(){HostDBStorage::Load();MythUISpinBoxSetting::Load();}
     275    virtual void Save(){HostDBStorage::Save();MythUISpinBoxSetting::Save();}
     276};
     277
     278class MPUBLIC GlobalSpinBoxSetting: public MythUISpinBoxSetting, public GlobalDBStorage
     279{
     280  public:
     281    GlobalSpinBoxSetting(const QString &name, int min, int max, int step, bool allow_single_step=false) :
     282        MythUISpinBoxSetting(this, min, max, step, allow_single_step), GlobalDBStorage(this, name) { }
     283
     284    //Don't want to have to do that but cannot think of a work around
     285    virtual void Load(){GlobalDBStorage::Load();MythUISpinBoxSetting::Load();}
     286    virtual void Save(){GlobalDBStorage::Save();MythUISpinBoxSetting::Save();}
     287};
     288
     289/*******************************************************************************
     290*                           CheckBox Setting                                   *
     291*******************************************************************************/
     292
     293class MPUBLIC MythUICheckBoxSetting : public StandardSetting
     294{
     295    Q_OBJECT
     296  public:
     297    virtual void resultEdit(DialogCompletionEvent *dce);
     298    virtual void edit(MythScreenType * screen);
     299    virtual void updateButton(MythUIButtonListItem *item);
     300    virtual void setValue(const QString&);
     301    virtual void setValue(bool value);
     302    bool boolValue();
     303  signals:
     304    void valueChanged(bool);
     305  protected:
     306    MythUICheckBoxSetting (Storage *_storage);
     307   
     308};
     309
     310class MPUBLIC TransMythUICheckBoxSetting: public MythUICheckBoxSetting, public TransientStorage
     311{
     312  public:
     313    TransMythUICheckBoxSetting() :
     314        MythUICheckBoxSetting(this), TransientStorage() { }
     315    //Don't want to have to do that but cannot think of a work around
     316    /*virtual void Load(){MythUICheckBoxSetting::Load();HostDBStorage::Load();}
     317    virtual void Save(){MythUICheckBoxSetting::Save();HostDBStorage::Save();}*/
     318};
     319
     320class MPUBLIC HostCheckBoxSetting: public MythUICheckBoxSetting, public HostDBStorage
     321{
     322  public:
     323    HostCheckBoxSetting(const QString &name) :
     324        MythUICheckBoxSetting(this), HostDBStorage(this, name) { }
     325    //Don't want to have to do that but cannot think of a work around
     326    virtual void Load(){HostDBStorage::Load();MythUICheckBoxSetting::Load();}
     327    virtual void Save(){HostDBStorage::Save();MythUICheckBoxSetting::Save();}
     328};
     329
     330class MPUBLIC GlobalCheckBoxSetting: public MythUICheckBoxSetting, public GlobalDBStorage
     331{
     332  public:
     333    GlobalCheckBoxSetting(const QString &name) :
     334        MythUICheckBoxSetting(this), GlobalDBStorage(this, name) { }
     335    //Don't want to have to do that but cannot think of a work around
     336    virtual void Load(){GlobalDBStorage::Load();MythUICheckBoxSetting::Load();}
     337    virtual void Save(){GlobalDBStorage::Save();MythUICheckBoxSetting::Save();}
     338};
     339
     340/*******************************************************************************
     341*                              Group Setting                                   *
     342*******************************************************************************/
     343
     344class MPUBLIC GroupSetting : public StandardSetting, public TransientStorage
     345{
     346  public:
     347    GroupSetting();
     348
     349    virtual void Load(void){StandardSetting::Load();};
     350    virtual void Save(void){StandardSetting::Save();};
     351    virtual void edit(MythScreenType * screen);
     352    virtual void resultEdit(DialogCompletionEvent *){};
     353    virtual void applyChange(){};
     354    virtual void updateButton(MythUIButtonListItem *item);
     355};
     356
     357class MPUBLIC ButtonStandardSetting : public StandardSetting, public TransientStorage
     358{
     359    Q_OBJECT
     360  public:
     361    ButtonStandardSetting(const QString &);
     362    virtual void Load(void){StandardSetting::Load();};
     363    virtual void Save(void){StandardSetting::Save();};
     364    virtual void edit(MythScreenType * screen);
     365    virtual void resultEdit(DialogCompletionEvent *){};
     366  signals:
     367    void clicked();
     368};
     369
     370/*******************************************************************************
     371*                            Standard Dialog Setting                            *
     372*******************************************************************************/
     373
     374class MPUBLIC StandardSettingDialog : public MythScreenType
     375{
     376    Q_OBJECT
     377
     378  public:
     379
     380    StandardSettingDialog(MythScreenStack *parent, const char *name);
     381    virtual ~StandardSettingDialog();
     382    bool Create(void);
     383    void loadSettings(GroupSetting * groupSettings);
     384    virtual void customEvent(QEvent *event);
     385    virtual bool keyPressEvent(QKeyEvent *);
     386  public slots:
     387    void Close(void);
     388    void updateSettings(StandardSetting * selectedSetting = NULL);
     389  protected:
     390
     391    MythUIButtonList *m_buttonList;
     392  private slots:
     393    void settingSelected(MythUIButtonListItem *item);
     394    void settingClicked(MythUIButtonListItem *item);
     395  private:
     396    void LevelUp();
     397    void LevelDown();
     398    void setCurrentGroupSetting(StandardSetting * groupSettings,StandardSetting * selectedSetting=0);
     399    void Save();
     400    MythUIText      *m_title;
     401    MythUIText      *m_groupHelp;
     402    MythUIText      *m_selectedSettingHelp;
     403    MythDialogBox   *m_menuPopup;
     404    GroupSetting    *m_settingsTree;
     405    StandardSetting *m_currentGroupSetting;
     406    bool m_loaded;
     407};
     408
     409Q_DECLARE_METATYPE(StandardSetting *);
     410
     411
     412#endif