예제 #1
0
QSize BoardItemDelegate::sizeHint(const QStyleOptionViewItem & option,
                                  const QModelIndex & index) const
{
    QVariant var = index.model()->data(index);

    if (qVariantCanConvert<BoardItem>(var))
    {
        BoardItem item = qvariant_cast<BoardItem>(var);
        //вычислить размер
        //QFontMetrics fm(option.font);
        if (item.image.isNull())
        {
            //просто текст
            QFont fn = option.font;
            fn.setBold(true);
            QFontMetrics fmCapt(fn);

            QRect rect = option.rect;
            rect = rect.adjusted(12, 0, 0, 0);
            QRect rectCapt = fmCapt.boundingRect(rect,
                                                 Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap,
                                                 item.caption);

            rect = option.rect;
            fn.setBold(false);
            QFontMetrics fmText(fn);
            rect = rect.adjusted(12, rectCapt.height(), -12, 0);
            QRect rectText = fmText.boundingRect(rect,
                                                 Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap,
                                                 item.text);

            QSize sz(option.rect.width(),
                     rectCapt.height() + rectText.height() + fmText.height());
            return sz;
        }
        else
        {
            QFontMetrics fm(option.font);
            //текст с картинкой
            QRect imgRect = item.image.rect();
            //вычислить область под текст
            QRect rect = option.rect;
            rect = rect.adjusted(imgRect.width() + 6, 0, -12, 0);
            rect = fm.boundingRect(rect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap,
                                   item.caption + "\n" + item.text);

            QSize sz(option.rect.width(), rect.height() + 12);
            //размер текста меньше чем картинка
            if (sz.height() < imgRect.height() + fm.height())
            {
                //выровнять по картинке
                return QSize(option.rect.width(), imgRect.height() + 2*fm.height());
            }
            else
            {
                //вернуть размер по тексту
                return sz;
            }
        }
    }

    return QStyledItemDelegate::sizeHint(option, index);
}
void
PlayableItemWidget::paintEvent( QPaintEvent* event )
{   
    static QPixmap m_radio_left_hover = QPixmap( ":/meta_radio_LEFT_HOVER.png" );
    static QPixmap m_radio_left_press = QPixmap( ":/meta_radio_LEFT_PRESS.png" );
    static QPixmap m_radio_left_rest = QPixmap( ":/meta_radio_LEFT_REST.png" );

    static QPixmap m_radio_middle_hover = QPixmap( ":/meta_radio_MIDDLE_HOVER.png" );
    static QPixmap m_radio_middle_press = QPixmap( ":/meta_radio_MIDDLE_PRESS.png" );
    static QPixmap m_radio_middle_rest = QPixmap( ":/meta_radio_MIDDLE_REST.png" );

    static QPixmap m_radio_right_hover = QPixmap( ":/meta_radio_RIGHT_HOVER.png" );
    static QPixmap m_radio_right_press = QPixmap( ":/meta_radio_RIGHT_PRESS.png" );
    static QPixmap m_radio_right_rest = QPixmap( ":/meta_radio_RIGHT_REST.png" );

    static QPixmap m_radio_small_hover = QPixmap( ":/radio_play_small_HOVER.png" );
    static QPixmap m_radio_small_press = QPixmap( ":/radio_play_small_PRESS.png" );
    static QPixmap m_radio_small_rest = QPixmap( ":/radio_play_small_REST.png" );

    if ( m_style == DescriptionNone )
    {
        QPushButton::paintEvent( event );
    }
    else if ( m_style == DescriptionElide )
    {
        QPainter p(this);

        QTextOption to;
        to.setAlignment( Qt::AlignLeft | Qt::AlignVCenter );

        QRect textRect = contentsRect().adjusted( 30, 0, 0, 0 );
        p.drawText( textRect, fontMetrics().elidedText( text(), Qt::ElideRight, textRect.width() ), to );

        if ( isDown() )
            p.drawPixmap( rect().topLeft(), m_radio_small_press );
        else if ( m_hovered )
            p.drawPixmap( rect().topLeft(), m_radio_small_hover );
        else
            p.drawPixmap( rect().topLeft(), m_radio_small_rest );
    }
    else if ( m_style == DescriptionBottom )
    {
        QPushButton::paintEvent( event );

        QPainter p( this );

        p.setPen( QColor( 0x898989 ) );

        QFont font = p.font();
        font.setPixelSize( 12 );
        p.setFont( font );

        QTextOption to;
        to.setAlignment( Qt::AlignBottom );

        QFontMetrics fm( font );

        QRect rect = contentsRect();
        rect.adjust( 54, 0, 0, -14 );
        p.drawText( rect, fm.elidedText( m_description, Qt::ElideRight, rect.width() ), to );
    }
    else if ( m_style == ThreePart )
    {
        QPainter p( this );

        QRect middleRect = QRect( m_radio_left_rest.width(), 0, rect().width() - m_radio_left_rest.width() - m_radio_right_rest.width(), 49 );

        if ( isDown() )
        {
            p.drawPixmap( rect().topLeft(), m_radio_left_press );
            p.drawPixmap( middleRect, m_radio_middle_press );
            p.drawPixmap( rect().topLeft() + QPoint( rect().width() - m_radio_right_press.width(), 0 ), m_radio_right_press );
        }
        else if ( m_hovered )
        {
            p.drawPixmap( rect().topLeft(), m_radio_left_hover );
            p.drawPixmap( middleRect, m_radio_middle_hover );
            p.drawPixmap( rect().topLeft() + QPoint( rect().width() - m_radio_right_hover.width(), 0 ), m_radio_right_hover );
        }
        else
        {
            p.drawPixmap( rect().topLeft(), m_radio_left_rest );
            p.drawPixmap( middleRect, m_radio_middle_rest );
            p.drawPixmap( rect().topLeft() + QPoint( rect().width() - m_radio_right_rest.width(), 0 ), m_radio_right_rest );
        }

        QFontMetrics fmText( font() );
        QRect textRect = rect().adjusted( 40, 8, -20, 0 );
        p.drawText( textRect, fmText.elidedText( text(), Qt::ElideRight, textRect.width() ) );

        p.setPen( QColor( 0x898989 ) );

        QFont font = p.font();
        font.setPixelSize( 10 );
        p.setFont( font );

        QFontMetrics fmDesc( font );
        QRect descRect = rect().adjusted( 40, 28, -20, 0 );
        p.drawText( descRect, fmDesc.elidedText( m_description, Qt::ElideRight, descRect.width() ) );
    }
}