diff --git a/widget/friendwidget.cpp b/widget/friendwidget.cpp index ad16ac75c..32971b098 100644 --- a/widget/friendwidget.cpp +++ b/widget/friendwidget.cpp @@ -202,6 +202,7 @@ void FriendWidget::onAvatarChange(int FriendId, const QPixmap& pic) isDefaultAvatar = false; avatar->setPixmap(pic); + avatar->autopickBackground(); } void FriendWidget::onAvatarRemoved(int FriendId) diff --git a/widget/groupwidget.cpp b/widget/groupwidget.cpp index d803063ce..949fefbdc 100644 --- a/widget/groupwidget.cpp +++ b/widget/groupwidget.cpp @@ -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() diff --git a/widget/maskablepixmapwidget.cpp b/widget/maskablepixmapwidget.cpp index 90eaf9552..8cdf5d21b 100644 --- a/widget/maskablepixmapwidget.cpp +++ b/widget/maskablepixmapwidget.cpp @@ -17,10 +17,11 @@ #include "maskablepixmapwidget.h" #include -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;xclickable = clickable; @@ -42,8 +84,15 @@ void MaskablePixmapWidget::setClickable(bool clickable) void MaskablePixmapWidget::setPixmap(const QPixmap &pmap) { - pixmap = pmap.scaled(width(), height(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); - update(); + if (!pmap.isNull()) + { + pixmap = pmap.scaled(width(), height(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); + + if (autoBackground) + autopickBackground(); + + update(); + } } QPixmap MaskablePixmapWidget::getPixmap() const diff --git a/widget/maskablepixmapwidget.h b/widget/maskablepixmapwidget.h index c4601d227..d3e8d3616 100644 --- a/widget/maskablepixmapwidget.h +++ b/widget/maskablepixmapwidget.h @@ -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