Ticket #197: subclass_horiz_list_and_gradient.diff

File subclass_horiz_list_and_gradient.diff, 17.3 KB (added by mfgalizi@…, 19 years ago)

This is functionally the same as the previous patch, but the horizontal list is a subclass of the vertical.

  • mythlistbutton.h

     
    5656    void SetDrawOffset(QPoint off) { m_drawoffset = off; }
    5757    QPoint GetDrawOffset(void) { return m_drawoffset; }
    5858
     59  protected:
     60
     61    /* accessors used by subclasses */
     62    inline int itemMargin(void) const { return m_itemMargin; }
     63    inline int itemSpacing(void) const { return m_itemSpacing; }
     64    inline int itemHeight(void) const { return m_itemHeight; }
     65
     66    /* just needed by subclasses */
     67    void LoadPixmap(MythImage **pix, const QString& fileName);
     68
     69    /* methods that subclasses need to override */
     70    inline virtual uint itemWidth(void) const { return m_contentsRect.width(); }
     71    virtual QRect CalculateContentsRect(const QRect & arrowsRect) const;
     72    virtual void loadArrowPixmaps(MythImage **PrevReg, MythImage **PrevAct,
     73                                  MythImage **NextReg, MythImage **NextAct);
     74
     75    virtual void CalculateVisibleItems(void);
     76    virtual const QRect placeArrows(const QSize & arrowSize);
     77    virtual QPoint getButtonPosition(uint i) const;
     78
     79    /* variables subclasses need to use */
     80    QRect m_contentsRect;
     81    uint m_itemsVisible;
     82    MythUIStateType *m_upArrow;
     83    MythUIStateType *m_downArrow;
     84
    5985  signals:
    6086    void itemSelected(MythListButtonItem* item);
    6187
    6288  private:
    6389    void Init();
    64     void LoadPixmap(MythImage **pix, const QString& fileName);
    6590
    6691    void InsertItem(MythListButtonItem *item);
    6792    void RemoveItem(MythListButtonItem *item);
     
    7095
    7196    int m_order;
    7297    QRect m_rect;
    73     QRect m_contentsRect;
    7498
    7599    int m_itemHeight;
    76100    int m_itemSpacing;
    77101    int m_itemMargin;
    78     uint m_itemsVisible;
    79102
    80103    bool m_active;
    81104    bool m_showScrollArrows;
    82105    bool m_showArrow;
    83106
    84     MythUIStateType *m_upArrow;
    85     MythUIStateType *m_downArrow;
    86 
    87107    QValueVector<MythUIButton *> m_ButtonList;
    88108
    89109    QColor m_itemRegBeg;
     
    119139    friend class MythListButtonItem;
    120140};
    121141
     142class MythHorizListButton : public MythListButton
     143{
     144  public:
     145    MythHorizListButton(MythUIType *parent, const char *name,
     146                             const QRect &area, bool showArrow = true,
     147                             bool showScrollArrows = false,
     148                             uint horizontalItems = 3);
     149
     150  protected:
     151    inline void CalculateVisibleItems(void) { return; }
     152
     153    void loadArrowPixmaps(MythImage **PrevReg, MythImage **PrevAct,
     154                          MythImage **NextReg, MythImage **NextAct);
     155
     156    const QRect placeArrows(const QSize & arrowSize);
     157
     158    QRect CalculateContentsRect(const QRect & arrowsRect) const;
     159    inline uint itemWidth(void) const { return m_itemWidth; }
     160    QPoint getButtonPosition(uint i) const;
     161
     162  private:
     163    int m_itemWidth;
     164};
     165
    122166class MythListButtonItem
    123167{
    124168  public:
  • mythuitype.cpp

     
    308308    SetRedraw();
    309309}
    310310
    311 QRect MythUIType::GetArea(void)
     311QRect MythUIType::GetArea(void) const
    312312{
    313313    return m_Area;
    314314}
  • mythimage.cpp

     
    11#include <cassert>
    22
     3#include <qpainter.h>
     4#include <qapplication.h>
     5
    36#include "mythimage.h"
    47#include "mythmainwindow.h"
    58#include "mythcontext.h"
     
    5962    return ret;
    6063}
    6164
     65MythImage *MythImage::Gradient(const QSize & size, const QColor &begin,
     66                               const QColor &end, uint alpha)
     67{
     68    QImage img(size.width(), size.height(), 32);
     69    img.setAlphaBuffer(true);
     70
     71    for (int y = 0; y < img.height(); y++)
     72    {
     73        for (int x = 0; x < img.width(); x++)
     74        {
     75            uint *p = (uint *)img.scanLine(y) + x;
     76            *p = qRgba(0, 0, 0, alpha);
     77        }
     78    }
     79
     80    // calculate how much to change the colour by at each step
     81    float rstep = float(end.red() - begin.red()) /
     82                  float(img.height());
     83    float gstep = float(end.green() - begin.green()) /
     84                  float(img.height());
     85    float bstep = float(end.blue() - begin.blue()) /
     86                  float(img.height());
     87
     88    qApp->lock();
     89
     90    /* create the painter */
     91    QPixmap pix = QPixmap(img);
     92    QPainter p(&pix);
     93
     94    float r = begin.red();
     95    float g = begin.green();
     96    float b = begin.blue();
     97
     98    for (int y = 0; y < img.height(); y++)
     99    {
     100        QColor c((int)r, (int)g, (int)b);
     101        p.setPen(c);
     102        p.drawLine(0, y, img.width(), y);
     103        r += rstep;
     104        g += gstep;
     105        b += bstep;
     106    }
     107    p.setPen(Qt::black);
     108    p.drawLine(0, 0, 0, img.height() - 1);
     109    p.drawLine(0, 0, img.width() - 1, 0);
     110    p.drawLine(0, img.height() - 1, img.width() - 1, img.height() - 1);
     111    p.drawLine(img.width() - 1, 0, img.width() - 1, img.height() - 1);
     112    p.end();
     113
     114    qApp->unlock();
     115    MythImage *ret = GetMythPainter()->GetFormatImage();
     116    ret->Assign(pix);
     117    return ret;
     118}
     119
    62120// FIXME: Get rid of LoadScaleImage
    63121bool MythImage::Load(const QString &filename)
    64122{
  • mythlistbutton.cpp

     
    562562
    563563    m_initialized = true;
    564564
    565     MythPainter *painter = GetMythPainter();
    566 
    567565    QFontMetrics fm(m_fontActive->face);
    568566    QSize sz1 = fm.size(Qt::SingleLine, "XXXXX");
    569567    fm = QFontMetrics(m_fontInactive->face);
     
    577575        m_downArrow = new MythUIStateType(this, "downscrollarrow");
    578576
    579577        MythImage *upReg, *upAct, *downReg, *downAct;
    580         LoadPixmap(&upReg, "uparrow-reg");
    581         LoadPixmap(&upAct, "uparrow-sel");
    582         LoadPixmap(&downReg, "dnarrow-reg");
    583         LoadPixmap(&downAct, "dnarrow-sel");
     578        loadArrowPixmaps(&upReg, &upAct, &downReg, &downAct);
    584579
    585580        m_upArrow->AddImage(MythUIStateType::Off, upReg);
    586581        m_upArrow->AddImage(MythUIStateType::Full, upAct);
    587582        m_downArrow->AddImage(MythUIStateType::Off, downReg);
    588583        m_downArrow->AddImage(MythUIStateType::Full, downAct);
    589584
    590         m_upArrow->SetPosition(QPoint(0,
    591                                       m_Area.height() - upAct->height() - 1));
    592         m_downArrow->SetPosition(QPoint(upAct->width() + m_itemMargin,
    593                                         m_Area.height() - upAct->height() - 1));
     585        arrowsRect = placeArrows(upAct->size());
    594586
    595         arrowsRect = QRect(0, m_Area.height() - upAct->height() - 1,
    596                            m_Area.width(), upAct->height());
    597 
    598587        upAct->DownRef();
    599588        upReg->DownRef();
    600589        downReg->DownRef();
     
    602591    }
    603592    else
    604593        arrowsRect = QRect(0, 0, 0, 0);
    605        
    606     m_contentsRect = QRect(0, 0, m_Area.width(), m_Area.height() -
    607                            arrowsRect.height() - 2 * m_itemMargin);
    608594
    609     m_itemsVisible = 0;
    610     int y = 0;
     595    m_contentsRect = CalculateContentsRect(arrowsRect);
    611596
    612     while (y <= m_contentsRect.height() - m_itemHeight)
    613     {
    614         y += m_itemHeight + m_itemSpacing;
    615         m_itemsVisible++;
    616     }
     597    /* now calculate the visible items */
     598    CalculateVisibleItems();
    617599
    618600    MythImage *itemRegPix, *itemSelActPix, *itemSelInactPix;
    619601    MythImage *arrowPix, *checkNonePix, *checkHalfPix, *checkFullPix;
    620602
    621603    LoadPixmap(&checkNonePix, "check-empty");
    622604    LoadPixmap(&checkHalfPix, "check-half");
    623     LoadPixmap(&checkFullPix, "check-full");
    624    
     605    LoadPixmap(&checkFullPix, "check-full");   
    625606    LoadPixmap(&arrowPix, "arrow");
    626607
    627     QImage img(m_Area.width(), m_itemHeight, 32);
    628     img.setAlphaBuffer(true);
     608    QSize size(itemWidth(), m_itemHeight);
    629609
    630     for (int y = 0; y < img.height(); y++)
    631     {
    632         for (int x = 0; x < img.width(); x++)
    633         {
    634             uint *p = (uint *)img.scanLine(y) + x;
    635             *p = qRgba(0, 0, 0, m_itemRegAlpha);
    636         }
    637     }
     610    itemRegPix = MythImage::Gradient(size, m_itemRegBeg, m_itemRegEnd,
     611                                     m_itemRegAlpha);
     612    itemSelActPix = MythImage::Gradient(size, m_itemSelBeg, m_itemSelEnd,
     613                                        m_itemSelAlpha);
     614    itemSelInactPix = MythImage::Gradient(size, m_itemSelBeg, m_itemSelEnd,
     615                                          m_itemRegAlpha);
    638616
    639     qApp->lock();
    640 
    641     {
    642         float rstep = float(m_itemRegEnd.red() - m_itemRegBeg.red()) /
    643                       float(m_itemHeight);
    644         float gstep = float(m_itemRegEnd.green() - m_itemRegBeg.green()) /
    645                       float(m_itemHeight);
    646         float bstep = float(m_itemRegEnd.blue() - m_itemRegBeg.blue()) /
    647                       float(m_itemHeight);
    648 
    649         QPixmap itemRegPmap = QPixmap(img);
    650         QPainter p(&itemRegPmap);
    651 
    652         float r = m_itemRegBeg.red();
    653         float g = m_itemRegBeg.green();
    654         float b = m_itemRegBeg.blue();
    655         for (int y = 0; y < img.height(); y++)
    656         {
    657             QColor c((int)r, (int)g, (int)b);
    658             p.setPen(c);
    659             p.drawLine(0, y, img.width(), y);
    660             r += rstep;
    661             g += gstep;
    662             b += bstep;
    663         }
    664         p.setPen(black);
    665         p.drawLine(0, 0, 0, img.height() - 1);
    666         p.drawLine(0, 0, img.width() - 1, 0);
    667         p.drawLine(0, img.height() - 1, img.width() - 1, img.height() - 1);
    668         p.drawLine(img.width() - 1, 0, img.width() - 1, img.height() - 1);
    669         p.end();
    670 
    671         itemRegPix = painter->GetFormatImage();
    672         itemRegPix->Assign(itemRegPmap.convertToImage());
    673     }   
    674 
    675     {
    676         float rstep = float(m_itemSelEnd.red() - m_itemSelBeg.red()) /
    677                       float(m_itemHeight);
    678         float gstep = float(m_itemSelEnd.green() - m_itemSelBeg.green()) /
    679                       float(m_itemHeight);
    680         float bstep = float(m_itemSelEnd.blue() - m_itemSelBeg.blue()) /
    681                       float(m_itemHeight);
    682 
    683         QPixmap itemSelInactPmap = QPixmap(img);
    684         QPainter p(&itemSelInactPmap);
    685 
    686         float r = m_itemSelBeg.red();
    687         float g = m_itemSelBeg.green();
    688         float b = m_itemSelBeg.blue();
    689         for (int y = 0; y < img.height(); y++)
    690         {
    691             QColor c((int)r, (int)g, (int)b);
    692             p.setPen(c);
    693             p.drawLine(0, y, img.width(), y);
    694             r += rstep;
    695             g += gstep;
    696             b += bstep;
    697         }
    698         p.setPen(black);
    699         p.drawLine(0, 0, 0, img.height() - 1);
    700         p.drawLine(0, 0, img.width() - 1, 0);
    701         p.drawLine(0, img.height() - 1, img.width() - 1, img.height() - 1);
    702         p.drawLine(img.width() - 1, 0, img.width() - 1, img.height() - 1);
    703         p.end();
    704 
    705         itemSelInactPix = painter->GetFormatImage();
    706         itemSelInactPix->Assign(itemSelInactPmap.convertToImage());
    707 
    708         for (int y = 0; y < img.height(); y++)
    709         {
    710             for (int x = 0; x < img.width(); x++)
    711             {
    712                 uint *p = (uint *)img.scanLine(y) + x;
    713                 *p = qRgba(0, 0, 0, 255);
    714             }
    715         }
    716        
    717         QPixmap itemSelActPmap = QPixmap(img);
    718         p.begin(&itemSelActPmap);
    719 
    720         r = m_itemSelBeg.red();
    721         g = m_itemSelBeg.green();
    722         b = m_itemSelBeg.blue();
    723         for (int y = 0; y < img.height(); y++)
    724         {
    725             QColor c((int)r, (int)g, (int)b);
    726             p.setPen(c);
    727             p.drawLine(0, y, img.width(), y);
    728             r += rstep;
    729             g += gstep;
    730             b += bstep;
    731         }
    732         p.setPen(black);
    733         p.drawLine(0, 0, 0, img.height() - 1);
    734         p.drawLine(0, 0, img.width() - 1, 0);
    735         p.drawLine(0, img.height() - 1, img.width() - 1, img.height() - 1);
    736         p.drawLine(img.width() - 1, 0, img.width() - 1, img.height() - 1);
    737         p.end();
    738 
    739         itemSelActPix = painter->GetFormatImage();
    740         itemSelActPix->Assign(itemSelActPmap.convertToImage());
    741     }
    742 
    743     qApp->unlock();
    744 
    745617    for (int i = 0; i < (int)m_itemsVisible; i++)
    746618    {
    747619        QString name = QString("buttonlist button %1").arg(i);
     
    762634
    763635        button->SetFont(MythUIButton::Normal, *m_fontActive);
    764636        button->SetFont(MythUIButton::Disabled, *m_fontInactive);
    765 
    766         button->SetPosition(0, i * (m_itemHeight + m_itemSpacing));
     637        button->SetPosition(getButtonPosition(i));
    767638        m_ButtonList.push_back(button);
    768639    }
    769640
     
    778649    SetPositionArrowStates();
    779650}
    780651
     652QPoint MythListButton::getButtonPosition(uint i) const
     653{
     654    return QPoint(0, i * (itemHeight() + itemSpacing()));
     655}
     656
    781657void MythListButton::LoadPixmap(MythImage **pix, const QString& fileName)
    782658{
    783659    QString file = "lb-" + fileName + ".png";
     
    785661    *pix = MythImage::FromQImage(&p);
    786662}
    787663
     664void MythListButton::loadArrowPixmaps(MythImage **PrevReg, MythImage **PrevAct,
     665                                      MythImage **NextReg, MythImage **NextAct)
     666{
     667    LoadPixmap(PrevReg, "uparrow-reg");
     668    LoadPixmap(PrevAct, "uparrow-sel");
     669    LoadPixmap(NextReg, "dnarrow-reg");
     670    LoadPixmap(NextAct, "dnarrow-sel");
     671}
     672
     673const QRect MythListButton::placeArrows(const QSize & arrowSize)
     674{
     675    int y = m_Area.height() - arrowSize.height() - 1;
     676    m_upArrow->SetPosition(QPoint(0, y));
     677    m_downArrow->SetPosition(QPoint(arrowSize.width() + m_itemMargin, y));
     678
     679    return QRect(0, y, m_Area.width(), arrowSize.height());
     680}
     681
     682QRect MythListButton::CalculateContentsRect(const QRect & arrowsRect) const
     683{
     684    return QRect(0, 0, GetArea().width(), GetArea().height() -
     685                 arrowsRect.height() - 2 * itemMargin());
     686}
     687
     688void MythListButton::CalculateVisibleItems(void)
     689{
     690    int y = 0;
     691    m_itemsVisible = 0;
     692    while (y <= m_contentsRect.height() - m_itemHeight)
     693    {
     694        y += m_itemHeight + m_itemSpacing;
     695        m_itemsVisible++;
     696    }
     697}
     698
    788699//////////////////////////////////////////////////////////////////////////////
    789700
     701MythHorizListButton::MythHorizListButton(MythUIType *parent,
     702                                         const char *name, const QRect &area,
     703                                         bool showArrow, bool showScrollArrows,
     704                                         uint horizontalItems)
     705    : MythListButton(parent, name, area, showArrow, showScrollArrows)
     706{
     707    m_itemsVisible = horizontalItems;
     708}
     709
     710const QRect MythHorizListButton::placeArrows(const QSize & arrowSize)
     711{
     712    int x = GetArea().width() - arrowSize.width() - 1;
     713    int ytop = GetArea().height()/2 - itemMargin()/2 - arrowSize.height();
     714    int ybottom = GetArea().height()/2 + itemMargin()/2;
     715    m_upArrow->SetPosition(QPoint(x, ybottom));
     716    m_downArrow->SetPosition(QPoint(x, ytop));
     717
     718    /* rightmost rectangle (full height) */
     719    QRect arrowsRect(x, 0, arrowSize.width() + 1, m_Area.height());
     720
     721    /* now calculate the item width */
     722    int dx = GetArea().width() - arrowSize.width() - 1 - 2 * itemMargin() -
     723        (m_itemsVisible - 1) * itemSpacing();
     724
     725    /* set the item width */
     726    m_itemWidth = dx/m_itemsVisible;
     727
     728    return QRect(x, 0, arrowSize.width() + 1, m_Area.height());
     729}
     730
     731QRect MythHorizListButton::CalculateContentsRect(const QRect & arrowsRect) const
     732{
     733    return QRect(0, 0,
     734                 GetArea().width() - arrowsRect.width() - 2 * itemMargin(),
     735                 GetArea().height());
     736}
     737
     738QPoint MythHorizListButton::getButtonPosition(uint i) const
     739{
     740    return QPoint(i * (itemWidth() + itemSpacing()),
     741                  GetArea().height()/2 - itemHeight()/2);
     742}
     743
     744void MythHorizListButton::loadArrowPixmaps(MythImage **PrevReg,
     745                                           MythImage **PrevAct,
     746                                           MythImage **NextReg,
     747                                           MythImage **NextAct)
     748{
     749    LoadPixmap(PrevReg, "ltarrow-reg");
     750    LoadPixmap(PrevAct, "ltarrow-sel");
     751    LoadPixmap(NextReg, "rtarrow-reg");
     752    LoadPixmap(NextAct, "rtarrow-sel");
     753}
     754
     755//////////////////////////////////////////////////////////////////////////////
     756
    790757MythListButtonItem::MythListButtonItem(MythListButton* lbtype,
    791758                                       const QString& text,
    792759                                       MythImage *image, bool checkable,
  • mythuitype.h

     
    4545    virtual void SetPosition(int x, int y);
    4646    virtual void SetPosition(const QPoint &pos);
    4747    virtual void SetArea(const QRect &rect);
    48     virtual QRect GetArea(void);
     48    virtual QRect GetArea(void) const;
    4949
    5050    virtual QRect GetDirtyArea(void);
    5151
  • mythimage.h

     
    2626    // *NOTE* *DELETES* img!
    2727    static MythImage *FromQImage(QImage **img);
    2828
     29    /**
     30     * @brief Create a gradient image.
     31     * @param size The size of the image.
     32     * @param begin The beginning colour.
     33     * @param end The ending colour.
     34     * @return A MythImage filled with a gradient.
     35     *
     36     * IMPORTANT: You must call QApplication::lock and
     37     * QApplication::unlock before and after this method. since it
     38     * uses a QPainter... I guess.
     39     */
     40    static MythImage *Gradient(const QSize & size, const QColor &begin, const QColor &end, uint alpha);
     41
    2942    bool Load(const QString &filename);
    3043
    3144  protected: