diff options
Diffstat (limited to 'Swift/QtUI/QtAvatarWidget.cpp')
-rw-r--r-- | Swift/QtUI/QtAvatarWidget.cpp | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/Swift/QtUI/QtAvatarWidget.cpp b/Swift/QtUI/QtAvatarWidget.cpp new file mode 100644 index 0000000..1ee7c73 --- /dev/null +++ b/Swift/QtUI/QtAvatarWidget.cpp @@ -0,0 +1,103 @@ +/* + * 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); + 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(), ""); + } +} + + + +} |