File indexing completed on 2024-05-05 05:40:53

0001 /***************************************************************************
0002  *  Copyright (C) 2021 by Renaud Guezennec                               *
0003  *   http://www.rolisteam.org/contact                                      *
0004  *                                                                         *
0005  *   This software is free software; you can redistribute it and/or modify *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #include "qml_views/image_selector.h"
0021 
0022 #include <QPainter>
0023 
0024 namespace qml
0025 {
0026 void registerType()
0027 {
0028     qmlRegisterType<ImageSelector>("Profile", 1, 0, "ImageSelector");
0029 }
0030 } // namespace qml
0031 
0032 ImageSelector::ImageSelector()
0033 {
0034     setFlags(QQuickItem::ItemHasContents);
0035     setAcceptedMouseButtons(Qt::AllButtons);
0036 }
0037 
0038 const QByteArray& ImageSelector::imageData() const
0039 {
0040     return m_imageData;
0041 }
0042 
0043 void ImageSelector::setImageData(const QByteArray& newImageData)
0044 {
0045     if(m_imageData == newImageData)
0046         return;
0047     m_imageData= newImageData;
0048     m_pixmap.loadFromData(m_imageData);
0049     emit imageDataChanged();
0050     update();
0051 }
0052 
0053 void ImageSelector::paint(QPainter* painter)
0054 {
0055     if(m_pixmap.isNull())
0056     {
0057         painter->fillRect(0, 0, width(), height(), Qt::gray);
0058     }
0059     else if(m_pixmap.height() == m_pixmap.width())
0060     {
0061         painter->drawPixmap(QRectF(0., 0., width(), height()), m_pixmap,
0062                             QRectF(0., 0., m_pixmap.width(), m_pixmap.height()));
0063     }
0064     else
0065     {
0066         painter->fillRect(0, 0, width(), height(), Qt::red);
0067     }
0068 }
0069 
0070 bool ImageSelector::isSquare() const
0071 {
0072     return m_pixmap.width() == m_pixmap.height();
0073 }
0074 
0075 void ImageSelector::mousePressEvent(QMouseEvent* event)
0076 {
0077     qDebug() << "click";
0078     if(event->button() & Qt::LeftButton)
0079     {
0080         emit mouseClicked();
0081     }
0082     QQuickPaintedItem::mousePressEvent(event);
0083 }
0084