Esempio n. 1
0
QString QtScaledAvatarCache::getScaledAvatarPath(const QString& path) {
	QFileInfo avatarFile(path);
	if (avatarFile.exists()) {
		if (!avatarFile.dir().exists(QString::number(size))) {
			if (!avatarFile.dir().mkdir(QString::number(size))) {
				return path;
			}
		}
		QDir targetDir(avatarFile.dir().absoluteFilePath(QString::number(size)));
		QString targetFile = targetDir.absoluteFilePath(avatarFile.baseName());
		if (!QFileInfo(targetFile).exists()) {
			QPixmap avatarPixmap;
			if (avatarPixmap.load(path)) {
				QPixmap maskedAvatar(avatarPixmap.size());
				maskedAvatar.fill(QColor(0, 0, 0, 0));
				QPainter maskPainter(&maskedAvatar);
				maskPainter.setBrush(Qt::black);
				maskPainter.drawRoundedRect(maskedAvatar.rect(), 25.0, 25.0, Qt::RelativeSize);
				maskPainter.setCompositionMode(QPainter::CompositionMode_SourceIn);
				maskPainter.drawPixmap(0, 0, avatarPixmap);
				maskPainter.end();

				if (!maskedAvatar.scaled(size, size, Qt::KeepAspectRatio, Qt::SmoothTransformation).save(targetFile, "PNG")) {
					return path;
				}
			} else {
				SWIFT_LOG(debug) << "Failed to load " << Q2PSTRING(path) << std::endl;
			}
		}
		return targetFile;
	}
	else {
		return path;
	}
}
Esempio n. 2
0
void MainWindow::whenAvatarReceived(const QString &contact, const QByteArray &data, const QString &mimeType)
{
    qDebug() << Q_FUNC_INFO << mimeType;

#ifdef CREATE_MEDIA_FILES
    QDir dir;
    dir.mkdir("avatars");

    QFile avatarFile(QString("avatars/%1.jpg").arg(contact));
    avatarFile.open(QIODevice::WriteOnly);
    avatarFile.write(data);
    avatarFile.close();
#endif

    QPixmap avatar = QPixmap::fromImage(QImage::fromData(data));

    if (avatar.isNull()) {
        return;
    }

    avatar = avatar.scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation);

    QPixmapCache::insert(m_core->contactAvatarToken(contact), avatar);

    updateAvatar(contact);
}