Example #1
0
QIcon generateIcon(const char* iconPath)
{
	QIcon icon;

	QString normalPath(iconPath);
	auto tokens = normalPath.split('.');
	TF_ASSERT(tokens.length() == 2);

	QString off(tokens[0]);
	QString on(tokens[0] + stateOn);
	QString ext("." + tokens[1]);

	QString disabledPath(off + modeDisabled + ext);
	QString activePath(off + modeActive + ext);
	QString selectedPath(off + modeSelected + ext);

	QString normalOnPath(on + ext);
	QString disabledOnPath(on + modeDisabled + ext);
	QString activeOnPath(on + modeActive + ext);
	QString selectedOnPath(on + modeSelected + ext);

	auto offIcon = QPixmap(normalPath);
	icon.addPixmap(offIcon, QIcon::Normal, QIcon::Off);
	icon.addPixmap(QPixmap(disabledPath), QIcon::Disabled, QIcon::Off);
	icon.addPixmap(QPixmap(activePath), QIcon::Active, QIcon::Off);
	icon.addPixmap(QPixmap(selectedPath), QIcon::Selected, QIcon::Off);

	auto onIcon = QPixmap(normalOnPath);
	if (onIcon.isNull())
	{
		QPainter p;

		auto img = offIcon.toImage().convertToFormat(QImage::Format_ARGB32);
		auto mask(img);

		auto width = mask.width();
		auto height = mask.height();
		auto outerRect = mask.rect();
		auto innerRect = QRect(width / 16, height / 16, width - width / 8, height - height / 8);

		p.begin(&mask);
		p.setCompositionMode(QPainter::CompositionMode_SourceIn);
		p.fillRect(outerRect, QApplication::palette().highlight());
		p.fillRect(innerRect, Qt::transparent);
		p.end();

		p.begin(&img);
		p.setCompositionMode(QPainter::CompositionMode_SourceOver);
		p.drawImage(0, 0, mask);
		p.end();

		onIcon.convertFromImage(img);
	}
	icon.addPixmap(onIcon, QIcon::Normal, QIcon::On);
	icon.addPixmap(QPixmap(disabledOnPath), QIcon::Disabled, QIcon::On);
	icon.addPixmap(QPixmap(activeOnPath), QIcon::Active, QIcon::On);
	icon.addPixmap(QPixmap(selectedOnPath), QIcon::Selected, QIcon::On);

	return icon;
}
QPixmap::QPixmap( const char *xpm[] )
    : QPaintDevice( QInternal::Pixmap )
{
    init( 0, 0, 0, FALSE, defOptim );
    QImage image( xpm );
    if ( !image.isNull() )
	convertFromImage( image );
}
bool QPixmap::load( const QString &fileName, const char *format,
		    int conversion_flags )
{
    QImageIO io( fileName, format );
    bool result = io.read();
    if ( result ) {
	detach(); // ###hanord: Why detach here, convertFromImage does it
	result = convertFromImage( io.image(), conversion_flags );
    }
    return result;
}
bool QPixmap::loadFromData( const uchar *buf, uint len, const char *format,
			    int conversion_flags )
{
    QByteArray a;
    a.setRawData( (char *)buf, len );
    QBuffer b( a );
    b.open( IO_ReadOnly );
    QImageIO io( &b, format );
    bool result = io.read();
    b.close();
    a.resetRawData( (char *)buf, len );
    if ( result ) {
	detach();
	result = convertFromImage( io.image(), conversion_flags );
    }
    return result;
}
bool QPixmap::convertFromImage( const QImage &image, ColorMode mode )
{
    if ( image.isNull() ) {
	// convert null image to null pixmap
	*this = QPixmap();
	return TRUE;
    }

    int conversion_flags = 0;
    switch (mode) {
      case Color:
	conversion_flags |= ColorOnly;
	break;
      case Mono:
	conversion_flags |= MonoOnly;
	break;
      default:
	break;// Nothing.
    }
    return convertFromImage( image, conversion_flags );
}
QPixmap &QPixmap::operator=( const QImage &image )
{
    convertFromImage( image );
    return *this;
}
QPixmap::QPixmap( const QImage& image )
    : QPaintDevice( QInternal::Pixmap )
{
    init( 0, 0, 0, FALSE, defOptim );
    convertFromImage( image );
}