File indexing completed on 2024-05-12 17:12:39

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 "imagepatheditor.h"
0021 
0022 #include <QFileDialog>
0023 #include <QHBoxLayout>
0024 #include <QStyle>
0025 
0026 #include "utils/iohelper.h"
0027 #include "worker/iohelper.h"
0028 
0029 namespace rwidgets
0030 {
0031 
0032 ImagePathEditor::ImagePathEditor(const QString& root, QWidget* parent) : QWidget(parent), m_root(root)
0033 {
0034     setUi();
0035 }
0036 
0037 ImagePathEditor::~ImagePathEditor() {}
0038 
0039 QString ImagePathEditor::filename() const
0040 {
0041     return m_filename;
0042 }
0043 
0044 void ImagePathEditor::focusInEvent(QFocusEvent* event)
0045 {
0046     QWidget::focusInEvent(event);
0047 }
0048 
0049 void ImagePathEditor::setUi()
0050 {
0051     QHBoxLayout* hbox= new QHBoxLayout();
0052     hbox->setContentsMargins(QMargins());
0053     hbox->setSpacing(0);
0054 
0055     setLayout(hbox);
0056 
0057     m_photoBrowser= new QPushButton(style()->standardIcon(QStyle::SP_DialogOpenButton), "");
0058 
0059     m_photoEdit= new QLineEdit();
0060 
0061     m_cleanButton= new QPushButton(QIcon::fromTheme("remove"), "");
0062 
0063     hbox->addWidget(m_photoEdit, 1);
0064     hbox->addWidget(m_photoBrowser);
0065     hbox->addWidget(m_cleanButton);
0066 
0067     connect(m_photoBrowser, &QPushButton::pressed, this, &ImagePathEditor::openFile);
0068     connect(m_photoEdit, &QLineEdit::textEdited, this, &ImagePathEditor::readPixmap);
0069     connect(m_cleanButton, &QPushButton::pressed, this, &ImagePathEditor::clearPixmap);
0070 }
0071 
0072 void ImagePathEditor::setPixmap(QPixmap str)
0073 {
0074     m_pixmap= str;
0075 }
0076 
0077 QPixmap ImagePathEditor::getData() const
0078 {
0079     return m_pixmap;
0080 }
0081 
0082 void ImagePathEditor::setFilename(const QString& file)
0083 {
0084     if(m_filename == file)
0085         return;
0086     m_filename= file;
0087     emit fileNameChanged();
0088 }
0089 
0090 void ImagePathEditor::clearPixmap()
0091 {
0092     setFilename(QString());
0093     m_pixmap= QPixmap();
0094 }
0095 
0096 void ImagePathEditor::openFile()
0097 {
0098     QString fileName= QFileDialog::getOpenFileName(this, tr("Get picture for Character State"), m_root,
0099                                                    "Images (*.jpg *jpeg *.png *.bmp *.svg)");
0100     if(!fileName.isEmpty())
0101     {
0102         if(!fileName.startsWith(m_root))
0103             fileName= utils::IOHelper::copyFile(fileName, m_root);
0104         QFileInfo info(fileName);
0105         if(info.exists())
0106         {
0107             setFilename(fileName);
0108             readPixmap(fileName);
0109         }
0110         // readPixmap(m_root);
0111         // setFilename(fileName);
0112         // readPixmap(fileName);
0113     }
0114 }
0115 void ImagePathEditor::readPixmap(const QString& str)
0116 {
0117     if(str.isEmpty())
0118         return;
0119 
0120     QPixmap pix(str);
0121     if(!pix.isNull())
0122     {
0123         m_pixmap= pix;
0124         m_photoEdit->setText(str);
0125     }
0126 }
0127 } // namespace rwidgets