2014-09-26 22:39:29 +08:00
|
|
|
/*
|
|
|
|
Copyright (C) 2014 by Project Tox <https://tox.im>
|
|
|
|
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
|
|
|
This program is libre software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the COPYING file for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "maskablepixmapwidget.h"
|
|
|
|
#include <QPainter>
|
|
|
|
|
2014-10-01 02:10:42 +08:00
|
|
|
MaskablePixmapWidget::MaskablePixmapWidget(QWidget *parent, QSize size, QString maskName)
|
2014-09-26 22:39:29 +08:00
|
|
|
: QWidget(parent)
|
2014-09-30 02:18:03 +08:00
|
|
|
, backgroundColor(Qt::white)
|
2014-09-28 00:00:13 +08:00
|
|
|
, clickable(false)
|
2014-09-26 22:39:29 +08:00
|
|
|
{
|
2014-09-27 21:39:17 +08:00
|
|
|
setFixedSize(size);
|
2014-09-28 23:30:00 +08:00
|
|
|
|
|
|
|
QPixmap pmapMask = QPixmap(maskName);
|
|
|
|
|
|
|
|
if (!pmapMask.isNull())
|
|
|
|
mask = QPixmap(maskName).scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
2014-09-26 22:39:29 +08:00
|
|
|
}
|
|
|
|
|
2014-09-30 02:18:03 +08:00
|
|
|
void MaskablePixmapWidget::autopickBackground()
|
|
|
|
{
|
|
|
|
QImage pic = pixmap.toImage();
|
|
|
|
|
|
|
|
if (pic.isNull())
|
|
|
|
return;
|
|
|
|
|
2014-10-01 02:10:42 +08:00
|
|
|
int r = 0;
|
|
|
|
int g = 0;
|
|
|
|
int b = 0;
|
|
|
|
int weight = 0;
|
2014-09-30 02:18:03 +08:00
|
|
|
|
|
|
|
for (int x=0;x<pic.width();++x)
|
|
|
|
{
|
|
|
|
for (int y=0;y<pic.height();++y)
|
|
|
|
{
|
|
|
|
QRgb color = pic.pixel(x,y);
|
2014-10-01 02:10:42 +08:00
|
|
|
r += qRed(color);
|
|
|
|
g += qGreen(color);
|
|
|
|
b += qBlue(color);
|
2014-09-30 02:18:03 +08:00
|
|
|
|
2014-10-01 02:10:42 +08:00
|
|
|
weight += qAlpha(color);
|
2014-09-30 02:18:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-01 02:10:42 +08:00
|
|
|
weight = qMax(1, weight / 255);
|
2014-09-30 02:18:03 +08:00
|
|
|
r /= weight;
|
|
|
|
g /= weight;
|
|
|
|
b /= weight;
|
|
|
|
|
2014-10-01 02:10:42 +08:00
|
|
|
QColor color = QColor::fromRgb(r,g,b);
|
2014-09-30 02:18:03 +08:00
|
|
|
backgroundColor = QColor::fromRgb(0xFFFFFF ^ color.rgb());
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MaskablePixmapWidget::setBackground(QColor color)
|
|
|
|
{
|
|
|
|
backgroundColor = color;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2014-09-28 00:00:13 +08:00
|
|
|
void MaskablePixmapWidget::setClickable(bool clickable)
|
|
|
|
{
|
|
|
|
this->clickable = clickable;
|
|
|
|
|
|
|
|
if (clickable)
|
|
|
|
setCursor(Qt::PointingHandCursor);
|
|
|
|
else
|
|
|
|
unsetCursor();
|
|
|
|
}
|
|
|
|
|
2014-10-01 02:10:42 +08:00
|
|
|
void MaskablePixmapWidget::setPixmap(const QPixmap &pmap, QColor background)
|
2014-09-26 22:39:29 +08:00
|
|
|
{
|
2014-09-30 02:18:03 +08:00
|
|
|
if (!pmap.isNull())
|
|
|
|
{
|
|
|
|
pixmap = pmap.scaled(width(), height(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
|
2014-10-01 02:10:42 +08:00
|
|
|
backgroundColor = background;
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
2014-09-30 02:18:03 +08:00
|
|
|
|
2014-10-01 02:10:42 +08:00
|
|
|
void MaskablePixmapWidget::setPixmap(const QPixmap &pmap)
|
|
|
|
{
|
|
|
|
if (!pmap.isNull())
|
|
|
|
{
|
|
|
|
pixmap = pmap.scaled(width(), height(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
|
|
|
|
autopickBackground();
|
2014-09-30 02:18:03 +08:00
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
2014-09-26 22:39:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap MaskablePixmapWidget::getPixmap() const
|
|
|
|
{
|
|
|
|
return pixmap;
|
|
|
|
}
|
|
|
|
|
2014-09-27 21:39:17 +08:00
|
|
|
void MaskablePixmapWidget::paintEvent(QPaintEvent *)
|
2014-09-26 22:39:29 +08:00
|
|
|
{
|
2014-09-27 21:39:17 +08:00
|
|
|
QPixmap tmp(width(), height());
|
2014-09-26 22:39:29 +08:00
|
|
|
tmp.fill(Qt::transparent);
|
|
|
|
|
2014-09-27 21:39:17 +08:00
|
|
|
QPoint offset((width() - pixmap.size().width())/2,(height() - pixmap.size().height())/2); // centering the pixmap
|
2014-09-26 22:39:29 +08:00
|
|
|
|
|
|
|
QPainter painter(&tmp);
|
2014-09-27 22:37:16 +08:00
|
|
|
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
|
2014-09-27 23:41:17 +08:00
|
|
|
painter.fillRect(0,0,width(),height(),backgroundColor);
|
2014-09-26 22:39:29 +08:00
|
|
|
painter.drawPixmap(offset,pixmap);
|
|
|
|
painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
|
|
|
|
painter.drawPixmap(0,0,mask);
|
|
|
|
painter.end();
|
2014-09-27 21:39:17 +08:00
|
|
|
|
2014-09-26 22:39:29 +08:00
|
|
|
painter.begin(this);
|
|
|
|
painter.drawPixmap(0,0,tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MaskablePixmapWidget::mousePressEvent(QMouseEvent*)
|
|
|
|
{
|
2014-09-28 00:00:13 +08:00
|
|
|
if(clickable)
|
|
|
|
emit clicked();
|
2014-09-26 22:39:29 +08:00
|
|
|
}
|