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

Fix #944: Allow transparent avatars

This commit is contained in:
tux3 2016-01-21 04:47:26 +01:00
parent 6cc36a969a
commit e45172ea1b
No known key found for this signature in database
GPG Key ID: 7E086DD661263264
6 changed files with 12 additions and 69 deletions

View File

@ -72,7 +72,7 @@ ChatForm::ChatForm(Friend* chatFriend)
nameLabel->setText(f->getDisplayedName()); nameLabel->setText(f->getDisplayedName());
avatar->setPixmap(QPixmap(":/img/contact_dark.svg"), Qt::transparent); avatar->setPixmap(QPixmap(":/img/contact_dark.svg"));
statusMessageLabel = new CroppingLabel(); statusMessageLabel = new CroppingLabel();
statusMessageLabel->setObjectName("statusLabel"); statusMessageLabel->setObjectName("statusLabel");
@ -673,7 +673,7 @@ void ChatForm::onAvatarRemoved(uint32_t FriendId)
if (FriendId != f->getFriendID()) if (FriendId != f->getFriendID())
return; return;
avatar->setPixmap(QPixmap(":/img/contact_dark.svg"), Qt::transparent); avatar->setPixmap(QPixmap(":/img/contact_dark.svg"));
} }
void ChatForm::loadHistory(QDateTime since, bool processUndelivered) void ChatForm::loadHistory(QDateTime since, bool processUndelivered)

View File

@ -63,7 +63,7 @@ GroupChatForm::GroupChatForm(Group* chatGroup)
nusersLabel->setObjectName("statusLabel"); nusersLabel->setObjectName("statusLabel");
retranslateUi(); retranslateUi();
avatar->setPixmap(Style::scaleSvgImage(":/img/group_dark.svg", avatar->width(), avatar->height()), Qt::transparent); avatar->setPixmap(Style::scaleSvgImage(":/img/group_dark.svg", avatar->width(), avatar->height()));
msgEdit->setObjectName("group"); msgEdit->setObjectName("group");

View File

@ -49,7 +49,7 @@ FriendWidget::FriendWidget(int FriendId, QString id)
, isDefaultAvatar{true} , isDefaultAvatar{true}
, historyLoaded{false} , historyLoaded{false}
{ {
avatar->setPixmap(QPixmap(":/img/contact.svg"), Qt::transparent); avatar->setPixmap(QPixmap(":/img/contact.svg"));
statusPic.setPixmap(QPixmap(":/img/status/dot_offline.svg")); statusPic.setPixmap(QPixmap(":/img/status/dot_offline.svg"));
statusPic.setMargin(3); statusPic.setMargin(3);
nameLabel->setText(id); nameLabel->setText(id);
@ -261,7 +261,7 @@ void FriendWidget::setAsActiveChatroom()
setActive(true); setActive(true);
if (isDefaultAvatar) if (isDefaultAvatar)
avatar->setPixmap(QPixmap(":img/contact_dark.svg"), Qt::transparent); avatar->setPixmap(QPixmap(":img/contact_dark.svg"));
} }
void FriendWidget::setAsInactiveChatroom() void FriendWidget::setAsInactiveChatroom()
@ -269,7 +269,7 @@ void FriendWidget::setAsInactiveChatroom()
setActive(false); setActive(false);
if (isDefaultAvatar) if (isDefaultAvatar)
avatar->setPixmap(QPixmap(":img/contact.svg"), Qt::transparent); avatar->setPixmap(QPixmap(":img/contact.svg"));
} }
void FriendWidget::updateStatusLight() void FriendWidget::updateStatusLight()
@ -365,7 +365,6 @@ void FriendWidget::onAvatarChange(int FriendId, const QPixmap& pic)
isDefaultAvatar = false; isDefaultAvatar = false;
avatar->setPixmap(pic); avatar->setPixmap(pic);
avatar->autopickBackground();
} }
void FriendWidget::onAvatarRemoved(int FriendId) void FriendWidget::onAvatarRemoved(int FriendId)
@ -376,9 +375,9 @@ void FriendWidget::onAvatarRemoved(int FriendId)
isDefaultAvatar = true; isDefaultAvatar = true;
if (isActive()) if (isActive())
avatar->setPixmap(QPixmap(":/img/contact_dark.svg"), Qt::transparent); avatar->setPixmap(QPixmap(":/img/contact_dark.svg"));
else else
avatar->setPixmap(QPixmap(":/img/contact.svg"), Qt::transparent); avatar->setPixmap(QPixmap(":/img/contact.svg"));
} }
void FriendWidget::mousePressEvent(QMouseEvent *ev) void FriendWidget::mousePressEvent(QMouseEvent *ev)

View File

@ -39,7 +39,7 @@
GroupWidget::GroupWidget(int GroupId, QString Name) GroupWidget::GroupWidget(int GroupId, QString Name)
: groupId{GroupId} : groupId{GroupId}
{ {
avatar->setPixmap(Style::scaleSvgImage(":img/group.svg", avatar->width(), avatar->height()), Qt::transparent); avatar->setPixmap(Style::scaleSvgImage(":img/group.svg", avatar->width(), avatar->height()));
statusPic.setPixmap(QPixmap(":img/status/dot_online.svg")); statusPic.setPixmap(QPixmap(":img/status/dot_online.svg"));
statusPic.setMargin(3); statusPic.setMargin(3);
nameLabel->setText(Name); nameLabel->setText(Name);
@ -164,13 +164,13 @@ void GroupWidget::onUserListChanged()
void GroupWidget::setAsActiveChatroom() void GroupWidget::setAsActiveChatroom()
{ {
setActive(true); setActive(true);
avatar->setPixmap(Style::scaleSvgImage(":img/group_dark.svg", avatar->width(), avatar->height()), Qt::transparent); avatar->setPixmap(Style::scaleSvgImage(":img/group_dark.svg", avatar->width(), avatar->height()));
} }
void GroupWidget::setAsInactiveChatroom() void GroupWidget::setAsInactiveChatroom()
{ {
setActive(false); setActive(false);
avatar->setPixmap(Style::scaleSvgImage(":img/group.svg", avatar->width(), avatar->height()), Qt::transparent); avatar->setPixmap(Style::scaleSvgImage(":img/group.svg", avatar->width(), avatar->height()));
} }
void GroupWidget::updateStatusLight() void GroupWidget::updateStatusLight()

View File

@ -23,7 +23,6 @@
MaskablePixmapWidget::MaskablePixmapWidget(QWidget *parent, QSize size, QString maskName) MaskablePixmapWidget::MaskablePixmapWidget(QWidget *parent, QSize size, QString maskName)
: QWidget(parent) : QWidget(parent)
, maskName(maskName) , maskName(maskName)
, backgroundColor(Qt::white)
, clickable(false) , clickable(false)
{ {
setSize(size); setSize(size);
@ -34,43 +33,6 @@ MaskablePixmapWidget::~MaskablePixmapWidget()
delete renderTarget; delete renderTarget;
} }
void MaskablePixmapWidget::autopickBackground()
{
QImage pic = pixmap.toImage();
if (pic.isNull())
return;
int r = 0;
int g = 0;
int b = 0;
int weight = 0;
for (int x=0;x<pic.width();++x)
{
for (int y=0;y<pic.height();++y)
{
QRgb color = pic.pixel(x,y);
r += qRed(color);
g += qGreen(color);
b += qBlue(color);
weight += qAlpha(color);
}
}
weight = qMax(1, weight / 255);
r /= weight;
g /= weight;
b /= weight;
QColor color = QColor::fromRgb(r,g,b);
backgroundColor = QColor::fromRgb(0xFFFFFF ^ color.rgb());
manualColor = false;
update();
}
void MaskablePixmapWidget::setClickable(bool clickable) void MaskablePixmapWidget::setClickable(bool clickable)
{ {
this->clickable = clickable; this->clickable = clickable;
@ -81,25 +43,12 @@ void MaskablePixmapWidget::setClickable(bool clickable)
unsetCursor(); unsetCursor();
} }
void MaskablePixmapWidget::setPixmap(const QPixmap &pmap, const QColor &background)
{
if (!pmap.isNull())
{
unscaled = pmap;
pixmap = pmap.scaled(width() - 2, height() - 2, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
backgroundColor = background;
manualColor = true;
update();
}
}
void MaskablePixmapWidget::setPixmap(const QPixmap &pmap) void MaskablePixmapWidget::setPixmap(const QPixmap &pmap)
{ {
if (!pmap.isNull()) if (!pmap.isNull())
{ {
unscaled = pmap; unscaled = pmap;
pixmap = pmap.scaled(width() - 2, height() - 2, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); pixmap = pmap.scaled(width() - 2, height() - 2, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
autopickBackground();
update(); update();
} }
} }
@ -122,8 +71,6 @@ void MaskablePixmapWidget::setSize(QSize size)
if (!unscaled.isNull()) if (!unscaled.isNull())
{ {
pixmap = unscaled.scaled(width() - 2, height() - 2, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); pixmap = unscaled.scaled(width() - 2, height() - 2, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
if (!manualColor)
autopickBackground();
update(); update();
} }
} }
@ -136,7 +83,6 @@ void MaskablePixmapWidget::paintEvent(QPaintEvent *)
QPainter painter(renderTarget); QPainter painter(renderTarget);
painter.setCompositionMode(QPainter::CompositionMode_SourceOver); painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
painter.fillRect(0,0,width(),height(),backgroundColor);
painter.drawPixmap(offset,pixmap); painter.drawPixmap(offset,pixmap);
painter.setCompositionMode(QPainter::CompositionMode_DestinationIn); painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
painter.drawPixmap(0,0,mask); painter.drawPixmap(0,0,mask);

View File

@ -30,7 +30,6 @@ public:
~MaskablePixmapWidget(); ~MaskablePixmapWidget();
void autopickBackground(); void autopickBackground();
void setClickable(bool clickable); void setClickable(bool clickable);
void setPixmap(const QPixmap &pmap, const QColor &background);
void setPixmap(const QPixmap &pmap); void setPixmap(const QPixmap &pmap);
QPixmap getPixmap() const; QPixmap getPixmap() const;
void setSize(QSize size); void setSize(QSize size);
@ -47,8 +46,7 @@ private:
QPixmap* renderTarget = nullptr; // pointer to dynamically call the constructor QPixmap* renderTarget = nullptr; // pointer to dynamically call the constructor
QSize size; QSize size;
QString maskName; QString maskName;
QColor backgroundColor; bool clickable;
bool clickable, manualColor = false;
}; };
#endif // MASKABLEPIXMAPWIDGET_H #endif // MASKABLEPIXMAPWIDGET_H