Ticket #13204: 20180508_1726_spinbox.patch

File 20180508_1726_spinbox.patch, 8.8 KB (added by Peter Bennett, 22 months ago)

Updated patch with a change to the title of the entry dialog

  • mythtv/libs/libmythui/mythuispinbox.cpp

    diff --git a/mythtv/libs/libmythui/mythuispinbox.cpp b/mythtv/libs/libmythui/mythuispinbox.cpp
    index ef6cfd8..0e0209f 100644
    a b  
    11
     2#include "mythmainwindow.h"
    23#include "mythuispinbox.h"
     4#include "mythlogging.h"
     5#include "mythuibutton.h"
     6#include "mythuitextedit.h"
     7#include "mythuitext.h"
    38
    49// QT headers
    510#include <QDomDocument>
     
    712
    813MythUISpinBox::MythUISpinBox(MythUIType *parent, const QString &name)
    914    : MythUIButtonList(parent, name), m_hasTemplate(false),
    10       m_moveAmount(0)
     15      m_moveAmount(0), m_low(0), m_high(0), m_step(0)
    1116{
    1217}
    1318
    void MythUISpinBox::SetRange(int low, int high, int step, uint pageMultiple) 
    3136    if ((high == low) || step == 0)
    3237        return;
    3338
     39    m_low = low;
     40    m_high = high;
     41    m_step = step;
     42
    3443    m_moveAmount = pageMultiple;
    3544
    3645    bool reverse = false;
    void MythUISpinBox::CopyFrom(MythUIType *base) 
    200209
    201210    MythUIButtonList::CopyFrom(base);
    202211}
     212
     213// Open the entry dialog on certain key presses. A select or search action will
     214// open the dialog. A number or minus sign will open the entry dialog with the
     215// given key as the first digit.
     216// If the spinbox uses a template, the entries are not just numbers
     217// but can be sentences. The whole sentence is put in the entry field,
     218// allowing the user to change the number part of it.
     219
     220bool MythUISpinBox::keyPressEvent(QKeyEvent *event)
     221{
     222    QStringList actions;
     223    bool handled = false;
     224    handled = GetMythMainWindow()->TranslateKeyPress("Global", event, actions);
     225    if (handled)
     226        return true;
     227
     228    QString initialEntry = GetItemCurrent()->GetText();
     229    bool doEntry = false;
     230    for (int i = 0; i < actions.size(); ++i)
     231    {
     232        if (actions[i] >= ACTION_0 && actions[i] <= ACTION_9)
     233        {
     234            if (!m_hasTemplate)
     235                initialEntry = actions[i];
     236            doEntry=true;
     237            break;
     238        }
     239        if (actions[i] == ACTION_SELECT || actions[i] == "SEARCH")
     240        {
     241            doEntry=true;
     242            break;
     243        }
     244    }
     245    if (actions.size() == 0 && event->text() == "-")
     246    {
     247        if (!m_hasTemplate)
     248            initialEntry = "-";
     249        doEntry=true;
     250    }
     251
     252    if (doEntry)
     253    {
     254        ShowEntryDialog(initialEntry);
     255        handled = true;
     256    }
     257
     258    if (handled)
     259        return true;
     260    else
     261        return MythUIButtonList::keyPressEvent(event);
     262}
     263
     264void MythUISpinBox::ShowEntryDialog(QString initialEntry)
     265{
     266    MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
     267
     268    SpinBoxEntryDialog *dlg = new SpinBoxEntryDialog(popupStack, "SpinBoxEntryDialog",
     269        this, initialEntry, m_low, m_high, m_step);
     270
     271    if (dlg->Create())
     272        popupStack->AddScreen(dlg);
     273    else
     274        delete dlg;
     275}
     276
     277// Convenience Dialog to allow entry of a Spinbox value
     278
     279SpinBoxEntryDialog::SpinBoxEntryDialog(MythScreenStack *parent, const char *name,
     280        MythUIButtonList *parentList, QString searchText,
     281        int low, int high, int step)
     282    : MythScreenType(parent, name, false),
     283        m_parentList(parentList),
     284        m_searchText(searchText),
     285        m_entryEdit(NULL),
     286        m_cancelButton(NULL),
     287        m_okButton(NULL),
     288        m_rulesText(NULL),
     289        m_okClicked(false),
     290        m_low(low),
     291        m_high(high),
     292        m_step(step)
     293
     294{
     295    m_selection = parentList->GetCurrentPos();
     296}
     297
     298
     299SpinBoxEntryDialog::~SpinBoxEntryDialog()
     300{
     301}
     302
     303bool SpinBoxEntryDialog::Create(void)
     304{
     305    if (!CopyWindowFromBase("SpinBoxEntryDialog", this))
     306        return false;
     307
     308    bool err = false;
     309    UIUtilE::Assign(this, m_entryEdit,  "entry", &err);
     310    UIUtilW::Assign(this, m_cancelButton,"cancel", &err);
     311    UIUtilW::Assign(this, m_rulesText,"rules", &err);
     312    UIUtilE::Assign(this, m_okButton,    "ok", &err);
     313
     314    if (err)
     315    {
     316        LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'SpinBoxEntryDialog'");
     317        return false;
     318    }
     319
     320    m_entryEdit->SetText(m_searchText);
     321    entryChanged();
     322    if (m_rulesText)
     323    {
     324        InfoMap infoMap;
     325        infoMap["low"] = QString::number(m_low);
     326        infoMap["high"] = QString::number(m_high);
     327        infoMap["step"] = QString::number(m_step);
     328        m_rulesText->SetTextFromMap(infoMap);
     329    }
     330
     331    connect(m_entryEdit, SIGNAL(valueChanged()), SLOT(entryChanged()));
     332    if (m_cancelButton)
     333        connect(m_cancelButton, SIGNAL(Clicked()), SLOT(Close()));
     334    connect(m_okButton, SIGNAL(Clicked()), SLOT(okClicked()));
     335
     336    BuildFocusList();
     337
     338    return true;
     339}
     340
     341void SpinBoxEntryDialog::entryChanged(void)
     342{
     343    int currPos = 0;
     344    int count = m_parentList->GetCount();
     345    QString searchText = m_entryEdit->GetText();
     346    bool found = false;
     347    for (currPos = 0; currPos < count; currPos++)
     348    {
     349        if (searchText.compare(m_parentList->GetItemAt(currPos)->GetText(),
     350            Qt::CaseInsensitive) == 0)
     351        {
     352            found = true;
     353            m_selection = currPos;
     354            break;
     355        }
     356    }
     357    m_okButton->SetEnabled(found);
     358}
     359
     360void SpinBoxEntryDialog::okClicked(void)
     361{
     362    m_parentList->SetItemCurrent(m_selection);
     363    Close();
     364}
  • mythtv/libs/libmythui/mythuispinbox.h

    diff --git a/mythtv/libs/libmythui/mythuispinbox.h b/mythtv/libs/libmythui/mythuispinbox.h
    index 27a72c1..aa239d5 100644
    a b class MUI_PUBLIC MythUISpinBox : public MythUIButtonList 
    2727    void AddSelection (int value, const QString &label = "");
    2828    QString GetValue(void) const { return GetDataValue().toString(); }
    2929    int GetIntValue(void) const { return GetDataValue().toInt(); }
     30    bool keyPressEvent(QKeyEvent *event);
    3031
    3132  protected:
    3233    virtual bool ParseElement(
    class MUI_PUBLIC MythUISpinBox : public MythUIButtonList 
    3637
    3738    virtual bool MoveDown(MovementUnit unit = MoveItem, uint amount = 0);
    3839    virtual bool MoveUp(MovementUnit unit = MoveItem, uint amount = 0);
     40    void ShowEntryDialog(QString initialEntry);
    3941
    4042    bool m_hasTemplate;
    4143    QString m_negativeTemplate;
    class MUI_PUBLIC MythUISpinBox : public MythUIButtonList 
    4345    QString m_positiveTemplate;
    4446
    4547    uint m_moveAmount;
     48    int m_low;
     49    int m_high;
     50    int m_step;
     51};
     52
     53// Convenience Dialog to allow entry of a Spinbox value
     54
     55class MUI_PUBLIC SpinBoxEntryDialog : public MythScreenType
     56{
     57    Q_OBJECT
     58  public:
     59    SpinBoxEntryDialog(MythScreenStack *parent, const char *name,
     60        MythUIButtonList *parentList, QString searchText,
     61        int low, int high, int step);
     62    ~SpinBoxEntryDialog(void);
     63
     64    bool Create(void);
     65
     66  protected slots:
     67    void entryChanged(void);
     68    void okClicked(void);
     69
     70  protected:
     71    MythUIButtonList  *m_parentList;
     72    QString            m_searchText;
     73    MythUITextEdit    *m_entryEdit;
     74    MythUIButton      *m_cancelButton;
     75    MythUIButton      *m_okButton;
     76    MythUIText        *m_rulesText;
     77    int                m_selection;
     78    bool               m_okClicked;
     79    int m_low;
     80    int m_high;
     81    int m_step;
     82
     83
    4684};
    4785
    4886#endif
  • mythtv/themes/default/base.xml

    diff --git a/mythtv/themes/default/base.xml b/mythtv/themes/default/base.xml
    index 0723b25..7e28b56 100644
    a b  
    12431243        </statetype>
    12441244    </window>
    12451245
     1246    <!-- Popup dialog to enter into a spin box -->
     1247    <window name="SpinBoxEntryDialog">
     1248        <area>-1,-1,620,165,800x600</area>
     1249        <imagetype name="backimg">
     1250            <area>0,0,620,165,800x600</area>
     1251            <filename>mythprogressdialog-background.png</filename>
     1252        </imagetype>
     1253
     1254        <textarea name="title" from="basetextarea">
     1255            <area>5,10,600,30,800x600</area>
     1256            <multiline>yes</multiline>
     1257            <align>allcenter</align>
     1258            <value>You may enter a value here or cancel to continue using the Spinbox.</value>
     1259        </textarea>
     1260
     1261        <textedit name="entry" from="basetextedit">
     1262            <area>120,40,375,50,800x600</area>
     1263        </textedit>
     1264
     1265        <button name="cancel" from="basebutton">
     1266            <area>20,105,150,40,800x600</area>
     1267            <value>Cancel</value>
     1268        </button>
     1269
     1270        <button name="ok" from="basebutton">
     1271            <area>445,105,150,40,800x600</area>
     1272            <value>OK</value>
     1273        </button>
     1274
     1275        <textarea name="rules" from="basetextarea">
     1276            <area>210,95,200,60,800x600</area>
     1277            <multiline>yes</multiline>
     1278            <align>allcenter</align>
     1279            <template>Values from %LOW% to %HIGH% in increments of %STEP%</template>
     1280        </textarea>
     1281
     1282    </window>
     1283
    12461284    <window name="MythFileBrowser">
    12471285        <area>-1,-1,730,510</area>
    12481286        <imagetype name="backimg">