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());
avatar->setPixmap(QPixmap(":/img/contact_dark.svg"), Qt::transparent);
avatar->setPixmap(QPixmap(":/img/contact_dark.svg"));
statusMessageLabel = new CroppingLabel();
statusMessageLabel->setObjectName("statusLabel");
@ -673,7 +673,7 @@ void ChatForm::onAvatarRemoved(uint32_t FriendId)
if (FriendId != f->getFriendID())
return;
avatar->setPixmap(QPixmap(":/img/contact_dark.svg"), Qt::transparent);
avatar->setPixmap(QPixmap(":/img/contact_dark.svg"));
}
void ChatForm::loadHistory(QDateTime since, bool processUndelivered)

View File

@ -63,7 +63,7 @@ GroupChatForm::GroupChatForm(Group* chatGroup)
nusersLabel->setObjectName("statusLabel");
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");

View File

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

View File

@ -39,7 +39,7 @@
GroupWidget::GroupWidget(int GroupId, QString Name)
: 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.setMargin(3);
nameLabel->setText(Name);
@ -164,13 +164,13 @@ void GroupWidget::onUserListChanged()
void GroupWidget::setAsActiveChatroom()
{
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()
{
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()

View File

@ -23,7 +23,6 @@
MaskablePixmapWidget::MaskablePixmapWidget(QWidget *parent, QSize size, QString maskName)
: QWidget(parent)
, maskName(maskName)
, backgroundColor(Qt::white)
, clickable(false)
{
setSize(size);
@ -34,43 +33,6 @@ MaskablePixmapWidget::~MaskablePixmapWidget()
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)
{
this->clickable = clickable;
@ -81,25 +43,12 @@ void MaskablePixmapWidget::setClickable(bool clickable)
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)
{
if (!pmap.isNull())
{
unscaled = pmap;
pixmap = pmap.scaled(width() - 2, height() - 2, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
autopickBackground();
update();
}
}
@ -122,8 +71,6 @@ void MaskablePixmapWidget::setSize(QSize size)
if (!unscaled.isNull())
{
pixmap = unscaled.scaled(width() - 2, height() - 2, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
if (!manualColor)
autopickBackground();
update();
}
}
@ -136,7 +83,6 @@ void MaskablePixmapWidget::paintEvent(QPaintEvent *)
QPainter painter(renderTarget);
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
painter.fillRect(0,0,width(),height(),backgroundColor);
painter.drawPixmap(offset,pixmap);
painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
painter.drawPixmap(0,0,mask);

View File

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