예제 #1
0
파일: paintutils.cpp 프로젝트: vasi/kdelibs
QPixmap texturedText(const QString &text, const QFont &font, Plasma::Svg *texture)
{
    QFontMetrics fm(font);
    //the text will be moved a bit from contentsRect
    QRect contentsRect = fm.boundingRect(text).adjusted(0, 0, 2, 2);
    contentsRect.moveTo(0,0);

    QPixmap pixmap(contentsRect.size());
    pixmap.fill(Qt::transparent);

    QPainter buffPainter(&pixmap);
    buffPainter.setPen(Qt::black);

    buffPainter.setFont(font);
    buffPainter.drawText(contentsRect, Qt::AlignCenter, text);
    buffPainter.setCompositionMode(QPainter::CompositionMode_SourceIn);
    texture->paint(&buffPainter, contentsRect, "foreground");
    buffPainter.end();

    //do the shadow
    QImage image(pixmap.size() + QSize(2, 2), QImage::Format_ARGB32_Premultiplied);
    image.fill(Qt::transparent);
    buffPainter.begin(&image);
    buffPainter.setFont(font);
    buffPainter.drawText(contentsRect.translated(1, 1), Qt::AlignCenter, text);
    buffPainter.setCompositionMode(QPainter::CompositionMode_SourceIn);
    texture->paint(&buffPainter, contentsRect.adjusted(-1, -1, 1, 1), "shadow");
    buffPainter.end();

    expblur<16, 7>(image, 1);
    //hole in the shadow
    buffPainter.begin(&image);
    buffPainter.setCompositionMode(QPainter::CompositionMode_DestinationOut);
    buffPainter.setFont(font);
    buffPainter.drawText(contentsRect.translated(1, 1), Qt::AlignCenter, text);
    buffPainter.end();

    QPixmap ret(image.size());
    ret.fill(Qt::transparent);
    buffPainter.begin(&ret);
    buffPainter.setCompositionMode(QPainter::CompositionMode_SourceOver);
    buffPainter.drawImage(QPoint(0,0), image);
    buffPainter.drawPixmap(QPoint(1,1), pixmap);
    return ret;
}
예제 #2
0
void PushButton::paint(QPainter *painter,
                       const QStyleOptionGraphicsItem *option,
                       QWidget *widget)
{
    if (!styleSheet().isNull() || Theme::defaultTheme()->useNativeWidgetStyle()) {
        QGraphicsProxyWidget::paint(painter, option, widget);
        return;
    }

    QPixmap bufferPixmap;

    //Normal button, pressed or not
    if (isEnabled()) {
        if (nativeWidget()->isDown() || nativeWidget()->isChecked()) {
            d->background->setElementPrefix("pressed");
        } else {
            d->background->setElementPrefix("normal");
        }

    //flat or disabled
    } else if (!isEnabled() || nativeWidget()->isFlat()) {
        bufferPixmap = QPixmap(rect().size().toSize());
        bufferPixmap.fill(Qt::transparent);

        QPainter buffPainter(&bufferPixmap);
        d->background->paintFrame(&buffPainter);
        buffPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
        buffPainter.fillRect(bufferPixmap.rect(), QColor(0, 0, 0, 128));

        painter->drawPixmap(0, 0, bufferPixmap);
    }

    //if is under mouse draw the animated glow overlay
    if (!nativeWidget()->isDown() && !nativeWidget()->isChecked() && isEnabled() && acceptHoverEvents() && d->background->hasElementPrefix("active")) {
        if (d->hoverAnimation->state() == QAbstractAnimation::Running && !isUnderMouse() && !nativeWidget()->isDefault()) {
            d->background->setElementPrefix("active");
            d->background->paintFrame(painter, d->activeRect.topLeft());
        } else {
            painter->drawPixmap(
                d->activeRect.topLeft(),
                d->hoverAnimation->property("currentPixmap").value<QPixmap>());
        }
    } else if (isEnabled()) {
        d->background->paintFrame(painter);
    }


    painter->setPen(Plasma::Theme::defaultTheme()->color(Theme::ButtonTextColor));

    if (nativeWidget()->isDown()) {
        painter->translate(QPoint(1, 1));
    }

    QRectF rect = contentsRect();

    if (!nativeWidget()->icon().isNull()) {
        const qreal iconSize = qMin(rect.width(), rect.height());
        QPixmap iconPix = nativeWidget()->icon().pixmap(iconSize);
        if (!isEnabled()) {
            KIconEffect *effect = KIconLoader::global()->iconEffect();
            iconPix = effect->apply(iconPix, KIconLoader::Toolbar, KIconLoader::DisabledState);
        }

        QRect pixmapRect;
        if (nativeWidget()->text().isEmpty()) {
            pixmapRect = nativeWidget()->style()->alignedRect(option->direction, Qt::AlignCenter, iconPix.size(), rect.toRect());
        } else {
            pixmapRect = nativeWidget()->style()->alignedRect(option->direction, Qt::AlignLeft|Qt::AlignVCenter, iconPix.size(), rect.toRect());
        }
        painter->drawPixmap(pixmapRect.topLeft(), iconPix);

        if (option->direction == Qt::LeftToRight) {
            rect.adjust(rect.height(), 0, 0, 0);
        } else {
            rect.adjust(0, 0, -rect.height(), 0);
        }
    }

    QFontMetricsF fm(font());
    // If the height is too small increase the Height of the button to shall the whole text #192988
    if (rect.height() < fm.height()) {
        rect.setHeight(fm.height());
        rect.moveTop(boundingRect().center().y() - rect.height() / 2);
    }

    // If there is not enough room for the text make it to fade out
    if (rect.width() < fm.width(nativeWidget()->text())) {
        if (bufferPixmap.isNull()) {
            bufferPixmap = QPixmap(rect.size().toSize());
        }
        bufferPixmap.fill(Qt::transparent);

        QPainter p(&bufferPixmap);
        p.setPen(painter->pen());
        p.setFont(font());

        // Create the alpha gradient for the fade out effect
        QLinearGradient alphaGradient(0, 0, 1, 0);
        alphaGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
        if (option->direction == Qt::LeftToRight) {
            alphaGradient.setColorAt(0, QColor(0, 0, 0, 255));
            alphaGradient.setColorAt(1, QColor(0, 0, 0, 0));
            p.drawText(bufferPixmap.rect(), Qt::AlignLeft|Qt::AlignVCenter|Qt::TextShowMnemonic,
                       nativeWidget()->text());
        } else {
            alphaGradient.setColorAt(0, QColor(0, 0, 0, 0));
            alphaGradient.setColorAt(1, QColor(0, 0, 0, 255));
            p.drawText(bufferPixmap.rect(), Qt::AlignRight|Qt::AlignVCenter|Qt::TextShowMnemonic,
                       nativeWidget()->text());
        }

        p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
        p.fillRect(bufferPixmap.rect(), alphaGradient);

        painter->drawPixmap(rect.topLeft(), bufferPixmap);
    } else {
        painter->setFont(font());
        painter->drawText(rect, Qt::AlignCenter|Qt::TextShowMnemonic, nativeWidget()->text());
    }
}