コード例 #1
0
ファイル: newsbutton.cpp プロジェクト: bjorn/tiled
void NewsButton::refreshButton()
{
    auto &feed = NewsFeed::instance();
    auto unreadCount = feed.unreadCount();

    if (unreadCount > 0) {
        QPixmap numberPixmap(Utils::smallIconSize());
        numberPixmap.fill(Qt::transparent);

        QPainter painter(&numberPixmap);
        painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

        painter.setBrush(QColor(250, 92, 92));
        painter.setPen(Qt::NoPen);
        painter.drawEllipse(numberPixmap.rect().adjusted(1, 1, -1, -1));

        auto font = painter.font();
        font.setBold(true);
        painter.setFont(font);
        painter.setBrush(Qt::white);
        painter.setPen(Qt::white);
        painter.drawText(numberPixmap.rect(), Qt::AlignCenter, QString::number(unreadCount));

        setIcon(QIcon(numberPixmap));
    } else {
        setIcon(QIcon());
    }

    setEnabled(!feed.isEmpty());
}
コード例 #2
0
//This function makes a pixmap which is based on icon, but has a number painted on it.
QPixmap BoxContainerItem::calcComplexPixmap(const QPixmap &icon, const QColor &fgColour, const QFont *font, const int count)
{
    QPixmap result(icon);
    QPixmap numberPixmap(icon.size());
    QImage iconImage(icon.convertToImage());
    QImage numberImage;
    QRgb *rgbline;
    QPainter p;

    //Make a transparent number; first make a white number on a black background.
    //This pixmap also is the base alpha-channel, the foreground colour is added later.
    numberPixmap.fill(Qt::black);
    p.begin(&numberPixmap, false);
    p.setPen(Qt::white);
    if(font)
        p.setFont(*font);
    p.drawText(icon.rect(), Qt::AlignCenter, QString::number(count));
    p.end();

    //Convert to image and add the alpha channel.
    numberImage = numberPixmap.convertToImage();
    if(numberImage.depth() != 32)   //Make sure depth is 32 (and thus can have an alpha channel)
        numberImage = numberImage.convertDepth(32);
    numberImage.setAlphaBuffer(true);   //Enable alpha channel
    for(int xx = 0; xx < numberImage.height(); ++xx)
    {
        rgbline = (QRgb *)numberImage.scanLine(xx);

        for(int yy = 0; yy < numberImage.width(); ++yy)
        {
            //Set colour and alpha channel
            rgbline[ yy ] = qRgba(fgColour.red(), fgColour.green(), fgColour.blue(), qRed(rgbline[ yy ]));
        }
    }

    //Merge icon and number and convert to result.
    KIconEffect::overlay(iconImage, numberImage);
    result.convertFromImage(iconImage);

    return result;
}