Exemple #1
0
void Lighting::animate()
{
    angle += (M_PI / 30);
    qreal xs = 200 * sin(angle) - 40 + 25;
    qreal ys = 200 * cos(angle) - 40 + 25;
    m_lightSource->setPos(xs, ys);

    for (int i = 0; i < m_items.size(); ++i) {
        QGraphicsItem *item = m_items.at(i);
        Q_ASSERT(item);

        QGraphicsDropShadowEffect *effect = static_cast<QGraphicsDropShadowEffect *>(item->graphicsEffect());
        Q_ASSERT(effect);

        QPointF delta(item->x() - xs, item->y() - ys);
        effect->setOffset(delta.toPoint() / 30);

        qreal dx = delta.x();
        qreal dy = delta.y();
        qreal dd = sqrt(dx * dx + dy * dy);
        QColor color = effect->color();
        color.setAlphaF(qBound(0.4, 1 - dd / 200.0, 0.7));
        effect->setColor(color);
    }

    m_scene.update();
}
Exemple #2
0
void CustomLabel::paintEvent(QPaintEvent *pe)
{
    if ((!text().isEmpty()) &&
            (textFormat() == Qt::PlainText ||
             (textFormat() == Qt::AutoText && !Qt::mightBeRichText(text()))))
    {
        QPainter painter(this);
#ifndef DEBUG_CUSTOMLABEL
        QRectF lr = contentsRect();
        lr.moveBottom(lr.bottom() - 1); // angry and dirty hack!
        QStyleOption opt;
        opt.initFrom(this);

        int align = QStyle::visualAlignment(text().isRightToLeft() ? Qt::RightToLeft : Qt::LeftToRight, alignment());
        int flags = align | (!text().isRightToLeft() ? Qt::TextForceLeftToRight : Qt::TextForceRightToLeft);
        if (wordWrap())
            flags |= Qt::TextWordWrap;
        switch (shadowType)
        {
        case NoShadow:
            flags |= TF_NOSHADOW;
            break;
        case DarkShadow:
            flags |= TF_DARKSHADOW;
            break;
        case LightShadow:
            flags |= TF_LIGHTSHADOW;
            break;
        default:
            break;
        }
        QString textToDraw = elidedText();
        style()->drawItemText(&painter, lr.toRect(), flags, opt.palette, isEnabled(), textToDraw, QPalette::WindowText);
#else // DEBUG_CUSTOMLABEL
        QTextDocument *doc = textDocument();
        QAbstractTextDocumentLayout::PaintContext ctx = textDocumentPaintContext(doc);
        QString shadowKey;
        switch (shadowType)
        {
        case DarkShadow:
            shadowKey = GFX_TEXTSHADOWS;
            break;
        case LightShadow:
            shadowKey = GFX_NOTICEWIDGET;
            break;
        case NoShadow:
        default:
            break;
        }

        // magic numbers
        int dx = -2;
        int dy = -2;
        // adding margins
        dx += contentsMargins().left();
        dy += contentsMargins().top();

# if 1 // for debug set 0
        QGraphicsDropShadowEffect *shadow = qobject_cast<QGraphicsDropShadowEffect *>(GraphicsEffectsStorage::staticStorage(RSR_STORAGE_GRAPHICSEFFECTS)->getFirstEffect(shadowKey));
# else // debug shadow
        QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect;
        shadow->setColor(Qt::red);
        shadow->setOffset(1, 1);
# endif
        if (shadow)
        {
# if 0 // for "image method" set 1
            QImage shadowedText(size(), QImage::Format_ARGB32_Premultiplied);
#  if defined(Q_WS_MAC) && !defined(__MAC_OS_X_NATIVE_FULLSCREEN)
            // TODO: fix that
            shadowedText.fill(Qt::red); // DUNNO WHY!!!
#  else
            shadowedText.fill(Qt::transparent);
#  endif
            QPainter tmpPainter(&shadowedText);
            tmpPainter.setRenderHint(QPainter::Antialiasing);
            tmpPainter.setRenderHint(QPainter::HighQualityAntialiasing);
            tmpPainter.setRenderHint(QPainter::TextAntialiasing);
            tmpPainter.setRenderHint(QPainter::SmoothPixmapTransform);
            tmpPainter.translate(dx, dy);
            doc->documentLayout()->draw(&tmpPainter, ctx);
            painter.drawImage(0, 0, shadowedText);
# else // text method
            QPalette origPal = ctx.palette;
            ctx.palette.setColor(QPalette::Text, shadow->color());

            // draw shadow
            painter.save();
            painter.translate(dx + shadow->xOffset(), dy + shadow->yOffset());
            doc->documentLayout()->draw(&painter, ctx);
            painter.restore();

            ctx.palette = origPal;

            // draw text
            painter.save();
            painter.translate(dx, dy);
            doc->documentLayout()->draw(&painter, ctx);
            painter.restore();
# endif // shadow method
        }
        else
        {
            painter.save();
            painter.translate(dx, dy);
            doc->documentLayout()->draw(&painter, ctx);
            painter.restore();
        }
        doc->deleteLater();
#endif // DEBUG_CUSTOMLABEL
    }
    else
        QLabel::paintEvent(pe);
}