summaryrefslogtreecommitdiffstats
blob: 941e20e69669f6b04bad177effe64acad5a50c6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * Copyright (c) 2011 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include "QtAvatarWidget.h"

#include <QLabel>
#include <QVBoxLayout>
#include <QPixmap>
#include <QMenu>
#include <QAction>
#include <QMouseEvent>
#include <QFileDialog>
#include <QImageReader>
#include <QBuffer>
#include <QMessageBox>
#include <QPainter>

#include <QtSwiftUtil.h>

namespace Swift {

QtAvatarWidget::QtAvatarWidget(QWidget* parent) : QWidget(parent) {
	QVBoxLayout* layout = new QVBoxLayout(this);
	layout->setContentsMargins(0,0,0,0);

	QSizePolicy sp(QSizePolicy::Fixed, QSizePolicy::Fixed);
	sp.setHorizontalStretch(0);
	sp.setVerticalStretch(0);
	setSizePolicy(sp);
	setMinimumSize(QSize(96, 96));
	setMaximumSize(QSize(96, 96));

	label = new QLabel(this);
	label->setWordWrap(true);
	label->setSizePolicy(sp);
	label->setMinimumSize(QSize(96, 96));
	label->setMaximumSize(QSize(96, 96));
	label->setAlignment(Qt::AlignCenter);
	layout->addWidget(label);
}

void QtAvatarWidget::setAvatar(const ByteArray& data, const String& type) {
	this->data = data;
	this->type = type;

	QImage image;
	if (!data.isEmpty()) {
		image.loadFromData(reinterpret_cast<const uchar*>(data.getData()), data.getSize());
	}

	if (image.isNull()) {
		image = QImage(":/icons/no-avatar.png");
		QPainter painter(&image);
		painter.setPen(Qt::gray);
		QFont font = painter.font();
		font.setPointSize(14);
		painter.setFont(font);
		painter.drawText(0, 0, image.height(), image.width(), Qt::AlignHCenter | Qt::AlignVCenter, "No picture");
	}

	if (image.height() > label->height() || image.width() > label->width()) {
		image = image.scaled(label->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
	}
	label->setPixmap(QPixmap::fromImage(image));
}

void QtAvatarWidget::mousePressEvent(QMouseEvent* event) {
	QMenu menu;

	QAction* selectPicture = new QAction("Select picture ...", this);
	menu.addAction(selectPicture);

	QAction* clearPicture = new QAction("Clear picture", this);
	menu.addAction(clearPicture);

	QAction* result = menu.exec(event->globalPos());
	if (result == selectPicture) {
		QString fileName = QFileDialog::getOpenFileName(this, tr("Select picture"), "", tr("Image Files (*.png *.jpg *.gif)"));
		if (!fileName.isEmpty()) {
			ByteArray data;
			data.readFromFile(Q2PSTRING(fileName));

			QBuffer buffer;
			buffer.setData(data.getData(), data.getSize());
			buffer.open(QIODevice::ReadOnly);
			QString type = QImageReader::imageFormat(&buffer).toLower();
			if (!type.isEmpty()) {
				type = "image/" + type;
				setAvatar(data, Q2PSTRING(type));
			}
			else {
				QMessageBox::critical(this, "Error", "The selected picture is in an unrecognized format");
			}
		}
	}
	else if (result == clearPicture) {
		setAvatar(ByteArray(), "");
	}
}



}