void DemoQNanoItemPainter::drawRuler(float x, float y, float w, float h, float t)
{
    float posX = x + w*0.05;
    double space = w*0.03 + sinf(t)*w*0.02;
    m_painter->setTextAlign(QNanoPainter::ALIGN_CENTER);
    m_painter->setTextBaseline(QNanoPainter::BASELINE_MIDDLE);
    // Note: Adjust font size to match QPainter sizing
    qreal fontSize = w/35.0 * 1.32 - 1;
    m_testFont.setPixelSize(fontSize);
    m_painter->setFont(m_testFont);
    m_painter->setStrokeStyle("#E0E0E0");
    m_painter->setFillStyle("#E0E0B0");
    m_painter->beginPath();
    int i = 0;
    while (posX < w) {
        m_painter->moveTo(posX, y);
        float height = h*0.2;
        QPointF textPoint(posX, y+h);
        if (i%10==0) {
            height = h*0.5;
            m_painter->fillText(QString::number(i), textPoint);
        } else if (i%5==0) {
            height = h*0.3;
            if (space > w*0.02) m_painter->fillText(QString::number(i), textPoint);
        }
        m_painter->lineTo(posX, y+height);
        posX += space;
        i++;
    }
    m_painter->setLineWidth(1.0f);
    m_painter->stroke();
}
Example #2
0
void MemChartPrivate::drawLegendText(QPainter *painter,qreal initX)
{
    painter->save();
    qreal initY = MC::Constant::TopSpace;
    qreal dY = (qreal)(height() - 2 * MC::Constant::TopSpace)/MC::Constant::GridCount;

    qreal maxTextLength = fontMetrics().width(tr("%1").arg(_max));
    qreal dValue = (qreal)(_max - _min)/MC::Constant::GridCount;
    int initValue = _max;
    QString strText;

    for(int i = 0;i <= MC::Constant::GridCount;i++)
    {
        // draw text
        strText = tr("%1").arg(initValue);
        qreal textLength = fontMetrics().width(strText);
        QPointF textPoint(initX ,initY + fontMetrics().height()/2);
        painter->drawText(textPoint,strText);
        // increment initY
        initY += dY;

        // decrement value
        initValue -= dValue;

        //
        initValue = initValue < 0 ? 0 : initValue;
    }

    painter->restore();

}
Example #3
0
// DrawSliderButton
void
SliderView::DrawSliderButton(BView* v, BRect r, int32 value,
							 const char* formatString, bool enabled)
{
	rgb_color background = ui_color(B_PANEL_BACKGROUND_COLOR);
	rgb_color light;
	rgb_color shadow;
	rgb_color button;
	rgb_color black;
	if (enabled) {
		light = tint_color(background, B_LIGHTEN_MAX_TINT);
		shadow = tint_color(background, B_DARKEN_1_TINT);
		button = tint_color(background, B_LIGHTEN_1_TINT);
		black = tint_color(background, B_DARKEN_MAX_TINT);
	} else {
		light = tint_color(background, B_LIGHTEN_1_TINT);
		shadow = tint_color(background, 1.1);
		button = tint_color(background, 0.8);
		black = tint_color(background, B_DISABLED_LABEL_TINT);
	}
	// border
	v->BeginLineArray(4);
		v->AddLine(BPoint(r.left, r.bottom),
				   BPoint(r.left, r.top), light);
		v->AddLine(BPoint(r.left + 1.0, r.top),
				   BPoint(r.right, r.top), light);
		v->AddLine(BPoint(r.right, r.top + 1.0),
				   BPoint(r.right, r.bottom), shadow);
		v->AddLine(BPoint(r.right - 1.0, r.bottom),
				   BPoint(r.left + 1.0, r.bottom), shadow);
	v->EndLineArray();
	// background & label
	r.InsetBy(1.0, 1.0);
	char label[256];
	sprintf(label, formatString, value);
	float width = v->StringWidth(label);
	font_height fh;
	v->GetFontHeight(&fh);
	BPoint textPoint((r.left + r.right) / 2.0 - width / 2.0,
					 (r.top + r.bottom) / 2.0 + fh.ascent / 2.0);
	v->SetHighColor(black);
	v->SetLowColor(button);
	v->FillRect(r, B_SOLID_LOW);
	v->DrawString(label, textPoint);
}
Example #4
0
// Draw
void
PopupSlider::Draw(BRect updateRect)
{
	bool enabled = IsEnabled();
	rgb_color background = ui_color(B_PANEL_BACKGROUND_COLOR);
	rgb_color black;
	if (enabled) {
		black = tint_color(background, B_DARKEN_MAX_TINT);
	} else {
		black = tint_color(background, B_DISABLED_LABEL_TINT);
	}
	// draw label
	BRect r(Bounds());
	r.right = fSliderButtonRect.left - 1.0;
	font_height fh;
	GetFontHeight(&fh);
	BPoint textPoint(0.0, (r.top + r.bottom) / 2.0 + fh.ascent / 2.0);
	SetLowColor(background);
	SetHighColor(black);
	FillRect(r, B_SOLID_LOW);
	DrawString(fLabel.String(), textPoint);
	// draw slider button
	DrawSlider(fSliderButtonRect, enabled);
}
void PlaylistItemDelegate::paintBody(QPainter *painter,
                                     const QStyleOptionViewItem &option,
                                     const QModelIndex &index) const {
    const bool isSelected = option.state & QStyle::State_Selected;
    if (isSelected)
        QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter);

    painter->save();
    painter->translate(option.rect.topLeft());

    QRect line(0, 0, option.rect.width(), option.rect.height());
    if (downloadInfo) line.setWidth(line.width() / 2);

    const bool isActive = index.data(ActiveTrackRole).toBool();

    // get the video metadata
    const Video *video = index.data(VideoRole).value<VideoPointer>().data();

    // draw the "current track" highlight underneath the text
    if (isActive && !isSelected) paintActiveOverlay(painter, option, line);

    // thumb
    const QPixmap &thumb = video->getThumbnail();
    if (!thumb.isNull()) {
        painter->drawPixmap(0, 0, thumb);
        if (video->getDuration() > 0) drawTime(painter, video->getFormattedDuration(), line);
    }

    const bool thumbsOnly = line.width() <= thumbWidth + 60;
    const bool isHovered = index.data(HoveredItemRole).toBool();

    // play icon overlayed on the thumb
    bool needPlayIcon = isActive;
    if (thumbsOnly) needPlayIcon = needPlayIcon && !isHovered;
    if (needPlayIcon) painter->drawPixmap(0, 0, playIcon);

    if (!thumbsOnly) {
        // text color
        if (isSelected)
            painter->setPen(QPen(option.palette.highlightedText(), 0));
        else
            painter->setPen(QPen(option.palette.text(), 0));

        // title
        QStringRef title(&video->getTitle());
        QString elidedTitle = video->getTitle();
        static const int titleFlags = Qt::AlignTop | Qt::TextWordWrap;
        QRect textBox = line.adjusted(padding + thumbWidth, padding, -padding, 0);
        textBox = painter->boundingRect(textBox, titleFlags, elidedTitle);
        while (textBox.height() > 55 && elidedTitle.length() > 10) {
#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
            title = title.left(title.length() - 1);
#elif QT_VERSION < QT_VERSION_CHECK(5, 8, 0)
            title.truncate(title.length() - 1);
#else
            title.chop(1);
#endif
            elidedTitle = title.trimmed() + QStringLiteral("…");
            textBox = painter->boundingRect(textBox, titleFlags, elidedTitle);
        }
        painter->drawText(textBox, titleFlags, elidedTitle);

        painter->setFont(smallerFont);
        painter->setOpacity(.5);
        QFontMetrics fontMetrics = painter->fontMetrics();
        static const int flags = Qt::AlignLeft | Qt::AlignTop;

        // published date
        const QString &published = video->getFormattedPublished();
        QSize textSize(fontMetrics.size(Qt::TextSingleLine, published));
        QPoint textPoint(padding + thumbWidth, padding * 2 + textBox.height());
        textBox = QRect(textPoint, textSize);
        painter->drawText(textBox, flags, published);

        bool elided = false;

        // author
        if (!listView || listView->isClickableAuthors()) {
            bool authorHovered = isHovered && index.data(AuthorHoveredRole).toBool();

            painter->save();
            painter->setFont(smallerBoldFont);
            if (!isSelected) {
                if (authorHovered)
                    painter->setPen(QPen(option.palette.brush(QPalette::Highlight), 0));
            }
            const QString &author = video->getChannelTitle();
            textPoint.setX(textBox.right() + padding);
            textSize = QSize(painter->fontMetrics().size(Qt::TextSingleLine, author));
            textBox = QRect(textPoint, textSize);
            authorRects.insert(index.row(), textBox);
            if (textBox.right() > line.width() - padding) {
                textBox.setRight(line.width());
                elided = drawElidedText(painter, textBox, flags, author);
            } else {
                painter->drawText(textBox, flags, author);
            }
            painter->restore();
        }

        // view count
        if (video->getViewCount() > 0) {
            const QString &viewCount = video->getFormattedViewCount();
            textPoint.setX(textBox.right() + padding);
            textSize = QSize(fontMetrics.size(Qt::TextSingleLine, viewCount));
            if (elided || textPoint.x() + textSize.width() > line.width() - padding) {
                textPoint.setX(thumbWidth + padding);
                textPoint.setY(textPoint.y() + textSize.height() + padding);
            }
            textBox = QRect(textPoint, textSize);
            if (textBox.bottom() <= line.height()) {
                painter->drawText(textBox, flags, viewCount);
            }
        }

        if (downloadInfo) {
            const QString &def = VideoDefinition::forCode(video->getDefinitionCode()).getName();
            textPoint.setX(textBox.right() + padding);
            textSize = QSize(fontMetrics.size(Qt::TextSingleLine, def));
            textBox = QRect(textPoint, textSize);
            painter->drawText(textBox, flags, def);
        }

    } else {
        // thumbs only
        if (isHovered) {
            painter->setFont(smallerFont);
            painter->setPen(Qt::white);
            QStringRef title(&video->getTitle());
            QString elidedTitle = video->getTitle();
            static const int titleFlags = Qt::AlignTop | Qt::TextWordWrap;
            QRect textBox(padding, padding, thumbWidth - padding * 2, thumbHeight - padding * 2);
            textBox = painter->boundingRect(textBox, titleFlags, elidedTitle);
            while (textBox.height() > 55 && elidedTitle.length() > 10) {
#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
                title = title.left(title.length() - 1);
#elif QT_VERSION < QT_VERSION_CHECK(5, 8, 0)
                title.truncate(title.length() - 1);
#else
                title.chop(1);
#endif
                elidedTitle = title.trimmed() + QStringLiteral("…");
                textBox = painter->boundingRect(textBox, titleFlags, elidedTitle);
            }
            painter->fillRect(QRect(0, 0, thumbWidth, textBox.height() + padding * 2),
                              QColor(0, 0, 0, 128));
            painter->drawText(textBox, titleFlags, elidedTitle);
        }
    }

    painter->restore();

    if (downloadInfo) paintDownloadInfo(painter, option, index);
}
Example #6
0
void MemChartPrivate::drawLegend(QPainter* painter)
{
    qreal maxTextLength = fontMetrics().width(tr("%1").arg(_max));

    painter->save();
    painter->setPen(MC::Color::TextColor);

    qreal initY = MC::Constant::TopSpace;
    qreal dY = (qreal)(height() - 2 * MC::Constant::TopSpace)/MC::Constant::GridCount;

    qreal dValue = (qreal)(_max - _min)/MC::Constant::GridCount;
    int initValue = _max;
    QString strText;

    // draw left legend
//    drawLegendText(painter,MC::Constant::ExtraSpace + maxTextLength - textLength);
    for(int i = 0;i <= MC::Constant::GridCount;i++)
    {
        // draw text
        strText = tr("%1").arg(initValue);
        qreal textLength = fontMetrics().width(strText);
        QPointF textPoint(MC::Constant::ExtraSpace + maxTextLength - textLength ,initY + fontMetrics().height()/2);
        painter->drawText(textPoint,strText);

        // draw mark
        QPointF handleLeftPoint(MC::Constant::ExtraSpace + maxTextLength,initY);
        QPointF handleRightPoint(handleLeftPoint.x() + MC::Constant::ExtraSpace,initY);
        painter->drawLine(handleLeftPoint,handleRightPoint);

        // increment initY
        initY += dY;

        // decrement value
        initValue -= dValue;


        //
        initValue = initValue < 0 ? 0 : initValue;
    }

    initY = MC::Constant::TopSpace;

    // draw right legend
    initValue = _max;
    for(int i = 0;i <= MC::Constant::GridCount;i++)
    {
        // draw text
        strText = tr("%1").arg(initValue);
        qreal textLength = fontMetrics().width(strText);
        QPointF textPoint(width() - MC::Constant::ExtraSpace - maxTextLength,initY + fontMetrics().height()/2);
        painter->drawText(textPoint,strText);

        // draw mark
        QPointF handleLeftPoint(width() - MC::Constant::ExtraSpace * 2 - maxTextLength,initY);
        QPointF handleRightPoint(handleLeftPoint.x() + MC::Constant::ExtraSpace,initY);
        painter->drawLine(handleLeftPoint,handleRightPoint);

        // increment initY
        initY += dY;

        // decrement value
        initValue -= dValue;

        //
        initValue = initValue < 0 ? 0 : initValue;
    }

    painter->restore();
}