Ticket #6139: buttonlistfix.diff

File buttonlistfix.diff, 14.0 KB (added by Isaac Richards, 15 years ago)
  • libs/libmythui/mythuibuttonlist.h

     
    143143    uint ItemWidth(void) const { return m_itemWidth; }
    144144    uint ItemHeight(void) const { return m_itemHeight; }
    145145
    146     bool MoveItemUpDown(MythUIButtonListItem *item, bool flag);
     146    bool MoveItemUpDown(MythUIButtonListItem *item, bool up);
    147147
    148148    void SetAllChecked(MythUIButtonListItem::CheckState state);
    149149
     
    221221    bool m_needsUpdate;
    222222    bool m_clearing;
    223223
    224     MythUIButtonListItem* m_topItem;
    225     MythUIButtonListItem* m_selItem;
    226 
    227224    int m_selPosition;
    228225    int m_topPosition;
    229226    int m_itemCount;
     227    bool m_keepSelAtBottom;
    230228
    231229    QList<MythUIButtonListItem*> m_itemList;
    232230
  • libs/libmythui/mythuibuttonlist.cpp

     
    4343    m_active         = false;
    4444    m_drawFromBottom = false;
    4545
    46     m_topItem = 0;
    47     m_selItem = 0;
    4846    m_selPosition = 0;
    4947    m_topPosition = 0;
    5048    m_itemCount = 0;
     
    118116
    119117    m_clearing = false;
    120118
    121     m_topItem     = NULL;
    122     m_selItem     = NULL;
    123119    m_selPosition = 0;
    124120    m_topPosition = 0;
    125121    m_itemCount   = 0;
     
    150146    {
    151147        int button = 0;
    152148
     149        // set topitem, top position
     150        if (m_selPosition < 0 || m_selPosition > m_itemList.size())
     151        {
     152            if (m_wrapStyle > WrapNone)
     153                m_selPosition = m_itemList.size() - 1;
     154            else
     155                m_selPosition = 0;
     156        }
     157
     158        switch (m_scrollStyle)
     159        {
     160            case ScrollCenter:
     161                m_topPosition = qMax(m_selPosition - (int)((float)m_itemsVisible / 2), 0);
     162                break;
     163            case ScrollFree:
     164            {
     165                int adjust = 0;
     166                if (m_topPosition == -1 || m_keepSelAtBottom)
     167                {
     168                    if (m_topPosition == -1)
     169                        m_topPosition = 0;
     170                    if (m_layout == LayoutHorizontal)
     171                        adjust = 1 - m_itemsVisible;
     172                    else
     173                        adjust = m_columns - m_itemsVisible;
     174                    m_keepSelAtBottom = false;
     175                }
     176
     177                if (m_selPosition < m_topPosition ||
     178                    m_topPosition + (int)m_itemsVisible <= m_selPosition)
     179                {
     180                    if (m_layout == LayoutHorizontal)
     181                        m_topPosition = m_selPosition + adjust;
     182                    else
     183                        m_topPosition = (m_selPosition + adjust) /
     184                                        (m_columns * m_columns);
     185                }
     186
     187                m_topPosition = qMax(m_topPosition, 0);
     188                break;
     189            }
     190        }   
     191
    153192        QList<MythUIButtonListItem*>::iterator it = m_itemList.begin() + m_topPosition;
    154193
    155194        if (m_scrollStyle == ScrollCenter)
     
    157196            if (m_selPosition <= (int)(m_itemsVisible/2))
    158197            {
    159198                button = (m_itemsVisible / 2) - m_selPosition;
    160                 if (m_wrapStyle == WrapItems && button > 0)
     199                if (m_wrapStyle == WrapItems && button > 0 &&
     200                    m_itemCount >= (int)m_itemsVisible)
    161201                {
    162202                    it = m_itemList.end() - button;
    163203                    button = 0;
     
    182222        if (it < m_itemList.begin())
    183223            it = m_itemList.begin();
    184224
     225        int curItem = GetItemPos(*it);
    185226        while (it < m_itemList.end() && button < (int)m_itemsVisible)
    186227        {
    187228            realButton = m_ButtonList[button];
    188229            buttonItem = *it;
    189230
    190             if (!realButton || ! buttonItem)
     231            if (!realButton || !buttonItem)
    191232                break;
    192233
    193234            bool selected = false;
    194             if (!seenSelected && (buttonItem == m_selItem))
     235            if (!seenSelected && (curItem == m_selPosition))
    195236            {
    196237                seenSelected = true;
    197238                selected = true;
     
    201242            buttonItem->SetToRealButton(realButton, selected);
    202243            realButton->SetVisible(true);
    203244
    204             if ((m_wrapStyle == WrapItems) && (it == (m_itemList.end()-1)))
     245            if (m_wrapStyle == WrapItems && it == (m_itemList.end()-1) &&
     246                m_itemCount >= (int)m_itemsVisible)
     247            {
    205248                it = m_itemList.begin();
     249                curItem = 0;
     250            }
    206251            else
     252            {
    207253                ++it;
     254                ++curItem;
     255            }
     256
    208257            button++;
    209258        }
    210259
     
    223272    }
    224273    else
    225274    {
    226         if (m_topItem != m_itemList.first())
     275        if (m_topPosition != 0)
    227276            m_upArrow->DisplayState(MythUIStateType::Full);
    228277        else
    229278            m_upArrow->DisplayState(MythUIStateType::Off);
     
    244293
    245294    if (wasEmpty)
    246295    {
    247         m_topItem = item;
    248         m_selItem = item;
    249296        m_selPosition = m_topPosition = 0;
    250 
    251297        emit itemSelected(item);
    252298    }
    253299
     
    263309    if (curIndex == -1)
    264310        return;
    265311
    266     if (item == m_topItem)
     312    if (curIndex == m_topPosition &&
     313        m_topPosition > 0 &&
     314        m_topPosition == m_itemCount - 1)
    267315    {
    268         if (m_topItem != m_itemList.last())
    269         {
    270             m_topItem = *(m_itemList.begin() + m_topPosition + 1);
    271         }
    272         else if (m_topItem != m_itemList.first())
    273         {
    274             --m_topPosition;
    275             m_topItem = *(m_itemList.begin() + m_topPosition);
    276         }
    277         else
    278         {
    279             m_topItem = NULL;
    280             m_topPosition = 0;
    281         }
     316        m_topPosition--;
    282317    }
    283318
    284     if (item == m_selItem)
     319    if (curIndex == m_selPosition &&
     320        m_selPosition > 0 &&
     321        m_selPosition == m_itemCount - 1)
    285322    {
    286         if (m_selItem != m_itemList.last())
    287         {
    288             m_selItem = *(m_itemList.begin() + m_selPosition + 1);
    289         }
    290         else if (m_selItem != m_itemList.first())
    291         {
    292             --m_selPosition;
    293             m_selItem = *(m_itemList.begin() + m_selPosition);
    294         }
    295         else
    296         {
    297             m_selItem = NULL;
    298             m_selPosition = 0;
    299         }
     323        m_selPosition--;
    300324    }
    301325
    302326    m_itemList.removeAt(curIndex);
     
    304328
    305329    Update();
    306330
    307     if (m_selItem)
    308         emit itemSelected(m_selItem);
     331    if (m_selPosition < m_itemCount)
     332        emit itemSelected(m_itemList.at(m_selPosition));
    309333}
    310334
    311335void MythUIButtonList::SetValueByData(QVariant data)
     
    326350
    327351void MythUIButtonList::SetItemCurrent(MythUIButtonListItem* item)
    328352{
     353    int newIndex = m_itemList.indexOf(item);
     354    SetItemCurrent(newIndex);
     355}
     356
     357void MythUIButtonList::SetItemCurrent(int current)
     358{
    329359    if (!m_initialized)
    330360        Init();
    331361
    332     if (m_selItem == item)
     362    if (current == -1 || current >= m_itemList.size())
    333363        return;
    334364
    335     m_selPosition = m_itemList.indexOf(item);
     365    if (current == m_selPosition)
     366        return;
    336367
    337     if (m_selPosition == -1)
    338         m_selPosition = 0;
    339 
    340     m_selItem = m_itemList.at(m_selPosition);
    341 
     368    m_selPosition = current;
    342369    if (m_itemsVisible == 0)
    343370        return;
    344371
    345     m_topPosition = 0;
     372    m_topPosition = -1;
    346373
    347     switch (m_scrollStyle)
    348     {
    349         case ScrollCenter :
    350             while (m_topPosition + (int)((float)m_itemsVisible/2) <
    351                         m_selPosition)
    352                 ++m_topPosition;
    353             break;
    354         case ScrollFree :
    355             if (m_topPosition + (int)m_itemsVisible <= m_selPosition)
    356             {
    357                 if (m_layout == LayoutHorizontal)
    358                     m_topPosition = m_selPosition - m_itemsVisible + 1;
    359                 else
    360                     m_topPosition = (m_selPosition - m_itemsVisible + m_columns)
    361                                         / m_columns * m_columns;
    362 
    363                 m_topPosition = qMax(0, m_topPosition);
    364             }
    365             break;
    366     }
    367 
    368     m_topItem = m_itemList.at(m_topPosition);
    369 
    370374    Update();
    371375
    372     emit itemSelected(m_selItem);
     376    emit itemSelected(GetItemCurrent());
    373377}
    374378
    375 void MythUIButtonList::SetItemCurrent(int current)
    376 {
    377     if (current >= m_itemList.size())
    378         return;
    379 
    380     MythUIButtonListItem* item = m_itemList.at(current);
    381     if (!item && !m_itemList.empty())
    382         item = m_itemList[0];
    383 
    384     if (item)
    385         SetItemCurrent(item);
    386 }
    387 
    388379MythUIButtonListItem* MythUIButtonList::GetItemCurrent() const
    389380{
    390     return m_selItem;
     381    return m_itemList.at(m_selPosition);
    391382}
    392383
    393384int MythUIButtonList::GetIntValue()
    394385{
    395     if (GetItemCurrent())
    396         return GetItemCurrent()->GetText().toInt();
     386    MythUIButtonListItem *item = GetItemCurrent();
     387    if (item)
     388        return item->GetText().toInt();
    397389
    398390    return 0;
    399391}
    400392
    401393QString MythUIButtonList::GetValue()
    402394{
    403     if (GetItemCurrent())
    404         return GetItemCurrent()->GetText();
     395    MythUIButtonListItem *item = GetItemCurrent();
     396    if (item)
     397        return item->GetText();
    405398
    406399    return QString();
    407400}
     
    511504            break;
    512505    }
    513506
    514     if (m_selPosition < 0 || m_selPosition >= m_itemList.size())
     507    if (pos != m_selPosition)
    515508    {
    516         if (m_wrapStyle > WrapNone)
    517             m_selPosition = m_itemList.size() - 1;
    518         else
    519             m_selPosition = 0;
     509        Update();
     510        emit itemSelected(GetItemCurrent());
    520511    }
    521512
    522     m_selItem = m_itemList.at(m_selPosition);
    523 
    524     switch (m_scrollStyle)
    525     {
    526         case ScrollCenter :
    527             m_topPosition = qMax(m_selPosition - (int)((float)m_itemsVisible/2), 0);
    528             m_topItem = m_itemList.at(m_topPosition);
    529             break;
    530         case ScrollFree :
    531             if (m_selPosition < m_topPosition ||
    532                 m_topPosition + (int)m_itemsVisible <= m_selPosition)
    533             {
    534                 if (m_layout == LayoutHorizontal)
    535                     m_topPosition = m_selPosition;
    536                 else
    537                     m_topPosition = m_selPosition / m_columns * m_columns;
    538             }
    539             break;
    540     }
    541 
    542     m_topItem = m_itemList.at(m_topPosition);
    543 
    544     Update();
    545 
    546     if (pos != m_selPosition)
    547         emit itemSelected(m_selItem);
    548 
    549513    return true;
    550514}
    551515
     
    591555            break;
    592556    }
    593557
    594     if (m_selPosition < 0)
    595         m_selPosition = 0;
    596 
    597     if (m_selPosition >= m_itemList.size())
    598         m_selPosition = m_itemList.size()-1;
    599 
    600     m_selItem = m_itemList.at(m_selPosition);
    601 
    602     switch (m_scrollStyle)
     558    if (pos != m_selPosition)
    603559    {
    604         case ScrollCenter :
    605             while (m_topPosition + (int)((float)m_itemsVisible/2) <
    606                         m_selPosition)
    607                 ++m_topPosition;
    608             break;
    609         case ScrollFree :
    610             if (m_topPosition + (int)m_itemsVisible <= m_selPosition ||
    611                 m_topPosition > m_selPosition)
    612             {
    613                 if (m_layout == LayoutHorizontal)
    614                     m_topPosition = m_selPosition - m_itemsVisible + 1;
    615                 else
    616                     m_topPosition = (m_selPosition - m_itemsVisible + m_columns)
    617                                         / m_columns * m_columns;
    618 
    619                 m_topPosition = std::max(0, m_topPosition);
    620             }
    621             break;
     560        m_keepSelAtBottom = true;
     561        Update();
     562        emit itemSelected(GetItemCurrent());
    622563    }
    623564
    624     m_topItem = m_itemList.at(m_topPosition);
    625 
    626     Update();
    627 
    628     if (pos != m_selPosition)
    629         emit itemSelected(m_selItem);
    630 
    631565    return true;
    632566}
    633567
     
    656590    if (!found_it || m_selPosition == selectedPosition)
    657591        return false;
    658592
    659     m_selPosition = selectedPosition;
    660     m_selItem =  m_itemList.at(m_selPosition);
    661 
    662     m_topPosition = 0;
    663 
    664     switch (m_scrollStyle)
    665     {
    666         case ScrollCenter :
    667             while (m_topPosition + (int)((float)m_itemsVisible/2) <
    668                         m_selPosition)
    669                 ++m_topPosition;
    670             break;
    671         case ScrollFree :
    672             if (m_topPosition + (int)m_itemsVisible <= m_selPosition)
    673             {
    674                 if (m_layout == LayoutHorizontal)
    675                     m_topPosition = m_selPosition - m_itemsVisible + 1;
    676                 else
    677                     m_topPosition = (m_selPosition - m_itemsVisible + m_columns)
    678                                         / m_columns * m_columns;
    679 
    680                 m_topPosition = qMax(0, m_topPosition);
    681             }
    682             break;
    683     }
    684 
    685     m_topItem = m_itemList.at(m_topPosition);
    686 
    687     Update();
    688 
    689     emit itemSelected(m_selItem);
    690 
     593    SetItemCurrent(selectedPosition);
    691594    return true;
    692595}
    693596
    694 bool MythUIButtonList::MoveItemUpDown(MythUIButtonListItem *item, bool flag)
     597bool MythUIButtonList::MoveItemUpDown(MythUIButtonListItem *item, bool up)
    695598{
    696     if (item != m_selItem)
    697     {
     599    if (GetItemCurrent() != item)
    698600        return false;
    699     }
    700 
    701     if (item == m_itemList.first() && flag)
     601    if (item == m_itemList.first() && up)
    702602        return false;
    703     if (item == m_itemList.last() && !flag)
     603    if (item == m_itemList.last() && !up)
    704604        return false;
    705605
    706606    int oldpos = m_selPosition;
    707607    int insertat = 0;
    708608    bool dolast = false;
    709609
    710     if (flag)
     610    if (up)
    711611    {
    712612        insertat = m_selPosition - 1;
    713613        if (item == m_itemList.last())
     
    715615        else
    716616            ++m_selPosition;
    717617
    718         if (item == m_topItem)
     618        if (item == m_itemList.at(m_topPosition))
    719619            ++m_topPosition;
    720620    }
    721621    else
    722622        insertat = m_selPosition + 1;
    723623
    724624    m_itemList.removeAt(oldpos);
    725 
    726625    m_itemList.insert(insertat, item);
    727626
    728     if (flag)
     627    if (up)
    729628    {
    730629        MoveUp();
    731630        if (!dolast)
     
    1076975    m_wrapStyle = lb->m_wrapStyle;
    1077976
    1078977    m_clearing = false;
    1079     m_topItem = m_selItem = NULL;
    1080 
    1081978    m_selPosition = m_topPosition = m_itemCount = 0;
    1082979
    1083980    MythUIType::CopyFrom(base);