File indexing completed on 2024-04-14 14:31:55

0001 /*
0002  * Copyright 1999 by Martin R. Jones <mjones@kde.org>
0003  *
0004  * This program is free software; you can redistribute it and/or modify
0005  * it under the terms of the GNU General Public License as published by
0006  * the Free Software Foundation; either version 2 of the License, or
0007  * (at your option) any later version.
0008  *
0009  * This program is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  * GNU General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU General Public License
0015  * along with this program; if not, write to the Free Software
0016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0017  */
0018 #include "amorwidget.h"
0019 
0020 #include <QBitmap>
0021 #include <QPainter>
0022 #include <QMouseEvent>
0023 #include <QApplication>
0024 
0025 #include <QDebug>
0026 
0027 AmorWidget::AmorWidget()
0028   : QWidget( nullptr, Qt::X11BypassWindowManagerHint | Qt::WindowStaysOnTopHint ),
0029     m_pixmap( nullptr ),
0030     m_dragging( false )
0031 {
0032 }
0033 
0034 
0035 #include <iostream>
0036 
0037 void AmorWidget::setPixmap(const QPixmap *pixmap)
0038 {
0039     m_pixmap = pixmap;
0040 
0041     if ( pixmap ) {
0042         const auto dpr = qApp->devicePixelRatio();
0043         const auto mask = m_pixmap->scaled(m_pixmap->width() * dpr, m_pixmap->height() * dpr,
0044                                            Qt::KeepAspectRatio, Qt::FastTransformation).mask();
0045         if (!mask.isNull()) {
0046             setMask(mask);
0047             repaint();
0048         }
0049         update();
0050     }
0051 }
0052 
0053 
0054 void AmorWidget::paintEvent(QPaintEvent *)
0055 {
0056     if( m_pixmap ) {
0057         QPainter p( this );
0058         p.drawPixmap( 0, 0, *m_pixmap);
0059     }
0060 }
0061 
0062 
0063 void AmorWidget::mousePressEvent(QMouseEvent *me)
0064 {
0065     m_clickPos = me->globalPos();
0066 }
0067 
0068 
0069 void AmorWidget::mouseMoveEvent(QMouseEvent *me)
0070 {
0071     if( me->buttons() & Qt::LeftButton ) {
0072         if( !m_dragging && ( m_clickPos-me->globalPos() ).manhattanLength() > 3 ) {
0073             m_dragging = true;
0074         }
0075         if( m_dragging ) {
0076             Q_EMIT dragged( me->globalPos() - m_clickPos, false );
0077             m_clickPos = me->globalPos();
0078         }
0079     }
0080 }
0081 
0082 
0083 void AmorWidget::mouseReleaseEvent(QMouseEvent *me)
0084 {
0085     if( m_dragging ) {
0086         Q_EMIT dragged( me->globalPos() - m_clickPos, true );
0087     }
0088     else if( me->button() == Qt::RightButton ) {
0089         Q_EMIT mouseClicked(me->globalPos());
0090     }
0091 
0092     m_clickPos = QPoint();
0093     m_dragging = false;
0094 }
0095 
0096 
0097 // kate: word-wrap off; encoding utf-8; indent-width 4; tab-width 4; line-numbers on; mixed-indent off; remove-trailing-space-save on; replace-tabs-save on; replace-tabs on; space-indent on;
0098 // vim:set spell et sw=4 ts=4 nowrap cino=l1,cs,U1: