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)
|
2015-01-26 21:09:19 +08:00
|
|
|
, maskName(maskName)
|
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
|
|
|
{
|
2015-01-26 21:09:19 +08:00
|
|
|
setSize(size);
|
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());
|
2015-01-26 21:09:19 +08:00
|
|
|
manualColor = false;
|
2014-09-30 02:18:03 +08:00
|
|
|
|
|
|
|
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())
|
|
|
|
{
|
2015-01-26 21:09:19 +08:00
|
|
|
unscaled = pmap;
|
2015-02-12 20:18:04 +08:00
|
|
|
pixmap = pmap.scaled(width() - 2, height() - 2, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
|
2014-10-01 02:10:42 +08:00
|
|
|
backgroundColor = background;
|
2015-01-26 21:09:19 +08:00
|
|
|
manualColor = true;
|
2014-10-01 02:10:42 +08:00
|
|
|
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())
|
|
|
|
{
|
2015-01-26 21:09:19 +08:00
|
|
|
unscaled = pmap;
|
2015-02-12 20:18:04 +08:00
|
|
|
pixmap = pmap.scaled(width() - 2, height() - 2, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
|
2014-10-01 02:10:42 +08:00
|
|
|
autopickBackground();
|
2014-09-30 02:18:03 +08:00
|
|
|
update();
|
|
|
|
}
|
2014-09-26 22:39:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap MaskablePixmapWidget::getPixmap() const
|
|
|
|
{
|
2015-01-26 21:09:19 +08:00
|
|
|
return *renderTarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MaskablePixmapWidget::setSize(QSize size)
|
|
|
|
{
|
|
|
|
setFixedSize(size);
|
|
|
|
delete renderTarget;
|
|
|
|
renderTarget = new QPixmap(size);
|
|
|
|
|
|
|
|
QPixmap pmapMask = QPixmap(maskName);
|
|
|
|
if (!pmapMask.isNull())
|
|
|
|
mask = pmapMask.scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
|
|
|
|
|
|
|
if (!unscaled.isNull())
|
|
|
|
{
|
2015-02-12 20:18:04 +08:00
|
|
|
pixmap = unscaled.scaled(width() - 2, height() - 2, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
|
2015-01-26 21:09:19 +08:00
|
|
|
if (!manualColor)
|
|
|
|
autopickBackground();
|
|
|
|
update();
|
|
|
|
}
|
2014-09-26 22:39:29 +08:00
|
|
|
}
|
|
|
|
|
2014-09-27 21:39:17 +08:00
|
|
|
void MaskablePixmapWidget::paintEvent(QPaintEvent *)
|
2014-09-26 22:39:29 +08:00
|
|
|
{
|
2015-01-26 21:09:19 +08:00
|
|
|
renderTarget->fill(Qt::transparent);
|
2014-09-26 22:39:29 +08:00
|
|
|
|
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
|
|
|
|
2015-01-26 21:09:19 +08:00
|
|
|
QPainter painter(renderTarget);
|
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);
|
2015-01-26 21:09:19 +08:00
|
|
|
painter.drawPixmap(0,0,*renderTarget);
|
2014-09-26 22:39:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MaskablePixmapWidget::mousePressEvent(QMouseEvent*)
|
|
|
|
{
|
2014-12-12 02:05:52 +08:00
|
|
|
if (clickable)
|
2014-09-28 00:00:13 +08:00
|
|
|
emit clicked();
|
2014-09-26 22:39:29 +08:00
|
|
|
}
|