Ticket #197: subclass_horiz_list_and_gradient.diff
File subclass_horiz_list_and_gradient.diff, 17.3 KB (added by , 19 years ago) |
---|
-
mythlistbutton.h
56 56 void SetDrawOffset(QPoint off) { m_drawoffset = off; } 57 57 QPoint GetDrawOffset(void) { return m_drawoffset; } 58 58 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 59 85 signals: 60 86 void itemSelected(MythListButtonItem* item); 61 87 62 88 private: 63 89 void Init(); 64 void LoadPixmap(MythImage **pix, const QString& fileName);65 90 66 91 void InsertItem(MythListButtonItem *item); 67 92 void RemoveItem(MythListButtonItem *item); … … 70 95 71 96 int m_order; 72 97 QRect m_rect; 73 QRect m_contentsRect;74 98 75 99 int m_itemHeight; 76 100 int m_itemSpacing; 77 101 int m_itemMargin; 78 uint m_itemsVisible;79 102 80 103 bool m_active; 81 104 bool m_showScrollArrows; 82 105 bool m_showArrow; 83 106 84 MythUIStateType *m_upArrow;85 MythUIStateType *m_downArrow;86 87 107 QValueVector<MythUIButton *> m_ButtonList; 88 108 89 109 QColor m_itemRegBeg; … … 119 139 friend class MythListButtonItem; 120 140 }; 121 141 142 class 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 122 166 class MythListButtonItem 123 167 { 124 168 public: -
mythuitype.cpp
308 308 SetRedraw(); 309 309 } 310 310 311 QRect MythUIType::GetArea(void) 311 QRect MythUIType::GetArea(void) const 312 312 { 313 313 return m_Area; 314 314 } -
mythimage.cpp
1 1 #include <cassert> 2 2 3 #include <qpainter.h> 4 #include <qapplication.h> 5 3 6 #include "mythimage.h" 4 7 #include "mythmainwindow.h" 5 8 #include "mythcontext.h" … … 59 62 return ret; 60 63 } 61 64 65 MythImage *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 62 120 // FIXME: Get rid of LoadScaleImage 63 121 bool MythImage::Load(const QString &filename) 64 122 { -
mythlistbutton.cpp
562 562 563 563 m_initialized = true; 564 564 565 MythPainter *painter = GetMythPainter();566 567 565 QFontMetrics fm(m_fontActive->face); 568 566 QSize sz1 = fm.size(Qt::SingleLine, "XXXXX"); 569 567 fm = QFontMetrics(m_fontInactive->face); … … 577 575 m_downArrow = new MythUIStateType(this, "downscrollarrow"); 578 576 579 577 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); 584 579 585 580 m_upArrow->AddImage(MythUIStateType::Off, upReg); 586 581 m_upArrow->AddImage(MythUIStateType::Full, upAct); 587 582 m_downArrow->AddImage(MythUIStateType::Off, downReg); 588 583 m_downArrow->AddImage(MythUIStateType::Full, downAct); 589 584 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()); 594 586 595 arrowsRect = QRect(0, m_Area.height() - upAct->height() - 1,596 m_Area.width(), upAct->height());597 598 587 upAct->DownRef(); 599 588 upReg->DownRef(); 600 589 downReg->DownRef(); … … 602 591 } 603 592 else 604 593 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);608 594 609 m_itemsVisible = 0; 610 int y = 0; 595 m_contentsRect = CalculateContentsRect(arrowsRect); 611 596 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(); 617 599 618 600 MythImage *itemRegPix, *itemSelActPix, *itemSelInactPix; 619 601 MythImage *arrowPix, *checkNonePix, *checkHalfPix, *checkFullPix; 620 602 621 603 LoadPixmap(&checkNonePix, "check-empty"); 622 604 LoadPixmap(&checkHalfPix, "check-half"); 623 LoadPixmap(&checkFullPix, "check-full"); 624 605 LoadPixmap(&checkFullPix, "check-full"); 625 606 LoadPixmap(&arrowPix, "arrow"); 626 607 627 QImage img(m_Area.width(), m_itemHeight, 32); 628 img.setAlphaBuffer(true); 608 QSize size(itemWidth(), m_itemHeight); 629 609 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); 638 616 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 745 617 for (int i = 0; i < (int)m_itemsVisible; i++) 746 618 { 747 619 QString name = QString("buttonlist button %1").arg(i); … … 762 634 763 635 button->SetFont(MythUIButton::Normal, *m_fontActive); 764 636 button->SetFont(MythUIButton::Disabled, *m_fontInactive); 765 766 button->SetPosition(0, i * (m_itemHeight + m_itemSpacing)); 637 button->SetPosition(getButtonPosition(i)); 767 638 m_ButtonList.push_back(button); 768 639 } 769 640 … … 778 649 SetPositionArrowStates(); 779 650 } 780 651 652 QPoint MythListButton::getButtonPosition(uint i) const 653 { 654 return QPoint(0, i * (itemHeight() + itemSpacing())); 655 } 656 781 657 void MythListButton::LoadPixmap(MythImage **pix, const QString& fileName) 782 658 { 783 659 QString file = "lb-" + fileName + ".png"; … … 785 661 *pix = MythImage::FromQImage(&p); 786 662 } 787 663 664 void 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 673 const 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 682 QRect MythListButton::CalculateContentsRect(const QRect & arrowsRect) const 683 { 684 return QRect(0, 0, GetArea().width(), GetArea().height() - 685 arrowsRect.height() - 2 * itemMargin()); 686 } 687 688 void 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 788 699 ////////////////////////////////////////////////////////////////////////////// 789 700 701 MythHorizListButton::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 710 const 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 731 QRect MythHorizListButton::CalculateContentsRect(const QRect & arrowsRect) const 732 { 733 return QRect(0, 0, 734 GetArea().width() - arrowsRect.width() - 2 * itemMargin(), 735 GetArea().height()); 736 } 737 738 QPoint MythHorizListButton::getButtonPosition(uint i) const 739 { 740 return QPoint(i * (itemWidth() + itemSpacing()), 741 GetArea().height()/2 - itemHeight()/2); 742 } 743 744 void 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 790 757 MythListButtonItem::MythListButtonItem(MythListButton* lbtype, 791 758 const QString& text, 792 759 MythImage *image, bool checkable, -
mythuitype.h
45 45 virtual void SetPosition(int x, int y); 46 46 virtual void SetPosition(const QPoint &pos); 47 47 virtual void SetArea(const QRect &rect); 48 virtual QRect GetArea(void) ;48 virtual QRect GetArea(void) const; 49 49 50 50 virtual QRect GetDirtyArea(void); 51 51 -
mythimage.h
26 26 // *NOTE* *DELETES* img! 27 27 static MythImage *FromQImage(QImage **img); 28 28 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 29 42 bool Load(const QString &filename); 30 43 31 44 protected: