CategorieButton::CategorieButton(QPixmap icon, QString name, QWidget *parent) : QWidget(parent) // This widget shows the icon and name of a menu's categorie
{
    resize(100, 100);
    isHovered = false;

    m_iconLabel = new QLabel(this);
    m_iconLabel->resize(size());
    m_iconLabel->setAlignment(Qt::AlignCenter);
    QPixmap modifiedIcon(48, 100);
    modifiedIcon.fill(Qt::transparent);
    QPainter p(&modifiedIcon);
    p.drawPixmap(0, 2, 48, 48, icon);
    QTransform t;
    p.drawPixmap(0, 50, 48, 48, icon.transformed(t.rotate(180, Qt::XAxis), Qt::SmoothTransformation));
    QLinearGradient alphaGradient(0, 0, 0, height());
    alphaGradient.setColorAt(0.0, Qt::black);
    alphaGradient.setColorAt(0.5, Qt::black);
    alphaGradient.setColorAt(0.8, Qt::transparent);
    QGraphicsOpacityEffect *iconEffect = new QGraphicsOpacityEffect(this);
    iconEffect->setOpacity(1);
    iconEffect->setOpacityMask(alphaGradient);
    m_iconLabel->setPixmap(modifiedIcon);
    m_iconLabel->setGraphicsEffect(iconEffect);

    m_nameLabel = new QLabel(name, this);
    m_nameLabel->setStyleSheet("color: white;");
    QGraphicsDropShadowEffect *nameEffect = new QGraphicsDropShadowEffect();
    nameEffect->setOffset(1);
    nameEffect->setColor(Qt::black);
    m_nameLabel->setGraphicsEffect(nameEffect);
    m_nameLabel->setAlignment(Qt::AlignCenter);
    m_nameLabel->setGeometry(0, 60, width(), m_nameLabel->height());
}
void TextLabel::drawTextLayout(QPainter *painter, const QTextLayout &layout, const QRect &rect)
{
    if (rect.width() < 1 || rect.height() < 1) {
        return;
    }

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

    QPainter p(&pixmap);
    p.setPen(painter->pen());

    // Create the alpha gradient for the fade out effect
    QLinearGradient alphaGradient(0, 0, 1, 0);
    alphaGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
    if (layout.textOption().textDirection() == Qt::LeftToRight) {
        alphaGradient.setColorAt(0, QColor(0, 0, 0, 255));
        alphaGradient.setColorAt(1, QColor(0, 0, 0, 0));
    } else {
        alphaGradient.setColorAt(0, QColor(0, 0, 0, 0));
        alphaGradient.setColorAt(1, QColor(0, 0, 0, 255));
    }

    QFontMetrics fm(layout.font());
    int textHeight = layout.lineCount() * fm.lineSpacing();

    QPointF position(0, (rect.height() - textHeight) / 2);
    QList<QRect> fadeRects;
    int fadeWidth = 30;

    // Draw each line in the layout
    for (int i = 0; i < layout.lineCount(); i++) {
        QTextLine line = layout.lineAt(i);
        line.draw(&p, position);

        // Add a fade out rect to the list if the line is too long
        if (line.naturalTextWidth() > rect.width())
        {
            int x = int(qMin(line.naturalTextWidth(), (qreal)pixmap.width())) - fadeWidth;
            int y = int(line.position().y() + position.y());
            QRect r = QStyle::visualRect(layout.textOption().textDirection(), pixmap.rect(),
                                         QRect(x, y, fadeWidth, int(line.height())));
            fadeRects.append(r);
        }
    }

    // Reduce the alpha in each fade out rect using the alpha gradient
    if (!fadeRects.isEmpty()) {
        p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
        foreach (const QRect &rect, fadeRects) {
            p.fillRect(rect, alphaGradient);
        }
Exemple #3
0
void MirrorItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * /*widget*/)
{
    // TODO: real HQ rendering, skipping the pixmap (better result)!

    // generate Reflection pixmap, if needed
    if (m_dirty || m_pixmap.isNull() || RenderOpts::HQRendering) {

        // change pixmap size to match the bounding rect
        if (m_pixmap.size() != m_boundingRect.size().toSize())
            m_pixmap = QPixmap(m_boundingRect.width(), m_boundingRect.height());

        // clear pixmap
        m_pixmap.fill(Qt::transparent);

        // find out the Transform chain to mirror a rotated item
        QRectF sceneRectF = m_source->mapToScene(m_source->boundingRect()).boundingRect();
        QTransform tFromItem = m_source->transform() * QTransform(1, 0, 0, 1, m_source->pos().x(), m_source->pos().y());
        QTransform tFromPixmap = QTransform(1, 0, 0, -1.0, sceneRectF.left(), sceneRectF.bottom());
        QTransform tItemToPixmap = tFromItem * tFromPixmap.inverted();

        // draw the transformed item onto the pixmap
        QPainter p(&m_pixmap);
        p.setRenderHint(QPainter::Antialiasing, true);
        p.setTransform(tItemToPixmap, true);
        m_source->paint(&p, 0, 0);
        p.end();

        // add a linear alpha channel to the image
        QPixmap alphaPixmap(m_pixmap.size());
        QPainter alphaPainter(&alphaPixmap);
        QLinearGradient alphaGradient(0, 0, 0, alphaPixmap.height());
        alphaGradient.setColorAt(0.0 , QColor(128, 128, 128));
        alphaGradient.setColorAt(0.50, QColor( 32,  32,  32));
        alphaGradient.setColorAt(1.0 , QColor(  0,   0,   0));
        alphaPainter.fillRect(alphaPixmap.rect(), alphaGradient);
        alphaPainter.end();
        m_pixmap.setAlphaChannel(alphaPixmap);

        // reset dirty
        m_dirty = false;
    }

    // draw the reflection pixmap
    if (!option)
        painter->drawPixmap(0, 0, m_pixmap);
    else
        painter->drawPixmap(option->rect, m_pixmap, option->rect);
}
Exemple #4
0
bool ElideFadeDrawer::eventFilter(QObject *obj, QEvent *event) {
	if ( event->type() == QEvent::Paint ) {
		QLabel *q = static_cast<QLabel*>(obj);
		QPainter painter(q);
		QFontMetrics fm(q->font());
		QRect rect = q->contentsRect();
		int flags = q->alignment() | Qt::TextSingleLine;

		if ( fm.width(q->text()) > rect.width() ) {
			//QPixmap pixmap(rect.size());//, QImage::Format_ARGB32);
			QImage pixmap(rect.size(), QImage::Format_ARGB32);
			pixmap.fill(Qt::transparent);

			QPainter p(&pixmap);
			p.setPen(painter.pen());
			p.setFont(painter.font());

			/*
			QLinearGradient gradient(rect.topLeft(), rect.topRight());
			QColor from = q->palette().color(QPalette::WindowText);
			QColor to = from;
			to.setAlpha(0);
			gradient.setColorAt(0.8, from);
			gradient.setColorAt(1.0, to);
			p.setPen(QPen(gradient, 0));
			*/
			p.drawText(pixmap.rect(), flags, q->text());

			QLinearGradient alphaGradient(rect.topLeft(), rect.topRight());
			alphaGradient.setColorAt(0.8, QColor(0,0,0,255));
			alphaGradient.setColorAt(1.0, QColor(0,0,0,0));
			p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
			p.fillRect(pixmap.rect(), alphaGradient);

			painter.drawImage(rect.topLeft(), pixmap);
		}
		else {
			painter.setPen(q->palette().color(QPalette::WindowText));
			painter.drawText(rect, flags, q->text());
		}
		
		return true;
	}

	// standard event processing
	return QObject::eventFilter(obj, event);
}
Exemple #5
0
void Carton::paintFaceReflectionTexture(QPainter *painter, Faces face)
{
    if (m_specularityValue <= 0)
        return;

    const Faces emittingFace =
        face == FrontReflection?Front
        :face == BackReflection?Back
        :face == LeftReflection?Left
        :/* face == RightReflection? */Right;

    const QSizeF faceSize(this->faceSize(face));
    const qreal faceWith = faceSize.width();
    const qreal faceHeight = faceSize.height();
    const qreal reflectionHeight = faceHeight/100 * m_specularityValue;
    const qreal remainingFaceHeight = faceHeight - reflectionHeight;
    QImage blendImage(QSizeF(faceWith, reflectionHeight).toSize(), QImage::Format_ARGB32);
    QPainter blendPainter(&blendImage);
    blendPainter.translate(0, -remainingFaceHeight);
    paintFaceTexture(&blendPainter, emittingFace);
    blendPainter.end();

    QImage alphaImage(QSizeF(faceWith, reflectionHeight).toSize(), QImage::Format_ARGB32);
    QPainter alphaPainter(&alphaImage);
    QLinearGradient alphaGradient(0, 0, 0, faceHeight);
    alphaGradient.setColorAt(0, Qt::black);
    alphaGradient.setColorAt(1, Qt::lightGray);
    alphaPainter.setPen(Qt::NoPen);
    alphaPainter.fillRect(QRectF(0, 0, faceWith, reflectionHeight), alphaGradient);
    blendImage.setAlphaChannel(alphaImage);
    alphaPainter.end();

    painter->save();
    painter->drawImage(0, int(remainingFaceHeight), blendImage);
    painter->restore();
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void FilterParameterWidget::fadeWidget(QWidget* widget, bool in)
{
#if 0
  if (m_FaderWidget)
  {
    m_FaderWidget->close();
  }
  m_FaderWidget = new FaderWidget(widget);
  if(in)
  {
    setVisible(true);
    m_FaderWidget->setFadeIn();
    connect(m_FaderWidget, SIGNAL(animationComplete() ),
            this, SLOT(show()));
    connect(m_FaderWidget, SIGNAL(animationComplete()),
            this, SLOT(showBorder()));

    widget->setStyleSheet("border: 2px solid MediumBlue;");
  }
  else
  {
    m_FaderWidget->setFadeOut();
    connect(m_FaderWidget, SIGNAL(animationComplete() ),
            this, SLOT(hide()));
  }

  m_FaderWidget->setStartColor(palette().color(QWidget::backgroundRole()));
  m_FaderWidget->start();
#else
  fadeIn = in;

  QString styleSheet;
  QTextStream ss (&styleSheet);
  ss << "QFrame#" << this->objectName() << " { ";
  ss << "background-color: rgb(223, 183, 175);";
  ss << "border: 0px Solid;";
  ss << "border-color: rgb(223, 183, 175);";
  ss << "border-radius: 5px;";
  ss <<  "}";

  if(!fadeIn)
  {
    startValue = detail::Max;
    endValue = 0;
  }
  else
  {
    widget->show();
    widget->setStyleSheet(styleSheet);
    //qDebug() << styleSheet();

    fadeIn = in;
    startValue = 0;
    endValue = detail::Max;
  }

  QRect rect = geometry();
  QLinearGradient alphaGradient(rect.topLeft(), rect.bottomLeft());
  alphaGradient.setColorAt(0.5, Qt::black);
  alphaGradient.setColorAt(1.0, Qt::black);

  if(!effect)
  {
    effect = new QGraphicsOpacityEffect(this);
    effect->setOpacityMask(alphaGradient);
    //this->setGraphicsEffect(effect);
  }

  if(!animation)
  {
    animation = new QPropertyAnimation(effect, "opacity", this);
    animation->setDuration(detail::Duration);
    connect(animation, SIGNAL(finished()),
            this, SLOT(animationFinished()));
    connect(animation, SIGNAL(finished()),
            this, SLOT(showBorder()));
  }

  animation->setStartValue(startValue);
  animation->setEndValue(endValue);

  animation->start();

#endif

}
Exemple #7
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());
    }
}