qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
maskablepixmapwidget.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2014-2019 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "maskablepixmapwidget.h"
21 #include <QPainter>
22 #include <QStyle>
23 
29 MaskablePixmapWidget::MaskablePixmapWidget(QWidget* parent, QSize size, QString maskName)
30  : QLabel("", parent)
31  , renderTarget(nullptr)
32  , maskName(maskName)
33  , clickable(false)
34 {
35  setSize(size);
36 }
37 
39 {
40  delete renderTarget;
41 }
42 
44 {
45  this->clickable = clickable;
46 
47  if (clickable) {
48  setCursor(Qt::PointingHandCursor);
49  } else {
50  unsetCursor();
51  }
52 }
53 
54 void MaskablePixmapWidget::setPixmap(const QPixmap& pmap)
55 {
56  if (pmap.isNull()) {
57  return;
58  }
59 
60  unscaled = pmap;
61  pixmap = pmap.scaled(width(), height(),
62  Qt::KeepAspectRatio,
63  Qt::SmoothTransformation);
64  updatePixmap();
65  update();
66 }
67 
69 {
70  return *renderTarget;
71 }
72 
74 {
75  setFixedSize(size);
76  delete renderTarget;
77  renderTarget = new QPixmap(size);
78 
79  QPixmap pmapMask = QPixmap(maskName);
80  if (!pmapMask.isNull()) {
81  mask = pmapMask.scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
82  }
83 
84  if (!unscaled.isNull()) {
85  pixmap = unscaled.scaled(width(), height(),
86  Qt::KeepAspectRatio,
87  Qt::SmoothTransformation);
88  updatePixmap();
89  update();
90  }
91 }
92 
94 {
95  if (clickable) {
96  emit clicked();
97  }
98 }
99 
101 {
102  renderTarget->fill(Qt::transparent);
103 
104  QPoint offset((width() - pixmap.size().width()) / 2,
105  (height() - pixmap.size().height()) / 2); // centering the pixmap
106 
107  QPainter painter(renderTarget);
108  painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
109  painter.drawPixmap(offset, pixmap);
110  painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
111  painter.drawPixmap(0, 0, mask);
112  painter.end();
113  QLabel::setPixmap(*renderTarget);
114 }
MaskablePixmapWidget::unscaled
QPixmap unscaled
Definition: maskablepixmapwidget.h:46
MaskablePixmapWidget::renderTarget
QPixmap * renderTarget
pointer to dynamically call the constructor.
Definition: maskablepixmapwidget.h:47
MaskablePixmapWidget::~MaskablePixmapWidget
~MaskablePixmapWidget() override
Definition: maskablepixmapwidget.cpp:38
MaskablePixmapWidget::getPixmap
QPixmap getPixmap() const
Definition: maskablepixmapwidget.cpp:68
MaskablePixmapWidget::setPixmap
void setPixmap(const QPixmap &pmap)
Definition: maskablepixmapwidget.cpp:54
MaskablePixmapWidget::clickable
bool clickable
Definition: maskablepixmapwidget.h:49
MaskablePixmapWidget::MaskablePixmapWidget
MaskablePixmapWidget(QWidget *parent, QSize size, QString maskName=QString())
Definition: maskablepixmapwidget.cpp:29
MaskablePixmapWidget::setClickable
void setClickable(bool clickable)
Definition: maskablepixmapwidget.cpp:43
maskablepixmapwidget.h
MaskablePixmapWidget::maskName
QString maskName
Definition: maskablepixmapwidget.h:48
MaskablePixmapWidget::mousePressEvent
void mousePressEvent(QMouseEvent *) final
Definition: maskablepixmapwidget.cpp:93
MaskablePixmapWidget::updatePixmap
void updatePixmap()
Definition: maskablepixmapwidget.cpp:100
MaskablePixmapWidget::mask
QPixmap mask
Definition: maskablepixmapwidget.h:46
MaskablePixmapWidget::setSize
void setSize(QSize size)
Definition: maskablepixmapwidget.cpp:73
MaskablePixmapWidget::pixmap
QPixmap pixmap
Definition: maskablepixmapwidget.h:46
MaskablePixmapWidget::clicked
void clicked()