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-09-27 23:41:17 +08:00
|
|
|
MaskablePixmapWidget::MaskablePixmapWidget(QWidget *parent, QSize size, QString maskName, QColor background)
|
2014-09-26 22:39:29 +08:00
|
|
|
: QWidget(parent)
|
2014-09-27 23:41:17 +08:00
|
|
|
, backgroundColor(background)
|
2014-09-26 22:39:29 +08:00
|
|
|
{
|
2014-09-27 21:39:17 +08:00
|
|
|
setFixedSize(size);
|
|
|
|
mask = QPixmap(maskName).scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
2014-09-26 22:39:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MaskablePixmapWidget::setPixmap(const QPixmap &pmap)
|
|
|
|
{
|
2014-09-27 21:39:17 +08:00
|
|
|
pixmap = pmap.scaled(width(), height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
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*)
|
|
|
|
{
|
|
|
|
emit clicked();
|
|
|
|
}
|