mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
MaskablePixmapWidget: auto-detect a suitable background color
This commit is contained in:
parent
4d91695169
commit
c260a7a100
|
@ -202,6 +202,7 @@ void FriendWidget::onAvatarChange(int FriendId, const QPixmap& pic)
|
|||
|
||||
isDefaultAvatar = false;
|
||||
avatar->setPixmap(pic);
|
||||
avatar->autopickBackground();
|
||||
}
|
||||
|
||||
void FriendWidget::onAvatarRemoved(int FriendId)
|
||||
|
|
|
@ -39,8 +39,8 @@ GroupWidget::GroupWidget(int GroupId, QString Name)
|
|||
textLayout.setMargin(0);
|
||||
setLayoutDirection(Qt::LeftToRight); // parent might have set Qt::RightToLeft
|
||||
|
||||
avatar = new MaskablePixmapWidget(this, QSize(40,40), QString(), Qt::transparent);
|
||||
avatar->setPixmap(QPixmap(":img/group.png"));
|
||||
avatar = new MaskablePixmapWidget(this, QSize(40,40), ":/img/avatar_mask.png");
|
||||
avatar->setPixmap(QPixmap(":img/group_dark.png"));
|
||||
statusPic.setPixmap(QPixmap(":img/status/dot_online.png"));
|
||||
name.setText(Name);
|
||||
QFont small;
|
||||
|
@ -139,7 +139,7 @@ void GroupWidget::setAsInactiveChatroom()
|
|||
QPalette pal3;
|
||||
pal3.setColor(QPalette::Background, QColor(65,65,65,255));
|
||||
this->setPalette(pal3);
|
||||
avatar->setPixmap(QPixmap(":img/group.png"));
|
||||
avatar->setPixmap(QPixmap(":img/group_dark.png"));
|
||||
}
|
||||
|
||||
void GroupWidget::updateStatusLight()
|
||||
|
|
|
@ -17,10 +17,11 @@
|
|||
#include "maskablepixmapwidget.h"
|
||||
#include <QPainter>
|
||||
|
||||
MaskablePixmapWidget::MaskablePixmapWidget(QWidget *parent, QSize size, QString maskName, QColor background)
|
||||
MaskablePixmapWidget::MaskablePixmapWidget(QWidget *parent, QSize size, QString maskName, bool autopickBackground)
|
||||
: QWidget(parent)
|
||||
, backgroundColor(background)
|
||||
, backgroundColor(Qt::white)
|
||||
, clickable(false)
|
||||
, autoBackground(autopickBackground)
|
||||
{
|
||||
setFixedSize(size);
|
||||
|
||||
|
@ -30,6 +31,47 @@ MaskablePixmapWidget::MaskablePixmapWidget(QWidget *parent, QSize size, QString
|
|||
mask = QPixmap(maskName).scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
||||
}
|
||||
|
||||
void MaskablePixmapWidget::autopickBackground()
|
||||
{
|
||||
QImage pic = pixmap.toImage();
|
||||
|
||||
if (pic.isNull())
|
||||
return;
|
||||
|
||||
qreal r = 0.0f;
|
||||
qreal g = 0.0f;
|
||||
qreal b = 0.0f;
|
||||
float weight = 1.0f;
|
||||
|
||||
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) / 255.0f;
|
||||
g += qGreen(color) / 255.0f;
|
||||
b += qBlue(color) / 255.0f;
|
||||
|
||||
weight += qAlpha(color) / 255.0f;
|
||||
}
|
||||
}
|
||||
|
||||
r /= weight;
|
||||
g /= weight;
|
||||
b /= weight;
|
||||
|
||||
QColor color = QColor::fromRgbF(r,g,b);
|
||||
backgroundColor = QColor::fromRgb(0xFFFFFF ^ color.rgb());
|
||||
|
||||
update();
|
||||
}
|
||||
|
||||
void MaskablePixmapWidget::setBackground(QColor color)
|
||||
{
|
||||
backgroundColor = color;
|
||||
update();
|
||||
}
|
||||
|
||||
void MaskablePixmapWidget::setClickable(bool clickable)
|
||||
{
|
||||
this->clickable = clickable;
|
||||
|
@ -41,10 +83,17 @@ void MaskablePixmapWidget::setClickable(bool clickable)
|
|||
}
|
||||
|
||||
void MaskablePixmapWidget::setPixmap(const QPixmap &pmap)
|
||||
{
|
||||
if (!pmap.isNull())
|
||||
{
|
||||
pixmap = pmap.scaled(width(), height(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
|
||||
|
||||
if (autoBackground)
|
||||
autopickBackground();
|
||||
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
QPixmap MaskablePixmapWidget::getPixmap() const
|
||||
{
|
||||
|
|
|
@ -23,8 +23,10 @@ class MaskablePixmapWidget : public QWidget
|
|||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
MaskablePixmapWidget(QWidget *parent, QSize size, QString maskName, QColor background = Qt::white);
|
||||
MaskablePixmapWidget(QWidget *parent, QSize size, QString maskName = QString(), bool autopickBackground = true);
|
||||
|
||||
void autopickBackground();
|
||||
void setBackground(QColor color);
|
||||
void setClickable(bool clickable);
|
||||
void setPixmap(const QPixmap &pmap);
|
||||
QPixmap getPixmap() const;
|
||||
|
@ -43,6 +45,7 @@ private:
|
|||
QString maskName;
|
||||
QColor backgroundColor;
|
||||
bool clickable;
|
||||
bool autoBackground;
|
||||
};
|
||||
|
||||
#endif // MASKABLEPIXMAPWIDGET_H
|
||||
|
|
Loading…
Reference in New Issue
Block a user