forked from blue/squawk
changing avatar in local vcard, no uploading yet
This commit is contained in:
parent
2a37f36b83
commit
652381b067
9 changed files with 221 additions and 63 deletions
|
@ -257,11 +257,11 @@ void Conversation::showEvent(QShowEvent* event)
|
|||
|
||||
void Conversation::onAttach()
|
||||
{
|
||||
QFileDialog* d = new QFileDialog(this, "Chose a file to send");
|
||||
QFileDialog* d = new QFileDialog(this, tr("Chose a file to send"));
|
||||
d->setFileMode(QFileDialog::ExistingFile);
|
||||
|
||||
connect(d, SIGNAL(accepted()), this, SLOT(onFileSelected()));
|
||||
connect(d, SIGNAL(rejected()), d, SLOT(deleteLater()));
|
||||
connect(d, &QFileDialog::accepted, this, &Conversation::onFileSelected);
|
||||
connect(d, &QFileDialog::rejected, d, &QFileDialog::deleteLater);
|
||||
|
||||
d->show();
|
||||
}
|
||||
|
|
|
@ -19,22 +19,37 @@
|
|||
#include "vcard.h"
|
||||
#include "ui_vcard.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
const std::set<QString> VCard::supportedTypes = {"image/jpeg", "image/png"};
|
||||
|
||||
VCard::VCard(const QString& jid, bool edit, QWidget* parent):
|
||||
QWidget(parent),
|
||||
m_ui(new Ui::VCard()),
|
||||
avatar(QApplication::palette().window().color().lightnessF() > 0.5 ? ":/images/fallback/dark/big/user.svg" : ":/images/fallback/light/big/user.svg", 256, 256, 256)
|
||||
avatarButtonMargins(),
|
||||
avatarMenu(nullptr),
|
||||
editable(edit),
|
||||
currentAvatarType(Shared::Avatar::empty),
|
||||
currentAvatarPath("")
|
||||
{
|
||||
m_ui->setupUi(this);
|
||||
m_ui->jabberID->setText(jid);
|
||||
m_ui->jabberID->setReadOnly(true);
|
||||
QGridLayout* general = static_cast<QGridLayout*>(m_ui->General->layout());
|
||||
general->addWidget(&avatar, 2, 2, 1, 1);
|
||||
avatar.setFrameShape(QFrame::StyledPanel);
|
||||
avatar.setFrameShadow(QFrame::Sunken);
|
||||
avatar.setMargin(6);
|
||||
|
||||
QAction* setAvatar = m_ui->actionSetAvatar;
|
||||
QAction* clearAvatar = m_ui->actionClearAvatar;
|
||||
|
||||
connect(setAvatar, &QAction::triggered, this, &VCard::onSetAvatar);
|
||||
connect(clearAvatar, &QAction::triggered, this, &VCard::onClearAvatar);
|
||||
|
||||
setAvatar->setEnabled(true);
|
||||
clearAvatar->setEnabled(false);
|
||||
|
||||
if (edit) {
|
||||
|
||||
avatarMenu = new QMenu();
|
||||
m_ui->avatarButton->setMenu(avatarMenu);
|
||||
avatarMenu->addAction(setAvatar);
|
||||
avatarMenu->addAction(clearAvatar);
|
||||
} else {
|
||||
m_ui->buttonBox->hide();
|
||||
m_ui->firstName->setReadOnly(true);
|
||||
|
@ -53,13 +68,17 @@ VCard::VCard(const QString& jid, bool edit, QWidget* parent):
|
|||
connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &VCard::onButtonBoxAccepted);
|
||||
connect(m_ui->buttonBox, &QDialogButtonBox::rejected, m_ui->buttonBox, &QDialogButtonBox::deleteLater);
|
||||
|
||||
int height = m_ui->personalForm->minimumSize().height();
|
||||
avatar.setMaximumSize(avatar.widthForHeight(height), height);
|
||||
avatar.setMinimumSize(avatar.widthForHeight(height), height);
|
||||
avatarButtonMargins = m_ui->avatarButton->size();
|
||||
|
||||
int height = m_ui->personalForm->minimumSize().height() - avatarButtonMargins.height();
|
||||
m_ui->avatarButton->setIconSize(QSize(height, height));
|
||||
}
|
||||
|
||||
VCard::~VCard()
|
||||
{
|
||||
if (editable) {
|
||||
avatarMenu->deleteLater();
|
||||
}
|
||||
}
|
||||
|
||||
void VCard::setVCard(const QString& jid, const Shared::VCard& card)
|
||||
|
@ -80,20 +99,10 @@ void VCard::setVCard(const Shared::VCard& card)
|
|||
//m_ui->organizationTitle->setText(card.get());
|
||||
//m_ui->organizationRole->setText(card.get());
|
||||
m_ui->description->setText(card.getDescription());
|
||||
currentAvatarType = card.getAvatarType();
|
||||
currentAvatarPath = card.getAvatarPath();
|
||||
|
||||
switch (card.getAvatarType()) {
|
||||
case Shared::Avatar::empty:
|
||||
avatar.setPath(QApplication::palette().window().color().lightnessF() > 0.5 ? ":/images/fallback/dark/big/user.svg" : ":/images/fallback/light/big/user.svg", 256, 256);
|
||||
break;
|
||||
case Shared::Avatar::autocreated:
|
||||
case Shared::Avatar::valid:
|
||||
avatar.setPath(card.getAvatarPath());
|
||||
break;
|
||||
}
|
||||
|
||||
int height = m_ui->personalForm->minimumSize().height();
|
||||
avatar.setMaximumSize(avatar.widthForHeight(height), height);
|
||||
avatar.setMinimumSize(avatar.widthForHeight(height), height);
|
||||
updateAvatar();
|
||||
}
|
||||
|
||||
QString VCard::getJid() const
|
||||
|
@ -110,6 +119,86 @@ void VCard::onButtonBoxAccepted()
|
|||
card.setNickName(m_ui->nickName->text());
|
||||
card.setBirthday(m_ui->birthday->date());
|
||||
card.setDescription(m_ui->description->toPlainText());
|
||||
card.setAvatarPath(currentAvatarPath);
|
||||
card.setAvatarType(currentAvatarType);
|
||||
|
||||
emit saveVCard(card);
|
||||
}
|
||||
|
||||
void VCard::onClearAvatar()
|
||||
{
|
||||
currentAvatarType = Shared::Avatar::empty;
|
||||
currentAvatarPath = "";
|
||||
|
||||
updateAvatar();
|
||||
}
|
||||
|
||||
void VCard::onSetAvatar()
|
||||
{
|
||||
QFileDialog* d = new QFileDialog(this, tr("Chose your new avatar"));
|
||||
d->setFileMode(QFileDialog::ExistingFile);
|
||||
d->setNameFilter(tr("Images (*.png *.jpg *.jpeg)"));
|
||||
|
||||
connect(d, &QFileDialog::accepted, this, &VCard::onAvatarSelected);
|
||||
connect(d, &QFileDialog::rejected, d, &QFileDialog::deleteLater);
|
||||
|
||||
d->show();
|
||||
}
|
||||
|
||||
void VCard::updateAvatar()
|
||||
{
|
||||
int height = m_ui->personalForm->minimumSize().height() - avatarButtonMargins.height();
|
||||
switch (currentAvatarType) {
|
||||
case Shared::Avatar::empty:
|
||||
m_ui->avatarButton->setIcon(Shared::icon("user", true));
|
||||
m_ui->avatarButton->setIconSize(QSize(height, height));
|
||||
m_ui->actionClearAvatar->setEnabled(false);
|
||||
break;
|
||||
case Shared::Avatar::autocreated:
|
||||
case Shared::Avatar::valid:
|
||||
QPixmap pixmap(currentAvatarPath);
|
||||
qreal h = pixmap.height();
|
||||
qreal w = pixmap.width();
|
||||
qreal aspectRatio = w / h;
|
||||
m_ui->avatarButton->setIconSize(QSize(height * aspectRatio, height));
|
||||
m_ui->avatarButton->setIcon(QIcon(currentAvatarPath));
|
||||
m_ui->actionClearAvatar->setEnabled(true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void VCard::onAvatarSelected()
|
||||
{
|
||||
QFileDialog* d = static_cast<QFileDialog*>(sender());
|
||||
QMimeDatabase db;
|
||||
QString path = d->selectedFiles().front();
|
||||
QMimeType type = db.mimeTypeForFile(path);
|
||||
d->deleteLater();
|
||||
|
||||
if (supportedTypes.find(type.name()) == supportedTypes.end()) {
|
||||
qDebug() << "Selected for avatar file is not supported, skipping";
|
||||
} else {
|
||||
QImage src(path);
|
||||
QImage dst;
|
||||
if (src.width() > 160 || src.height() > 160) {
|
||||
dst = src.scaled(160, 160, Qt::KeepAspectRatio);
|
||||
}
|
||||
QString path = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/" + m_ui->jabberID->text() + ".temp." + type.preferredSuffix();
|
||||
QFile oldTemp(path);
|
||||
if (oldTemp.exists()) {
|
||||
if (!oldTemp.remove()) {
|
||||
qDebug() << "Error removing old temp avatar" << path;
|
||||
return;
|
||||
}
|
||||
}
|
||||
bool success = dst.save(path);
|
||||
if (success) {
|
||||
currentAvatarPath = path;
|
||||
currentAvatarType = Shared::Avatar::valid;
|
||||
|
||||
updateAvatar();
|
||||
} else {
|
||||
qDebug() << "couldn't save avatar" << path << ", skipping";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,9 +21,16 @@
|
|||
|
||||
#include <QWidget>
|
||||
#include <QScopedPointer>
|
||||
#include <QPixmap>
|
||||
#include <QMenu>
|
||||
#include <QFileDialog>
|
||||
#include <QMimeDatabase>
|
||||
#include <QImage>
|
||||
#include <QStandardPaths>
|
||||
|
||||
#include <set>
|
||||
|
||||
#include "../../global.h"
|
||||
#include "../utils/image.h"
|
||||
|
||||
namespace Ui
|
||||
{
|
||||
|
@ -49,10 +56,22 @@ signals:
|
|||
|
||||
private slots:
|
||||
void onButtonBoxAccepted();
|
||||
void onClearAvatar();
|
||||
void onSetAvatar();
|
||||
void onAvatarSelected();
|
||||
|
||||
private:
|
||||
QScopedPointer<Ui::VCard> m_ui;
|
||||
Image avatar;
|
||||
QSize avatarButtonMargins;
|
||||
QMenu* avatarMenu;
|
||||
bool editable;
|
||||
Shared::Avatar currentAvatarType;
|
||||
QString currentAvatarPath;
|
||||
|
||||
static const std::set<QString> supportedTypes;
|
||||
|
||||
private:
|
||||
void updateAvatar();
|
||||
};
|
||||
|
||||
#endif // VCARD_H
|
||||
|
|
|
@ -6,8 +6,8 @@
|
|||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>560</width>
|
||||
<height>534</height>
|
||||
<width>537</width>
|
||||
<height>539</height>
|
||||
</rect>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_2">
|
||||
|
@ -411,6 +411,43 @@
|
|||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="2">
|
||||
<widget class="QToolButton" name="avatarButton">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>0</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset theme="user"/>
|
||||
</property>
|
||||
<property name="iconSize">
|
||||
<size>
|
||||
<width>0</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="popupMode">
|
||||
<enum>QToolButton::InstantPopup</enum>
|
||||
</property>
|
||||
<property name="toolButtonStyle">
|
||||
<enum>Qt::ToolButtonIconOnly</enum>
|
||||
</property>
|
||||
<property name="arrowType">
|
||||
<enum>Qt::NoArrow</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QWidget" name="Contact">
|
||||
|
@ -459,8 +496,8 @@
|
|||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>548</width>
|
||||
<height>410</height>
|
||||
<width>525</width>
|
||||
<height>415</height>
|
||||
</rect>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_3" columnstretch="1,4,1">
|
||||
|
@ -625,7 +662,7 @@
|
|||
<item row="9" column="1">
|
||||
<layout class="QVBoxLayout" name="addressesLayout">
|
||||
<item>
|
||||
<widget class="QLabel" name="EmptyAddressesPlaceholder">
|
||||
<widget class="QLabel" name="emptyAddressesPlaceholder">
|
||||
<property name="text">
|
||||
<string><html><head/><body><p><span style=" font-style:italic;">User has no contact e-mail addresses</span></p></body></html></string>
|
||||
</property>
|
||||
|
@ -717,6 +754,22 @@
|
|||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
<action name="actionSetAvatar">
|
||||
<property name="icon">
|
||||
<iconset theme="photo"/>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Set avatar</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionClearAvatar">
|
||||
<property name="icon">
|
||||
<iconset theme="edit-clear-all"/>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Clear avatar</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<tabstops>
|
||||
<tabstop>firstName</tabstop>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue