MythTV  0.26-pre
settings.cpp
Go to the documentation of this file.
00001 // C++ headers
00002 #include <algorithm>
00003 #include <cmath>
00004 using namespace std;
00005 
00006 // POSIX headers
00007 #include <unistd.h>
00008 
00009 // Qt widgets
00010 #include <QLineEdit>
00011 #include <QLabel>
00012 #include <QSlider>
00013 #include <QLCDNumber>
00014 #include <QButtonGroup>
00015 #include <QRadioButton>
00016 #include <QProgressBar>
00017 
00018 // Qt utils
00019 #include <QFile>
00020 #include <QDateTime>
00021 #include <QDir>
00022 
00023 // MythTV headers
00024 #define MYTHCONFIG
00025 #include "settings.h"
00026 #include "mythconfiggroups.h"
00027 #undef MYTHCONFIG
00028 
00029 #include "mythwidgets.h"
00030 #include "mythcontext.h"
00031 #include "mythdb.h"
00032 #include "mythlogging.h"
00033 #include "DisplayRes.h"
00034 #include "mythuihelper.h"
00035 
00046 QWidget* Configurable::configWidget(ConfigurationGroup *cg, QWidget* parent,
00047                                     const char* widgetName)
00048 {
00049     (void)cg;
00050     (void)parent;
00051     (void)widgetName;
00052     LOG(VB_GENERAL, LOG_ALERT,
00053              "BUG: Configurable is visible, but has no configWidget");
00054     return NULL;
00055 }
00056 
00067 void Configurable::widgetDeleted(QObject *obj)
00068 {
00069     widgetInvalid(obj);
00070 }
00071 
00077 void Configurable::enableOnSet(const QString &val)
00078 {
00079     setEnabled( val != "0" );
00080 }
00081 
00087 void Configurable::enableOnUnset(const QString &val)
00088 {
00089     setEnabled( val == "0" );
00090 }
00091 
00092 QString Setting::getValue(void) const
00093 {
00094     return settingValue;
00095 }
00096 
00097 void Setting::setValue(const QString &newValue)
00098 {
00099     settingValue = newValue;
00100     emit valueChanged(settingValue);
00101 }
00102 
00103 int SelectSetting::findSelection(const QString &label, QString value) const
00104 {
00105     value = (value.isEmpty()) ? label : value;
00106 
00107     for (uint i = 0; i < values.size(); i++)
00108     {
00109         if ((values[i] == value) && (labels[i] == label))
00110             return i;
00111     }
00112 
00113     return -1;
00114 }
00115 
00116 void SelectSetting::addSelection(const QString &label, QString value,
00117                                  bool select)
00118 {
00119     value = (value.isEmpty()) ? label : value;
00120 
00121     int found = findSelection(label, value);
00122     if (found < 0)
00123     {
00124         labels.push_back(label);
00125         values.push_back(value);
00126         emit selectionAdded( label, value);
00127     }
00128 
00129     if (select || !isSet)
00130         setValue(value);
00131 }
00132 
00133 bool SelectSetting::removeSelection(const QString &label, QString value)
00134 {
00135     value = (value.isEmpty()) ? label : value;
00136 
00137     int found = findSelection(label, value);
00138     if (found < 0)
00139         return false;
00140 
00141     bool wasSet = isSet;
00142     isSet = false;
00143 
00144     labels.erase(labels.begin() + found);
00145     values.erase(values.begin() + found);
00146 
00147     isSet = wasSet && labels.size();
00148     if (isSet)
00149     {
00150         current = (current > (uint)found) ? current - 1 : current;
00151         current = min(current, (uint) (labels.size() - 1));
00152     }
00153 
00154     emit selectionRemoved(label, value);
00155 
00156     return true;
00157 }
00158 
00159 void SelectSetting::fillSelectionsFromDir(const QDir& dir, bool absPath)
00160 {
00161     QFileInfoList il = dir.entryInfoList();
00162 
00163     for (QFileInfoList::Iterator it = il.begin();
00164                                  it != il.end();
00165                                ++it )
00166     {
00167         QFileInfo &fi = *it;
00168 
00169         if (absPath)
00170             addSelection( fi.absoluteFilePath() );
00171         else
00172             addSelection( fi.fileName() );
00173     }
00174 }
00175 
00176 void SelectSetting::clearSelections(void) {
00177     labels.clear();
00178     values.clear();
00179     isSet = false;
00180     emit selectionsCleared();
00181 }
00182 
00183 void SelectSetting::setValue(const QString &newValue)
00184 {
00185     int found = getValueIndex(newValue);
00186     if (found < 0)
00187     {
00188         addSelection(newValue, newValue, true);
00189     }
00190     else
00191     {
00192         current = found;
00193         isSet   = true;
00194         Setting::setValue(newValue);
00195     }
00196 }
00197 
00198 void SelectSetting::setValue(int which)
00199 {
00200     if ((which >= ((int) values.size())) || (which < 0))
00201     {
00202         LOG(VB_GENERAL, LOG_ERR,
00203                  QString("SelectSetting::setValue(): invalid index: %1 size: ")
00204                      .arg(which).arg(values.size()));
00205     }
00206     else
00207     {
00208         current = which;
00209         isSet   = true;
00210         Setting::setValue(values[current]);
00211     }
00212 }
00213 
00214 QString SelectSetting::getSelectionLabel(void) const
00215 {
00216     if (!isSet || (current >= values.size()))
00217         return QString::null;
00218 
00219     return labels[current];
00220 }
00221 
00225 int SelectSetting::getValueIndex(QString value)
00226 {
00227     int ret = 0;
00228 
00229     selectionList::const_iterator it = values.begin();
00230     for (; it != values.end(); ++it, ++ret)
00231     {
00232         if (*it == value)
00233             return ret;
00234     }
00235 
00236     return -1;
00237 }
00238 
00239 bool SelectSetting::ReplaceLabel(const QString &new_label, const QString &value)
00240 {
00241     int i = getValueIndex(value);
00242 
00243     if (i >= 0)
00244         labels[i] = new_label;
00245 
00246     return (i >= 0);
00247 }
00248 
00249 QWidget* LabelSetting::configWidget(ConfigurationGroup *cg, QWidget* parent,
00250                                     const char* widgetName) {
00251     (void)cg;
00252 
00253     QWidget *widget = new QWidget(parent);
00254     widget->setObjectName(widgetName);
00255 
00256     QHBoxLayout *layout = new QHBoxLayout();
00257     layout->setContentsMargins(0,0,0,0);
00258     layout->setSpacing(0);
00259 
00260     if (getLabel() != "")
00261     {
00262         QLabel *label = new QLabel();
00263         label->setText(getLabel() + ":     ");
00264         layout->addWidget(label);
00265     }
00266 
00267     QLabel *value = new QLabel();
00268     value->setText(getValue());
00269     layout->addWidget(value);
00270 
00271     connect(this, SIGNAL(valueChanged(const QString&)),
00272             value, SLOT(setText(const QString&)));
00273 
00274     widget->setLayout(layout);
00275 
00276     return widget;
00277 }
00278 
00279 QWidget* LineEditSetting::configWidget(ConfigurationGroup *cg, QWidget* parent,
00280                                        const char *widgetName)
00281 {
00282     QWidget *widget = new QWidget(parent);
00283     widget->setObjectName(widgetName);
00284 
00285     QBoxLayout *layout = NULL;
00286     if (labelAboveWidget)
00287     {
00288         layout = new QVBoxLayout();
00289         widget->setSizePolicy(QSizePolicy(QSizePolicy::Preferred,
00290                                           QSizePolicy::Maximum));
00291     }
00292     else
00293         layout = new QHBoxLayout();
00294 
00295     layout->setContentsMargins(0,0,0,0);
00296     layout->setSpacing(0);
00297 
00298     if (getLabel() != "")
00299     {
00300         QLabel *label = new QLabel();
00301         label->setText(getLabel() + ":     ");
00302         layout->addWidget(label);
00303     }
00304 
00305     bxwidget = widget;
00306     connect(bxwidget, SIGNAL(destroyed(QObject*)),
00307             this,     SLOT(widgetDeleted(QObject*)));
00308 
00309     edit = new MythLineEdit(
00310         settingValue, NULL,
00311         QString(QString(widgetName) + "-edit").toAscii().constData());
00312     edit->setHelpText(getHelpText());
00313     edit->setText( getValue() );
00314     edit->setMinimumHeight(25);
00315     layout->addWidget(edit);
00316 
00317     connect(this, SIGNAL(valueChanged(const QString&)),
00318             edit, SLOT(setText(const QString&)));
00319     connect(edit, SIGNAL(textChanged(const QString&)),
00320             this, SLOT(setValue(const QString&)));
00321 
00322     if (cg)
00323         connect(edit, SIGNAL(changeHelpText(QString)), cg,
00324                 SIGNAL(changeHelpText(QString)));
00325 
00326     setRW(rw);
00327     SetPasswordEcho(password_echo);
00328 
00329     widget->setLayout(layout);
00330 
00331     return widget;
00332 }
00333 
00334 void LineEditSetting::widgetInvalid(QObject *obj)
00335 {
00336     if (bxwidget == obj)
00337     {
00338         bxwidget = NULL;
00339         edit     = NULL;
00340     }
00341 }
00342 
00343 void LineEditSetting::setEnabled(bool b)
00344 {
00345     Configurable::setEnabled(b);
00346     if (edit)
00347         edit->setEnabled(b);
00348 }
00349 
00350 void LineEditSetting::setVisible(bool b)
00351 {
00352     Configurable::setVisible(b);
00353     if (edit)
00354     {
00355         //QWidget *parent = edit->parentWidget();
00356         if (b)
00357             edit->show();
00358         else
00359             edit->hide();
00360     }
00361 }
00362 
00363 void LineEditSetting::SetPasswordEcho(bool b)
00364 {
00365     password_echo = b;
00366     if (edit)
00367         edit->setEchoMode(b ? QLineEdit::Password : QLineEdit::Normal);
00368 }
00369 
00370 void LineEditSetting::setHelpText(const QString &str)
00371 {
00372     if (edit)
00373         edit->setHelpText(str);
00374     Setting::setHelpText(str);
00375 }
00376 
00377 void BoundedIntegerSetting::setValue(int newValue)
00378 {
00379     newValue = std::max(std::min(newValue, max), min);
00380     IntegerSetting::setValue(newValue);
00381 }
00382 
00383 QWidget* SliderSetting::configWidget(ConfigurationGroup *cg, QWidget* parent,
00384                                      const char* widgetName)
00385 {
00386     QWidget *widget = new QWidget(parent);
00387     widget->setObjectName(widgetName);
00388 
00389     QBoxLayout *layout = NULL;
00390     if (labelAboveWidget)
00391     {
00392         layout = new QVBoxLayout();
00393         widget->setSizePolicy(QSizePolicy(QSizePolicy::Preferred,
00394                                           QSizePolicy::Maximum));
00395     }
00396     else
00397         layout = new QHBoxLayout();
00398 
00399     layout->setContentsMargins(0,0,0,0);
00400     layout->setSpacing(0);
00401 
00402     if (getLabel() != "")
00403     {
00404         QLabel *label = new QLabel();
00405         label->setObjectName(QString(widgetName) + "-label");
00406         label->setText(getLabel() + ":     ");
00407         layout->addWidget(label);
00408     }
00409 
00410     MythSlider *slider = new MythSlider(
00411         NULL, QString(QString(widgetName) + "-slider").toAscii().constData());
00412     slider->setHelpText(getHelpText());
00413     slider->setMinimum(min);
00414     slider->setMaximum(max);
00415     slider->setOrientation( Qt::Horizontal );
00416     slider->setSingleStep(step);
00417     slider->setValue(intValue());
00418     layout->addWidget(slider);
00419 
00420     QLCDNumber *lcd = new QLCDNumber();
00421     lcd->setObjectName(QString(QString(widgetName) + "-lcd")
00422                        .toAscii().constData());
00423     lcd->setMode(QLCDNumber::Dec);
00424     lcd->setSegmentStyle(QLCDNumber::Flat);
00425     lcd->display(intValue());
00426     layout->addWidget(lcd);
00427 
00428     connect(slider, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)));
00429     connect(slider, SIGNAL(valueChanged(int)), this, SLOT(setValue(int)));
00430     connect(this, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int)));
00431 
00432     if (cg)
00433         connect(slider, SIGNAL(changeHelpText(QString)), cg,
00434                 SIGNAL(changeHelpText(QString)));
00435 
00436     widget->setLayout(layout);
00437 
00438     return widget;
00439 }
00440 
00441 SpinBoxSetting::SpinBoxSetting(
00442     Storage *_storage, int _min, int _max, int _step,
00443     bool _allow_single_step, QString _special_value_text) :
00444     BoundedIntegerSetting(_storage, _min, _max, _step),
00445     spinbox(NULL), relayEnabled(true),
00446     sstep(_allow_single_step), svtext("")
00447 {
00448     if (!_special_value_text.isEmpty())
00449         svtext = _special_value_text;
00450 
00451     IntegerSetting *iset = (IntegerSetting *) this;
00452     connect(iset, SIGNAL(valueChanged(     int)),
00453             this, SLOT(  relayValueChanged(int)));
00454 }
00455 
00456 QWidget* SpinBoxSetting::configWidget(ConfigurationGroup *cg, QWidget* parent,
00457                                       const char* widgetName)
00458 {
00459     QWidget *widget = new QWidget(parent);
00460     widget->setObjectName(widgetName);
00461 
00462     QBoxLayout *layout = NULL;
00463     if (labelAboveWidget)
00464     {
00465         layout = new QVBoxLayout();
00466         widget->setSizePolicy(QSizePolicy(QSizePolicy::Preferred,
00467                                           QSizePolicy::Maximum));
00468     }
00469     else
00470         layout = new QHBoxLayout();
00471 
00472     layout->setContentsMargins(0,0,0,0);
00473     layout->setSpacing(0);
00474 
00475     if (getLabel() != "")
00476     {
00477         QLabel *label = new QLabel();
00478         label->setText(getLabel() + ":     ");
00479         layout->addWidget(label);
00480     }
00481 
00482     bxwidget = widget;
00483     connect(bxwidget, SIGNAL(destroyed(QObject*)),
00484             this,     SLOT(widgetDeleted(QObject*)));
00485 
00486     QString sbname = QString(widgetName) + "MythSpinBox";
00487     spinbox = new MythSpinBox(NULL, sbname.toAscii().constData(), sstep);
00488     spinbox->setHelpText(getHelpText());
00489     spinbox->setMinimum(min);
00490     spinbox->setMaximum(max);
00491     spinbox->setMinimumHeight(25);
00492     layout->addWidget(spinbox);
00493 
00494     // only set step size if greater than default (1), otherwise
00495     // this will screw up the single-step/jump behavior of the MythSpinBox
00496     if (1 < step)
00497         spinbox->setSingleStep(step);
00498     spinbox->setValue(intValue());
00499     if (!svtext.isEmpty())
00500         spinbox->setSpecialValueText(svtext);
00501 
00502     connect(spinbox, SIGNAL(valueChanged(int)), this, SLOT(setValue(int)));
00503 
00504     if (cg)
00505         connect(spinbox, SIGNAL(changeHelpText(QString)), cg,
00506                 SIGNAL(changeHelpText(QString)));
00507 
00508     widget->setLayout(layout);
00509 
00510     return widget;
00511 }
00512 
00513 void SpinBoxSetting::widgetInvalid(QObject *obj)
00514 {
00515     if (bxwidget == obj)
00516     {
00517         bxwidget = NULL;
00518         spinbox  = NULL;
00519     }
00520 }
00521 
00522 void SpinBoxSetting::setValue(int newValue)
00523 {
00524     newValue = std::max(std::min(newValue, max), min);
00525     if (spinbox && (spinbox->value() != newValue))
00526     {
00527         //int old = intValue();
00528         spinbox->setValue(newValue);
00529     }
00530     else if (intValue() != newValue)
00531     {
00532         BoundedIntegerSetting::setValue(newValue);
00533     }
00534 }
00535 
00536 void SpinBoxSetting::setFocus(void)
00537 {
00538     if (spinbox)
00539         spinbox->setFocus();
00540 }
00541 
00542 void SpinBoxSetting::clearFocus(void)
00543 {
00544     if (spinbox)
00545         spinbox->clearFocus();
00546 }
00547 
00548 bool SpinBoxSetting::hasFocus(void) const
00549 {
00550     if (spinbox)
00551         return spinbox->hasFocus();
00552 
00553     return false;
00554 }
00555 
00556 void SpinBoxSetting::relayValueChanged(int newValue)
00557 {
00558     if (relayEnabled)
00559         emit valueChanged(configName, newValue);
00560 }
00561 
00562 void SpinBoxSetting::setHelpText(const QString &str)
00563 {
00564     if (spinbox)
00565         spinbox->setHelpText(str);
00566     BoundedIntegerSetting::setHelpText(str);
00567 }
00568 
00569 QWidget* SelectLabelSetting::configWidget(ConfigurationGroup *cg,
00570                                           QWidget    *parent,
00571                                           const char *widgetName)
00572 {
00573     (void)cg;
00574 
00575     QWidget *widget = new QWidget(parent);
00576     widget->setObjectName(widgetName);
00577 
00578     QBoxLayout *layout = NULL;
00579     if (labelAboveWidget)
00580     {
00581         layout = new QVBoxLayout();
00582         widget->setSizePolicy(QSizePolicy(QSizePolicy::Preferred,
00583                                           QSizePolicy::Maximum));
00584     }
00585     else
00586         layout = new QHBoxLayout();
00587 
00588     layout->setContentsMargins(0,0,0,0);
00589     layout->setSpacing(0);
00590 
00591     if (getLabel() != "")
00592     {
00593         QLabel *label = new QLabel();
00594         label->setText(getLabel() + ":     ");
00595         layout->addWidget(label);
00596     }
00597 
00598 
00599     QLabel *value = new QLabel();
00600     value->setText(labels[current]);
00601     layout->addWidget(value);
00602 
00603     connect(this, SIGNAL(valueChanged(const QString&)),
00604             value, SLOT(setText(const QString&)));
00605 
00606     widget->setLayout(layout);
00607 
00608     return widget;
00609 }
00610 
00611 QWidget* ComboBoxSetting::configWidget(ConfigurationGroup *cg, QWidget* parent,
00612                                        const char* widgetName)
00613 {
00614     QWidget *widget = new QWidget(parent);
00615     widget->setObjectName(widgetName);
00616 
00617     QBoxLayout *layout = NULL;
00618     if (labelAboveWidget)
00619     {
00620         layout = new QVBoxLayout();
00621         widget->setSizePolicy(QSizePolicy(QSizePolicy::Preferred,
00622                                           QSizePolicy::Maximum));
00623     }
00624     else
00625         layout = new QHBoxLayout();
00626 
00627     layout->setContentsMargins(0,0,0,0);
00628     layout->setSpacing(0);
00629 
00630     if (getLabel() != "")
00631     {
00632         QLabel *label = new QLabel();
00633         label->setText(getLabel() + ":     ");
00634         layout->addWidget(label);
00635     }
00636 
00637     bxwidget = widget;
00638     connect(bxwidget, SIGNAL(destroyed(QObject*)),
00639             this,     SLOT(widgetDeleted(QObject*)));
00640 
00641     cbwidget = new MythComboBox(rw);
00642     cbwidget->setHelpText(getHelpText());
00643 
00644     for(unsigned int i = 0 ; i < labels.size() ; ++i)
00645         cbwidget->insertItem(labels[i]);
00646 
00647     resetMaxCount(cbwidget->count());
00648 
00649     if (isSet)
00650         cbwidget->setCurrentIndex(current);
00651 
00652     if (1 < step)
00653         cbwidget->setStep(step);
00654 
00655     connect(cbwidget, SIGNAL(highlighted(int)),
00656             this, SLOT(setValue(int)));
00657     connect(cbwidget, SIGNAL(activated(int)),
00658             this, SLOT(setValue(int)));
00659     connect(this, SIGNAL(selectionsCleared()),
00660             cbwidget, SLOT(clear()));
00661 
00662     if (rw)
00663         connect(cbwidget, SIGNAL(editTextChanged(const QString &)),
00664                 this, SLOT(editTextChanged(const QString &)));
00665 
00666     if (cg)
00667         connect(cbwidget, SIGNAL(changeHelpText(QString)), cg,
00668                 SIGNAL(changeHelpText(QString)));
00669 
00670     cbwidget->setMinimumHeight(25);
00671 
00672     layout->addWidget(cbwidget);
00673     layout->setStretchFactor(cbwidget, 1);
00674 
00675     widget->setLayout(layout);
00676 
00677     return widget;
00678 }
00679 
00680 void ComboBoxSetting::widgetInvalid(QObject *obj)
00681 {
00682     if (bxwidget == obj)
00683     {
00684         bxwidget = NULL;
00685         cbwidget = NULL;
00686     }
00687 }
00688 
00689 void ComboBoxSetting::setEnabled(bool b)
00690 {
00691     Configurable::setEnabled(b);
00692     if (cbwidget)
00693         cbwidget->setEnabled(b);
00694 }
00695 
00696 void ComboBoxSetting::setVisible(bool b)
00697 {
00698     Configurable::setVisible(b);
00699     if (cbwidget)
00700     {
00701         if (b)
00702             cbwidget->show();
00703         else
00704             cbwidget->hide();
00705     }
00706 }
00707 
00708 void ComboBoxSetting::setValue(const QString &newValue)
00709 {
00710     for (uint i = 0; i < values.size(); i++)
00711     {
00712         if (values[i] == newValue)
00713         {
00714             setValue(i);
00715             break;
00716         }
00717     }
00718 
00719     if (rw)
00720     {
00721         Setting::setValue(newValue);
00722         if (cbwidget)
00723             cbwidget->setCurrentIndex(current);
00724     }
00725 }
00726 
00727 void ComboBoxSetting::setValue(int which)
00728 {
00729     if (cbwidget)
00730         cbwidget->setCurrentIndex(which);
00731     SelectSetting::setValue(which);
00732 }
00733 
00734 void ComboBoxSetting::addSelection(
00735     const QString &label, QString value, bool select)
00736 {
00737     if ((findSelection(label, value) < 0) && cbwidget)
00738     {
00739         resetMaxCount(cbwidget->count()+1);
00740         cbwidget->insertItem(label);
00741     }
00742 
00743     SelectSetting::addSelection(label, value, select);
00744 
00745     if (cbwidget && isSet)
00746         cbwidget->setCurrentIndex(current);
00747 }
00748 
00749 bool ComboBoxSetting::removeSelection(const QString &label, QString value)
00750 {
00751     SelectSetting::removeSelection(label, value);
00752     if (!cbwidget)
00753         return true;
00754 
00755     for (uint i = 0; ((int) i) < cbwidget->count(); i++)
00756     {
00757         if (cbwidget->itemText(i) == label)
00758         {
00759             cbwidget->removeItem(i);
00760             if (isSet)
00761                 cbwidget->setCurrentIndex(current);
00762             resetMaxCount(cbwidget->count());
00763             return true;
00764         }
00765     }
00766 
00767     return false;
00768 }
00769 
00770 void ComboBoxSetting::editTextChanged(const QString &newText)
00771 {
00772     if (cbwidget)
00773     {
00774         for (uint i = 0; i < labels.size(); i++)
00775             if (labels[i] == newText)
00776                 return;
00777 
00778         if (labels.size() == static_cast<size_t>(cbwidget->maxCount()))
00779         {
00780             SelectSetting::removeSelection(labels[cbwidget->maxCount() - 1],
00781                                            values[cbwidget->maxCount() - 1]);
00782             cbwidget->setItemText(cbwidget->maxCount() - 1, newText);
00783         }
00784         else
00785         {
00786             cbwidget->insertItem(newText);
00787         }
00788 
00789         SelectSetting::addSelection(newText, newText, true);
00790         cbwidget->setCurrentIndex(cbwidget->maxCount() - 1);
00791     }
00792 }
00793 
00794 void ComboBoxSetting::setHelpText(const QString &str)
00795 {
00796     if (cbwidget)
00797         cbwidget->setHelpText(str);
00798     SelectSetting::setHelpText(str);
00799 }
00800 
00801 void HostRefreshRateComboBox::ChangeResolution(const QString& resolution)
00802 {
00803     clearSelections();
00804 
00805     const vector<double> list = GetRefreshRates(resolution);
00806     addSelection(QObject::tr("Auto"), "0");
00807     int hz50 = -1, hz60 = -1;
00808     for (uint i=0; i<list.size(); ++i)
00809     {
00810         QString sel = QString::number((double) list[i],'f', 3);
00811         addSelection(sel+" Hz", sel);
00812         hz50 = (fabs(50.0 - list[i]) < 0.01) ? i : hz50;
00813         hz60 = (fabs(60.0 - list[i]) < 0.01) ? i : hz60;
00814     }
00815 
00816     setValue(0);
00817     if ("640x480" == resolution || "720x480" == resolution)
00818         setValue(hz60+1);
00819     if ("640x576" == resolution || "720x576" == resolution)
00820         setValue(hz50+1);
00821 
00822     setEnabled(list.size());
00823 }
00824 
00825 const vector<double> HostRefreshRateComboBox::GetRefreshRates(const QString &res)
00826 {
00827     QStringList slist = res.split("x");
00828     int w = 0, h = 0;
00829     bool ok0 = false, ok1 = false;
00830     if (2 == slist.size())
00831     {
00832         w = slist[0].toInt(&ok0);
00833         h = slist[1].toInt(&ok1);
00834     }
00835 
00836     DisplayRes *display_res = DisplayRes::GetDisplayRes();
00837     if (display_res && ok0 && ok1)
00838         return display_res->GetRefreshRates(w, h);
00839 
00840     vector<double> list;
00841     return list;
00842 }
00843 
00844 void PathSetting::addSelection(const QString& label,
00845                                QString value,
00846                                bool select) {
00847     QString pathname = label;
00848     if (value != QString::null)
00849         pathname = value;
00850 
00851     if (mustexist && !QFile(pathname).exists())
00852         return;
00853 
00854     ComboBoxSetting::addSelection(label, value, select);
00855 }
00856 
00857 QTime TimeSetting::timeValue(void) const {
00858     return QTime::fromString(getValue(), Qt::ISODate);
00859 }
00860 
00861 void TimeSetting::setValue(const QTime& newValue) {
00862     Setting::setValue(newValue.toString(Qt::ISODate));
00863 }
00864 
00865 QString DateSetting::getValue(void) const
00866 {
00867     return settingValue;
00868 }
00869 
00870 QDate DateSetting::dateValue(void) const {
00871     return QDate::fromString(getValue(), Qt::ISODate);
00872 }
00873 
00874 void DateSetting::setValue(const QDate& newValue) {
00875     Setting::setValue(newValue.toString(Qt::ISODate));
00876 }
00877 
00878 void DateSetting::setValue(const QString &newValue)
00879 {
00880     QDate date = QDate::fromString(newValue, Qt::ISODate);
00881     if (date.isValid())
00882         setValue(date);
00883 }
00884 
00885 void TimeSetting::setValue(const QString &newValue)
00886 {
00887     QTime time = QTime::fromString(newValue, Qt::ISODate);
00888     if (time.isValid())
00889         setValue(time);
00890 }
00891 
00892 QWidget* CheckBoxSetting::configWidget(ConfigurationGroup *cg, QWidget* parent,
00893                                        const char* widgetName) {
00894     widget = new MythCheckBox(parent, widgetName);
00895     connect(widget, SIGNAL(destroyed(QObject*)),
00896             this,   SLOT(widgetDeleted(QObject*)));
00897 
00898     widget->setHelpText(getHelpText());
00899     widget->setText(getLabel());
00900     widget->setChecked(boolValue());
00901 
00902     connect(widget, SIGNAL(toggled(bool)),
00903             this, SLOT(setValue(bool)));
00904     connect(this, SIGNAL(valueChanged(bool)),
00905             widget, SLOT(setChecked(bool)));
00906 
00907     if (cg)
00908         connect(widget, SIGNAL(changeHelpText(QString)), cg,
00909                 SIGNAL(changeHelpText(QString)));
00910 
00911     return widget;
00912 }
00913 
00914 void CheckBoxSetting::widgetInvalid(QObject *obj)
00915 {
00916     widget = (widget == obj) ? NULL : widget;
00917 }
00918 
00919 void CheckBoxSetting::setVisible(bool b)
00920 {
00921     BooleanSetting::setVisible(b);
00922     if (widget)
00923     {
00924         if (b)
00925             widget->show();
00926         else
00927             widget->hide();
00928     }
00929 }
00930 
00931 void CheckBoxSetting::setLabel(QString str)
00932 {
00933     BooleanSetting::setLabel(str);
00934     if (widget)
00935         widget->setText(str);
00936 }
00937 
00938 void CheckBoxSetting::setEnabled(bool fEnabled)
00939 {
00940     BooleanSetting::setEnabled(fEnabled);
00941     if (widget)
00942         widget->setEnabled(fEnabled);
00943 }
00944 
00945 void CheckBoxSetting::setHelpText(const QString &str)
00946 {
00947     if (widget)
00948         widget->setHelpText(str);
00949     BooleanSetting::setHelpText(str);
00950 }
00951 
00952 void AutoIncrementDBSetting::Save(QString table)
00953 {
00954     if (intValue() == 0)
00955     {
00956         // Generate a new, unique ID
00957         QString querystr = QString("INSERT INTO " + table + " ("
00958                 + GetColumnName() + ") VALUES (0);");
00959 
00960         MSqlQuery query(MSqlQuery::InitCon());
00961 
00962         if (!query.exec(querystr))
00963         {
00964             MythDB::DBError("inserting row", query);
00965             return;
00966         }
00967         // XXX -- HACK BEGIN:
00968         // lastInsertID fails with "QSqlQuery::value: not positioned on a valid record"
00969         // if we get a invalid QVariant we workaround the problem by taking advantage
00970         // of mysql always incrementing the auto increment pointer
00971         // this breaks if someone modifies the auto increment pointer
00972         //setValue(query.lastInsertId().toInt());
00973 
00974         QVariant var = query.lastInsertId();
00975 
00976         if (var.type())
00977             setValue(var.toInt());
00978         else
00979         {
00980             querystr = QString("SELECT MAX(" + GetColumnName() + ") FROM " + table + ";");
00981             if (query.exec(querystr) && query.next())
00982             {
00983                 int lii = query.value(0).toInt();
00984                 lii = lii ? lii : 1;
00985                 setValue(lii);
00986             }
00987             else
00988                 LOG(VB_GENERAL, LOG_EMERG,
00989                          "Can't determine the Id of the last insert "
00990                          "QSqlQuery.lastInsertId() failed, the workaround "
00991                          "failed too!");
00992         }
00993         // XXX -- HACK END:
00994     }
00995 }
00996 
00997 void AutoIncrementDBSetting::Save(void)
00998 {
00999     Save(GetTableName());
01000 }
01001 
01002 void ListBoxSetting::setEnabled(bool b)
01003 {
01004     Configurable::setEnabled(b);
01005     if (lbwidget)
01006         lbwidget->setEnabled(b);
01007 }
01008 
01009 void ListBoxSetting::clearSelections(void)
01010 {
01011     SelectSetting::clearSelections();
01012     if (lbwidget)
01013         lbwidget->clear();
01014 }
01015 
01016 void ListBoxSetting::addSelection(
01017     const QString &label, QString value, bool select)
01018 {
01019     SelectSetting::addSelection(label, value, select);
01020     if (lbwidget)
01021     {
01022         lbwidget->insertItem(label);
01023         //lbwidget->triggerUpdate(true);
01024     }
01025 };
01026 
01027 bool ListBoxSetting::ReplaceLabel(
01028     const QString &new_label, const QString &value)
01029 {
01030     int i = getValueIndex(value);
01031 
01032     if ((i >= 0) && SelectSetting::ReplaceLabel(label, value) && lbwidget)
01033     {
01034         lbwidget->changeItem(new_label, i);
01035         return true;
01036     }
01037 
01038     return false;
01039 }
01040 
01041 QWidget* ListBoxSetting::configWidget(ConfigurationGroup *cg, QWidget* parent,
01042                                       const char* widgetName)
01043 {
01044     QWidget *widget = new QWidget(parent);
01045     widget->setObjectName(widgetName);
01046 
01047     QVBoxLayout *layout = new QVBoxLayout();
01048 
01049     if (getLabel() != "")
01050     {
01051         QLabel *label = new QLabel();
01052         label->setText(getLabel());
01053         layout->addWidget(label);
01054     }
01055 
01056     layout->setContentsMargins(0,0,0,0);
01057     layout->setSpacing(0);
01058 
01059     bxwidget = widget;
01060     connect(bxwidget, SIGNAL(destroyed(QObject*)),
01061             this,     SLOT(widgetDeleted(QObject*)));
01062 
01063     lbwidget = new MythListBox(NULL);
01064     lbwidget->setHelpText(getHelpText());
01065     if (eventFilter)
01066         lbwidget->installEventFilter(eventFilter);
01067 
01068     for(unsigned int i = 0 ; i < labels.size() ; ++i) {
01069         lbwidget->insertItem(labels[i]);
01070         if (isSet && current == i)
01071             lbwidget->setCurrentRow(i);
01072     }
01073 
01074     connect(this,     SIGNAL(selectionsCleared()),
01075             lbwidget, SLOT(  clear()));
01076     connect(this,     SIGNAL(valueChanged(const QString&)),
01077             lbwidget, SLOT(  setCurrentItem(const QString&)));
01078 
01079     connect(lbwidget, SIGNAL(accepted(int)),
01080             this,     SIGNAL(accepted(int)));
01081     connect(lbwidget, SIGNAL(menuButtonPressed(int)),
01082             this,     SIGNAL(menuButtonPressed(int)));
01083     connect(lbwidget, SIGNAL(editButtonPressed(int)),
01084             this,     SIGNAL(editButtonPressed(int)));
01085     connect(lbwidget, SIGNAL(deleteButtonPressed(int)),
01086             this,     SIGNAL(deleteButtonPressed(int)));
01087 
01088     connect(lbwidget, SIGNAL(highlighted(int)),
01089             this,     SLOT(  setValueByIndex(int)));
01090 
01091     if (cg)
01092         connect(lbwidget, SIGNAL(changeHelpText(QString)), cg,
01093                 SIGNAL(changeHelpText(QString)));
01094 
01095     lbwidget->setFocus();
01096     lbwidget->setSelectionMode(selectionMode);
01097     layout->addWidget(lbwidget);
01098 
01099     widget->setLayout(layout);
01100 
01101     return widget;
01102 }
01103 
01104 void ListBoxSetting::widgetInvalid(QObject *obj)
01105 {
01106     if (bxwidget == obj)
01107     {
01108         bxwidget = NULL;
01109         lbwidget = NULL;
01110     }
01111 }
01112 
01113 void ListBoxSetting::setSelectionMode(MythListBox::SelectionMode mode)
01114 {
01115    selectionMode = mode;
01116    if (lbwidget)
01117        lbwidget->setSelectionMode(selectionMode);
01118 }
01119 
01120 void ListBoxSetting::setValueByIndex(int index)
01121 {
01122     if (((uint)index) < values.size())
01123         setValue(values[index]);
01124 }
01125 
01126 void ListBoxSetting::setHelpText(const QString &str)
01127 {
01128     if (lbwidget)
01129         lbwidget->setHelpText(str);
01130     SelectSetting::setHelpText(str);
01131 }
01132 
01133 HostnameSetting::HostnameSetting(Storage *storage) : Setting(storage)
01134 {
01135     setVisible(false);
01136 
01137     setValue(gCoreContext->GetHostName());
01138 }
01139 
01140 void ChannelSetting::fillSelections(SelectSetting* setting) {
01141 
01142     // this should go somewhere else, in something that knows about
01143     // channels and how they're stored in the database.  We're just a
01144     // selector.
01145 
01146     MSqlQuery query(MSqlQuery::InitCon());
01147     query.prepare("SELECT name, chanid FROM channel;");
01148     if (query.exec() && query.isActive() && query.size() > 0)
01149         while (query.next())
01150             setting->addSelection(query.value(0).toString(),
01151                                   QString::number(query.value(1).toInt()));
01152 }
01153 
01154 QWidget* ButtonSetting::configWidget(ConfigurationGroup* cg, QWidget* parent,
01155                                      const char* widgetName)
01156 {
01157     (void) cg;
01158     button = new MythPushButton(parent, widgetName);
01159     connect(button, SIGNAL(destroyed(QObject*)),
01160             this,   SLOT(widgetDeleted(QObject*)));
01161 
01162     button->setText(getLabel());
01163     button->setHelpText(getHelpText());
01164 
01165     connect(button, SIGNAL(pressed()), this, SIGNAL(pressed()));
01166     connect(button, SIGNAL(pressed()), this, SLOT(SendPressedString()));
01167 
01168     if (cg)
01169         connect(button, SIGNAL(changeHelpText(QString)),
01170                 cg, SIGNAL(changeHelpText(QString)));
01171 
01172     return button;
01173 }
01174 
01175 void ButtonSetting::widgetInvalid(QObject *obj)
01176 {
01177     button = (button == obj) ? NULL : button;
01178 }
01179 
01180 void ButtonSetting::SendPressedString(void)
01181 {
01182     emit pressed(name);
01183 }
01184 
01185 void ButtonSetting::setEnabled(bool fEnabled)
01186 {
01187     Configurable::setEnabled(fEnabled);
01188     if (button)
01189         button->setEnabled(fEnabled);
01190 }
01191 
01192 void ButtonSetting::setLabel(QString str)
01193 {
01194     if (button)
01195         button->setText(str);
01196     Setting::setLabel(str);
01197 }
01198 
01199 void ButtonSetting::setHelpText(const QString &str)
01200 {
01201     if (button)
01202         button->setHelpText(str);
01203     Setting::setHelpText(str);
01204 }
01205 
01206 QWidget* ProgressSetting::configWidget(ConfigurationGroup* cg, QWidget* parent,
01207                                        const char* widgetName) {
01208     (void)cg;
01209     QWidget *widget = new QWidget(parent);
01210     widget->setObjectName(widgetName);
01211     QBoxLayout *layout = new QHBoxLayout();
01212     layout->setContentsMargins(0,0,0,0);
01213     layout->setSpacing(0);
01214 
01215     if (getLabel() != "")
01216     {
01217         QLabel* label = new QLabel();
01218         label->setObjectName(QString(widgetName) + "_label");
01219         label->setText(getLabel() + ":     ");
01220         layout->addWidget(label);
01221     }
01222 
01223     QProgressBar *progress = new QProgressBar(NULL);
01224     progress->setObjectName(widgetName);
01225     progress->setRange(0,totalSteps);
01226     layout->addWidget(progress);
01227 
01228     connect(this, SIGNAL(valueChanged(int)), progress, SLOT(setValue(int)));
01229     progress->setValue(intValue());
01230 
01231     widget->setLayout(layout);
01232 
01233     return widget;
01234 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends