Esempio n. 1
0
void TPanelTitleBar::paintEvent(QPaintEvent *) {
  QPainter painter(this);
  QRect rect = this->rect();

  bool isPanelActive;

  TPanel *dw = qobject_cast<TPanel *>(parentWidget());
  Q_ASSERT(dw != 0);
  // docked panel
  if (!dw->isFloating()) {
    isPanelActive = dw->widgetInThisPanelIsFocused();
    qDrawBorderPixmap(&painter, rect, QMargins(3, 3, 3, 3),
                      (isPanelActive) ? m_activeBorderPm : m_borderPm);
  }
  // floating panel
  else {
    isPanelActive = isActiveWindow();
    qDrawBorderPixmap(
        &painter, rect, QMargins(3, 3, 3, 3),
        (isPanelActive) ? m_floatActiveBorderPm : m_floatBorderPm);
  }

  if (dw->getOrientation() == TDockWidget::vertical) {
    QString titleText = painter.fontMetrics().elidedText(
        dw->windowTitle(), Qt::ElideRight, rect.width() - 50);

    painter.setBrush(Qt::NoBrush);
    painter.setPen(isPanelActive ? m_activeTitleColor : m_titleColor);
    painter.drawText(QPointF(10, 15), titleText);
  }

  if (dw->isFloating()) {
    const static QPixmap closeButtonPixmap(
        svgToPixmap(":/Resources/pane_close.svg", QSize(18, 18)));
    const static QPixmap closeButtonPixmapOver(
        svgToPixmap(":/Resources/pane_close_rollover.svg", QSize(18, 18)));

    QPoint closeButtonPos(rect.right() - 18, rect.top() + 1);

    if (m_closeButtonHighlighted)
      painter.drawPixmap(closeButtonPos, closeButtonPixmapOver);
    else
      painter.drawPixmap(closeButtonPos, closeButtonPixmap);
  }

  painter.end();
}
void MScalableImagePrivate::drawScalable1(qreal x, qreal y, qreal w, qreal h, QPainter *painter) const
{
    QMargins margins = m_preferredMargins;

#if QT_VERSION < QT_VERSION_CHECK(4,8,0)
    if ((tileRules.horizontal != Qt::StretchTile || tileRules.vertical != Qt::StretchTile)) {
        //There is a known bug in Qt 4.7 qDrawBorderPixmap/QPainter which
        //can cause graphical glitches if the chosen borders result in
        //zero-sized blocks on the image, so as a workaround the borders
        //must be altered to prevent that in cases where rendering is done
        //using qDrawBorderPixmap.

        // the margins are always zero for Scalable1, so just change them to 1
        margins = QMargins(1,1,1,1);
    }
#endif

#if defined(M_OS_MAEMO5)
    // don't use smooth pixmap transformation on the N900, as this
    // decreases the performance
    if (tileRules.horizontal == Qt::StretchTile && tileRules.vertical == Qt::StretchTile)
        painter->drawPixmap(QRectF(x, y, w, h), *m_image, m_image->rect());
    else
        qDrawBorderPixmap(painter, QRect(x, y, w, h), margins, *m_image, m_image->rect(), margins, tileRules);
#else
    //the image is used in its native size
    //no need to scale just draw it
    if (m_image->size() == QSizeF(w, h)) {
        painter->drawPixmap(QRectF(x, y, w, h), *m_image, m_image->rect());
    }
    //the image needs some scaling, draw the image using smooth scaling
    else {
        if (!downscaleWarningPrinted && (w < m_image->size().width() || h < m_image->size().height()))
            outputDownscaleWarning("MScalableImage1", w, h);
        else if(!nearscaleWarningPrinted && qAbs(m_image->size().width()/w-1.0) < SCALE_WARN_LIMIT && qAbs(m_image->size().height()/h-1.0) < SCALE_WARN_LIMIT)
            outputNearscaleWarning("MScalableImage1", w, h);
        bool enabled = painter->renderHints() & QPainter::SmoothPixmapTransform;
        painter->setRenderHint(QPainter::SmoothPixmapTransform);
        if (tileRules.horizontal == Qt::StretchTile && tileRules.vertical == Qt::StretchTile)
            painter->drawPixmap(QRectF(x, y, w, h), *m_image, m_image->rect());
        else
            qDrawBorderPixmap(painter, QRect(x, y, w, h), margins, *m_image, m_image->rect(), margins, tileRules);
        painter->setRenderHint(QPainter::SmoothPixmapTransform, enabled);
    }
#endif //defined(M_OS_MAEMO5)
}
void QDeclarativeBorderImage::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *)
{
    Q_D(QDeclarativeBorderImage);
    if (d->pix.isNull() || d->width() <= 0.0 || d->height() <= 0.0)
        return;

    bool oldAA = p->testRenderHint(QPainter::Antialiasing);
    bool oldSmooth = p->testRenderHint(QPainter::SmoothPixmapTransform);
    QTransform oldTransform;
    if (d->smooth)
        p->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform, d->smooth);
    if (d->mirror) {
        oldTransform = p->transform();
        QTransform mirror;
        mirror.translate(d->width(), 0).scale(-1, 1.0);
        p->setWorldTransform(mirror * oldTransform);
    }

    const QDeclarativeScaleGrid *border = d->getScaleGrid();
    int left = border->left();
    int right = border->right();
    qreal borderWidth = left + right;
    if (borderWidth > 0.0 && d->width() < borderWidth) {
        qreal diff = borderWidth - d->width() - 1;
        left -= qRound(diff * qreal(left) / borderWidth);
        right -= qRound(diff * qreal(right) / borderWidth);
    }
    int top = border->top();
    int bottom = border->bottom();
    qreal borderHeight = top + bottom;
    if (borderHeight > 0.0 && d->height() < borderHeight) {
        qreal diff = borderHeight - d->height() - 1;
        top -= qRound(diff * qreal(top) / borderHeight);
        bottom -= qRound(diff * qreal(bottom) / borderHeight);
    }
    QMargins margins(left, top, right, bottom);
    QTileRules rules((Qt::TileRule)d->horizontalTileMode, (Qt::TileRule)d->verticalTileMode);
    qDrawBorderPixmap(p, QRect(0, 0, (int)d->width(), (int)d->height()), margins, d->pix, d->pix.rect(), margins, rules);
    if (d->smooth) {
        p->setRenderHint(QPainter::Antialiasing, oldAA);
        p->setRenderHint(QPainter::SmoothPixmapTransform, oldSmooth);
    }
    if (d->mirror)
        p->setWorldTransform(oldTransform);
}
void QDeclarativeBorderImage::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *)
{
    Q_D(QDeclarativeBorderImage);
    if (d->pix.isNull())
        return;

    bool oldAA = p->testRenderHint(QPainter::Antialiasing);
    bool oldSmooth = p->testRenderHint(QPainter::SmoothPixmapTransform);
    if (d->smooth)
        p->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform, d->smooth);

    const QDeclarativeScaleGrid *border = d->getScaleGrid();
    QMargins margins(border->left(), border->top(), border->right(), border->bottom());
    QTileRules rules((Qt::TileRule)d->horizontalTileMode, (Qt::TileRule)d->verticalTileMode);
    qDrawBorderPixmap(p, QRect(0, 0, (int)d->width(), (int)d->height()), margins, d->pix, d->pix.rect(), margins, rules);
    if (d->smooth) {
        p->setRenderHint(QPainter::Antialiasing, oldAA);
        p->setRenderHint(QPainter::SmoothPixmapTransform, oldSmooth);
    }
}
void BorderImage::paintEvent(QPaintEvent *) {
    Q_D(BorderImage);
    
    if ((d->pix.isNull()) || (this->width() < 0) || (this->height() < 0)) {
        return;
    }
    
    QPainter painter(this);
    painter.setOpacity(this->opacity());
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform, this->smooth());
    
    if (this->mirror()) {
        QTransform mirror;
        mirror.translate(this->width(), 0).scale(-1, 1.0);
        painter.setTransform(mirror);
    }
    
    int left = d->imageBorder ? d->imageBorder->left() : 0;
    int right = d->imageBorder ? d->imageBorder->right() : 0;
    int borderWidth = left + right;
    
    if ((borderWidth > 0) && (this->width() < borderWidth)) {
        int diff = borderWidth - this->width() - 1;
        left -= qRound(diff * left / borderWidth);
        right -= qRound(diff * right / borderWidth);
    }
    
    int top = d->imageBorder ? d->imageBorder->top() : 0;
    int bottom = d->imageBorder ? d->imageBorder->bottom() : 0;
    int borderHeight = top + bottom;
    
    if ((borderHeight > 0) && (this->height() < borderHeight)) {
        int diff = borderHeight - this->height() - 1;
        top -= qRound(diff * top / borderHeight);
        bottom -= qRound(diff * bottom / borderHeight);
    }
    
    QMargins margins(left, top, right, bottom);
    QTileRules rules((Qt::TileRule)this->horizontalTileMode(), (Qt::TileRule)this->verticalTileMode());
    qDrawBorderPixmap(&painter, QRect(0, 0, this->width(), this->height()), margins, d->pix, d->pix.rect(), margins, rules);
}
Esempio n. 6
0
void paint_QPixmapCachedRoundedRect(QPainter &p)
{
    static bool first = true;
    static QPixmap cacheRect;
    if (first) {
        const int pw = 0;
        const int radius = 8;
        cacheRect = QPixmap(radius*2 + 3 + pw*2, radius*2 + 3 + pw*2);
        cacheRect.fill(Qt::transparent);
        QPainter paint(&cacheRect);
        paint.setRenderHint(QPainter::Antialiasing);
        paint.setPen(Qt::black);
        paint.setBrush(Qt::red);
        if (pw%2)
            paint.drawRoundedRect(QRectF(qreal(pw)/2+1, qreal(pw)/2+1, cacheRect.width()-(pw+1), cacheRect.height()-(pw+1)), radius, radius);
        else
            paint.drawRoundedRect(QRectF(qreal(pw)/2, qreal(pw)/2, cacheRect.width()-pw, cacheRect.height()-pw), radius, radius);

        first = false;
    }
    for (int i = 0; i < count; i++) {
        for (int j = 0; j < lines; ++j) {
            QSize size((j+1)*50, spacing-1);

            p.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform, true);

            const int pw = 0;

            int xOffset = (cacheRect.width()-1)/2;
            int yOffset = (cacheRect.height()-1)/2;

            QMargins margins(xOffset, yOffset, xOffset, yOffset);
            QTileRules rules(Qt::StretchTile, Qt::StretchTile);
            //NOTE: even though our item may have qreal-based width and height, qDrawBorderPixmap only supports QRects
            qDrawBorderPixmap(&p, QRect(-pw/2, j*spacing-pw/2, size.width()+pw, size.height()+pw), margins, cacheRect, cacheRect.rect(), margins, rules);
        }
    }
}
void MScalableImagePrivate::drawScalable9(qreal x, qreal y, qreal w, qreal h, QPainter *painter) const
{
    QMargins margins = m_preferredMargins;

    if (w == -1 )
        w = m_image->width();
    if (h == -1 )
        h = m_image->height();

    int cornerWidth = 0;
    int cornerHeight = 0;
    if (m_imageType == MScalable9 ) {
        cornerWidth = margins.left() + margins.right();
        cornerHeight = margins.top() + margins.bottom();
    }

    //Make sure that the size of the drawn image is
    //bigger than the 4 corner blocks. If necessary,
    //use smaller border values than those set with setBorders API
    //call.
    if (w <= cornerWidth) {
        margins.setLeft(qMax((qreal)0.0, margins.left() - (cornerWidth - w + 1) / 2));
        margins.setRight(qMax((qreal)0.0, margins.right() - (cornerWidth - w + 1) / 2));
    }
    if (h <= cornerHeight) {
        margins.setTop(qMax((qreal)0.0, margins.top() - (cornerHeight - h + 1) / 2));
        margins.setBottom(qMax((qreal)0.0, margins.bottom() - (cornerHeight - h + 1) / 2));
    }

    if (w <= 0 || h <= 0) {
        // this should really not happen
        mWarning("MScalableImage") <<
                                   "Received request to draw pixmap of invalid size" << w << "x" << h;
        return;
    }

    //the image is used in its native size
    //no need to scale just draw it
    QSize requiredSize(w, h);
    if (m_image->size() == requiredSize) {
        painter->drawPixmap(x, y, *m_image);
        return;
    }
    else {
#if QT_VERSION < QT_VERSION_CHECK(4,8,0)
        //There is a known bug in Qt 4.7 qDrawBorderPixmap/QPainter which
        //can cause graphical glitches if the chosen borders result in
        //zero-sized blocks on the image, so as a workaround the borders
        //must be altered to prevent that in cases where rendering is done
        //using qDrawBorderPixmap.
        if (margins.top() == 0)
            margins.setTop(1);
        if (margins.left() == 0)
            margins.setLeft(1);

        if (margins.bottom() == 0)
            margins.setBottom(1);
        if (margins.right() == 0)
            margins.setRight(1);
#endif

#ifdef __arm__
        if (!downscaleWarningPrinted && (w < m_image->size().width() || h < m_image->size().height()))
            outputDownscaleWarning("MScalableImage9", w, h);
        else if(!nearscaleWarningPrinted && qAbs(m_image->size().width()/w-1.0) < SCALE_WARN_LIMIT && qAbs(m_image->size().height()/h-1.0) < SCALE_WARN_LIMIT)
            outputNearscaleWarning("MScalableImage9", w, h);
        qDrawBorderPixmap(painter, QRect(x, y, w, h), margins, *m_image, m_image->rect(), margins, tileRules);
#else
        //the image doesn't fit directly into the required size.
        //check whether or not we're allowed to cache
        bool docache = painter->paintEngine()->type() != QPaintEngine::OpenGL
                       && painter->paintEngine()->type() != QPaintEngine::OpenGL2;

        if (docache) {
            //software rendering is not fast when scaling pixmaps, so we use the
            //global pixmap cache to avoid rescaling more than needed.
            //
            //TODO: this cache gets thrashed a bit with widgetsgallery, may want to look into possibly
            //increasing the size of QPixmapCache.
            QString key = QString("msi-%1-%2,%3").arg((*m_image).cacheKey()).arg(w).arg(h);
            QPixmap scaled;
            if (QPixmapCache::find(key, &scaled)) {
                //cached! draw and we're done
                painter->drawPixmap(x, y, scaled);
                return;
            }

            // draw into cache
            scaled = QPixmap(requiredSize);
            scaled.fill(Qt::transparent);
            QPainter p;
            if (p.begin(&scaled)) {
                p.setRenderHint(QPainter::SmoothPixmapTransform);
                qDrawBorderPixmap(&p, QRect(0, 0, w, h), margins, *m_image, m_image->rect(), margins, tileRules);
                p.end();
            }

            // draw to screen
            painter->drawPixmap(x, y, scaled);
            QPixmapCache::insert(key, scaled);

            // remember the key so that the entry can be removed on-demand
            const_cast<MScalableImagePrivate*>(this)->cachedImageKey = key;
        } else {
            if (!downscaleWarningPrinted && (w < m_image->size().width() || h < m_image->size().height()))
                outputDownscaleWarning("MScalableImage9", w, h);
            else if(!nearscaleWarningPrinted && qAbs(m_image->size().width()/w-1.0) < SCALE_WARN_LIMIT && qAbs(m_image->size().height()/h-1.0) < SCALE_WARN_LIMIT)
                outputNearscaleWarning("MScalableImage9", w, h);
            // caching isn't permitted for this case; scale and render direct to screen.
            qDrawBorderPixmap(painter, QRect(x, y, w, h), margins, *m_image, m_image->rect(), margins, tileRules);

            if (!cachedImageKey.isEmpty()) {
                QPixmapCache::remove(cachedImageKey);
                const_cast<MScalableImagePrivate*>(this)->cachedImageKey.clear();
            }
        }
#endif // __arm__
    }
}
void ContextPaneWidgetImage::setPixmap(const QString &fileName)
{
    QPixmap pix(76,76);
    pix.fill(Qt::black);

    if (m_borderImage) {
        QString localFileName(fileName);
        if (QFile(fileName).exists()) {
            if (fileName.endsWith(QLatin1String("sci"))) {
                QString pixmapFileName;
                int left = 0;
                int right = 0;
                int top = 0;
                int bottom = 0;

                Qt::TileRule horizontalTileRule;
                Qt::TileRule verticalTileRule;
                if (parseSciFile(fileName, pixmapFileName, left, right, top, bottom, horizontalTileRule, verticalTileRule)) {
                    localFileName = QFileInfo(fileName).absoluteDir().absolutePath() + '/' + pixmapFileName;
                    previewDialog()->previewLabel()->setMargins(left, top, right, bottom);
                } else { // sci file not parsed correctly
                    uiBorderImage->sizeLabel->setText("");
                    return;
                }
            }
            QPixmap source(localFileName);
            if (source.isNull())
                source = pix;
            previewDialog()->setPixmap(source, previewDialog()->zoom());
            uiBorderImage->sizeLabel->setText(QString::number(source.width()) + 'x' + QString::number(source.height()));
            QPainter p(&pix);
            Qt::TileRule horizontalTileMode = Qt::StretchTile;
            Qt::TileRule verticalTileMode = Qt::StretchTile;
            if (uiBorderImage->horizontalTileRadioButton->isChecked())
                horizontalTileMode =Qt::RepeatTile;
            if (uiBorderImage->horizontalTileRadioButtonNoCrop->isChecked())
                horizontalTileMode =Qt::RoundTile;
            if (uiBorderImage->verticalTileRadioButton->isChecked())
                verticalTileMode =Qt::RepeatTile;
            if (uiBorderImage->verticalTileRadioButtonNoCrop->isChecked())
                verticalTileMode =Qt::RoundTile;
            QTileRules rules(horizontalTileMode, verticalTileMode);
            QMargins margins(previewDialog()->previewLabel()->leftMarging() ,previewDialog()->previewLabel()->topMarging() ,previewDialog()->previewLabel()->rightMarging(), previewDialog()->previewLabel()->bottomMarging());
            qDrawBorderPixmap(&p, QRect(0, 0, 76, 76), margins, source, source.rect(), margins, rules);
            //p.drawPixmap(0,0,76,76, source);
        } else {
            uiBorderImage->sizeLabel->setText("");
        }
        uiBorderImage->label->setPixmap(pix);
    } else {
        if (QFile(fileName).exists()) {
            QPixmap source(fileName);
            previewDialog()->setPixmap(source, 1);
            ui->sizeLabel->setText(QString::number(source.width()) + 'x' + QString::number(source.height()));
            QPainter p(&pix);
            if (ui->stretchRadioButton->isChecked()) {
                p.drawPixmap(0,0,76,76, source);
            } else if (ui->tileRadioButton->isChecked()) {
                QPixmap small = source.scaled(38,38);
                p.drawTiledPixmap(0,0,76,76, small);
            } else if (ui->horizontalStretchRadioButton->isChecked()) {
                QPixmap small = source.scaled(38,38);
                QPixmap half = pix.scaled(38, 76);
                QPainter p2(&half);
                p2.drawTiledPixmap(0,0,38,76, small);
                p.drawPixmap(0,0,76,76, half);
            } else if (ui->verticalStretchRadioButton->isChecked()) {
                QPixmap small = source.scaled(38,38);
                QPixmap half = pix.scaled(76, 38);
                QPainter p2(&half);
                p2.drawTiledPixmap(0,0,76,38, small);
                p.drawPixmap(0,0,76,76, half);
            } else if (ui->preserveAspectFitRadioButton->isChecked()) {
                QPixmap preserved = source.scaledToWidth(76);
                int offset = (76 - preserved.height()) / 2;
                p.drawPixmap(0, offset, 76, preserved.height(), source);
            } else if (ui->cropAspectFitRadioButton->isChecked()) {
                QPixmap cropped = source.scaledToHeight(76);
                int offset = (76 - cropped.width()) / 2;
                p.drawPixmap(offset, 0, cropped.width(), 76, source);
            }
        } else {
            ui->sizeLabel->setText("");
        }

        ui->label->setPixmap(pix);

    }
}
Esempio n. 9
0
void QDeclarativeRectangle::drawRect(QPainter &p)
{
    Q_D(QDeclarativeRectangle);
    if ((d->gradient && d->gradient->gradient())
        || d->radius > width()/2 || d->radius > height()/2
        || width() < 3 || height() < 3) {
        // XXX This path is still slower than the image path
        // Image path won't work for gradients or invalid radius though
        bool oldAA = p.testRenderHint(QPainter::Antialiasing);
        if (d->smooth)
            p.setRenderHint(QPainter::Antialiasing);
        if (d->pen && d->pen->isValid()) {
            QPen pn(QColor(d->pen->color()), d->pen->width());
            pn.setJoinStyle(Qt::MiterJoin);
            p.setPen(pn);
        } else {
            p.setPen(Qt::NoPen);
        }
        if (d->gradient && d->gradient->gradient())
            p.setBrush(*d->gradient->gradient());
        else
            p.setBrush(d->color);
        const int pw = d->pen && d->pen->isValid() ? d->pen->width() : 0;
        QRectF rect;
        if (pw%2)
            rect = QRectF(0.5, 0.5, width()-1, height()-1);
        else
            rect = QRectF(0, 0, width(), height());
        qreal radius = d->radius;
        if (radius > width()/2 || radius > height()/2)
            radius = qMin(width()/2, height()/2);
        if (radius > 0.)
            p.drawRoundedRect(rect, radius, radius);
        else
            p.drawRect(rect);
        if (d->smooth)
            p.setRenderHint(QPainter::Antialiasing, oldAA);
    } else {
        bool oldAA = p.testRenderHint(QPainter::Antialiasing);
        bool oldSmooth = p.testRenderHint(QPainter::SmoothPixmapTransform);
        if (d->smooth)
            p.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform, d->smooth);

        const int pw = d->pen && d->pen->isValid() ? (d->pen->width()+1)/2*2 : 0;

        if (d->radius > 0)
            generateRoundedRect();
        else
            generateBorderedRect();

        int xOffset = (d->rectImage.width()-1)/2;
        int yOffset = (d->rectImage.height()-1)/2;
        Q_ASSERT(d->rectImage.width() == 2*xOffset + 1);
        Q_ASSERT(d->rectImage.height() == 2*yOffset + 1);

        // check whether we've eliminated the center completely
        if (2*xOffset > width()+pw)
            xOffset = (width()+pw)/2;
        if (2*yOffset > height()+pw)
            yOffset = (height()+pw)/2;

        QMargins margins(xOffset, yOffset, xOffset, yOffset);
        QTileRules rules(Qt::StretchTile, Qt::StretchTile);
        //NOTE: even though our item may have qreal-based width and height, qDrawBorderPixmap only supports QRects
        qDrawBorderPixmap(&p, QRect(-pw/2, -pw/2, width()+pw, height()+pw), margins, d->rectImage, d->rectImage.rect(), margins, rules);

        if (d->smooth) {
            p.setRenderHint(QPainter::Antialiasing, oldAA);
            p.setRenderHint(QPainter::SmoothPixmapTransform, oldSmooth);
        }
    }
}