1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Added a context menu to edit/remove the avatar

This commit is contained in:
Impyy 2015-10-04 13:51:01 +02:00
parent c5127e3654
commit 98c21f3832
4 changed files with 62 additions and 7 deletions

View File

@ -341,7 +341,8 @@ void Core::start()
}
else
{
qDebug() << "Self avatar not found";
qDebug() << "Self avatar not found, will broadcast empty avatar to friends";
setAvatar({});
}
ready = true;
@ -792,10 +793,17 @@ void Core::setUsername(const QString& username)
void Core::setAvatar(const QByteArray& data)
{
QPixmap pic;
pic.loadFromData(data);
Settings::getInstance().saveAvatar(pic, getSelfId().toString());
emit selfAvatarChanged(pic);
if (!data.isEmpty())
{
QPixmap pic;
pic.loadFromData(data);
Settings::getInstance().saveAvatar(pic, getSelfId().toString());
emit selfAvatarChanged(pic);
}
else
{
emit selfAvatarChanged(QPixmap(":/img/contact_dark.svg"));
}
AvatarBroadcaster::setAvatar(data);
AvatarBroadcaster::enableAutoBroadcast();

View File

@ -56,12 +56,20 @@ void CoreFile::sendAvatarFile(Core* core, uint32_t friendId, const QByteArray& d
{
QMutexLocker mlocker(&fileSendMutex);
if (data.isEmpty())
{
tox_file_send(core->tox, friendId, TOX_FILE_KIND_AVATAR, 0,
nullptr, nullptr, 0, nullptr);
return;
}
static_assert(TOX_HASH_LENGTH <= TOX_FILE_ID_LENGTH, "TOX_HASH_LENGTH > TOX_FILE_ID_LENGTH!");
uint8_t avatarHash[TOX_HASH_LENGTH];
tox_hash(avatarHash, (uint8_t*)data.data(), data.size());
uint64_t filesize = data.size();
uint32_t fileNum = tox_file_send(core->tox, friendId, TOX_FILE_KIND_AVATAR, filesize,
avatarHash, avatarHash, TOX_HASH_LENGTH, nullptr);
avatarHash, avatarHash, TOX_HASH_LENGTH, nullptr);
if (fileNum == std::numeric_limits<uint32_t>::max())
{
qWarning() << "sendAvatarFile: Can't create the Tox file sender";

View File

@ -45,6 +45,8 @@
#include <QMessageBox>
#include <QComboBox>
#include <QWindow>
#include <QMenu>
#include <QMouseEvent>
ProfileForm::ProfileForm(QWidget *parent) :
QWidget{parent}, qr{nullptr}
@ -84,8 +86,11 @@ ProfileForm::ProfileForm(QWidget *parent) :
profilePicture = new MaskablePixmapWidget(this, QSize(64, 64), ":/img/avatar_mask.svg");
profilePicture->setPixmap(QPixmap(":/img/contact_dark.svg"));
profilePicture->setContextMenuPolicy(Qt::CustomContextMenu);
profilePicture->setClickable(true);
profilePicture->installEventFilter(this);
connect(profilePicture, SIGNAL(clicked()), this, SLOT(onAvatarClicked()));
connect(profilePicture, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showProfilePictureContextMenu(const QPoint&)));
QHBoxLayout *publicGrouplayout = qobject_cast<QHBoxLayout*>(bodyUI->publicGroup->layout());
publicGrouplayout->insertWidget(0, profilePicture);
publicGrouplayout->insertSpacing(1, 7);
@ -163,6 +168,38 @@ void ProfileForm::show(ContentLayout* contentLayout)
bodyUI->userName->selectAll();
}
bool ProfileForm::eventFilter(QObject *object, QEvent *event)
{
if (object == static_cast<QObject*>(profilePicture) && event->type() == QEvent::MouseButtonPress)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
if (mouseEvent->button() == Qt::RightButton)
return true;
}
return false;
}
void ProfileForm::showProfilePictureContextMenu(const QPoint &point)
{
QPoint pos = profilePicture->mapToGlobal(point);
QMenu contextMenu;
QAction *removeAction = contextMenu.addAction(tr("Remove"));
QAction *selectedItem = contextMenu.exec(pos);
if (selectedItem == removeAction)
{
QString selfPubKey = Core::getInstance()->getSelfId().publicKey;
if (!QFile::remove(Settings::getInstance().getSettingsDirPath()+"avatars/"+selfPubKey.left(64)+".png"))
{
GUI::showError(tr("Error"), tr("Could not remove avatar."));
return;
}
Core::getInstance()->setAvatar({});
}
}
void ProfileForm::copyIdClicked()
{
toxId->selectAll();

View File

@ -67,7 +67,6 @@ public slots:
private slots:
void setToxId(const QString& id);
void copyIdClicked();
void onAvatarClicked();
void onUserNameEdited();
void onStatusMessageEdited();
void onRenameClicked();
@ -78,12 +77,15 @@ private slots:
void onSaveQrClicked();
void onDeletePassClicked();
void onChangePassClicked();
void onAvatarClicked();
void showProfilePictureContextMenu(const QPoint &point);
private:
void retranslateUi();
void prFileLabelUpdate();
private:
bool eventFilter(QObject *object, QEvent *event);
void refreshProfiles();
Ui::IdentitySettings* bodyUI;
MaskablePixmapWidget* profilePicture;